-
Notifications
You must be signed in to change notification settings - Fork 22
/
plugin_x64dbg.c
361 lines (279 loc) · 6.71 KB
/
plugin_x64dbg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include "stdafx.h"
#include "plugin.h"
HWND hwollymain;
// Config functions
BOOL MyGetintfromini(HINSTANCE dllinst, TCHAR *key, int *p_val, int min, int max, int def)
{
duint val;
if(!BridgeSettingGetUint(DEF_PLUGINNAME, key, &val) || (val & ~0xFFFFFFFF) != 0)
{
*p_val = def;
return FALSE;
}
int val_int = (int)val;
if(min && max && (val_int < min || val_int > max))
*p_val = def;
else
*p_val = val_int;
return TRUE;
}
BOOL MyWriteinttoini(HINSTANCE dllinst, TCHAR *key, int val)
{
return BridgeSettingSetUint(DEF_PLUGINNAME, key, val) != false;
}
int MyGetstringfromini(HINSTANCE dllinst, TCHAR *key, TCHAR *s, int length)
{
char *buf;
int len;
if(length >= MAX_SETTING_SIZE)
{
if(!BridgeSettingGet(DEF_PLUGINNAME, key, s))
{
*s = '\0';
return 0;
}
return lstrlen(s);
}
buf = (char *)HeapAlloc(GetProcessHeap(), 0, MAX_SETTING_SIZE*sizeof(char));
if(!buf)
return 0;
if(!BridgeSettingGet(DEF_PLUGINNAME, key, buf))
{
HeapFree(GetProcessHeap(), 0, buf);
*s = '\0';
return 0;
}
len = lstrlen(buf);
if(len > length - 1)
len = length - 1;
lstrcpyn(s, buf, len + 1);
HeapFree(GetProcessHeap(), 0, buf);
return len;
}
BOOL MyWritestringtoini(HINSTANCE dllinst, TCHAR *key, TCHAR *s)
{
return BridgeSettingSet(DEF_PLUGINNAME, key, s) != false;
}
// Assembler functions
DWORD SimpleDisasm(BYTE *cmd, SIZE_T cmdsize, DWORD_PTR ip, BYTE *dec, BOOL bSizeOnly,
TCHAR *pszResult, DWORD_PTR *jmpconst, DWORD_PTR *adrconst, DWORD_PTR *immconst)
{
BYTE cmd_safe[MAXCMDSIZE];
if(cmdsize < MAXCMDSIZE)
{
CopyMemory(cmd_safe, cmd, cmdsize);
ZeroMemory(cmd_safe + cmdsize, MAXCMDSIZE - cmdsize);
cmd = cmd_safe;
}
BASIC_INSTRUCTION_INFO basicinfo;
if(!DbgFunctions()->DisasmFast(cmd, ip, &basicinfo))
return 0;
if(!bSizeOnly)
{
if(basicinfo.type == TYPE_ADDR &&
basicinfo.branch &&
!basicinfo.call &&
basicinfo.size == 2)
{
// Add "short" for a short jump
// We add 6 chars, make sure that basicinfo.instruction is not too long
basicinfo.instruction[COMMAND_MAX_LEN - 1 - 6] = '\0';
BOOL bUppercase = (basicinfo.instruction[0] >= 'A' && basicinfo.instruction[0] <= 'Z');
char *p = basicinfo.instruction;
char *q = pszResult;
// Copy command name
while(*p != '\0' && *p != ' ' && *p != '\t')
{
*q++ = *p++;
}
// Copy spaces
while(*p == ' ' || *p == '\t')
{
*q++ = *p++;
}
if(*p != '\0')
{
// Add "short "
lstrcpy(q, bUppercase ? "SHORT " : "short ");
q += 6;
}
// Copy the rest
lstrcpy(q, p);
}
else
{
// pszResult should have at least COMMAND_MAX_LEN chars
lstrcpy(pszResult, basicinfo.instruction);
}
*jmpconst = basicinfo.addr;
*adrconst = basicinfo.memory.value;
*immconst = basicinfo.value.value;
}
return basicinfo.size;
}
int AssembleShortest(TCHAR *lpCommand, DWORD_PTR dwAddress, BYTE *bBuffer, TCHAR *lpError)
{
int size;
if(!DbgFunctions()->Assemble(dwAddress, bBuffer, &size, lpCommand, lpError))
return 0;
return size;
}
int AssembleWithGivenSize(TCHAR *lpCommand, DWORD_PTR dwAddress, int nReqSize, BYTE *bBuffer, TCHAR *lpError)
{
int size;
if(!DbgFunctions()->Assemble(dwAddress, bBuffer, &size, lpCommand, lpError))
return 0;
// TODO: fix when implemented
if(size > nReqSize)
{
lstrcpy(lpError, "AssembleWithGivenSize: internal assembler error");
return 0;
}
while(size < nReqSize)
bBuffer[size++] = 0x90; // Fill with NOPs
return size;
}
// Memory functions
BOOL SimpleReadMemory(void *buf, DWORD_PTR addr, SIZE_T size)
{
return DbgMemRead(addr, buf, size);
}
BOOL SimpleWriteMemory(void *buf, DWORD_PTR addr, SIZE_T size)
{
return DbgFunctions()->MemPatch(addr, buf, size);
}
// Symbolic functions
int GetLabel(DWORD_PTR addr, TCHAR *name)
{
if(!DbgGetLabelAt(addr, SEG_DEFAULT, name))
return 0;
return lstrlen(name);
}
int GetComment(DWORD_PTR addr, TCHAR *name)
{
if(!DbgGetCommentAt(addr, name))
return 0;
if(name[0] == '\1') // Automatic comment
return 0;
return lstrlen(name);
}
BOOL QuickInsertLabel(DWORD_PTR addr, TCHAR *s)
{
return DbgSetLabelAt(addr, s);
}
BOOL QuickInsertComment(DWORD_PTR addr, TCHAR *s)
{
return DbgSetCommentAt(addr, s);
}
void MergeQuickData(void)
{
}
void DeleteRangeLabels(DWORD_PTR addr0, DWORD_PTR addr1)
{
DbgClearLabelRange(addr0, addr1);
}
void DeleteRangeComments(DWORD_PTR addr0, DWORD_PTR addr1)
{
DbgClearCommentRange(addr0, addr1);
}
// Module functions
PLUGIN_MODULE FindModuleByName(TCHAR *lpModule)
{
return (PLUGIN_MODULE)DbgFunctions()->ModBaseFromName(lpModule);
}
PLUGIN_MODULE FindModuleByAddr(DWORD_PTR dwAddress)
{
return (PLUGIN_MODULE)DbgFunctions()->ModBaseFromAddr(dwAddress);
}
DWORD_PTR GetModuleBase(PLUGIN_MODULE module)
{
return (DWORD_PTR)module;
}
SIZE_T GetModuleSize(PLUGIN_MODULE module)
{
return DbgFunctions()->ModSizeFromAddr((duint)module);
}
BOOL GetModuleName(PLUGIN_MODULE module, TCHAR *pszModuleName)
{
return DbgFunctions()->ModNameFromAddr((duint)module, pszModuleName, FALSE);
}
BOOL IsModuleWithRelocations(PLUGIN_MODULE module)
{
IMAGE_DOS_HEADER *pDosHeader = (IMAGE_DOS_HEADER *)module;
LONG e_lfanew;
if(!DbgMemRead(
(ULONG_PTR)&pDosHeader->e_lfanew,
(BYTE *)&e_lfanew,
sizeof(LONG)))
{
return FALSE;
}
IMAGE_NT_HEADERS *pNtHeader = (IMAGE_NT_HEADERS *)((char *)pDosHeader + e_lfanew);
DWORD dwRelocVirtualAddress;
if(!DbgMemRead(
(ULONG_PTR)&pNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress,
(BYTE *)&dwRelocVirtualAddress,
sizeof(DWORD)))
{
return FALSE;
}
return dwRelocVirtualAddress != 0;
}
// Memory functions
PLUGIN_MEMORY FindMemory(DWORD_PTR dwAddress)
{
return (PLUGIN_MEMORY)DbgMemFindBaseAddr(dwAddress, NULL);
}
DWORD_PTR GetMemoryBase(PLUGIN_MEMORY mem)
{
return (DWORD_PTR)mem;
}
SIZE_T GetMemorySize(PLUGIN_MEMORY mem)
{
SIZE_T size;
if(!DbgMemFindBaseAddr((duint)mem, &size))
return 0;
return size;
}
void EnsureMemoryBackup(PLUGIN_MEMORY mem)
{
}
// Analysis functions
BYTE *FindDecode(DWORD_PTR addr, SIZE_T *psize)
{
// TODO: not implemented
return NULL;
}
int DecodeGetType(BYTE decode)
{
// TODO: not implemented
return DECODE_UNKNOWN;
}
// Misc.
BOOL IsProcessLoaded()
{
return DbgIsDebugging();
}
void SuspendAllThreads()
{
// Note: I'm not sure it's required to be implemented here
// It's recommended to call for OllyDbg v2, though
//ThreaderPauseAllThreads(false);
}
void ResumeAllThreads()
{
// Note: I'm not sure it's required to be implemented here
// It's recommended to call for OllyDbg v2, though
//ThreaderResumeAllThreads(false);
}
DWORD_PTR GetCpuBaseAddr()
{
SELECTIONDATA selection;
if(!GuiSelectionGet(GUI_DISASSEMBLY, &selection))
return 0;
return DbgMemFindBaseAddr(selection.start, NULL);
}
void InvalidateGui()
{
GuiUpdateAllViews();
}