forked from pstolarz/dumpext
-
Notifications
You must be signed in to change notification settings - Fork 2
/
imports.cpp
3931 lines (3308 loc) · 130 KB
/
imports.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 <imagehlp.h>
#include <errno.h>
/* max number of symbols in a dll with an identical addr */
#define MAX_SYNONS 20U
/* max number of forwarders' modules taken into account
during resolving an owning module of its IAT table */
#define MAX_FRWRDS 32U
#define IDTSPEC_NO_PADD_NAMES 0x01U
#define IDTSPEC_NO_ILTS 0x02U
typedef struct _imp_proc_desc_t
{
/* ordinal of a proc (index in the export addr table;
"base-ordinal" based); required to be set if proc has no name */
DWORD ord;
/* hint of a proc (index in name/hint table; 0-based); if a proc
has no name (only an ordinal) hint is set to (DWORD)-1 */
DWORD hint;
/* number of duplicates of the proc (set only
for the 1st dup in the module's list of procs) */
DWORD dups;
/* next proc in the chain */
_imp_proc_desc_t *next;
/* proc name (NULL term; rounded to even bytes by padding zero);
if a proc has no name (only ordinal) this string is set to "" */
char name[1];
} imp_proc_desc_t;
typedef struct _imp_mod_desc_t
{
/* imported module's base (may be NULL for imports read
form the conf file and not able to be resolved) */
ULONG64 mod_base;
/* module's IAT table address */
ULONG64 iat_addr;
/* num of proc descs in the list */
DWORD n_procs;
/* 1st imported proc desc of the list */
imp_proc_desc_t *proc;
/* number of duplicates of the module (set only
for the 1st dup in the list of importing mods) */
DWORD dups;
/* size of module's Hint/Name Table (in bytes) */
DWORD hnt_sz;
/* size of module's Hint/Name Table (in bytes) w/o even rounded proc names */
DWORD hnt_nrnd_sz;
/* next module in the chain */
_imp_mod_desc_t *next;
/* module name (NULL term; rounded to even bytes by padding zero) */
char name[1];
} imp_mod_desc_t;
/* selected module's export directory's fields */
typedef struct _mod_exp_dir_t
{
/* module's base addr */
ULONG64 mod_base;
/* exp dir data addr & size */
ULONG64 exp_dd_addr;
DWORD exp_dd_sz;
/* exp dir data content */
DWORD ord_base; /* ordinal's base */
DWORD n_faddrs; /* number of functions in EAT */
DWORD n_fnames; /* number of exported names */
ULONG64 mod_name_addr; /* module's name address */
ULONG64 faddrs_addr; /* address of EAT */
ULONG64 fnames_addr; /* address of names table */
ULONG64 fords_addr; /* address of ordinals table */
} mod_exp_dir_t;
/* struct of internal handle for imports scan */
typedef struct _scan_imps_hndl_t
{
/* module's base */
ULONG64 mod_base;
/* module's headers */
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
/* sections table */
DWORD n_sects;
IMAGE_SECTION_HEADER sectab[MAX_SECTIONS];
/* TRUE if 32bit target emulated on Wow64 platform */
BOOL wow64_emul;
/* flag indicating end of current module's
imports and a need to create a new one */
BOOL crt_new_list;
DWORD n_mods; /* number of modules in the imp_mods list */
imp_mod_desc_t *p_imp_mods; /* pointer to the head of mod desc's struct */
imp_mod_desc_t *p_last_mod; /* lastly added mod desc */
imp_proc_desc_t *p_last_proc; /* lastly added proc desc */
mod_exp_dir_t ed_reslv; /* cache of exp dir - proc resolving */
mod_exp_dir_t ed_frwrd; /* cache of exp dir - forwards resolving */
} scan_imps_hndl_t;
/* delay load description struct */
typedef struct _img_delay_descr_t
{
DWORD attrs; /* attributes */
DWORD rvaDLLName; /* RVA to dll name */
DWORD rvaHmod; /* RVA of module handle */
DWORD rvaIAT; /* RVA of the IAT */
DWORD rvaILT; /* RVA of the ILT */
DWORD rvaBoundIAT; /* RVA of the optional bound IAT */
DWORD rvaUnloadIAT; /* RVA of optional copy of original IAT */
DWORD dwTimeStamp; /* 0 if not bound, otherwise
date/time stamp of DLL bound to (Old BIND) */
} img_delay_descr_t;
/* Delay Load Attributes */
enum delay_imp_attr
{
/* RVAs are used instead of pointers */
dlattr_rva = 0x1
};
/* Read the export directory and write the data into the structure pointed by
'p_ed'. The func requires p_ed->mod_base to be set to the base of the module
whose export dir need to be read. If 'b_logs' is TRUE print error info.
Returns TRUE for success, FALSE otherwise
*/
static BOOL get_mod_exp_dir(mod_exp_dir_t *p_ed, BOOL b_logs)
{
BOOL ret=FALSE;
IMAGE_DOS_HEADER dos_hdr;
image_nt_headers_t nt_hdrs;
/* read PE header and the export directory */
if (!read_pe_headers(p_ed->mod_base, &dos_hdr, &nt_hdrs, NULL, b_logs))
goto finish;
IMAGE_DATA_DIRECTORY *p_dd_exp;
if (!get_data_dir(&nt_hdrs, IMAGE_DIRECTORY_ENTRY_EXPORT, &p_dd_exp, b_logs))
goto finish;
DWORD exp_dd_rva = get_32uint_le(&p_dd_exp->VirtualAddress);
p_ed->exp_dd_addr = RVA2ADDR(exp_dd_rva, p_ed->mod_base);
p_ed->exp_dd_sz = get_32uint_le(&p_dd_exp->Size);
if (!exp_dd_rva || !p_ed->exp_dd_sz) {
if (b_logs)
err_dbgprintf(
"No export directory for a module with the base addr: 0x%p\n",
p_ed->mod_base);
goto finish;
}
ULONG cb;
IMAGE_EXPORT_DIRECTORY exp_dir;
/* retrieve export table info */
if (!(read_memory(p_ed->exp_dd_addr,
&exp_dir, sizeof(exp_dir), &cb) && cb==sizeof(exp_dir))) goto finish;
p_ed->ord_base = get_32uint_le(&exp_dir.Base);
p_ed->n_faddrs = get_32uint_le(&exp_dir.NumberOfFunctions);
p_ed->n_fnames = get_32uint_le(&exp_dir.NumberOfNames);
p_ed->mod_name_addr =
RVA2ADDR(get_32uint_le(&exp_dir.Name), p_ed->mod_base);
p_ed->faddrs_addr =
RVA2ADDR(get_32uint_le(&exp_dir.AddressOfFunctions), p_ed->mod_base);
p_ed->fnames_addr =
RVA2ADDR(get_32uint_le(&exp_dir.AddressOfNames), p_ed->mod_base);
p_ed->fords_addr =
RVA2ADDR(get_32uint_le(&exp_dir.AddressOfNameOrdinals), p_ed->mod_base);
ret=TRUE;
finish:
return ret;
}
/* Search for the proc name pointed by 'pc_proc_name' in the exported table of
names. Optimized method basing on a lexical order of names in the exporting
table. If the name is found the func returns TRUE and proc's hint & ordinal
(zero-based) are returned under 'p_hint' and 'p_ord'.
*/
static BOOL get_name_hint_ord(const mod_exp_dir_t *p_ed,
const char *pc_proc_name, DWORD *p_hint, DWORD *p_ord)
{
ULONG cb;
BOOL ret=FALSE, found=FALSE;
BOOL loop_finish=FALSE;
if (!p_ed->n_fnames) goto finish;
for (DWORD h_min=0, h_max=p_ed->n_fnames-1, h_mid; !loop_finish;)
{
if (h_min==h_max) loop_finish=TRUE;
/* read proc name address in the middle of the searching range */
h_mid = (h_min+h_max)/2;
DWORD proc_name_rva;
if (!(read_memory(p_ed->fnames_addr + h_mid*sizeof(proc_name_rva),
&proc_name_rva, sizeof(proc_name_rva), &cb) &&
cb==sizeof(proc_name_rva))) goto finish;
ULONG64 proc_name_addr =
RVA2ADDR(get_32uint_le(&proc_name_rva), p_ed->mod_base);
int cmp_res = string_cmp_lt(pc_proc_name, proc_name_addr);
if (!cmp_res) {
*p_hint = h_mid;
found = TRUE;
break;
} else
if (cmp_res>0) {
h_min = (h_min!=h_mid ? h_mid : h_max);
} else {
if (h_min!=h_mid) h_max=h_mid;
else break;
}
}
if (found) {
WORD ord;
if (!(read_memory(p_ed->fords_addr + *p_hint*sizeof(ord),
&ord, sizeof(ord), &cb) && cb==sizeof(ord))) goto finish;
*p_ord = get_16uint_le(&ord);
}
ret=TRUE;
finish:
return ret;
}
/* Count how many modules from 'p_mods' list ('n_mods' long, less or equal to
MAX_FRWRDS) are referenced as forwarders from the EAT table of the import
module with 'mod_base'. Name of the module is additionally returned under
'pc_mod_name' (min MAX_PATH+1 long).
The func returns TRUE on success and the count number under 'p_cnt'.
*/
static BOOL count_forwards(
IDebugSymbols *DebugSymbols, ULONG64 mod_base, ULONG64 *p_mods,
UINT n_mods, UINT *p_cnt, char *pc_mod_name, BOOL b_logs=FALSE)
{
BOOL ret=FALSE;
*p_cnt=0;
mod_exp_dir_t ed;
ed.mod_base = mod_base;
if (!get_mod_exp_dir(&ed, b_logs)) goto finish;
if (!string_cpy_lt(pc_mod_name, ed.mod_name_addr, MAX_PATH+1)) goto finish;
DWORD ord;
ULONG64 faddrs_addr;
/* flags table of already found modules */
BOOL found_mods[MAX_FRWRDS];
memset(found_mods, 0, sizeof(found_mods));
ret=TRUE;
/* go through EAT looking for forwards */
DWORD exp_proc_rva;
for (ord=0, faddrs_addr=ed.faddrs_addr;
ord < ed.n_faddrs;
faddrs_addr+=sizeof(exp_proc_rva), ord++)
{
ULONG cb;
if (!(read_memory(faddrs_addr, &exp_proc_rva, sizeof(exp_proc_rva), &cb)
&& cb==sizeof(exp_proc_rva))) goto finish;
ULONG64 exp_proc_addr = RVA2ADDR(get_32uint_le(&exp_proc_rva), mod_base);
if ((ed.exp_dd_addr <= exp_proc_addr) &&
(exp_proc_addr < ed.exp_dd_addr+ed.exp_dd_sz))
{
/* forward is found, get its mod name & base */
char frwrd_name[MAX_PATH+1];
ULONG64 frwrd_base;
if (!string_cpy_lt(
frwrd_name, exp_proc_addr, sizeof(frwrd_name), '.')) continue;
if (DebugSymbols->GetModuleByModuleName(
frwrd_name, 0, NULL, &frwrd_base)!=S_OK) continue;
for (UINT i=0; i<n_mods; i++)
if (p_mods[i]==frwrd_base && !found_mods[i])
{
(*p_cnt)++;
found_mods[i]=TRUE;
break;
}
}
}
finish:
return ret;
}
/* Get import module desc from index 'i' (0-based) */
static imp_mod_desc_t *get_imp_mod(const imp_mod_desc_t *p_imp_mods, UINT i)
{
UINT j;
const imp_mod_desc_t *p_mod = p_imp_mods;
for (j=0; j!=i && p_mod; j++, p_mod=p_mod->next);
if (j!=i) p_mod=NULL;
return (imp_mod_desc_t*)p_mod;
}
/* Free all memory allocated by the imp_mods's structs */
static void free_imp_mods(imp_mod_desc_t *p_imp_mods)
{
void *to_free;
for (imp_mod_desc_t *mod=p_imp_mods; mod;) {
for (imp_proc_desc_t *proc=mod->proc; proc; ) {
to_free=proc;
proc=proc->next;
free(to_free);
}
to_free=mod;
mod=mod->next;
free(to_free);
}
}
/* Free imports stored in scan imports handle */
static void free_imps_in_scan_imps_hndl(scan_imps_hndl_t *p_hndl)
{
if (p_hndl->p_imp_mods) {
free_imp_mods(p_hndl->p_imp_mods);
p_hndl->p_imp_mods=NULL;
}
p_hndl->p_last_mod=NULL;
p_hndl->p_last_proc=NULL;
p_hndl->n_mods=0;
p_hndl->crt_new_list=FALSE;
}
/* Free scan imports handle */
static void free_scan_imps_hndl(scan_imps_hndl_t *p_hndl)
{
free_imps_in_scan_imps_hndl(p_hndl);
}
/* Initialize scan imports handle; returns TRUE on success */
static BOOL init_scan_imps_hndl(
scan_imps_hndl_t *p_hndl, ULONG64 mod_base, BOOL b_wow_chk)
{
BOOL ret=FALSE;
IDebugSymbols *DebugSymbols=NULL;
memset(p_hndl, 0, sizeof(*p_hndl));
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols), (void **)&DebugSymbols)) != S_OK) goto finish;
p_hndl->mod_base = mod_base;
ULONG64 sectab_addr;
if (!read_pe_headers(mod_base,
&p_hndl->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);
if (!p_hndl->n_sects) goto finish;
ULONG64 mb;
if (b_wow_chk &&
DebugSymbols->GetModuleByModuleName("wow64", 0, NULL, &mb)==S_OK &&
DebugSymbols->GetModuleByModuleName("ntdll", 0, NULL, &mb)==S_OK &&
DebugSymbols->GetModuleByModuleName("ntdll32", 0, NULL, &mb)==S_OK &&
p_hndl->nt_hdrs.pe_tpy==pe_32bit)
{
p_hndl->wow64_emul = TRUE;
}
ret=TRUE;
finish:
if (DebugSymbols) DebugSymbols->Release();
if (!ret) free_scan_imps_hndl(p_hndl);
return ret;
}
static void init_scan_imps_hndl(
scan_imps_hndl_t *p_hndl, const dump_pe_hndl_t *p_dpe_hndl)
{
memset(p_hndl, 0, sizeof(*p_hndl));
p_hndl->mod_base = p_dpe_hndl->mod_base;
p_hndl->dos_hdr = p_dpe_hndl->dos_hdr;
p_hndl->nt_hdrs = p_dpe_hndl->nt_hdrs;
memcpy(p_hndl->sectab,
p_dpe_hndl->sectab, p_dpe_hndl->n_sects*sizeof(p_dpe_hndl->sectab[0]));
p_hndl->n_sects = p_dpe_hndl->n_sects;
/* wow64_emul is set to FALSE */
}
/* Resolve forwarder redirection string pointed by 'frwrd_name_addr' (the string
in the format "module.proc_name" or "module.#ordinal") to the forwarder proc
address. The function tries to resolve forwards to forwards until the final
code reference. Returns TRUE on success.
*/
static BOOL get_frwrd_proc_addr(scan_imps_hndl_t *p_hndl,
ULONG64 frwrd_name_addr, ULONG64 *p_reslv_addr, BOOL b_logs=FALSE)
{
BOOL ret=FALSE;
IDebugSymbols *DebugSymbols=NULL;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols), (void **)&DebugSymbols)) != S_OK) goto finish;
char mod_name[MAX_PATH+1];
char proc_name[MAX_SYM_NAME+1];
{
/* read module and proc name from forwarder string */
char rd_buf[sizeof(mod_name)+sizeof(proc_name)+4];
if (!string_cpy_lt(rd_buf, frwrd_name_addr, sizeof(rd_buf))) goto finish;
/* mod name separated from proc name by '.' */
char *pc_dot = strchr(rd_buf, '.');
if (!pc_dot) {
if (b_logs) err_dbgprintf("Invalid forwarder name: %s\n", rd_buf);
goto finish;
}
*pc_dot++ = 0;
strncpy(proc_name, pc_dot, sizeof(proc_name));
proc_name[sizeof(proc_name)-1] = 0;
if (p_hndl->wow64_emul && !strcmpi(rd_buf, "ntdll")) {
/* on emulated subsystem ntdll is substituted by the ntdll32 lib by
the Wow64 emulator; we need to search in the 32-bit version of
the library */
strcpy(mod_name, "ntdll32");
} else {
strncpy(mod_name, rd_buf, sizeof(mod_name));
mod_name[sizeof(mod_name)-1] = 0;
}
}
ULONG64 mod_base;
if (DebugSymbols->GetModuleByModuleName(mod_name, 0, NULL, &mod_base)!=S_OK)
{
if (b_logs)
err_dbgprintf("Unable to find the forwarder's module: %s\n", mod_name);
goto finish;
}
/* check if the cache may be used */
mod_exp_dir_t *p_ed = &p_hndl->ed_frwrd;
if (p_ed->mod_base != mod_base) {
p_ed->mod_base = mod_base;
if (!get_mod_exp_dir(p_ed, b_logs)) goto finish;
}
ULONG cb;
DWORD ord; /* index in EAT */
const char *pc_ord = strchr(proc_name, '#');
if (pc_ord)
{
/* proc given by an ordinal number */
pc_ord++;
errno = 0;
ord = strtoul(pc_ord, NULL, 10);
if (errno) {
if (b_logs)
err_dbgprintf("Forwarder's ordinal number conversion "
"problem: %s\n", &proc_name[0]);
goto finish;
}
if (ord < p_ed->ord_base) {
if (b_logs)
err_dbgprintf("Forwarder's ordinal number [0x%04X] larger than "
"the ordinal base [0x%04X]\n", ord, p_ed->ord_base);
goto finish;
}
ord = ord-p_ed->ord_base;
} else
{
/* proc given by name */
DWORD hint;
if (!get_name_hint_ord(p_ed, proc_name, &hint, &ord)) {
if (b_logs)
err_dbgprintf("Can't find forwarder's proc with the name: %s\n",
proc_name);
goto finish;
}
}
/* get forwarder proc addr basing on the EAT index */
DWORD proc_rva;
if (!(read_memory(p_ed->faddrs_addr + ord*sizeof(proc_rva),
&proc_rva, sizeof(proc_rva), &cb) && cb==sizeof(proc_rva))) goto finish;
ULONG64 proc_addr = RVA2ADDR(get_32uint_le(&proc_rva), p_ed->mod_base);
if ((p_ed->exp_dd_addr <= proc_addr) &&
(proc_addr < p_ed->exp_dd_addr+p_ed->exp_dd_sz))
{
/* if the resolved proc is forwarder we need to recursively resolve it */
ret = get_frwrd_proc_addr(p_hndl, proc_addr, p_reslv_addr, b_logs);
} else {
*p_reslv_addr = proc_addr;
ret=TRUE;
}
finish:
if (DebugSymbols) DebugSymbols->Release();
return ret;
}
/* Get the export information from a dll module about a proc pointed by the
'proc_addr'. 'ords', 'hints' and 'proc_names' are populated with the info
from the dll's export tab. These tabs have the same number of output elements
stored on response under the addr of 'p_n_synons'. If an elem in the
'proc_names' is NULL, then the corresponding item has no name (only an
ordinal). The pointers of 'proc_names' point to addresses in the
'pc_names_buf' with the length 'names_buf_sz'. If 'p_proc_mod_base' points to
value other than NULL, the value indicates the module base address used for
resolving the proc_addr. If the value is NULL the owning module of the
proc_addr will be used. 'p_proc_mod_base' will be set to the owning module
base. 'pc_mod_name' points to value other than NULL, it will get the owner's
module name. Min size of this buf is MAX_PATH+1. If 'b_logs' is TRUE print
error info. Returns TRUE for success, FALSE - error or 'proc_addr' not found.
*/
static BOOL get_exp_proc_info(scan_imps_hndl_t *p_hndl, ULONG64 *p_proc_mod_base,
ULONG64 proc_addr, DWORD ords[MAX_SYNONS], DWORD hints[MAX_SYNONS],
char* (proc_names)[MAX_SYNONS], char *pc_names_buf, size_t names_buf_sz,
DWORD *p_n_synons, char *pc_mod_name, BOOL b_logs)
{
BOOL ret=FALSE;
IDebugSymbols *DebugSymbols=NULL;
/* requested module base to search */
ULONG64 srch_mod_base = *p_proc_mod_base;
/* owning proc module base */
ULONG64 own_mod_base = NULL;
*p_n_synons=0;
if ((get_client()->QueryInterface(
__uuidof(IDebugSymbols), (void **)&DebugSymbols)) != S_OK) goto err;
if (DebugSymbols->GetModuleByOffset(proc_addr, 0, NULL, &own_mod_base)!=S_OK)
{
if (b_logs)
err_dbgprintf("Unable to find a module owning the proc addr: 0x%p\n",
proc_addr);
goto err;
}
*p_proc_mod_base = own_mod_base;
if (srch_mod_base==NULL) srch_mod_base=own_mod_base;
/* check if the cache may be used */
mod_exp_dir_t *p_ed = &p_hndl->ed_reslv;
if (p_ed->mod_base != srch_mod_base) {
p_ed->mod_base = srch_mod_base;
if (!get_mod_exp_dir(p_ed, b_logs)) goto err;
}
ULONG cb;
BOOL forwd_resv;
for (UINT pass=0; *p_n_synons==0 && pass<2; pass++)
{
if (pass==0) {
/* at the 1st resolving pass, forwards are resolved only for
proc located in external module to the currently searched */
forwd_resv = (srch_mod_base!=own_mod_base);
} else {
/* at the 2nd pass additionally resolve
forwards to the owning module */
if (forwd_resv) break;
else
if ((p_ed->exp_dd_addr <= proc_addr) &&
(proc_addr < p_ed->exp_dd_addr+p_ed->exp_dd_sz)) break;
else
forwd_resv=TRUE;
}
DWORD ord;
ULONG64 faddrs_addr;
/* go through Export Address Table looking for the proc_addr */
DWORD exp_proc_rva;
for (ord=0, faddrs_addr=p_ed->faddrs_addr;
ord < p_ed->n_faddrs;
faddrs_addr+=sizeof(exp_proc_rva), ord++)
{
if (!(read_memory(
faddrs_addr, &exp_proc_rva, sizeof(exp_proc_rva), &cb) &&
cb==sizeof(exp_proc_rva))) goto err;
ULONG64 exp_proc_addr =
RVA2ADDR(get_32uint_le(&exp_proc_rva), srch_mod_base);
if (forwd_resv && (p_ed->exp_dd_addr <= exp_proc_addr) &&
(exp_proc_addr < p_ed->exp_dd_addr+p_ed->exp_dd_sz))
{
/* the exported address is a forward reference; we need to
convert this address to the actual address inside the
referenced module */
if (!get_frwrd_proc_addr(p_hndl, exp_proc_addr, &exp_proc_addr))
continue;
}
if (exp_proc_addr==proc_addr) {
if (*p_n_synons < MAX_SYNONS) {
ords[(*p_n_synons)++] = p_ed->ord_base+ord;
/* don't brake and search for more entries with the same addr */
} else {
if (b_logs) {
warn_dbgprintf(
"Too much symbols with the same addr [0x%p] in the "
"module (base: 0x%p); break searching for more "
"symbol duplicates for this addr\n",
proc_addr, srch_mod_base);
}
break;
}
}
}
}
if (*p_n_synons==0) {
if (b_logs)
err_dbgprintf("Unable to find the proc for the addr: 0x%p\n",
proc_addr);
goto err;
}
/* look for the names for the found ordinals */
size_t alloc_bts=0;
for (DWORD i=0; i<*p_n_synons; i++)
{
ULONG64 fords_addr = p_ed->fords_addr;
proc_names[i] = NULL;
hints[i] = 0;
WORD ot_ord; /* ord read from the ordinals table */
for (DWORD hint=0;
hint < p_ed->n_fnames;
fords_addr+=sizeof(ot_ord), hint++)
{
if (!(read_memory(fords_addr,
&ot_ord, sizeof(ot_ord), &cb) && cb==sizeof(ot_ord))) goto err;
ot_ord = get_16uint_le(&ot_ord);
if (ords[i]==p_ed->ord_base+(DWORD)ot_ord)
{
DWORD proc_name_rva;
char buf[MAX_SYM_NAME+1];
/* read proc name address */
if (!(read_memory(p_ed->fnames_addr + hint*sizeof(proc_name_rva),
&proc_name_rva, sizeof(proc_name_rva), &cb) &&
cb==sizeof(proc_name_rva))) goto err;
/* ... and store the proc name */
ULONG64 proc_name_addr =
RVA2ADDR(get_32uint_le(&proc_name_rva), srch_mod_base);
if (!string_cpy_lt(buf, proc_name_addr, sizeof(buf))) goto err;
size_t name_len = strlen(buf);
/* ... in the names_buf */
if (alloc_bts+name_len+1 <= names_buf_sz) {
proc_names[i] = pc_names_buf+alloc_bts;
alloc_bts += name_len+1;
strcpy(proc_names[i], buf);
hints[i] = hint;
} else {
if (b_logs)
warn_dbgprintf(
"Buffer too small to store proc name %s [0x%p] from "
"module (base: 0x%p)", buf, proc_addr, srch_mod_base);
}
break;
}
}
}
/* store mod name */
if (pc_mod_name)
if (!string_cpy_lt(pc_mod_name, p_ed->mod_name_addr, MAX_PATH+1))
goto err;
no_err:
ret=TRUE;
err:
if (DebugSymbols) DebugSymbols->Release();
return ret;
}
/* Resolve proc name, its ord, hint and write them under 'p_out_ord', 'p_out_hint'
and 'pc_out_proc_name' (min MAX_PATH+1 long). Number of entries with the same
'proc_addr' is returned under 'p_n_synons'. Returns TRUE if the resolution
finishes with success, FALSE otherwise.
*/
static BOOL resv_proc_name_mod(scan_imps_hndl_t *p_hndl, ULONG64 mod_base,
ULONG64 proc_addr, DWORD *p_out_ord, DWORD *p_out_hint, char *pc_out_proc_name,
DWORD *p_n_synons, BOOL b_logs)
{
BOOL ret=FALSE;
DWORD n_synons;
DWORD ords[MAX_SYNONS];
DWORD hints[MAX_SYNONS];
char* (proc_names)[MAX_SYNONS];
char name_buf[0x1000];
char mod_name[MAX_PATH+1];
ULONG64 proc_mod_base = mod_base;
if (!get_exp_proc_info(p_hndl, &proc_mod_base, proc_addr, ords, hints,
proc_names, name_buf, sizeof(name_buf), &n_synons, mod_name, b_logs))
goto finish;
DWORD i;
*p_n_synons = n_synons;
/* The resolved proc is:
- the 1st matching proc with a name
- the 1st ord if the proc doesn't have any name */
for (i=0; i<n_synons; i++) {
if (proc_names[i]) {
*p_out_ord = ords[i];
*p_out_hint = hints[i];
strncpy(pc_out_proc_name, proc_names[i], MAX_PATH+1);
pc_out_proc_name[MAX_PATH]=0;
break;
}
}
if (i>=n_synons) {
*p_out_ord = ords[0];
*p_out_hint = 0;
*pc_out_proc_name = 0;
}
if (n_synons>1)
{
/* printed regardless of b_logs flag - the func finished with
success but there is a need to inform about the duplicates */
warn_dbgprintf(
"Proc with many aliases has been found [0x%p]: ", proc_addr);
for (i=0; i<n_synons; i++) {
dbgprintf("%s#0x%04X%s", (proc_names[i] ? proc_names[i] : ""),
ords[i], (i+1<n_synons ? ", " : ""));
}
dbgprintf("\n Resolved to: %s!%s#0x%04X\n",
mod_name, pc_out_proc_name, *p_out_ord);
}
ret=TRUE;
finish:
return ret;
}
/* Info about proc duplicates in the lastly resolved module's list of imports */
static void print_info_proc_dups(const scan_imps_hndl_t *p_hndl)
{
BOOL hdr_printed=FALSE;
const char *sep = " ";
/* are there any modules loaded? */
if (!p_hndl->p_imp_mods) goto finish;
for (const imp_proc_desc_t *p_proc = p_hndl->p_last_mod->proc;
p_proc != p_hndl->p_last_proc;
p_proc=p_proc->next)
{
if (p_proc->dups>0)
{
const char *pc_mod_name = (*(p_hndl->p_last_mod->name) ?
p_hndl->p_last_mod->name : "<unspec>");
if (!hdr_printed)
warn_dbgprintf("Found duplicates in a module %s:", pc_mod_name);
hdr_printed = TRUE;
dbgprintf("%s%s#0x%04X [%d]",
sep, p_proc->name, p_proc->ord, p_proc->dups+1);
sep = ", ";
}
}
if (hdr_printed) dbgprintf("\n");
finish:
return;
}
/* Info about module duplicates in the resolved list of imports */
static void print_info_mod_dups(const scan_imps_hndl_t *p_hndl)
{
BOOL hdr_printed=FALSE;
const char *sep = " ";
for (const imp_mod_desc_t *p_mod = p_hndl->p_imp_mods;
p_mod != p_hndl->p_last_mod;
p_mod = p_mod->next)
{
if (p_mod->dups>0 && *(p_mod->name))
{
if (!hdr_printed) info_dbgprintf("Duplicated modules found:");
hdr_printed = TRUE;
dbgprintf("%s%s [%d]", sep, p_mod->name, p_mod->dups+1);
sep = ", ";
}
}
if (hdr_printed) dbgprintf("\n");
}
/* Mark end of a module's imports list */
static void mark_end_mod_imp_list(scan_imps_hndl_t *p_hndl)
{
p_hndl->crt_new_list = TRUE;
if (p_hndl->p_imp_mods)
{
if (*(p_hndl->p_last_mod->name))
{
if (p_hndl->wow64_emul &&
!strcmpi(p_hndl->p_last_mod->name, "ntdll32.dll"))
{
/* for 32-bit target emulated on Wow64 platform the ntdll32 is
an alias to ntdll, whereas ntdll itself is a 64-bit version
of the library; therefore we need to rename the ntdll32 to
ntdll keeping all ordinals, hints and proc names untouched */
/* even num of bytes, enough name space */
strcpy(p_hndl->p_last_mod->name, "ntdll.dll");
}
/* calculate dups of the added module */
for (imp_mod_desc_t *p_mod = p_hndl->p_imp_mods;
p_mod != p_hndl->p_last_mod;
p_mod = p_mod->next)
{
if (!strcmpi(p_mod->name, p_hndl->p_last_mod->name)) {
p_mod->dups++;
break;
}
}
}
print_info_proc_dups(p_hndl);
}
finish:
return;
}
/* Compare 2 proc; returns TRUE if the same */
inline static BOOL cmp_procs(const char *pc_proc1_name,
DWORD proc1_ord, const char *pc_proc2_name, DWORD proc2_ord)
{
BOOL ret=FALSE;
if (*pc_proc1_name && *pc_proc2_name)
ret = !strcmp(pc_proc1_name, pc_proc2_name);
else
ret = (proc1_ord==proc2_ord);
return ret;
}
/* Add importing module to the scan imports handle's list */
static BOOL add_imp_mod(scan_imps_hndl_t *p_hndl,
ULONG64 mod_iat_addr, ULONG64 mod_base, const char *pc_proc_mod_name)
{
BOOL ret=FALSE;
size_t proc_mod_name_len = strlen(pc_proc_mod_name);
imp_mod_desc_t *p_new_mod_desc;
size_t to_alloc = sizeof(*p_new_mod_desc)+proc_mod_name_len+4;
p_new_mod_desc = (imp_mod_desc_t*)malloc(to_alloc);
if (!p_new_mod_desc) goto finish;
p_new_mod_desc->mod_base = mod_base;
p_new_mod_desc->iat_addr = mod_iat_addr;
p_new_mod_desc->n_procs = 0;
p_new_mod_desc->proc = NULL;
p_new_mod_desc->dups = 0;
p_new_mod_desc->hnt_sz = 0;
p_new_mod_desc->hnt_nrnd_sz = 0;
p_new_mod_desc->next = NULL;
strcpy(p_new_mod_desc->name, pc_proc_mod_name);
/* even padd byte */
if (!(proc_mod_name_len&1)) p_new_mod_desc->name[proc_mod_name_len+1]=0;
if (p_hndl->p_imp_mods)
p_hndl->p_last_mod->next = p_new_mod_desc;
else
p_hndl->p_imp_mods = p_new_mod_desc;
p_hndl->p_last_mod = p_new_mod_desc;
p_hndl->p_last_proc = NULL;
p_hndl->crt_new_list = FALSE;
p_hndl->n_mods++;
ret=TRUE;
finish:
return ret;
}
/* Add to the scan imports handle's list of procs of an importing module. If
required the importing module itself may also be added. The input params
as returned by the resv_proc_name_mod(). TRUE returned on success. FALSE: no
memory error.
*/
static BOOL add_imp_mod_proc(scan_imps_hndl_t *p_hndl, DWORD ord, DWORD hint,
const char *pc_proc_name, ULONG64 mod_iat_addr, ULONG64 mod_base,
const char *pc_proc_mod_name, BOOL b_check_proc_dups)
{
BOOL ret=FALSE;
/* create new mod's list if required */
if (!p_hndl->p_imp_mods || p_hndl->crt_new_list) {
if (!add_imp_mod(p_hndl, mod_iat_addr, mod_base, pc_proc_mod_name))
goto finish;
}
/* add proc name to the current module's list */
size_t proc_name_len = strlen(pc_proc_name);
imp_proc_desc_t *p_new_proc_desc;
size_t to_alloc = sizeof(*p_new_proc_desc)+proc_name_len+4;
p_new_proc_desc = (imp_proc_desc_t*)malloc(to_alloc);
if (!p_new_proc_desc) goto finish;
p_new_proc_desc->ord = ord;
p_new_proc_desc->hint = (proc_name_len>0 ? hint : (DWORD)-1);
p_new_proc_desc->dups = 0;
p_new_proc_desc->next = NULL;
strcpy(p_new_proc_desc->name, pc_proc_name);
/* even padd byte */
if (!(proc_name_len&1)) p_new_proc_desc->name[proc_name_len+1]=0;
if (p_hndl->p_last_proc)
p_hndl->p_last_proc->next = p_new_proc_desc;
else
p_hndl->p_last_mod->proc = p_new_proc_desc;
p_hndl->p_last_proc = p_new_proc_desc;
p_hndl->p_last_mod->n_procs++;
if (proc_name_len>0) {
p_hndl->p_last_mod->hnt_sz += 2+RNDUP_W(proc_name_len+1);
p_hndl->p_last_mod->hnt_nrnd_sz += 2+proc_name_len+1;
}