This repository has been archived by the owner on Aug 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdumpext.cpp
784 lines (650 loc) · 25.7 KB
/
dumpext.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
/*
Copyright (c) 2015 Piotr Stolarz
dumpext: PE files fix, dump & 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 "config.h"
#include "imports.h"
#include "resrc.h"
#include "except.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());
if (get_tls_i()!=TLS_OUT_OF_INDEXES) init_config(hinstDLL);
else ret=FALSE;
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 a 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;
}
/* Get module base addr form *p_args (the pointer is changed after the read
accordingly). If the base address is not specified as an argument take the
mod base owning the address 'addr' if 'addr' is not NULL. Otherwise take the
first exe module starting from index 0. The name of image file name is
written under 'pc_img_name'. This buf must be at least MAX_PATH+1 long.
Returns 0 if error.
*/
static ULONG64 get_mod_base(PCSTR *p_args, ULONG64 addr, char *pc_img_name)
{
ULONG64 mod_base=0;
IDebugSymbols *DebugSymbols=NULL;
IDebugSymbols2 *DebugSymbols2=NULL;
if (!get_expression(*p_args, &mod_base, p_args))
{
mod_base=0;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols), (void **)&DebugSymbols))!=S_OK) goto finish;
if (addr) {
if (DebugSymbols->GetModuleByOffset(addr, 0, NULL, &mod_base)!=S_OK)
mod_base=0;
}
if (!mod_base)
{
ULONG ld_mods_n, uld_mods_n;
if (DebugSymbols->GetNumberModules(&ld_mods_n, &uld_mods_n)!=S_OK)
goto finish;
UINT i;
ULONG64 mod_base_1st=0;
for (i=0; i<ld_mods_n; i++)
{
if (DebugSymbols->GetModuleByIndex(i, &mod_base)!=S_OK)
continue;
if (!mod_base_1st) mod_base_1st=mod_base;
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
if (!read_pe_headers(mod_base, &dos_hdr, &nt_hdrs, NULL, FALSE))
continue;
WORD fchr =
get_16uint_le(&get_FileHeader(&nt_hdrs).Characteristics);
if (fchr&IMAGE_FILE_EXECUTABLE_IMAGE && !(fchr&IMAGE_FILE_DLL))
break;
}
if (i>=ld_mods_n) {
mod_base=mod_base_1st;
if (!mod_base_1st) goto finish;
}
}
}
if (pc_img_name)
{
ULONG img_name_sz=0;
*pc_img_name=0;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols2), (void **)&DebugSymbols2))!=S_OK)
goto finish;
if (DebugSymbols2->GetModuleNameString(DEBUG_MODNAME_IMAGE,
DEBUG_ANY_ID, mod_base, pc_img_name, MAX_PATH+1, &img_name_sz)==S_OK
&& img_name_sz>0)
{
pc_img_name[MAX_PATH]=0;
} else
*pc_img_name=0;
}
finish:
if (DebugSymbols2) DebugSymbols2->Release();
if (DebugSymbols) DebugSymbols->Release();
return mod_base;
}
/* dump_imp_scan family of commands */
HRESULT CALLBACK
dump_imp_scan(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
flag_desc_t flags_dsc_iat[] =
{{'w', FALSE}, {'s', TRUE}, {'a', TRUE}, {'r', TRUE}, {'l', TRUE}, {0}};
flag_desc_t flags_dsc_idt[] =
{{'w', FALSE}, {'a', TRUE}, {'r', TRUE}, {'l', TRUE}, {0}};
iscan_tpy_t scan_tpy;
flag_desc_t *p_flags_dsc;
if (is_cmd(args, "iat", 2, &args)) {
scan_tpy=iscan_iat;
p_flags_dsc=flags_dsc_iat;
} else
if (is_cmd(args, "idt", 2, &args)) {
scan_tpy=iscan_idt;
p_flags_dsc=flags_dsc_idt;
} else goto finish;
rng_spec_t rng;
rngspc_rc_t rc = get_range_spec(&args, p_flags_dsc, &rng);
if (rc==rngspc_err) goto finish;
DWORD flags=0;
if (p_flags_dsc[0].is_pres) flags|=SCANIMPS_WRITE_CONF;
ULONG64 rng_addr=NULL;
if (rc==rngspc_ok && !rng.is_sect && !rng.rng.is_rva) rng_addr=rng.rng.addr;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, rng_addr, img_name);
if (!mod_base || strlen(args)) goto finish;
info_dbgprintf("Base address of the module being scanned: 0x%p [%s]\n",
mod_base, img_name);
scan_imports(
mod_base, scan_tpy, (rc==rngspc_not_prov ? NULL : &rng), flags);
ret=S_OK;
finish:
return ret;
}
/* dump_pe [-s *|n] [mod_base] */
HRESULT CALLBACK
dump_pe(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
flag_desc_t flags_dsc[] = {{'s', TRUE}, {0}};
rng_spec_t rng;
rngspc_rc_t rc = get_range_spec(&args, flags_dsc, &rng);
if (rc==rngspc_err) goto finish;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, NULL, img_name);
if (!mod_base || strlen(args)) goto finish;
info_dbgprintf(
"Base address of the dumped module: 0x%p [%s]\n", mod_base, img_name);
if (!pe_dump(mod_base, (rc==rngspc_not_prov ? 0 : rng.sect))) goto finish;
ret=S_OK;
finish:
return ret;
}
/* dump_pe_info family of commands */
HRESULT CALLBACK
dump_pe_info(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
PCSTR args_org=args;
set_client(Client);
BOOL b_import=FALSE, b_export=FALSE, b_bimport=FALSE, b_dimport=FALSE;
BOOL b_debug=FALSE, b_lconf=FALSE, b_rsrc=FALSE, b_tls=FALSE, b_reloc=FALSE,
b_except=FALSE;
if (!(b_import=is_cmd(args, "import", 1, &args)))
if (!(b_export=is_cmd(args, "export", 3, &args)))
if (!(b_bimport=is_cmd(args, "bimport", 1, &args)))
if (!(b_dimport=is_cmd(args, "dimport", 2, &args)))
if (!(b_debug=is_cmd(args, "debug", 2, &args)))
if (!(b_lconf=is_cmd(args, "lconf", 1, &args)))
if (!(b_rsrc=is_cmd(args, "rsrc", 2, &args)))
if (!(b_tls=is_cmd(args, "tls", 1, &args)))
if (!(b_reloc=is_cmd(args, "reloc", 2, &args)))
if (!(b_except=is_cmd(args, "except", 3, &args)));
if (b_import || b_export || b_bimport || b_dimport ||
b_debug || b_lconf || b_rsrc || b_tls || b_reloc ||
b_except)
{
flag_desc_t flags_dsc_rng[] =
{{'a', TRUE}, {'r', TRUE}, {0}};
flag_desc_t flags_dsc_rng_l[] =
{{'a', TRUE}, {'r', TRUE}, {'l', TRUE}, {0}};
flag_desc_t flags_dsc_imp[] =
{{'x', FALSE}, {'a', TRUE}, {'r', TRUE}, {'l', TRUE}, {0}};
flag_desc_t flags_dsc_rsrc[] =
{{'c', FALSE}, {'C', FALSE}, {'a', TRUE}, {'r', TRUE}, {0}};
flag_desc_t flags_dsc_except[] =
{{'v', FALSE}, {'V', FALSE}, {'a', TRUE}, {'r', TRUE},
{'l', TRUE}, {0}};
flag_desc_t *p_flags_dsc;
if (b_import) {
p_flags_dsc = flags_dsc_imp;
} else
if (b_bimport || b_dimport || b_reloc) {
p_flags_dsc = flags_dsc_rng_l;
} else
if (b_export || b_debug || b_lconf || b_tls) {
p_flags_dsc = flags_dsc_rng;
} else
if (b_rsrc) {
p_flags_dsc = flags_dsc_rsrc;
} else
if (b_except) {
p_flags_dsc = flags_dsc_except;
}
rng_spec_t rng;
rngspc_rc_t rc = get_range_spec(&args, p_flags_dsc, &rng);
if (rc==rngspc_err) goto finish;
ULONG64 rng_addr=NULL;
if (rc==rngspc_ok && !rng.is_sect && !rng.rng.is_rva)
rng_addr=rng.rng.addr;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, rng_addr, img_name);
if (!mod_base || strlen(args)) goto finish;
info_dbgprintf(
"Base address of the module: 0x%p [%s]\n", mod_base, img_name);
if (b_import) {
DWORD flags = 0;
if (p_flags_dsc[0].is_pres) flags|=PRNTIMP_IMPSPEC;
print_imports(mod_base, (rc==rngspc_not_prov ? NULL : &rng), flags);
} else
if (b_export) {
print_exports(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_bimport) {
print_bound_imps(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_dimport) {
print_delay_imps(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_debug) {
print_debug(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_lconf) {
print_lconf(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_rsrc) {
DWORD flags = 0;
if (p_flags_dsc[0].is_pres) flags|=PRNTRSRC_CAPAC;
if (p_flags_dsc[1].is_pres) flags|=PRNTRSRC_CAPAC_ONLY;
print_rsrc(mod_base, (rc==rngspc_not_prov ? NULL : &rng), flags);
} else
if (b_tls) {
print_tls(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_reloc) {
print_reloc(mod_base, (rc==rngspc_not_prov ? NULL : &rng));
} else
if (b_except) {
DWORD info_details=PRNTEXCPT_INFO_NORMAL;
if (p_flags_dsc[1].is_pres) info_details=PRNTEXCPT_INFO_VERBOSE;
else
if (p_flags_dsc[0].is_pres) info_details=PRNTEXCPT_INFO_DETAILED;
print_except(
mod_base, (rc==rngspc_not_prov ? NULL : &rng), info_details);
}
} else
{
/* try default "header" command */
args=args_org;
/* if the "header command is specified - omit it "*/
if (is_cmd(args, "header", 1, &args));
else args=args_org;
DWORD flags=0;
flag_desc_t flags_dsc[] =
{{'m', FALSE}, {'h', FALSE}, {'d', FALSE}, {'s', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=PRNTPE_DOS_HEADER;
if (flags_dsc[1].is_pres) flags|=PRNTPE_PE_HEADERS;
if (flags_dsc[2].is_pres) flags|=PRNTPE_DIRS;
if (flags_dsc[3].is_pres) flags|=PRNTPE_SECTS;
/* default print flags (if not specified) */
if (!flags) flags = PRNTPE_PE_HEADERS|PRNTPE_DIRS|PRNTPE_SECTS;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, NULL, img_name);
if (!mod_base || strlen(args)) goto finish;
info_dbgprintf(
"Base address of the module: 0x%p [%s]\n\n", mod_base, img_name);
print_pe_details(mod_base, flags);
}
ret=S_OK;
finish:
return ret;
}
/* dump_offset_info [-v] {-a addr}|{-f ftpr [mod_base]} */
HRESULT CALLBACK
dump_offset_info(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
ULONG64 addr;
BOOL b_virt_det=FALSE;
IDebugSymbols *DebugSymbols=NULL;
IDebugSymbols2 *DebugSymbols2=NULL;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols), (void **)&DebugSymbols))!=S_OK) goto err;
flag_desc_t flags_dsc[] = {{'a', TRUE}, {'f', TRUE}, {'v', FALSE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres)
{
size_t arg_len = flags_dsc[0].arg_len;
if (!arg_len || strlen(args)) goto err;
ULONG64 mod_base;
char c = flags_dsc[0].pc_arg[arg_len];
flags_dsc[0].pc_arg[arg_len] = 0;
if (!get_expression(flags_dsc[0].pc_arg, &addr, NULL)) goto err;
flags_dsc[0].pc_arg[arg_len] = c;
b_virt_det=TRUE;
dbgprintf("Address 0x%p details:\n", addr);
if (DebugSymbols->GetModuleByOffset(addr, 0, NULL, &mod_base)!=S_OK) {
dbgprintf(" Can not establish the owning module\n");
goto no_err;
}
ULONG img_name_sz=0;
char img_name[MAX_PATH+1];
*img_name=0;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols2), (void **)&DebugSymbols2))==S_OK)
{
if (DebugSymbols2->GetModuleNameString(
DEBUG_MODNAME_IMAGE, DEBUG_ANY_ID, mod_base, img_name,
sizeof(img_name), &img_name_sz)==S_OK && img_name_sz>0)
{
img_name[sizeof(img_name)-1]=0;
} else
*img_name=0;
}
DWORD rva = ADDR2RVA(addr, mod_base);
dbgprintf(" Module base: 0x%p\n", mod_base);
if (*img_name) dbgprintf(" Image name: %s\n", img_name);
dbgprintf(" RVA: 0x%08X\n", rva);
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
DWORD n_sects;
ULONG64 sectab_addr;
IMAGE_SECTION_HEADER sectab[MAX_SECTIONS];
if (!read_pe_headers(
mod_base, &dos_hdr, &nt_hdrs, §ab_addr, TRUE)) goto no_err;
DWORD hdrs_sz = (nt_hdrs.pe_tpy==pe_32bit ?
get_32uint_le(&nt_hdrs.hdr.pe32.OptionalHeader.SizeOfHeaders):
get_32uint_le(&nt_hdrs.hdr.pe64.OptionalHeader.SizeOfHeaders));
if (0<=rva && rva<hdrs_sz) {
dbgprintf(" Contained in: header\n");
dbgprintf(" File ptr: 0x%08X\n", rva);
} else
{
if (!(n_sects=read_sectab(
&nt_hdrs, sectab_addr, sectab, TRUE, TRUE))) goto no_err;
DWORD sect_i, rptr;
if (get_rva_info(sectab, n_sects, rva, §_i, NULL, NULL, &rptr))
{
char sec_name[IMAGE_SIZEOF_SHORT_NAME+1];
strncpy(sec_name,
(char*)§ab[sect_i].Name[0], sizeof(sec_name)-1);
sec_name[sizeof(sec_name)-1] = 0;
DWORD sec_rva = get_32uint_le(§ab[sect_i].VirtualAddress);
DWORD sec_vsz = get_32uint_le(§ab[sect_i].Misc.VirtualSize);
dbgprintf(
" Contained in: sect %d [%s], sect mem range: 0x%p..0x%p\n",
sect_i+1, sec_name, RVA2ADDR(sec_rva, mod_base),
RVA2ADDR(sec_rva+sec_vsz-1, mod_base));
if (rptr) dbgprintf(" File ptr: 0x%08X\n", rptr);
else dbgprintf(" Not contained in PE file!\n");
} else {
dbgprintf(" Outside sects!\n");
}
}
}
else if (flags_dsc[1].is_pres)
{
size_t arg_len = flags_dsc[1].arg_len;
if (!arg_len) goto err;
ULONG64 expr;
char c = flags_dsc[1].pc_arg[arg_len];
flags_dsc[1].pc_arg[arg_len] = 0;
if (!get_expression(flags_dsc[1].pc_arg, &expr, NULL)) goto err;
flags_dsc[1].pc_arg[arg_len] = c;
DWORD rptr = (DWORD)expr;
if (rptr!=expr) {
err_dbgprintf("File pointer too large\n");
goto err;
}
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, NULL, img_name);
if (!mod_base || strlen(args)) goto err;
dbgprintf("File pointer 0x%08X details:\n", rptr);
dbgprintf(" Module base: 0x%p\n", mod_base);
if (*img_name) dbgprintf(" Image name: %s\n", img_name);
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
DWORD n_sects;
ULONG64 sectab_addr;
IMAGE_SECTION_HEADER sectab[MAX_SECTIONS];
if (!read_pe_headers(mod_base, &dos_hdr, &nt_hdrs, §ab_addr, TRUE))
goto no_err;
DWORD hdrs_sz = (nt_hdrs.pe_tpy==pe_32bit ?
get_32uint_le(&nt_hdrs.hdr.pe32.OptionalHeader.SizeOfHeaders):
get_32uint_le(&nt_hdrs.hdr.pe64.OptionalHeader.SizeOfHeaders));
if (0<=rptr && rptr<hdrs_sz)
{
addr = RVA2ADDR(rptr, mod_base);
b_virt_det=TRUE;
dbgprintf(" Contained in: header, mem range: 0x%p..0x%p\n",
addr, addr+hdrs_sz-1);
dbgprintf(" Address [RVA]:0x%p [0x%08X]\n", addr, rptr);
} else
{
if (!(n_sects=read_sectab(
&nt_hdrs, sectab_addr, sectab, TRUE, TRUE))) goto no_err;
DWORD sect_i, rva;
if (get_rptr_info(sectab, n_sects, rptr, §_i, NULL, &rva))
{
char sec_name[IMAGE_SIZEOF_SHORT_NAME+1];
strncpy(sec_name,
(char*)§ab[sect_i].Name[0], sizeof(sec_name)-1);
sec_name[sizeof(sec_name)-1] = 0;
DWORD sec_rva = get_32uint_le(§ab[sect_i].VirtualAddress);
DWORD sec_vsz = get_32uint_le(§ab[sect_i].Misc.VirtualSize);
dbgprintf(
" Contained in: sect %d [%s], sect mem range: 0x%p..0x%p\n",
sect_i+1, sec_name, RVA2ADDR(sec_rva, mod_base),
RVA2ADDR(sec_rva+sec_vsz-1, mod_base));
if (rva) {
addr = RVA2ADDR(rva, mod_base);
b_virt_det=TRUE;
dbgprintf(" Address [RVA]:0x%p [0x%08X]\n", addr, rva);
} else dbgprintf(" Not loaded to memory!\n");
} else {
dbgprintf(" Outside sects!\n");
}
}
} else goto err;
no_err:
if (b_virt_det && flags_dsc[2].is_pres)
{
IDebugDataSpaces2 *DebugDataSpaces2;
if (get_client()->QueryInterface(
__uuidof(IDebugDataSpaces2), (void **)&DebugDataSpaces2)==S_OK)
{
MEMORY_BASIC_INFORMATION64 vinfo;
if (DebugDataSpaces2->QueryVirtual(addr, &vinfo)==S_OK)
{
dbgprintf("Virtual memory info:\n");
dbgprintf(" Base address: 0x%p\n", vinfo.BaseAddress);
dbgprintf(" Alloc base: 0x%p\n", vinfo.AllocationBase);
dbgprintf(" Alloc protect:0x%08X", vinfo.AllocationProtect);
print_flags(MEMINFOVALS_HT, vinfo.AllocationProtect, 32);
dbgprintf(" Region size: 0x%p\n", vinfo.RegionSize);
dbgprintf(" State: 0x%08X", vinfo.State);
print_flags(MEMINFOVALS_HT, vinfo.State, 32);
dbgprintf(" Protect: 0x%08X", vinfo.Protect);
print_flags(MEMINFOVALS_HT, vinfo.Protect, 32);
dbgprintf(" Type: 0x%08X", vinfo.Type);
print_flags(MEMINFOVALS_HT, vinfo.Type, 32);
}
DebugDataSpaces2->Release();
}
}
ret=S_OK;
err:
if (DebugSymbols) DebugSymbols->Release();
if (DebugSymbols2) DebugSymbols2->Release();
return ret;
}
/* dump_sects_chrt [-c] [mod_base] */
HRESULT CALLBACK
dump_sects_chrt(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
DWORD flags=0;
flag_desc_t flags_dsc[] = {{'c', TRUE}, {0}};
size_t rd_sz = read_flags(args, flags_dsc);
args += rd_sz;
if (flags_dsc[0].is_pres) flags|=PROPSC_READ_CONF;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, NULL, img_name);
if (!mod_base || strlen(args)) goto finish;
info_dbgprintf(
"Base address of the module: 0x%p [%s]\n", mod_base, img_name);
suggest_sects_chrt_name(mod_base, flags);
ret=S_OK;
finish:
return ret;
}
/* dump_serach family of commands */
HRESULT CALLBACK
dump_serach(PDEBUG_CLIENT4 Client, PCSTR args)
{
HRESULT ret=E_FAIL;
set_client(Client);
if (is_cmd(args, "idt", 1, &args))
{
DWORD flags = SRCHIDT_NO_ORD;
flag_desc_t flags_dsc[] =
{{'x', FALSE}, {'s', TRUE}, {'r', TRUE}, {'a', TRUE}, {'l', TRUE},
{0}};
rng_spec_t rng;
rngspc_rc_t rc = get_range_spec(&args, flags_dsc, &rng);
if (rc==rngspc_err) goto finish;
if (flags_dsc[0].is_pres) flags|=SRCHIDT_SILENT;
ULONG64 rng_addr=NULL;
if (rc==rngspc_ok && !rng.is_sect && !rng.rng.is_rva)
rng_addr=rng.rng.addr;
char img_name[MAX_PATH+1];
ULONG64 mod_base = get_mod_base(&args, rng_addr, img_name);
if (!mod_base || strlen(args)) goto finish;
if (!(flags&SRCHIDT_SILENT))
info_dbgprintf(
"Base address of the module: 0x%p [%s]\n", mod_base, img_name);
search_idt(mod_base, (rc==rngspc_not_prov ? NULL : &rng), flags);
}
ret=S_OK;
finish:
return ret;
}
/* dump_conf [conf_file] */
HRESULT CALLBACK
dump_conf(PDEBUG_CLIENT4 Client, PCSTR args)
{
PCSTR pc;
set_client(Client);
for (pc=args; isspace(*pc); pc++);
if (*pc)
{
set_prop_file(pc);
FILE *f = fopen(PROP_FILE, "r");
if (!f) {
warn_dbgprintf("Can not open config file: ");
} else {
info_dbgprintf("New config file: ");
fclose(f);
}
} else {
info_dbgprintf("Current config file: ");
}
dbgprintf("%s\n", PROP_FILE);
return S_OK;
}
/* help info */
HRESULT CALLBACK
help(PDEBUG_CLIENT4 Client, PCSTR args)
{
set_client(Client);
dbgprintf(
"dumpext: PE files fix, dump & analysis\n\n"
"dump_imp_scan iat [-w] [[-s *|n]|[-a|-r addr -l len]] [mod_base]\n"
" IAT imports scanning at specified memory range or of a given module with a\n"
" base mod_base. This is the most generic method for imports scanning.\n"
" -s,r,a,l: Scanning range specification, that is a section number ('*' denotes\n"
" all sections) or a memory range specified by an address or rva.\n"
" -w: Write resolved imports to the config file.\n"
"dump_imp_scan idt [-w] [-a|-r addr [-l len]] [mod_base]\n"
" IDT imports scanning at specified address of the IDT table or of a given\n"
" module with a base mod_base. This command may be used only if IDT location\n"
" is known and the IDT is not corrupted.\n"
" -a,r,l: Specifies an address or rva of the IDT table with an optional length\n"
" constraint.\n"
" -w: Write resolved imports to the config file.\n\n"
"dump_pe [-s *|n] [mod_base]\n"
" Dump a module with an address mod_base to a file. The configuration file is\n"
" inspected for parameters controlling the dump (e.g. sections specification,\n"
" various methods of fixing etc.)\n"
" -s: Specifies section to extract into separate file. '*' denotes all sections.\n\n"
"dump_pe_info [header] [-m] [-h] [-d] [-s] [mod_base]\n"
" Show PE details contained in the headers' part of a module with the address\n"
" mod_base. If no options are provided all PE details except the DOS header\n"
" are printed out.\n"
" -m: DOS header.\n"
" -h: PE headers (file and optional).\n"
" -d: PE directories info.\n"
" -s: Sections table.\n"
"dump_pe_info export|import|bimport|dimport|debug|lconf|rsrc|tls|reloc|except\n"
" [-c|-C] [-x] [-v|-V] [-a|-r addr [-l len]] [mod_base]\n"
" Show PE directory details at specified addr or of a given module with a base\n"
" mod_base.\n"
" -c,C: {rsrc} Show resource capacity details (-c); exclusively (-C).\n"
" -x: {import} Use the imports spec. format of the config file in the output.\n"
" -v,V: {except} Show verbose (-v) and even more verbose (-V) exception table\n"
" details. All the provided informations are platform specific.\n"
" -a,r: {all cases} Specifies an address or rva of the directory.\n"
" -l: {import,bimport,dimport,reloc,except} An optional length constraint.\n\n"
"dump_offset_info [-v] {-a addr}|{-f ftpr [mod_base]}\n"
" Show address or file pointer details.\n"
" -a: Address details.\n"
" -f: File pointer details.\n"
" -v: Show virtual memory details.\n\n"
"dump_sects_chrt [-c] [mod_base]\n"
" Analyse PE headers to recognize sections names and characteristics. Show\n"
" the result in the format ready to use in the configuration file. Useful for\n"
" packers destroying mentioned data.\n"
" -c: Read and take into account the configuration (PE headers, sections,\n"
" directories).\n\n"
"dump_serach idt [-x] [[-s *|n]|[-a|-r addr -l len]] [mod_base]\n"
" Search for and analyse the IDT table with IAT addresses matching the ones\n"
" specified in the imports spec. in the config file. The command shall be used\n"
" for searching destination location of the fixed imports with not modified\n"
" IDT table.\n"
" -s,r,a,l: Searching range specification, that is a section number ('*'\n"
" denotes all sections) or a memory range specified by an address or rva.\n"
" -x: Silent mode. In case of successful search the result is printed in the\n"
" format used by the configuration file.\n\n"
"dump_conf [conf_file]\n"
" Set configuration file to conf_file. If the file is not specified the current\n"
" configuration file is displayed.\n\n"
"help\n"
" Show this help.\n");
return S_OK;
}