This repository has been archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
asprext.cpp
executable file
·354 lines (288 loc) · 9.87 KB
/
asprext.cpp
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
/*
Copyright (c) 2016 Piotr Stolarz
asprext: ASProtect reverse engineering & analysis WinDbg extension
Distributed under the GNU General Public License (the License)
see accompanying file LICENSE for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
*/
#include "common.h"
#include "vm_aspr.h"
#include "imports.h"
#include "oep_aspr.h"
#include "init_aspr.h"
/* DLL entry point */
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
BOOL ret=TRUE;
switch(fdwReason)
{
case DLL_PROCESS_ATTACH:
set_tls_i(TlsAlloc());
ret = (get_tls_i()!=TLS_OUT_OF_INDEXES);
break;
case DLL_PROCESS_DETACH:
if (get_tls_i()!=TLS_OUT_OF_INDEXES) TlsFree(get_tls_i());
break;
}
return ret;
}
/* Extension initialization */
HRESULT CALLBACK
DebugExtensionInitialize(PULONG Version, PULONG Flags)
{
*Version = DEBUG_EXTENSION_VERSION(1, 0);
*Flags = 0;
return S_OK;
}
/* Recognize command string 'p_cmd' in arguments input 'args'. Min recognized
length of the command is passed by 'min_len'. If command has been recognized
TRUE is returned and 'p_args' will point just after the recognized command
in 'args'.
*/
static BOOL is_cmd(PCSTR args, const char *p_cmd, size_t min_len, PCSTR *p_args)
{
BOOL ret=FALSE;
*p_args=args;
size_t i, cmd_len;
for (i=0; isspace(args[i]); i++);
for (cmd_len=0; args[i+cmd_len] && !isspace(args[i+cmd_len]); cmd_len++);
BOOL cmd_match = (!cmd_len ? TRUE : !strncmp(&args[i], p_cmd, cmd_len));
if (!min_len && !cmd_match) {
/* for default command and not matching input: if the input indicates
the command's flags (staring by '-') then the command matches, since
it seems it has been omitted; else some other command is provided -
return FALSE */
ret = args[i]=='-';
} else
if (cmd_match && cmd_len>=min_len) {
i += cmd_len;
*p_args = &args[i];
ret=TRUE;
}
return ret;
}
/* aspr_vmctx_dump [-l] vm_ctx_addr [vm_in_addr] */
HRESULT CALLBACK
aspr_vmctx_dump(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
ULONG64 vmctx_addr, vmin_addr;
flag_desc_t flags_dsc[] = {{'l', FALSE}, {0}};
DWORD flags = 0;
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=PRNCTX_LINE;
/* read mandatory vm_ctx addr & optional vm_in */
if (!get_expression(args, &vmctx_addr, &args)) goto finish;
if (!get_expression(args, &vmin_addr, &args)) vmin_addr=0;
print_vmctx(vmctx_addr, vmin_addr, flags);
ret=S_OK;
finish:
return ret;
}
/* aspr_vmin_disasm vm_in_addr */
HRESULT CALLBACK
aspr_vmin_disasm(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
/* read mandatory vm_in addr */
ULONG64 vmin_addr;
if (!get_expression(args, &vmin_addr, &args)) goto finish;
vm_disasm(vmin_addr);
ret=S_OK;
finish:
return ret;
}
/* aspr_imports family of commands */
HRESULT CALLBACK
aspr_imports(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
if (is_cmd(args, "load", 1, &args))
{
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'e', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=LDIMPS_AIP_ENV_ADDR;
ULONG64 in_addr;
if (!get_expression(args, &in_addr, &args)) goto finish;
load_imports(in_addr, flags);
} else
if (is_cmd(args, "unload", 1, &args)) {
unload_imports();
} else
if (is_cmd(args, "print", 0, &args))
{
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'x', FALSE}, {0}};
read_flags(args, flags_dsc);
if (flags_dsc[0].is_pres) print_imp_spec();
else print_imports();
}
ret=S_OK;
finish:
return ret;
}
/* aspr_ipatch family of commands */
HRESULT CALLBACK
aspr_ipatch(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
if (is_cmd(args, "load", 1, &args))
{
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'1', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=LDPTCHS_1_DUPL;
ULONG64 aip_env_addr, chk_mem_addr;
if (!get_expression(args, &aip_env_addr, &args)) goto finish;
if (!get_expression(args, &chk_mem_addr, &args)) goto finish;
load_patches(aip_env_addr, chk_mem_addr, flags);
} else
if (is_cmd(args, "unload", 1, &args)) {
unload_patches();
} else
if (is_cmd(args, "apply", 1, &args))
{
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'i', FALSE}, {'v', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=APLPTCHS_IAT_PATCH;
if (flags_dsc[1].is_pres) flags|=APLPTCHS_VERBOSE;
apply_patches(flags);
} else
if (is_cmd(args, "print", 0, &args)) {
print_patches();
}
ret=S_OK;
finish:
return ret;
}
/* aspr_init family of commands */
HRESULT CALLBACK
aspr_init(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
if (is_cmd(args, "dump", 1, &args))
{
size_t i;
ULONG64 init_tab_addr, code_base;
if (!get_expression(args, &init_tab_addr, &args)) goto finish;
if (!get_expression(args, &code_base, &args)) goto finish;
for (i=0; isspace(args[i]); i++);
if (!args[i]) goto finish;
if (!dump_init_tab(init_tab_addr, code_base, &args[i])) goto finish;
info_dbgprintf(
"Borland init table code successfully dumped to file %s\n", &args[i]);
} else
if (is_cmd(args, "patch", 1, &args))
{
size_t i;
ULONG64 init_tab_addr, code_base;
if (!get_expression(args, &init_tab_addr, &args)) goto finish;
if (!get_expression(args, &code_base, &args)) goto finish;
for (i=0; isspace(args[i]); i++);
if (!args[i]) goto finish;
if (!patch_init_tab(init_tab_addr, code_base, &args[i])) goto finish;
info_dbgprintf("Borland init table code successfully patched\n");
} goto finish;
ret=S_OK;
finish:
return ret;
}
/* aspr_oep [-x] oep_env_addr */
HRESULT CALLBACK
aspr_oep(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'x', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=GETOEP_DUMPEXT;
ULONG64 oep_env_addr;
if (!get_expression(args, &oep_env_addr, &args)) goto finish;
print_oep_addr(oep_env_addr, flags);
ret=S_OK;
finish:
return ret;
}
/* TODO */
HRESULT CALLBACK
aspr_poly_buf(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
ULONG64 poly_buf_env_addr, base_addr;
if (!get_expression(args, &poly_buf_env_addr, &args)) goto finish;
trace_poly_buf(poly_buf_env_addr);
ret=S_OK;
finish:
return ret;
}
/* help info */
HRESULT CALLBACK
help(PDEBUG_CLIENT4 Client, PCSTR args)
{
set_client(Client);
dbgprintf(
"asprext: ASProtect reverse engineering & analysis utils\n\n"
"aspr_vmctx_dump [-l] vm_ctx_addr [vm_in_addr]\n"
" Show the VM context under an address vm_ctx_addr. If an address of the\n"
" VM input is provided: vm_in_addr, the currently executed opcode will be\n"
" disassembled.\n"
" -l: single line output\n\n"
"aspr_vmin_disasm vm_in_addr\n"
" Disassembly opcodes of the VM input from an address vm_in_addr.\n\n"
"aspr_imports load [-e] addr\n"
" Load imports. addr is an address of ASPR AIP environment or ASPR imports\n"
" description struct.\n"
" -e: addr points to the ASPR AIP environment struct\n"
"aspr_imports unload\n"
" Unload previously loaded imports\n"
"aspr_imports [print] [-x]\n"
" Show loaded imports\n"
" -x: Display loaded imports in the dumpext extension's imports spec. format\n\n"
"aspr_ipatch load [-1] aip_env_addr chk_mem_addr\n"
" Load list of import patches. aip_env_addr is an address of ASPR AIP\n"
" environment struct. chk_mem_addr identifies start of memory range used for\n"
" calculation of the seed. The seed is utilized for computation of the ids of\n"
" imported libraries and their procs. To successfully generate proper patches\n"
" the loaded imports spec. must contain modules with non-destroyed IAT addresses.\n"
" -1: Some compilers (e.g. Delphi) may produce duplicated imports in their\n"
" imports dir. In this case there is possible to resolve a patch to many\n"
" equivalent alternatives. This flag causes to set a patch for the first\n"
" import in the set of duplicates.\n"
"aspr_ipatch unload\n"
" Unload previously loaded patches list\n"
"aspr_ipatch [print]\n"
" Show loaded patches list\n"
"aspr_ipatch apply [-i] [-v]\n"
" Apply loaded patches into memory\n"
" -i: additionally apply patches to the module's IAT table\n"
" -v: verbose mode\n\n"
"aspr_init dump init_tab_addr code_base file_name\n"
" Dump fixed Borland init table code of the table located at the address\n"
" init_tab_addr to the file file_name. Base of the fixed code shall be set to\n"
" base_code.\n"
"aspr_init patch init_tab_addr code_base file_name\n"
" Patch the init code previously dumped by the \"dump\" command. Parameters\n"
" shall be the same as for the \"dump\" command\n\n"
"aspr_oep [-x] oep_env_addr\n"
" Print the Original Entry Point. oep_env_addr is an address to the OEP environment.\n"
" -x: Display OEP RVA in the format used by the dumpext extension library\n\n"
"help\n"
" Display this help.\n");
return S_OK;
}