forked from pstolarz/dumpext
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pebase.cpp
3147 lines (2730 loc) · 112 KB
/
pebase.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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
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 <sys/types.h>
#include <sys/stat.h>
/* type of a PE pointers */
typedef enum _pe_ptrtpy_t
{
pe_ptrtpy_raw=0, /* pointer to raw data */
pe_ptrtpy_rva /* relative virtual address */
} pe_ptrtpy_t;
/* PE dirs names */
static struct
{
const char *name_rva; /* name of a dir (rva part) */
const char *name_sz; /* name of a dir (size part) */
} dir_names[IMAGE_NUMBEROF_DIRECTORY_ENTRIES] =
{
{PROP_DIRS_EXP_RVA, PROP_DIRS_EXP_SZ},
{PROP_DIRS_IDT_RVA, PROP_DIRS_IDT_SZ},
{PROP_DIRS_RSRC_RVA, PROP_DIRS_RSRC_SZ},
{PROP_DIRS_EXPT_RVA, PROP_DIRS_EXPT_SZ},
{PROP_DIRS_CERT_RVA, PROP_DIRS_CERT_SZ},
{PROP_DIRS_RELOC_RVA, PROP_DIRS_RELOC_SZ},
{PROP_DIRS_DBG_RVA, PROP_DIRS_DBG_SZ},
{PROP_DIRS_ARCH_RVA, PROP_DIRS_ARCH_SZ},
{PROP_DIRS_GPTR_RVA, PROP_DIRS_GPTR_SZ},
{PROP_DIRS_TLS_RVA, PROP_DIRS_TLS_SZ},
{PROP_DIRS_CFG_RVA, PROP_DIRS_CFG_SZ},
{PROP_DIRS_BOUND_RVA, PROP_DIRS_BOUND_SZ},
{PROP_DIRS_IAT_RVA, PROP_DIRS_IAT_SZ},
{PROP_DIRS_DELAY_RVA, PROP_DIRS_DELAY_SZ},
{PROP_DIRS_CLR_RVA, PROP_DIRS_CLR_SZ},
{"Reserved.rva", "Reserved.size"}
};
/* section content bitmap flags */
#define SCONT_EXPORT 0x00000001U
#define SCONT_IMPORT 0x00000002U
#define SCONT_RSRC 0x00000004U
#define SCONT_EXCEPTION 0x00000008U
#define SCONT_SECURITY 0x00000010U
#define SCONT_RELOC 0x00000020U
#define SCONT_DEBUG 0x00000040U
#define SCONT_DEBUG_DTA 0x00000080U
#define SCONT_ARCH 0x00000100U
#define SCONT_TLS 0x00000200U
#define SCONT_TLS_DTA 0x00000400U
#define SCONT_LOAD_CFG 0x00000800U
#define SCONT_BND_IMPORT 0x00001000U
#define SCONT_DELAY_IMPORT 0x00002000U
#define SCONT_COM_DESC 0x00004000U
#define SCONT_CODE 0x00008000U
#define SCONT_DATA 0x00010000U
#define SCONT_COFF_SYMTAB 0x00020000U
#define SCONT_COFF_RELOC 0x00040000U
#define SCONT_COFF_LINE_NUM 0x00080000U
static struct
{
const char *pc_name; /* section name */
DWORD cont; /* max content */
DWORD chrt; /* typical characteristics */
} sects_pattrn[] =
{
{
".edata",
SCONT_EXPORT,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{
".idata",
SCONT_IMPORT,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{".rsrc",
SCONT_RSRC,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{
".tls",
SCONT_TLS_DTA,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_WRITE
},
{
".reloc",
SCONT_RELOC,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_DISCARDABLE
},
{
".debug",
SCONT_DEBUG_DTA|SCONT_COFF_SYMTAB|SCONT_COFF_RELOC|SCONT_COFF_LINE_NUM,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_DISCARDABLE
},
{
".didat",
SCONT_DELAY_IMPORT,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{
".pdata",
SCONT_EXCEPTION,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{
".rdata",
SCONT_EXPORT|SCONT_IMPORT|SCONT_EXCEPTION|SCONT_SECURITY|SCONT_RELOC|
SCONT_DEBUG|SCONT_DEBUG_DTA|SCONT_ARCH|SCONT_TLS|SCONT_LOAD_CFG|
SCONT_BND_IMPORT|SCONT_DELAY_IMPORT|SCONT_COM_DESC|SCONT_DATA|
SCONT_COFF_SYMTAB|SCONT_COFF_RELOC|SCONT_COFF_LINE_NUM,
IMAGE_SCN_CNT_INITIALIZED_DATA|IMAGE_SCN_MEM_READ
},
{
".text",
SCONT_EXPORT|SCONT_IMPORT|SCONT_EXCEPTION|SCONT_SECURITY|SCONT_RELOC|
SCONT_DEBUG|SCONT_DEBUG_DTA|SCONT_ARCH|SCONT_TLS|SCONT_LOAD_CFG|
SCONT_BND_IMPORT|SCONT_DELAY_IMPORT|SCONT_COM_DESC|SCONT_CODE|
SCONT_DATA|SCONT_COFF_SYMTAB|SCONT_COFF_RELOC|SCONT_COFF_LINE_NUM,
IMAGE_SCN_CNT_CODE|IMAGE_SCN_MEM_EXECUTE|IMAGE_SCN_MEM_READ
},
{NULL} /* end marker */
};
/* exported; see header for details */
BOOL read_pe_headers(ULONG64 mod_base, IMAGE_DOS_HEADER *p_dos_hdr,
image_nt_headers_t *p_nt_hdrs, ULONG64 *p_sectab_addr, BOOL b_logs)
{
ULONG cb;
BOOL ret=FALSE;
DWORD hdrs_off=0;
/* read DOS header */
if (!(read_memory(mod_base, p_dos_hdr, sizeof(*p_dos_hdr), &cb) &&
cb==sizeof(*p_dos_hdr))) goto finish;
if (p_dos_hdr->e_magic != IMAGE_DOS_SIGNATURE) {
if (b_logs) {
err_dbgprintf("Invalid DOS header; magic: 0x%04X\n",
(UINT)get_16uint_le(&p_dos_hdr->e_magic));
}
goto finish;
}
/* read PE signature, PE header & optional header */
hdrs_off = get_32uint_le(&p_dos_hdr->e_lfanew);
ULONG64 nt_hdrs_addr = mod_base + hdrs_off;
if (!(read_memory(
nt_hdrs_addr, &(p_nt_hdrs->hdr), sizeof(p_nt_hdrs->hdr), &cb)
&& cb==sizeof(p_nt_hdrs->hdr))) goto finish;
if (get_Signature(p_nt_hdrs) != IMAGE_NT_SIGNATURE)
{
if (b_logs) {
err_dbgprintf("Invalid PE header; signature 0x%08X\n",
get_32uint_le(&get_Signature(p_nt_hdrs)));
}
goto finish;
}
DWORD opt_hdr_sz =
get_16uint_le(&get_FileHeader(p_nt_hdrs).SizeOfOptionalHeader);
hdrs_off += sizeof(get_Signature(p_nt_hdrs)) +
sizeof(get_FileHeader(p_nt_hdrs)) + opt_hdr_sz;
if (!opt_hdr_sz) {
if (b_logs) err_dbgprintf("No optional header\n");
goto finish;
}
/* check the opt header */
WORD opt_magic = get_16uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.Magic);
if (opt_magic==IMAGE_NT_OPTIONAL_HDR32_MAGIC) p_nt_hdrs->pe_tpy=pe_32bit;
else
if (opt_magic==IMAGE_NT_OPTIONAL_HDR64_MAGIC) p_nt_hdrs->pe_tpy=pe_64bit;
else
{
if (b_logs)
err_dbgprintf("Unsupported PE type: 0x%04X\n", (UINT)opt_magic);
goto finish;
}
DWORD base_opt_hdr_len =
(p_nt_hdrs->pe_tpy==pe_32bit ?
((UINT8*)&p_nt_hdrs->hdr.pe32.OptionalHeader.DataDirectory -
(UINT8*)&p_nt_hdrs->hdr.pe32.OptionalHeader.Magic):
((UINT8*)&p_nt_hdrs->hdr.pe64.OptionalHeader.DataDirectory -
(UINT8*)&p_nt_hdrs->hdr.pe64.OptionalHeader.Magic));
if (base_opt_hdr_len >= opt_hdr_sz) {
if (b_logs) err_dbgprintf("No NT specific optional header\n");
goto finish;
}
if (p_sectab_addr) *p_sectab_addr = mod_base+hdrs_off;
ret=TRUE;
finish:
return ret;
}
/* exported; see header for details */
DWORD read_sectab(const image_nt_headers_t *p_nt_hdrs, ULONG64 sectab_addr,
IMAGE_SECTION_HEADER *p_sectab, BOOL b_fix_empty_rng, BOOL b_logs)
{
DWORD n_sects = get_16uint_le(&get_FileHeader(p_nt_hdrs).NumberOfSections);
if (n_sects > MAX_SECTIONS) {
err_dbgprintf("Number of sections in PE file exceeds %d\n", MAX_SECTIONS);
n_sects=0;
} else
if (n_sects)
{
ULONG cb;
DWORD sectab_len = sizeof(*p_sectab)*n_sects;
if (!(read_memory(
sectab_addr, p_sectab, sectab_len, &cb) && cb==sectab_len))
{
n_sects = 0;
} else
{
if (b_fix_empty_rng) {
for (DWORD i=0; i<n_sects; i++) {
DWORD sec_vsz = get_32uint_le(&p_sectab[i].Misc.VirtualSize);
DWORD sec_rva = get_32uint_le(&p_sectab[i].VirtualAddress);
DWORD sec_rsz = get_32uint_le(&p_sectab[i].SizeOfRawData);
DWORD sec_rptr = get_32uint_le(&p_sectab[i].PointerToRawData);
if (!sec_vsz || !sec_rva) {
set_32uint_le(&p_sectab[i].Misc.VirtualSize, 0);
set_32uint_le(&p_sectab[i].VirtualAddress, 0);
}
if (!sec_rsz || !sec_rptr) {
set_32uint_le(&p_sectab[i].SizeOfRawData, 0);
set_32uint_le(&p_sectab[i].PointerToRawData, 0);
}
}
}
}
}
return n_sects;
}
/* exported; see header for details */
BOOL get_data_dir(const image_nt_headers_t *p_nt_hdrs,
UINT dir_id, IMAGE_DATA_DIRECTORY **pp_dir_entry, BOOL b_logs)
{
BOOL ret=FALSE;
DWORD num_dir_ents =
(p_nt_hdrs->pe_tpy==pe_32bit ?
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.NumberOfRvaAndSizes):
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.NumberOfRvaAndSizes));
if (num_dir_ents <= dir_id) {
if (b_logs)
err_dbgprintf("No PE dir entry for directory 0x%02X\n", dir_id);
goto finish;
}
*pp_dir_entry =
(IMAGE_DATA_DIRECTORY*)(p_nt_hdrs->pe_tpy==pe_32bit ?
&p_nt_hdrs->hdr.pe32.OptionalHeader.DataDirectory[dir_id]:
&p_nt_hdrs->hdr.pe64.OptionalHeader.DataDirectory[dir_id]);
ret=TRUE;
finish:
return ret;
}
/* exported; see header for details */
BOOL get_rva_info(const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects, DWORD rva,
DWORD *p_sect_i, DWORD *p_n_raw_rem, DWORD *p_n_va_rem, DWORD *p_rptr)
{
if (p_sect_i) *p_sect_i = 0;
if (p_n_raw_rem) *p_n_raw_rem = 0;
if (p_n_va_rem) *p_n_va_rem = 0;
if (p_rptr) *p_rptr = 0;
DWORD i;
for (i=0; i<n_sects; i++)
{
DWORD sec_vsz = get_32uint_le(&p_sectab[i].Misc.VirtualSize);
DWORD sec_rva = get_32uint_le(&p_sectab[i].VirtualAddress);
if (sec_vsz && sec_rva<=rva && rva<sec_rva+sec_vsz)
{
if (p_sect_i) *p_sect_i = i;
DWORD sec_rsz = get_32uint_le(&p_sectab[i].SizeOfRawData);
DWORD sec_rptr = get_32uint_le(&p_sectab[i].PointerToRawData);
DWORD rva_off = rva-sec_rva;
if (p_n_va_rem) *p_n_va_rem = sec_vsz-rva_off;
if (rva_off<sec_rsz) {
if (p_rptr) *p_rptr = sec_rptr+rva_off;
if (p_n_raw_rem) *p_n_raw_rem = sec_rsz-rva_off;
}
break;
}
}
return i<n_sects;
}
/* exported; see header for details */
BOOL get_rptr_info(const IMAGE_SECTION_HEADER *p_sectab,
DWORD n_sects, DWORD rptr, DWORD *p_sect_i, DWORD *p_n_raw_rem, DWORD *p_rva)
{
if (p_sect_i) *p_sect_i = 0;
if (p_n_raw_rem) *p_n_raw_rem = 0;
if (p_rva) *p_rva = 0;
DWORD i;
for (i=0; i<n_sects; i++)
{
DWORD sec_rsz = get_32uint_le(&p_sectab[i].SizeOfRawData);
DWORD sec_rptr = get_32uint_le(&p_sectab[i].PointerToRawData);
if (sec_rsz && sec_rptr<=rptr && rptr<sec_rptr+sec_rsz)
{
if (p_sect_i) *p_sect_i = i;
DWORD sec_vsz = get_32uint_le(&p_sectab[i].Misc.VirtualSize);
DWORD sec_rva = get_32uint_le(&p_sectab[i].VirtualAddress);
DWORD rptr_off = rptr-sec_rptr;
if (p_n_raw_rem) *p_n_raw_rem = sec_rsz-rptr_off;
if (p_rva && sec_vsz) *p_rva = sec_rva+rptr_off;
break;
}
}
return i<n_sects;
}
typedef enum _ownfo_tpy_t
{
ownfo_info_cont=0, /* containment info */
ownfo_wrn_out, /* outside sects/header */
ownfo_wrn_sticks /* partially contained */
} ownfo_tpy_t;
/* Get info string (written under 'pc_sect_info' buffer at least 100 bytes long)
informing about owning section or header of 'ptr' pointer (type of the pointer
indicated by 'ptrtpy'). 'p_sectab' & 'n_sects' describes sections table. If
'p_info_tpy' is not NULL type of returned information is written. If sz>0
then additionally check inclusion in the owning section/header of a block 'sz'
long (starting from 'ptr').
*/
static void get_owner_info(ULONG64 mod_base, const image_nt_headers_t *p_nt_hdrs,
char *pc_sect_info, const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects,
DWORD ptr, pe_ptrtpy_t ptrtpy, DWORD sz, ownfo_tpy_t *p_info_tpy)
{
ownfo_tpy_t info_tpy = ownfo_info_cont;
DWORD hdrs_sz = (p_nt_hdrs->pe_tpy==pe_32bit ?
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfHeaders):
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfHeaders));
char ptr_addr[32];
if (ptrtpy==pe_ptrtpy_rva) {
sprintf(ptr_addr, "addr: 0x%p, ", RVA2ADDR(ptr, mod_base));
} else
*ptr_addr=0;
/* check belonging to the PE header;
RVA and raw pointer have the same values inside header */
if (0<=ptr && ptr<hdrs_sz)
{
if (ptr+sz>hdrs_sz) info_tpy=ownfo_wrn_sticks;
if (pc_sect_info) {
sprintf(pc_sect_info, " ; %sheader%s", ptr_addr,
(info_tpy==ownfo_wrn_sticks ?
", WARN: sticks out beyond header area!" : ""));
}
} else
{
/* check belonging to the sections */
DWORD sect_i;
DWORD n_rem;
BOOL rc = (ptrtpy==pe_ptrtpy_rva ?
get_rva_info(p_sectab, n_sects, ptr, §_i, NULL, &n_rem, NULL):
get_rptr_info(p_sectab, n_sects, ptr, §_i, &n_rem, NULL));
if (rc) {
if (sz>n_rem) info_tpy=ownfo_wrn_sticks;
if (pc_sect_info) {
sprintf(pc_sect_info, " ; %ssection %d%s", ptr_addr, sect_i+1,
(info_tpy==ownfo_wrn_sticks ?
", WARN: sticks out beyond its section!" : ""));
}
} else {
info_tpy=ownfo_wrn_out;
if (pc_sect_info) sprintf(pc_sect_info,
" ; %sWARN: not contained in any section!", ptr_addr);
}
}
if (p_info_tpy) *p_info_tpy=info_tpy;
}
/* Print DOS header details */
static void print_hdr_dos(const IMAGE_DOS_HEADER *p_dos_hdr)
{
dbgprintf("[dos_header]\n");
dbgprintf("e_magic = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_magic));
dbgprintf("e_cblp = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_cblp));
dbgprintf("e_cp = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_cp));
dbgprintf("e_crlc = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_crlc));
dbgprintf("e_cparhdr = 0x%04X\n",
(UINT)get_16uint_le(&p_dos_hdr->e_cparhdr));
dbgprintf("e_minalloc = 0x%04X\n",
(UINT)get_16uint_le(&p_dos_hdr->e_minalloc));
dbgprintf("e_maxalloc = 0x%04X\n",
(UINT)get_16uint_le(&p_dos_hdr->e_maxalloc));
dbgprintf("e_ss = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_ss));
dbgprintf("e_sp = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_sp));
dbgprintf("e_csum = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_csum));
dbgprintf("e_ip = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_ip));
dbgprintf("e_cs = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_cs));
dbgprintf("e_lfarlc = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_lfarlc));
dbgprintf("e_ovno = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_ovno));
dbgprintf("e_oemid = 0x%04X\n", (UINT)get_16uint_le(&p_dos_hdr->e_oemid));
dbgprintf("e_oeminfo = 0x%04X\n",
(UINT)get_16uint_le(&p_dos_hdr->e_oeminfo));
dbgprintf("e_lfanew = 0x%08X\n\n", get_32uint_le(&p_dos_hdr->e_lfanew));
}
/* print PE file header details */
static void print_hdr_file(ULONG64 mod_base, const image_nt_headers_t *p_nt_hdrs,
const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects)
{
const static str_num_t machine_ht[] = {
{"unknown", 0},
{"x86", 0x014c},
{"mips r3000 BE", 0x160},
{"mips r3000 LE", 0x162},
{"mips r4000 LE", 0x0166},
{"mips r10000 LE", 0x0168},
{"mips wce v2 LE", 0x0169},
{"alpha", 0x0184},
{"sh3 LE", 0x01a2},
{"sh3 dsp", 0x01a3},
{"sh3e LE", 0x01a4},
{"sh4 LE", 0x01a6},
{"sh5", 0x01a8},
{"arm LE", 0x01c0},
{"thumb", 0x01c2},
{"arm v7", 0x01c4},
{"am33", 0x01d3},
{"ppc LE", 0x01f0},
{"ppc fpu", 0x01f1},
{"ia64", 0x0200},
{"mips 16", 0x0266},
{"alpha64", 0x0284},
{"mips fpu", 0x0366},
{"mips fpu 16", 0x0466},
{"infineon", 0x0520},
{"cef", 0x0CEF},
{"efi", 0x0EBC},
{"x64", 0x8664},
{"m32r LE", 0x9041},
{"cee", 0xC0EE},
{NULL}
};
const IMAGE_FILE_HEADER *p_fh = &get_FileHeader(p_nt_hdrs);
DWORD rptr;
WORD chrt, machine;
char sect_info[100];
dbgprintf("[file_header]\n");
dbgprintf("Machine = 0x%04X ; %s\n",
(UINT)(machine=get_16uint_le(&p_fh->Machine)),
get_ht_str(machine_ht, (DWORD)machine, "???"));
dbgprintf("NumberOfSections = 0x%04X\n",
(UINT)get_16uint_le(&p_fh->NumberOfSections));
dbgprintf("TimeDateStamp = 0x%08X\n", get_32uint_le(&p_fh->TimeDateStamp));
DWORD n_syms = get_32uint_le(&p_fh->NumberOfSymbols);
if (rptr = get_32uint_le(&p_fh->PointerToSymbolTable)) {
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rptr, pe_ptrtpy_raw, n_syms*sizeof(IMAGE_SYMBOL), NULL);
} else *sect_info=0;
dbgprintf("PointerToSymbolTable = 0x%08X%s\n", rptr, sect_info);
dbgprintf("NumberOfSymbols = 0x%08X\n", n_syms);
dbgprintf("SizeOfOptionalHeader = 0x%04X\n",
(UINT)get_16uint_le(&p_fh->SizeOfOptionalHeader));
dbgprintf("Characteristics = 0x%04X",
(UINT)(chrt = get_16uint_le(&p_fh->Characteristics)));
print_flags(FLCHRVALS_HT, (DWORD)chrt, 16);
dbgprintf("\n");
}
/* Print PE optional header details */
static void print_hdr_opt(ULONG64 mod_base, const image_nt_headers_t *p_nt_hdrs,
const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects)
{
const static str_num_t subsys_ht[] = {
{"unknown", 0},
{"native", 1},
{"win gui", 2},
{"win cui", 3},
{"os2 cui", 5},
{"posix cui", 7},
{"win native", 8},
{"win ce gui", 9},
{"efi app", 10},
{"efi boot service driver", 11},
{"efi runtime driver", 12},
{"efi rom", 13},
{"xbox", 14},
{"win boot app", 16},
{NULL}
};
DWORD rva;
WORD chrt, subsys;
char sect_info[100];
dbgprintf("[%s]\n", PROP_SECT_OPTH);
if (p_nt_hdrs->pe_tpy==pe_32bit)
{
dbgprintf("Magic = 0x%04X ; PE32\n",
(UINT)get_16uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.Magic));
dbgprintf("MajorLinkerVersion = 0x%02X\n",
(UINT)p_nt_hdrs->hdr.pe32.OptionalHeader.MajorLinkerVersion);
dbgprintf("MinorLinkerVersion = 0x%02X\n",
(UINT)p_nt_hdrs->hdr.pe32.OptionalHeader.MinorLinkerVersion);
dbgprintf("SizeOfCode = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfCode));
dbgprintf("SizeOfInitializedData = 0x%08X\n",
get_32uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfInitializedData));
dbgprintf("SizeOfUninitializedData = 0x%08X\n",
get_32uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfUninitializedData));
if (rva = get_32uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.AddressOfEntryPoint))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rva, pe_ptrtpy_rva, 0, NULL);
} else
*sect_info=0;
dbgprintf("%s = 0x%08X%s\n", PROP_OPTH_ENTRY_POINT, rva, sect_info);
if (rva = get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.BaseOfCode))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rva, pe_ptrtpy_rva, 0, NULL);
} else
*sect_info=0;
dbgprintf("%s = 0x%08X%s\n", PROP_OPTH_BASE_CODE, rva, sect_info);
if (rva = get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.BaseOfData))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rva, pe_ptrtpy_rva, 0, NULL);
} else
*sect_info=0;
dbgprintf("%s = 0x%08X%s\n", PROP_OPTH_BASE_DATA, rva, sect_info);
dbgprintf("ImageBase = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.ImageBase));
dbgprintf("SectionAlignment = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SectionAlignment));
dbgprintf("FileAlignment = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.FileAlignment));
dbgprintf("MajorOperatingSystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MajorOperatingSystemVersion));
dbgprintf("MinorOperatingSystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MinorOperatingSystemVersion));
dbgprintf("MajorImageVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MajorImageVersion));
dbgprintf("MinorImageVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MinorImageVersion));
dbgprintf("MajorSubsystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MajorSubsystemVersion));
dbgprintf("MinorSubsystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.MinorSubsystemVersion));
dbgprintf("Win32VersionValue = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.Win32VersionValue));
dbgprintf("SizeOfImage = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfImage));
dbgprintf("SizeOfHeaders = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfHeaders));
dbgprintf("CheckSum = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.CheckSum));
dbgprintf("Subsystem = 0x%04X ; %s\n",
(UINT)(subsys=get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.Subsystem)),
get_ht_str(subsys_ht, (DWORD)subsys, "???"));
dbgprintf("DllCharacteristics = 0x%04X",
(UINT)(chrt=get_16uint_le(
&p_nt_hdrs->hdr.pe32.OptionalHeader.DllCharacteristics)));
print_flags(DLLCHRVALS_HT, (DWORD)chrt, 16);
dbgprintf("SizeOfStackReserve = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfStackReserve));
dbgprintf("SizeOfStackCommit = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfStackCommit));
dbgprintf("SizeOfHeapReserve = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfHeapReserve));
dbgprintf("SizeOfHeapCommit = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.SizeOfHeapCommit));
dbgprintf("LoaderFlags = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.LoaderFlags));
dbgprintf("NumberOfRvaAndSizes = 0x%08X\n\n",
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.NumberOfRvaAndSizes));
} else
{
dbgprintf("Magic = 0x%04X ; PE32+\n",
(UINT)get_16uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.Magic));
dbgprintf("MajorLinkerVersion = 0x%02X\n",
(UINT)p_nt_hdrs->hdr.pe64.OptionalHeader.MajorLinkerVersion);
dbgprintf("MinorLinkerVersion = 0x%02X\n",
(UINT)p_nt_hdrs->hdr.pe64.OptionalHeader.MinorLinkerVersion);
dbgprintf("SizeOfCode = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfCode));
dbgprintf("SizeOfInitializedData = 0x%08X\n",
get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfInitializedData));
dbgprintf("SizeOfUninitializedData = 0x%08X\n",
get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfUninitializedData));
if (rva = get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.AddressOfEntryPoint))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rva, pe_ptrtpy_rva, 0, NULL);
} else
*sect_info=0;
dbgprintf("%s = 0x%08X%s\n", PROP_OPTH_ENTRY_POINT, rva, sect_info);
if (rva = get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.BaseOfCode))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rva, pe_ptrtpy_rva, 0, NULL);
} else
*sect_info=0;
dbgprintf("%s = 0x%08X%s\n", PROP_OPTH_BASE_CODE, rva, sect_info);
dbgprintf("ImageBase = 0x%016I64X\n",
get_64uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.ImageBase));
dbgprintf("SectionAlignment = 0x%08X\n", get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SectionAlignment));
dbgprintf("FileAlignment = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.FileAlignment));
dbgprintf("MajorOperatingSystemVersion = 0x%04X\n", (UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MajorOperatingSystemVersion));
dbgprintf("MinorOperatingSystemVersion = 0x%04X\n", (UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MinorOperatingSystemVersion));
dbgprintf("MajorImageVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MajorImageVersion));
dbgprintf("MinorImageVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MinorImageVersion));
dbgprintf("MajorSubsystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MajorSubsystemVersion));
dbgprintf("MinorSubsystemVersion = 0x%04X\n",
(UINT)get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.MinorSubsystemVersion));
dbgprintf("Win32VersionValue = 0x%08X\n", get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.Win32VersionValue));
dbgprintf("SizeOfImage = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfImage));
dbgprintf("SizeOfHeaders = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfHeaders));
dbgprintf("CheckSum = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.CheckSum));
dbgprintf("Subsystem = 0x%04X ; %s\n",
(UINT)(subsys=get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.Subsystem)),
get_ht_str(subsys_ht, (DWORD)subsys, "???"));
dbgprintf("DllCharacteristics = 0x%04X",
(UINT)(chrt=get_16uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.DllCharacteristics)));
print_flags(DLLCHRVALS_HT, (DWORD)chrt, 16);
dbgprintf("SizeOfStackReserve = 0x%016I64X\n", get_64uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfStackReserve));
dbgprintf("SizeOfStackCommit = 0x%016I64X\n", get_64uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfStackCommit));
dbgprintf("SizeOfHeapReserve = 0x%016I64X\n", get_64uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfHeapReserve));
dbgprintf("SizeOfHeapCommit = 0x%016I64X\n", get_64uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.SizeOfHeapCommit));
dbgprintf("LoaderFlags = 0x%08X\n",
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.LoaderFlags));
dbgprintf("NumberOfRvaAndSizes = 0x%08X\n\n", get_32uint_le(
&p_nt_hdrs->hdr.pe64.OptionalHeader.NumberOfRvaAndSizes));
}
}
typedef enum _dirfld_t
{
dirfld_rva=0,
dirfld_size
} dirfld_t;
/* Get PE's directory's field name and write it to the buffer 'pc_out_name' min.
32 bytes long.
*/
static void get_dir_fld_name(UINT dir_id, dirfld_t dirfld, char *pc_out_name)
{
if (dirfld==dirfld_rva)
{
if (dir_id<IMAGE_NUMBEROF_DIRECTORY_ENTRIES) {
sprintf(pc_out_name, "%s", dir_names[dir_id].name_rva);
} else {
sprintf(pc_out_name, "%d.rva", dir_id+1);
}
} else
{
if (dir_id<IMAGE_NUMBEROF_DIRECTORY_ENTRIES) {
sprintf(pc_out_name, "%s", dir_names[dir_id].name_sz);
} else {
sprintf(pc_out_name, "%d.size", dir_id+1);
}
}
}
/* Print PE directories details */
static void print_pe_dirs(ULONG64 mod_base, const image_nt_headers_t *p_nt_hdrs,
const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects)
{
DWORD num_dir_ents =
(p_nt_hdrs->pe_tpy==pe_32bit ?
get_32uint_le(&p_nt_hdrs->hdr.pe32.OptionalHeader.NumberOfRvaAndSizes):
get_32uint_le(&p_nt_hdrs->hdr.pe64.OptionalHeader.NumberOfRvaAndSizes));
dbgprintf("[%s]\n", PROP_SECT_DIRS);
for (DWORD i=0; i<num_dir_ents; i++)
{
const IMAGE_DATA_DIRECTORY *p_dir_ent =
(p_nt_hdrs->pe_tpy==pe_32bit ?
&p_nt_hdrs->hdr.pe32.OptionalHeader.DataDirectory[i]:
&p_nt_hdrs->hdr.pe64.OptionalHeader.DataDirectory[i]);
DWORD rva = get_32uint_le(&p_dir_ent->VirtualAddress);
DWORD sz = get_32uint_le(&p_dir_ent->Size);
char sect_info[100];
*sect_info = 0;
if (rva) {
if (i==IMAGE_DIRECTORY_ENTRY_SECURITY)
{
ownfo_tpy_t ownfo_tpy;
get_owner_info(mod_base, p_nt_hdrs, sect_info,
p_sectab, n_sects, rva, pe_ptrtpy_raw, sz, &ownfo_tpy);
if (ownfo_tpy!=ownfo_info_cont)
{
if (ownfo_tpy==ownfo_wrn_out) {
strcpy(sect_info, " ; file ptr, "
"WARN: not contained in any section!");
} else {
strcpy(sect_info, " ; file ptr, "
"WARN: sticks out beyond its section size!");
}
}
} else {
get_owner_info(mod_base, p_nt_hdrs, sect_info,
p_sectab, n_sects, rva, pe_ptrtpy_rva, sz, NULL);
}
}
char prm_name[32];
get_dir_fld_name(i, dirfld_rva, prm_name);
dbgprintf("%s = 0x%08X%s\n", prm_name, rva, sect_info);
get_dir_fld_name(i, dirfld_size, prm_name);
dbgprintf("%s = 0x%08X\n", prm_name, sz);
}
dbgprintf("\n");
}
/* Print sections table details */
static void print_sectab(ULONG64 mod_base, const image_nt_headers_t *p_nt_hdrs,
const IMAGE_SECTION_HEADER *p_sectab, DWORD n_sects)
{
dbgprintf("[%s]\n", PROP_SECT_SECTS);
for (DWORD i=0; i<n_sects; i++)
{
DWORD chrt=0, rptr, rva;
char sect_info[100];
char sec_name[IMAGE_SIZEOF_SHORT_NAME+1];
strncpy(sec_name, (char*)&p_sectab[i].Name[0], sizeof(sec_name)-1);
sec_name[sizeof(sec_name)-1] = 0;
dbgprintf("%d.%s = %s\n", i+1, PROP_SECTS_NAME, sec_name);
dbgprintf("%d.%s = 0x%08X\n", i+1,
PROP_SECTS_VSZ, get_32uint_le(&p_sectab[i].Misc.VirtualSize));
rva = get_32uint_le(&p_sectab[i].VirtualAddress);
dbgprintf("%d.VirtualAddress = 0x%08X", i+1, rva);
if (rva) dbgprintf(" ; addr: 0x%p\n", RVA2ADDR(rva, mod_base));
else dbgprintf("\n");
dbgprintf("%d.%s = 0x%08X\n",
i+1, PROP_SECTS_RSZ, get_32uint_le(&p_sectab[i].SizeOfRawData));
dbgprintf("%d.PointerToRawData = 0x%08X\n",
i+1, get_32uint_le(&p_sectab[i].PointerToRawData));
DWORD n_relocs = get_16uint_le(&p_sectab[i].NumberOfRelocations);
if (rptr = get_32uint_le(&p_sectab[i].PointerToRelocations))
{
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rptr, pe_ptrtpy_raw, n_relocs*sizeof(IMAGE_RELOCATION), NULL);
} else *sect_info=0;
dbgprintf("%d.PointerToRelocations = 0x%08X%s\n", i+1, rptr, sect_info);
DWORD n_lines = get_16uint_le(&p_sectab[i].NumberOfLinenumbers);
if (rptr = get_32uint_le(&p_sectab[i].PointerToLinenumbers)) {
get_owner_info(mod_base, p_nt_hdrs, sect_info, p_sectab, n_sects,
rptr, pe_ptrtpy_raw, n_lines*sizeof(IMAGE_LINENUMBER), NULL);
} else *sect_info=0;
dbgprintf("%d.PointerToLinenumbers = 0x%08X%s\n", i+1, rptr, sect_info);
dbgprintf("%d.NumberOfRelocations = 0x%04X\n", i+1, n_relocs);
dbgprintf("%d.NumberOfLinenumbers = 0x%04X\n", i+1, n_lines);
dbgprintf("%d.%s = 0x%08X", i+1, PROP_SECTS_CHARACTER,
(chrt = get_32uint_le(&p_sectab[i].Characteristics)));
print_flags(SECCHRVALS_HT, chrt, 32);
}
dbgprintf("\n");
}
/* exported; see header for details */
void print_pe_details(ULONG64 mod_base, DWORD flags)
{
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
ULONG64 sectab_addr;
if (!read_pe_headers(mod_base, &dos_hdr, &nt_hdrs, §ab_addr, TRUE))
goto finish;
IMAGE_SECTION_HEADER sectab[MAX_SECTIONS];
DWORD n_sects = read_sectab(&nt_hdrs, sectab_addr, sectab, FALSE, TRUE);
if (flags&PRNTPE_DOS_HEADER) print_hdr_dos(&dos_hdr);
if (flags&PRNTPE_PE_HEADERS) {
print_hdr_file(mod_base, &nt_hdrs, sectab, n_sects);
print_hdr_opt(mod_base, &nt_hdrs, sectab, n_sects);
}
if (flags&PRNTPE_DIRS) print_pe_dirs(mod_base, &nt_hdrs, sectab, n_sects);
if (flags&PRNTPE_SECTS) print_sectab(mod_base, &nt_hdrs, sectab, n_sects);
finish:
return;
}
/* exported; see header for details */
BOOL init_prnt_dir_hndl(prnt_dir_hndl_t *p_hndl,
ULONG64 mod_base, UINT dir_id, const rng_spec_t *p_rng)
{
BOOL ret=FALSE;
memset(p_hndl, 0, sizeof(*p_hndl));
p_hndl->mod_base = mod_base;
/* read PE header and the directory */
ULONG64 sectab_addr;
IMAGE_DOS_HEADER dos_hdr;
if (!read_pe_headers(
mod_base, &dos_hdr, &p_hndl->nt_hdrs, §ab_addr, TRUE)) goto finish;
p_hndl->n_sects =
read_sectab(&p_hndl->nt_hdrs, sectab_addr, p_hndl->sectab, TRUE, TRUE);
p_hndl->dir_id = dir_id;
if (!p_rng)
{
IMAGE_DATA_DIRECTORY *p_dd;
if (!get_data_dir(&p_hndl->nt_hdrs, dir_id, &p_dd, FALSE)) {
info_dbgprintf("No data directory no. %d in the module\n", dir_id);
goto finish;
}
p_hndl->dir_addr =
RVA2ADDR(get_32uint_le(&p_dd->VirtualAddress), mod_base);
p_hndl->dir_sz = get_32uint_le(&p_dd->Size);
} else
{
if (p_rng->is_sect) goto finish;
if (p_rng->rng.is_rva) {
p_hndl->dir_addr = RVA2ADDR(p_rng->rng.rva, mod_base);
} else {
p_hndl->dir_addr = p_rng->rng.addr;
}
if (!p_hndl->dir_addr || !p_rng->rng.len) goto finish;
p_hndl->dir_sz = 0;
}
ret=TRUE;
finish:
return ret;
}
/* print_lconf() support routine */
static void print_seh_hndlrs(
UINT64 seh_hndlrs_addr, UINT64 n_seh_hndlrs, UINT64 mod_base)
{
ULONG cb;
DWORD hndlr_rva;
if (n_seh_hndlrs) dbgprintf(" SEH handlers:\n");
for (;n_seh_hndlrs; n_seh_hndlrs--, seh_hndlrs_addr+=sizeof(hndlr_rva))
{
if (!(read_memory(seh_hndlrs_addr, &hndlr_rva, sizeof(hndlr_rva), &cb)
&& cb==sizeof(hndlr_rva))) break;
dbgprintf(" 0x%p[0x%08X]\n", RVA2ADDR(hndlr_rva, mod_base), hndlr_rva);
}
}
/* exported; see header for details */
void print_lconf(ULONG64 mod_base, const rng_spec_t *p_rng)
{
prnt_dir_hndl_t hndl;
if (!init_prnt_dir_hndl(
&hndl, mod_base, IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG, p_rng)) goto finish;
/* print header */
if (!hndl.dir_addr || (!p_rng && !hndl.dir_sz)) {
info_dbgprintf("No load config descr. in this module!\n");
goto finish;
} else
if (!p_rng && hndl.dir_sz!=0x40) {
info_dbgprintf(
"Unrecognised load config descr. with size 0x04X\n", hndl.dir_sz);
goto finish;
} else {
info_dbgprintf("Load config descr. at: 0x%p\n", hndl.dir_addr);
}
info_dbgprintf("RVA provided in []\n\n");
ULONG cb;
ULONG64 addr, n_seh_hndlrs;
if (hndl.nt_hdrs.pe_tpy==pe_32bit)
{
DWORD sec_cookie;
/* 32-bit load conf */
IMAGE_LOAD_CONFIG_DIRECTORY32 lconf;
if (!(read_memory(hndl.dir_addr, &lconf, sizeof(lconf), &cb) &&