This repository was archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpe.cpp
More file actions
2457 lines (2069 loc) · 61.8 KB
/
Copy pathpe.cpp
File metadata and controls
2457 lines (2069 loc) · 61.8 KB
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) 2011. Kim Zhang[analyst004@gmail.com].
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
#undef __STRICT_ANSI__
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <errno.h>
#include <assert.h>
#include <strconv.h>
#include "petype.h"
#include <locale.h>
#include <slist.h>
#include <filemap.h>
#include "petype.h"
#include "peformat.h"
#include "verfmt.h"
#include "pe.h"
#ifdef __GNUC__
#ifdef __MINGW32__
#else
//linux or linux
#define _snprintf snprintf
#define _snwprintf swprintf
#endif
#endif
//用于字节边界对齐
#define ALIGN(o,a) (((a))?(((o)/(a)+((o)%(a)!=0))*(a)):(o))
/*
* IS_CONTAINED(buf1, size1, buf2, size2) checks ifbuf2 is contained
* within buf1.
*
* buf1 and buf2 are pointers (or offsets) for the main buffer and the
* sub-buffer respectively, and size1/2 are their sizes
*
* The macro can be used to protect against wraps.
*/
#define IS_CONTAINED(bb, bb_size, sb, sb_size) \
(bb_size > 0 && sb_size > 0 && sb_size <= bb_size \
&& sb >= bb && sb + sb_size <= bb + bb_size)
#define DIRECTORY_ENTRY(s, i) \
(&(GET_NT_HEADER(s)->OptionalHeader.DataDirectory[i]))
#define GET_DOS_HEADER( s ) ((IMAGE_DOS_HEADER*)s)
#define GET_NT_HEADER( s ) \
((IMAGE_NT_HEADERS32*)((const char*)(s)+GET_DOS_HEADER(s)->e_lfanew ))
#define GET_SECTION_HEADER(nt, i) \
((IMAGE_SECTION_HEADER*)((char*)nt + sizeof(int) + sizeof(IMAGE_FILE_HEADER) + nt->FileHeader.SizeOfOptionalHeader+(i)*sizeof(IMAGE_SECTION_HEADER)))
//获取基地址
#define GET_IMAGE_BASE( s ) \
(unsigned int)(/*1*/(/*2*/(IMAGE_NT_HEADERS32*)((const char*)s + ((IMAGE_DOS_HEADER*)s)->e_lfanew) /*2*/ )->OptionalHeader.ImageBase/*1*/)
typedef struct _pe_t{
IMAGE_DOS_HEADER* dos; //dos header
IMAGE_NT_HEADERS32* nt; //nt header
const char* stream;
size_t size;
const char* dll_name; //export dll name
slist_t export_apis;
slist_t import_dlls;
slist_t version;
slist_t reloc_list;
slist_t resource;
slist_t bound_list;
slist_t gap_list;
slist_t section_list;
slist_t icon_list;
bool open_by_file;
MAPPED_FILE view;
IMAGE_OVERLAY overlay;
IMAGE_SIGNATURE sign;
}pe_t;
typedef struct _export_api_t{
IMAGE_EXPORT_FUNCTION data;
snode_t node;
}export_api_t;
//导入函数
typedef struct _import_api_t {
IMAGE_IMPORT_FUNCTION data;
snode_t node;
}import_api_t;
//导入模块
typedef struct _import_dll_t{
IMAGE_IMPORT_DESCRIPTOR data;
slist_t api_list;
snode_t node;
}import_dll_t;
typedef struct _version_t{
IMAGE_VERSION data;
snode_t node;
}version_t;
typedef struct _reloc_t{
IMAGE_RELOCATION_ITEM data;
snode_t node;
}reloc_t;
typedef struct _resource_t{
IMAGE_RESOURCE_DIRECTORY_ENTRY data;
snode_t node;
slist_t child;
}resource_t;
typedef struct _bound_t{
IMAGE_BOUND_IMPORT_DESCRIPTOR data;
snode_t node;
}bound_t;
typedef struct _gap_t{
IMAGE_GAP data;
snode_t node;
}gap_t;
typedef struct _section_t{
IMAGE_SECTION_HEADER data;
snode_t node;
}section_t;
typedef struct _icon_t{
IMAGE_ICON_ENTRY data;
snode_t node;
}icon_t;
bool parse_section(int fd);
bool parse_export(int fd);
bool parse_import(int fd);
bool parse_reloc(int fd);
bool parse_version(int fd);
bool parse_overlay(int fd);
bool parse_resource(int fd);
bool parse_bound(int fd);
bool parse_gap(pe_t* pe);
bool parse_signature(pe_t* pe);
bool parse_icon(pe_t* pe);
bool pe_init(pe_t* pe, const char* stream, int size)
{
pe->stream = stream;
pe->size = size;
pe->dos = GET_DOS_HEADER(stream);
pe->nt = GET_NT_HEADER(stream);
//读取PE头部数据
if (pe->dos->e_magic != IMAGE_DOS_SIGNATURE) {
return false;
}
if ((size_t)pe->dos->e_lfanew >= (size_t)size) {
return false;
}
if (pe->nt->Signature != IMAGE_NT_SIGNATURE
|| pe->nt->FileHeader.Machine != IMAGE_FILE_MACHINE_I386
|| pe->nt->OptionalHeader.Magic != IMAGE_NT_OPTIONAL_HDR32_MAGIC ) {
return false;
}
parse_section((intptr_t)pe);
parse_export((intptr_t)pe);
parse_import((intptr_t)pe);
parse_reloc((intptr_t)pe);
parse_version((intptr_t)pe);
parse_overlay((intptr_t)pe);
parse_resource((intptr_t)pe);
parse_bound((intptr_t)pe);
parse_icon(pe);
parse_gap(pe);
parse_signature(pe);
return true;
}
int pe_open_file(const char* file)
{
if (file == NULL) {
errno = EINVAL;
return INVALID_PE;
}
pe_t* pe = (pe_t*)malloc(sizeof(pe_t));
if (pe == NULL) {
return INVALID_PE;
}
memset((uint8_t*)pe, 0, sizeof(pe_t));
if (0 != map_file(file, &pe->view)) {
free(pe);
pe = NULL;
return INVALID_PE;
}
pe->open_by_file = true;
if(!pe_init(pe, (const char*)pe->view.data, pe->view.size)) {
free(pe);
pe = NULL;
return INVALID_PE;
}
return (intptr_t)pe;
}
int pe_open(const char* stream, size_t size)
{
if (stream==NULL || size == 0){
errno = EINVAL;
return INVALID_PE;
}
pe_t* pe = (pe_t*)malloc(sizeof(pe_t));
if (pe==NULL) {
return INVALID_PE;
}
memset(pe, 0, sizeof(pe_t));
if (!pe_init(pe, stream, size)) {
free(pe);
pe = NULL;
return INVALID_PE;
}
return (intptr_t)pe;
}
int pe_size(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return -1;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return pe->size;
}
uint8_t* pe_stream(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return (uint8_t*)pe->stream;
}
void clean_resource(slist_t* list)
{
resource_t* res = NULL;
slist_for_each_safe(res, list, resource_t, node) {
if (!slist_empty(&res->child))
clean_resource(&res->child);
free(res);
res = NULL;
}
}
void pe_close(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
export_api_t* item = NULL;
slist_for_each_safe(item, &pe->export_apis, export_api_t, node) {
free(item);
item = NULL;
}
import_dll_t* dll = NULL;
slist_for_each_safe(dll, &pe->import_dlls, import_dll_t, node) {
import_api_t* api = NULL;
slist_for_each_safe(api, &dll->api_list, import_api_t, node) {
free(api);
api = NULL;
}
free(dll);
dll = NULL;
}
icon_t* icon = NULL;
slist_for_each_safe(icon, &pe->icon_list, icon_t, node) {
free(icon);
icon = NULL;
}
version_t* ver = NULL;
slist_for_each_safe(ver, &pe->version, version_t, node) {
free(ver);
ver = NULL;
}
bound_t* bound = NULL;
slist_for_each_safe(bound, &pe->bound_list, bound_t, node) {
free(bound);
bound = NULL;
}
gap_t* gap = NULL;
slist_for_each_safe(gap, &pe->gap_list, gap_t, node) {
free(gap);
gap = NULL;
}
reloc_t* reloc = NULL;
slist_for_each_safe(reloc, &pe->reloc_list, reloc_t, node) {
free(reloc);
reloc = NULL;
}
clean_resource(&pe->resource);
section_t* section = NULL;
slist_for_each_safe(section, &pe->section_list, section_t, node) {
free(section);
section = NULL;
}
if (pe->open_by_file) {
unmap_file(&pe->view);
memset(&pe->view, 0, sizeof(MAPPED_FILE));
}
free(pe);
pe = NULL;
}
IMAGE_NT_HEADERS32* pe_nt_header(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
return ((pe_t*)(intptr_t)fd)->nt;
}
IMAGE_DOS_HEADER* pe_dos_header(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
return ((pe_t*)(intptr_t)fd)->dos;
}
raw_t rva_to_raw(int fd, rva_t rva)
{
if (fd == INVALID_PE || rva == INVALID_RVA) {
errno = EINVAL;
return INVALID_RAW;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
//枚举所有Section
IMAGE_SECTION_HEADER *section = NULL;
//uint32_t sectin_align_mask = pe->nt->OptionalHeader.SectionAlignment - 1;
//uint32_t file_align = (uint32_t)pe->nt->OptionalHeader.FileAlignment;
//uint32_t file_align_mask = file_align - 1;
//计算rva在哪个节中
raw_t raw = 0;
for (size_t i=0; i < pe->nt->FileHeader.NumberOfSections; i++) {
section = GET_SECTION_HEADER(pe->nt, i);
if (rva >= section->VirtualAddress
&& rva <= (section->VirtualAddress + section->Misc.VirtualSize - 1)){
raw = rva - section->VirtualAddress
+ section->PointerToRawData;
goto ret;
}
}
raw = rva;
ret:
if (raw >= pe->size) {
errno = ERANGE;
return INVALID_RAW;
}
return raw;
}
rva_t raw_to_rva(int fd, raw_t raw)
{
if (fd == INVALID_PE || raw == INVALID_RAW) {
errno = EINVAL;
return INVALID_RVA;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
for (size_t i=0; i < pe->nt->FileHeader.NumberOfSections; i++) {
//判断FileOffset是否在该Section地址范围内
IMAGE_SECTION_HEADER *section = GET_SECTION_HEADER(pe->nt, i);
if (raw >= (raw_t)section->PointerToRawData
&& raw <= (raw_t)(section->PointerToRawData+section->SizeOfRawData-1)){
//rva = File Offset + k.
return raw + section->VirtualAddress - section->PointerToRawData;
}
}
return INVALID_RVA;
}
uint8_t* pe_data_by_raw(int fd, raw_t raw)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
if (raw >= pe->size) {
errno = ERANGE;
return NULL;
}
return (uint8_t*)pe->stream + raw;
}
uint8_t* pe_data_by_rva(int fd, rva_t rva)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
raw_t raw = rva_to_raw(fd, rva);
if (raw == INVALID_RAW) {
return NULL;
}
return pe_data_by_raw(fd, raw);
}
/***********************************************************************
*
* pe section
*
**********************************************************************/
bool parse_section(int fd)
{
if (fd== INVALID_PE) {
errno = EINVAL;
return false;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
int count = pe->nt->FileHeader.NumberOfSections;
for(int i=0; i<count; i++) {
IMAGE_SECTION_HEADER* header = GET_SECTION_HEADER(pe->nt, i);
section_t* section = (section_t*)malloc(sizeof(section_t));
if (section == NULL) {
return false;
}
memset(section, 0, sizeof(sizeof(section_t)));
memcpy(§ion->data, header, sizeof(IMAGE_SECTION_HEADER));
slist_add(&pe->section_list, §ion->node);
}
return true;
}
IMAGE_SECTION_HEADER* pe_section_first(int fd)
{
if (fd == INVALID_PE){
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return slist_first_entry(&pe->section_list, section_t, data, node);
}
IMAGE_SECTION_HEADER* pe_section_next(IMAGE_SECTION_HEADER* it)
{
if (it == NULL) {
errno = EINVAL;
return NULL;
}
return slist_next_entry(it, section_t, data, node);
}
/***********************************************************************
*
* pe export
*
**********************************************************************/
bool parse_export(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return false;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
rva_t block_rva
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_EXPORT)->VirtualAddress;
size_t block_size
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_EXPORT)->Size;
//无导出函数
if (block_rva == 0 || block_size == 0){
return true;
}
raw_t block_raw = rva_to_raw(fd, block_rva);
if (block_raw == INVALID_RAW) {
errno = EINVAL;
return false;
}
IMAGE_EXPORT_DIRECTORY* export_header
= (IMAGE_EXPORT_DIRECTORY*)(pe->stream + block_raw);
raw_t raw_name = rva_to_raw(fd, export_header->Name);
if (raw_name == INVALID_RAW) {
errno = EINVAL;
return false;
}
pe->dll_name = (const char*)(pe->stream + raw_name);
if (export_header->NumberOfFunctions > 1024){
errno = EINVAL;
return false;
}
//获取导出函数名称序号数组
raw_t raw_name_ordinals = rva_to_raw(fd, export_header->AddressOfNameOrdinals);
if (raw_name_ordinals == INVALID_RAW) {
errno = EINVAL;
return false;
}
//获取导出函数名称数组
raw_t raw_names = rva_to_raw(fd, export_header->AddressOfNames);
if (raw_names == INVALID_RAW) {
errno = EINVAL;
return false;
}
//获取导出函数地址数组
raw_t raw_functions = rva_to_raw(fd, export_header->AddressOfFunctions);
if (raw_functions == INVALID_RAW) {
errno = EINVAL;
return false;
}
//先初始化序号和地址
for (size_t i=0; i < export_header->NumberOfFunctions; i++) {
export_api_t* api = (export_api_t*)malloc(sizeof(export_api_t));
if (api == NULL) {
return false;
}
memset(api, 0, sizeof(export_api_t));
api->data.FunctionVirtualAddress = ((rva_t*)(pe->stream + raw_functions))[i];
api->data.Ordinal = export_header->Base + (int32_t)i;
//api name
for(size_t j = 0; i < export_header->NumberOfNames; j++) {
int oridinal = (int)(((uint16_t*)(pe->stream + raw_name_ordinals))[j]);
if (oridinal != (int)i)
continue;
rva_t rva = ((rva_t*)(pe->stream + raw_names))[j];
//可以按照名称进行导出
raw_t raw = rva_to_raw(fd, rva);
if (raw == INVALID_RAW)
continue;
strncpy(api->data.FunctionName, pe->stream + raw,
sizeof(api->data.FunctionName) - 1);
break;
}
slist_add(&pe->export_apis, &api->node);
}
return true;
}
IMAGE_EXPORT_FUNCTION *pe_export_first(int fd)
{
if (fd == INVALID_PE){
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return slist_first_entry(&pe->export_apis, export_api_t, data, node);
}
IMAGE_EXPORT_FUNCTION *pe_export_next(IMAGE_EXPORT_FUNCTION* iter)
{
if (iter == NULL) {
errno = EINVAL;
return NULL;
}
return slist_next_entry(iter, export_api_t, data, node);
}
const char* pe_export_dllname(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return pe->dll_name;
}
/***********************************************************************
*
* pe import
*
**********************************************************************/
bool parse_import(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return false;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
rva_t block_rva
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_IMPORT)->VirtualAddress;
size_t block_size
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_IMPORT)->Size;
if (block_rva == 0 || block_size == 0) {
//表示没有导入表, 这种情况极少, 但还是存在
//c:\\windows\\system32\\lz32.dll就是
errno = ENOENT;
return false;
}
raw_t raw = rva_to_raw(fd, block_rva);
if (raw == INVALID_RAW) {
errno = ERANGE;
return false;
}
//计算IMAGE_IMPORT_DESCRIPTOR的个数
IMAGE_IMPORT_DESCRIPTOR* pDescriptor = (IMAGE_IMPORT_DESCRIPTOR*)(pe->stream + raw);
int num_module = 0;
do {
//最后一个全零的DESCRIPTOR表示结束
IMAGE_IMPORT_DESCRIPTOR zero;
memset(&zero, 0, sizeof(IMAGE_IMPORT_DESCRIPTOR));
if (0 == memcmp(&pDescriptor[num_module],
&zero,
sizeof(IMAGE_IMPORT_DESCRIPTOR)))
break;
IMAGE_IMPORT_DESCRIPTOR *import_descriptor
= (IMAGE_IMPORT_DESCRIPTOR*)(pe->stream
+ raw
+ num_module * sizeof(IMAGE_IMPORT_DESCRIPTOR));
import_dll_t* dll = (import_dll_t*)malloc(sizeof(import_dll_t));
if (dll == NULL) {
return false;
}
memset(dll, 0, sizeof(import_dll_t));
memcpy(&dll->data, import_descriptor, sizeof(IMAGE_IMPORT_DESCRIPTOR));
slist_add(&pe->import_dlls, &dll->node);
//枚举该模块的导入函数列表
raw_t raw_thunk = 0;
rva_t rva_iat = 0;
if (import_descriptor->OriginalFirstThunk == 0) {
//OriginalFirstThunk为0, 只能去读FirstThunk的值了
raw_thunk = rva_to_raw(fd, import_descriptor->FirstThunk);
rva_iat = import_descriptor->FirstThunk;
} else {
raw_thunk = rva_to_raw(fd, import_descriptor->OriginalFirstThunk);
rva_iat = import_descriptor->FirstThunk;
}
if (raw_thunk >= pe->size) {
errno = ERANGE;
return false;
}
IMAGE_THUNK_DATA32* thunks = (IMAGE_THUNK_DATA32*)(pe->stream + raw_thunk);
uint32_t num_api = 0;
do {
//最后一个全零的IMAGE_THUNK_DATA表示结束
IMAGE_THUNK_DATA32 zero;
memset(&zero, 0, sizeof(IMAGE_THUNK_DATA32));
if (0 == memcmp(&thunks[num_api], &zero, sizeof(IMAGE_THUNK_DATA32)))
break;
import_api_t* api = (import_api_t*)malloc(sizeof(import_api_t));
if (api == NULL) {
return false;
}
memset(api, 0, sizeof(import_api_t));
api->data.ThunkOffset = (uint32_t)((char*)&thunks[num_api] - pe->stream);
api->data.ThunkRVA
= raw_to_rva(fd, raw_thunk + num_api*sizeof(IMAGE_THUNK_DATA32));
api->data.ThunkValue = thunks[num_api].u1.Function;
if (thunks[num_api].u1.Ordinal & IMAGE_ORDINAL_FLAG32) {
//最高位为1, 表示序号方式导入函数, 函数名称
api->data.OffsetName = 0;
api->data.FunctionOrdinal
= (uint16_t)(thunks[num_api].u1.Ordinal & 0x0000FFFF);
} else {
//字符串类型导入函数
raw_t raw_name = rva_to_raw(fd, thunks[num_api].u1.AddressOfData);
if (raw_name == INVALID_RAW) {
free(api);
api = NULL;
errno = ERANGE;
continue;
}
api->data.OffsetName = raw_name + sizeof(uint16_t);
IMAGE_IMPORT_BY_NAME *import_name
= (IMAGE_IMPORT_BY_NAME*)(pe->stream + raw_name);
strncpy(api->data.FunctionName,
(char*)import_name->Name,
sizeof(api->data.FunctionName) - 1);
api->data.FunctionHint = (uint16_t)(import_name->Hint);
}
api->data.iat = rva_iat + num_api * sizeof(rva_t);
slist_add(&dll->api_list, &api->node);
} while(++num_api);
}while(++num_module);
return true;
}
bool pe_import_dllname(
int fd,
IMAGE_IMPORT_DESCRIPTOR* import_dll,
char* dllname,
int name_len)
{
if (fd == INVALID_PE || import_dll == NULL) {
errno = EINVAL;
return false;
}
//获取导入模块名称
raw_t raw_name = rva_to_raw(fd, import_dll->Name);
if (raw_name == INVALID_RAW ) {
errno = ERANGE;
return false;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
strncpy(dllname, pe->stream + raw_name, name_len - 1);
return true;
}
IMAGE_IMPORT_DESCRIPTOR* pe_import_dll_first(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
return slist_first_entry(&pe->import_dlls, import_dll_t, data, node);
}
IMAGE_IMPORT_DESCRIPTOR* pe_import_dll_next(IMAGE_IMPORT_DESCRIPTOR* iter)
{
if (iter == NULL) {
errno = EINVAL;
return NULL;
}
return slist_next_entry(iter, import_dll_t, data, node);
}
int pe_import_dll_count(int fd)
{
if (fd == INVALID_PE) {
errno = EINVAL;
return -1;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
int count = 0;
IMAGE_IMPORT_DESCRIPTOR* dll = pe_import_dll_first(fd);
for(; dll != NULL; dll = pe_import_dll_next(dll)) {
count++;
}
return count;
}
IMAGE_IMPORT_FUNCTION* pe_import_api_first(IMAGE_IMPORT_DESCRIPTOR* import_dll)
{
if (import_dll == NULL) {
errno = EINVAL;
return NULL;
}
import_dll_t* dll = container_of(import_dll, import_dll_t, data);
if (dll->api_list.first == NULL) {
return NULL;
}
return slist_first_entry(&dll->api_list, import_api_t, data, node);
}
IMAGE_IMPORT_FUNCTION* pe_import_api_next(IMAGE_IMPORT_FUNCTION* iter)
{
if (iter == NULL) {
errno = EINVAL;
return NULL;
}
return slist_next_entry(iter, import_api_t, data, node);
}
int pe_import_api_count(IMAGE_IMPORT_DESCRIPTOR* dll)
{
if (dll == NULL) {
errno = EINVAL;
return NULL;
}
int count = 0;
IMAGE_IMPORT_FUNCTION* api = pe_import_api_first(dll);
for(; api != NULL; api = pe_import_api_next(api)) {
count ++;
}
return count;
}
/**********************************************************************
*
* pe bound
*
**********************************************************************/
char* pe_restype_name(int res_type)
{
switch(res_type)
{
case 1:
return (char*)"CURSOR";
break;
case 2:
return (char*)"BITMAP";
break;
case 3:
return (char*)"ICON";
break;
case 4:
return (char*)"MENU";
break;
case 5:
return (char*)"DIALOG";
break;
case 6:
return (char*)"STRING";
break;
case 7:
return (char*)"FONTDIR";
break;
case 8:
return (char*)"FONT";
break;
case 9:
return (char*)"ACCELERATOR";
break;
case 10:
return (char*)"RCDATA";
break;
case 11:
return (char*)"MESSAGETABLE";
break;
case 12:
return (char*)"GROUP_CURSOR";
break;
case 14:
return (char*)"GROUP_ICON";
break;
case 16:
return (char*)"VERSION";
break;
case 17:
return (char*)"DLGINCLUDE";
break;
case 19:
return (char*)"PLUGPLAY";
break;
case 20:
return (char*)"VXD";
break;
case 21:
return (char*)"ANICURSOR";
break;
case 22:
return (char*)"ANIICON";
break;
case 23:
return (char*)"RT_HTML";
break;
case 24:
return (char*)"MANIFEST";
break;
case 0xF0:
return (char*)"DIALOG_DATA";
break;
case 0xF1:
return (char*)"TOOLBAR";
break;
default:
return (char*)"UNKNOWN";
break;
}
}
bool pe_resource_name(int fd, IMAGE_RESOURCE_DIRECTORY_ENTRY* res,
char* name, int max_len)
{
if (fd == INVALID_PE || res == NULL) {
errno = EINVAL;
return NULL;
}
pe_t* pe = (pe_t*)(intptr_t)fd;
rva_t block_rva
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_RESOURCE)->VirtualAddress;
size_t block_size
= DIRECTORY_ENTRY(pe->stream, IMAGE_DIRECTORY_ENTRY_RESOURCE)->Size;
if (block_rva == 0 || block_size == 0) {
errno = ENOENT;
return NULL;