-
Notifications
You must be signed in to change notification settings - Fork 7
/
PELite.asm
1317 lines (1131 loc) · 42.6 KB
/
PELite.asm
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
;==============================================================================
;
; PE LIBRARY - Special Lite Version For Dll2Def
;
;==============================================================================
.686
.MMX
.XMM
.model flat,stdcall
option casemap:none
include \masm32\macros\macros.asm
;DEBUG32 EQU 1
;IFDEF DEBUG32
; PRESERVEXMMREGS equ 1
; includelib M:\Masm32\lib\Debug32.lib
; DBG32LIB equ 1
; DEBUGEXE textequ <'M:\Masm32\DbgWin.exe'>
; include M:\Masm32\include\debug32.inc
;ENDIF
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include PELite.inc
;------------------------------------------------------------------------------
; Prototypes for internal use
;------------------------------------------------------------------------------
PESignature PROTO :DWORD
PEJustFname PROTO :DWORD, :DWORD
PEDwordToAscii PROTO :DWORD, :DWORD
PE_SetError PROTO :DWORD, :DWORD
PUBLIC PELIB_ErrorNo
.CONST
.DATA
PELIB_ErrorNo DD PE_ERROR_NO_HANDLE ; Global to store error no
DEFLIBRARY DB 'LIBRARY ',0
dwLenDEFLIBRARY DD ($-DEFLIBRARY)-1
DEFEXPORTS DB 'EXPORTS',13,10,0
dwLenDEFEXPORTS DD ($-DEFEXPORTS)-1
DEFINDENT DB ' ',0
dwLenDEFINDENT DD ($-DEFINDENT)-1
DEFCRLF DB 13,10,0
dwLenDEFCRLF DD ($-DEFCRLF)-1
.CODE
PE_ALIGN
;------------------------------------------------------------------------------
; PE_OpenFile - Opens a PE file (exe/dll/ocx/cpl etc)
; Returns: TRUE or FALSE. If TRUE a PE handle (hPE) is stored in the variable
; pointed to by lpdwPEHandle. If FALSE, use PE_GetError to get further info.
;
; Note: Calls PE_Analyze to process the PE file. Use PE_CloseFile when finished
;------------------------------------------------------------------------------
PE_OpenFile PROC USES EBX lpszPEFilename:DWORD, bReadOnly:DWORD, lpdwPEHandle:DWORD
LOCAL hPE:DWORD
LOCAL hPEFile:DWORD
LOCAL PEMemMapHandle:DWORD
LOCAL PEMemMapPtr:DWORD
LOCAL PEFilesize:DWORD
LOCAL PEVersion:DWORD
IFDEF DEBUG32
PrintText 'PE_OpenFile'
ENDIF
.IF lpdwPEHandle == NULL
Invoke PE_SetError, NULL, PE_ERROR_NO_HANDLE
xor eax, eax
ret
.ENDIF
.IF lpszPEFilename == NULL
Invoke PE_SetError, NULL, PE_ERROR_OPEN_FILE
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
;--------------------------------------------------------------------------
; Open file for read only or read/write access
;--------------------------------------------------------------------------
.IF bReadOnly == TRUE
Invoke CreateFile, lpszPEFilename, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
.ELSE
Invoke CreateFile, lpszPEFilename, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL
.ENDIF
.IF eax == INVALID_HANDLE_VALUE
Invoke PE_SetError, NULL, PE_ERROR_OPEN_FILE
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov hPEFile, eax ; store file handle
;--------------------------------------------------------------------------
; Get file size and verify its not too low or too high in size
;--------------------------------------------------------------------------
Invoke GetFileSize, hPEFile, NULL
.IF eax < 268d ; https://www.bigmessowires.com/2015/10/08/a-handmade-executable-file/
; http://archive.is/w01DO#selection-265.0-265.44
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_SIZE_LOW
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ELSEIF eax > 1FFFFFFFh ; 536,870,911 536MB+ - rare to be this size or larger
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_SIZE_HIGH
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov PEFilesize, eax ; file size
;--------------------------------------------------------------------------
; Create file mapping of entire file
;--------------------------------------------------------------------------
.IF bReadOnly == TRUE
Invoke CreateFileMapping, hPEFile, NULL, PAGE_READONLY, 0, 0, NULL ; Create memory mapped file
.ELSE
Invoke CreateFileMapping, hPEFile, NULL, PAGE_READWRITE, 0, 0, NULL ; Create memory mapped file
.ENDIF
.IF eax == NULL
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_MAP
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov PEMemMapHandle, eax ; store mapping handle
;--------------------------------------------------------------------------
; Create view of file
;--------------------------------------------------------------------------
.IF bReadOnly == TRUE
Invoke MapViewOfFileEx, PEMemMapHandle, FILE_MAP_READ, 0, 0, 0, NULL
.ELSE
Invoke MapViewOfFileEx, PEMemMapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0, NULL
.ENDIF
.IF eax == NULL
Invoke CloseHandle, PEMemMapHandle
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_VIEW
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov PEMemMapPtr, eax ; store map view pointer
;--------------------------------------------------------------------------
; Check PE file signature - to make sure MZ and PE sigs are located
;--------------------------------------------------------------------------
Invoke PESignature, PEMemMapPtr
.IF eax == PE_INVALID
;----------------------------------------------------------------------
; Invalid PE file, so close all handles and return error
;----------------------------------------------------------------------
Invoke UnmapViewOfFile, PEMemMapPtr
Invoke CloseHandle, PEMemMapHandle
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_INVALID
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ELSE ; eax == PE_ARCH_32 || eax == PE_ARCH_64
;----------------------------------------------------------------------
; PE file is valid. So we process PE file and get pointers and other
; information and store in a 'handle' (hPE) that we return.
; Handle is a pointer to a PEINFO struct that stores PE file info.
;----------------------------------------------------------------------
Invoke PE_Analyze, PEMemMapPtr, lpdwPEHandle
.IF eax == FALSE
;------------------------------------------------------------------
; Error processing PE file, so close all handles and return error
;------------------------------------------------------------------
Invoke UnmapViewOfFile, PEMemMapPtr
Invoke CloseHandle, PEMemMapHandle
Invoke CloseHandle, hPEFile
xor eax, eax
ret
.ENDIF
.ENDIF
;--------------------------------------------------------------------------
; Success in processing PE file. Store additional information like file and
; map handles and filesize in our PEINFO struct (hPE) if we reach here.
;--------------------------------------------------------------------------
.IF lpdwPEHandle == NULL
Invoke UnmapViewOfFile, PEMemMapPtr
Invoke CloseHandle, PEMemMapHandle
Invoke CloseHandle, hPEFile
Invoke PE_SetError, NULL, PE_ERROR_OPEN_INVALID
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov ebx, lpdwPEHandle
mov eax, [ebx]
mov hPE, eax
mov ebx, hPE
mov eax, lpdwPEHandle
mov [ebx].PEINFO.PEHandle, eax
mov eax, bReadOnly
mov [ebx].PEINFO.PEOpenMode, eax
mov eax, PEMemMapHandle
mov [ebx].PEINFO.PEMemMapHandle, eax
mov eax, hPEFile
mov [ebx].PEINFO.PEFileHandle, eax
mov eax, PEFilesize
mov [ebx].PEINFO.PEFilesize, eax
.IF lpszPEFilename != NULL
lea eax, [ebx].PEINFO.PEFilename
Invoke lstrcpyn, eax, lpszPEFilename, MAX_PATH
.ENDIF
Invoke PE_SetError, NULL, PE_ERROR_SUCCESS
mov ebx, lpdwPEHandle
mov eax, hPE
mov [ebx], eax
;mov eax, hPE ; Return handle for our user to store and use in other functions
mov eax, TRUE
ret
PE_OpenFile ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_CloseFile - Close PE File
; Returns: None
;------------------------------------------------------------------------------
PE_CloseFile PROC USES EBX hPE:DWORD
IFDEF DEBUG32
PrintText 'PE_CloseFile'
ENDIF
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov ebx, [ebx].PEINFO.PEHandle
.IF ebx != 0
mov eax, 0 ; null out hPE handle if it exists
mov [ebx], eax
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEMemMapPtr
.IF eax != NULL
Invoke UnmapViewOfFile, eax
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEMemMapHandle
.IF eax != NULL
Invoke CloseHandle, eax
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEFileHandle
.IF eax != NULL
Invoke CloseHandle, eax
.ENDIF
mov eax, hPE
.IF eax != NULL
Invoke GlobalFree, eax
.ENDIF
Invoke PE_SetError, NULL, PE_ERROR_SUCCESS
xor eax, eax
ret
PE_CloseFile ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_Analyze - Process memory mapped PE file
; Returns: TRUE or FALSE. If TRUE a PE handle (hPE) is stored in the variable
; pointed to by lpdwPEHandle. If FALSE, use PE_GetError to get further info.
;
; Can be used directly on memory region where PE is already loaded/mapped
;
; PE_Analyze is also called by PE_OpenFile.
; Note: Use PE_Finish when finished with PE file if using PE_Analyze directly.
;------------------------------------------------------------------------------
PE_Analyze PROC USES EBX EDX pPEInMemory:DWORD, lpdwPEHandle:DWORD
LOCAL hPE:DWORD
LOCAL PEMemMapPtr:DWORD
LOCAL pFileHeader:DWORD
LOCAL pOptionalHeader:DWORD
LOCAL pDataDirectories:DWORD
LOCAL pSectionTable:DWORD
LOCAL pImportDirectoryTable:DWORD
LOCAL pCurrentSection:DWORD
LOCAL dwNumberOfSections:DWORD
LOCAL dwSizeOfOptionalHeader:DWORD
LOCAL dwNumberOfRvaAndSizes:DWORD
LOCAL dwCurrentSection:DWORD
LOCAL bPE64:DWORD
LOCAL dwRVA:DWORD
LOCAL dwOffset:DWORD
IFDEF DEBUG32
PrintText 'PE_Analyze'
ENDIF
.IF lpdwPEHandle == NULL
Invoke PE_SetError, NULL, PE_ERROR_NO_HANDLE
xor eax, eax
ret
.ENDIF
.IF pPEInMemory == NULL
Invoke PE_SetError, NULL, PE_ERROR_ANALYZE_NULL
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov eax, pPEInMemory
mov PEMemMapPtr, eax
;--------------------------------------------------------------------------
; Alloc mem for our PE Handle (PEINFO)
;--------------------------------------------------------------------------
Invoke GlobalAlloc, GMEM_FIXED or GMEM_ZEROINIT, SIZEOF PEINFO
.IF eax == NULL
Invoke PE_SetError, NULL, PE_ERROR_ANALYZE_ALLOC
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov hPE, eax
mov edx, hPE
mov eax, PEMemMapPtr
mov [edx].PEINFO.PEMemMapPtr, eax
mov [edx].PEINFO.PEDOSHeader, eax
; Process PE in memory
mov eax, PEMemMapPtr
mov ebx, eax ; ebx points to IMAGE_DOS_HEADER in memory
.IF [ebx].IMAGE_DOS_HEADER.e_lfanew == 0
Invoke PE_SetError, hPE, PE_ERROR_ANALYZE_INVALID
.IF hPE != NULL
Invoke GlobalFree, hPE
.ENDIF
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
;--------------------------------------------------------------------------
; Get headers: NT, File, Optional & other useful fields
;--------------------------------------------------------------------------
; ebx points to IMAGE_DOS_HEADER in memory
add eax, [ebx].IMAGE_DOS_HEADER.e_lfanew
mov [edx].PEINFO.PENTHeader, eax
mov ebx, eax ; ebx points to IMAGE_NT_HEADERS
lea eax, [ebx].IMAGE_NT_HEADERS.FileHeader
mov [edx].PEINFO.PEFileHeader, eax
mov pFileHeader, eax
lea eax, [ebx].IMAGE_NT_HEADERS.OptionalHeader
mov [edx].PEINFO.PEOptionalHeader, eax
mov pOptionalHeader, eax
mov ebx, pFileHeader ; ebx points to IMAGE_FILE_HEADER
movzx eax, word ptr [ebx].IMAGE_FILE_HEADER.NumberOfSections
mov [edx].PEINFO.PESectionCount, eax
mov dwNumberOfSections, eax
movzx eax, word ptr [ebx].IMAGE_FILE_HEADER.SizeOfOptionalHeader
mov [edx].PEINFO.PEOptionalHeaderSize, eax
mov dwSizeOfOptionalHeader, eax
movzx eax, word ptr [ebx].IMAGE_FILE_HEADER.Characteristics
and eax, IMAGE_FILE_DLL
.IF eax == IMAGE_FILE_DLL
mov [edx].PEINFO.PEDLL, TRUE
.ELSE
mov [edx].PEINFO.PEDLL, FALSE
.ENDIF
.IF dwSizeOfOptionalHeader == 0
mov pOptionalHeader, 0
mov pDataDirectories, 0
mov dwNumberOfRvaAndSizes, 0
mov bPE64, FALSE
.ELSE
;----------------------------------------------------------------------
; Get PE32/PE32+ magic number
;----------------------------------------------------------------------
mov ebx, pOptionalHeader; ebx points to IMAGE_OPTIONAL_HEADER
movzx eax, word ptr [ebx]
.IF eax == IMAGE_NT_OPTIONAL_HDR32_MAGIC ; PE32
mov ebx, hPE
mov [edx].PEINFO.PE64, FALSE
mov bPE64, FALSE
.ELSEIF eax == IMAGE_NT_OPTIONAL_HDR64_MAGIC ; PE32+ (PE64)
mov ebx, hPE
mov [edx].PEINFO.PE64, TRUE
mov bPE64, TRUE
.ELSE ; ROM or something else
Invoke PE_SetError, hPE, PE_ERROR_ANALYZE_INVALID
.IF hPE != NULL
Invoke GlobalFree, hPE
.ENDIF
mov ebx, lpdwPEHandle
mov eax, 0
mov [ebx], eax
xor eax, eax
ret
.ENDIF
mov eax, dwSizeOfOptionalHeader
.IF eax == 28 || eax == 24
;------------------------------------------------------------------
; Standard fields in IMAGE_OPTIONAL_HEADER
;------------------------------------------------------------------
mov pDataDirectories, 0
mov dwNumberOfRvaAndSizes, 0
.ELSEIF eax == 68 || eax == 88 ; Windows specific fields in IMAGE_OPTIONAL_HEADER
;------------------------------------------------------------------
; Windows specific fields in IMAGE_OPTIONAL_HEADER
; Get ImageBase, Subsystem, DllCharacteristics
;------------------------------------------------------------------
mov pDataDirectories, 0
mov dwNumberOfRvaAndSizes, 0
mov ebx, pOptionalHeader ; ebx points to IMAGE_OPTIONAL_HEADER
.IF bPE64 == TRUE ; ebx points to IMAGE_OPTIONAL_HEADER64
mov eax, dword ptr [ebx].IMAGE_OPTIONAL_HEADER64.ImageBase
mov dword ptr [edx].PEINFO.PE64ImageBase, eax
mov eax, dword ptr [ebx+4].IMAGE_OPTIONAL_HEADER64.ImageBase
mov dword ptr [edx+4].PEINFO.PE64ImageBase, eax
mov [edx].PEINFO.PEImageBase, 0
.ELSE ; ebx points to IMAGE_OPTIONAL_HEADER32
mov eax, [ebx].IMAGE_OPTIONAL_HEADER32.ImageBase
mov [edx].PEINFO.PEImageBase, eax
.ENDIF
.ELSE
;------------------------------------------------------------------
; Data Directories in IMAGE_OPTIONAL_HEADER
;------------------------------------------------------------------
mov ebx, pOptionalHeader ; ebx points to IMAGE_OPTIONAL_HEADER
.IF bPE64 == TRUE ; ebx points to IMAGE_OPTIONAL_HEADER64
mov eax, dword ptr [ebx].IMAGE_OPTIONAL_HEADER64.ImageBase
mov dword ptr [edx].PEINFO.PE64ImageBase, eax
mov eax, dword ptr [ebx+4].IMAGE_OPTIONAL_HEADER64.ImageBase
mov dword ptr [edx+4].PEINFO.PE64ImageBase, eax
mov [edx].PEINFO.PEImageBase, 0
mov eax, [ebx].IMAGE_OPTIONAL_HEADER64.NumberOfRvaAndSizes
mov [edx].PEINFO.PENumberOfRvaAndSizes, eax
mov dwNumberOfRvaAndSizes, eax
mov ebx, pOptionalHeader
add ebx, SIZEOF_STANDARD_FIELDS_PE64
add ebx, SIZEOF_WINDOWS_FIELDS_PE64
mov pDataDirectories, ebx
.ELSE ; ebx points to IMAGE_OPTIONAL_HEADER32
mov eax, [ebx].IMAGE_OPTIONAL_HEADER32.ImageBase
mov [edx].PEINFO.PEImageBase, eax
mov eax, [ebx].IMAGE_OPTIONAL_HEADER32.NumberOfRvaAndSizes
mov [edx].PEINFO.PENumberOfRvaAndSizes, eax
mov dwNumberOfRvaAndSizes, eax
mov ebx, pOptionalHeader
add ebx, SIZEOF_STANDARD_FIELDS_PE32
add ebx, SIZEOF_WINDOWS_FIELDS_PE32
mov pDataDirectories, ebx
.ENDIF
.ENDIF
.ENDIF
;--------------------------------------------------------------------------
; Get pointer to SectionTable
;--------------------------------------------------------------------------
mov eax, pFileHeader
add eax, SIZEOF IMAGE_FILE_HEADER
add eax, dwSizeOfOptionalHeader
mov [edx].PEINFO.PESectionTable, eax
mov pSectionTable, eax
mov pCurrentSection, eax
mov dwCurrentSection, 0
mov eax, 0
.WHILE eax < dwNumberOfSections
mov ebx, pCurrentSection
; do stuff with sections
; PointerToRawData to get section data
add pCurrentSection, SIZEOF IMAGE_SECTION_HEADER
inc dwCurrentSection
mov eax, dwCurrentSection
.ENDW
;--------------------------------------------------------------------------
; Get Data Directories
;--------------------------------------------------------------------------
IFDEF DEBUG32
mov eax, dwNumberOfRvaAndSizes
mov ebx, SIZEOF IMAGE_DATA_DIRECTORY
mul ebx
DbgDump pDataDirectories, eax
ENDIF
mov pImportDirectoryTable, 0
.IF pDataDirectories != 0
mov edx, hPE
.IF dwNumberOfRvaAndSizes > 0 ; Export Table
mov ebx, pDataDirectories
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEExportDirectoryTable, eax
.ENDIF
.ENDIF
.ENDIF
IFDEF DEBUG32
mov eax, dwNumberOfSections
mov ebx, SIZEOF IMAGE_SECTION_HEADER
mul ebx
DbgDump pSectionTable, eax
ENDIF
;--------------------------------------------------------------------------
; Update PEINFO handle information
;--------------------------------------------------------------------------
mov edx, hPE
mov eax, lpdwPEHandle
mov [edx].PEINFO.PEHandle, eax
mov ebx, lpdwPEHandle
mov eax, hPE
mov [ebx], eax
mov eax, TRUE
ret
PE_Analyze ENDP
;##############################################################################
; E X P O R T S E C T I O N F U N C T I O N S
;##############################################################################
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportDirectoryTable - Get pointer to ExportDirectoryTable
; Returns: pointer to ExportDirectoryTable or NULL
;------------------------------------------------------------------------------
PE_ExportDirectoryTable PROC USES EBX hPE:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEExportDirectoryTable
ret
PE_ExportDirectoryTable ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportNamePointerTable
;------------------------------------------------------------------------------
PE_ExportNamePointerTable PROC USES EBX hPE:DWORD
LOCAL PEMemMapPtr:DWORD
LOCAL pExportDirectoryTable:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEMemMapPtr
mov PEMemMapPtr, eax
Invoke PE_ExportDirectoryTable, hPE
.IF eax == 0
ret
.ENDIF
mov ebx, eax ; ebx is pExportDirectoryTable
mov eax, [ebx].IMAGE_EXPORT_DIRECTORY.AddressOfNames
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
; eax has pointer to Export Name Pointer Table RVA
ret
PE_ExportNamePointerTable ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportNameCount - Get count of names in the ExportDirectoryTable
; Returns: count of names or 0
;------------------------------------------------------------------------------
PE_ExportNameCount PROC USES EBX hPE:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
Invoke PE_ExportDirectoryTable, hPE
.IF eax == 0
ret
.ENDIF
mov ebx, eax
mov eax, [ebx].IMAGE_EXPORT_DIRECTORY.NumberOfNames
ret
PE_ExportNameCount ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportDLLName - Get DLL name for exports
; Returns: address of zero terminated DLL name string, or NULL
;------------------------------------------------------------------------------
PE_ExportDLLName PROC USES EBX hPE:DWORD
LOCAL PEMemMapPtr:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEMemMapPtr
mov PEMemMapPtr, eax
Invoke PE_ExportDirectoryTable, hPE
.IF eax == 0
ret
.ENDIF
mov ebx, eax ; ebx is pExportDirectoryTable
mov eax, [ebx].IMAGE_EXPORT_DIRECTORY.nName
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
; eax has pointer to DLL name
ret
PE_ExportDLLName ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportFunctionNames - Get function names exported in the DLL
; Returns: count of functions in lpdwFunctionsList array or 0.
; On succesful return lpdwFunctionsList points to a DWORD array containing
; pointers to the function names. Use GlobalFree on this array once finished.
;------------------------------------------------------------------------------
PE_ExportFunctionNames PROC USES EBX hPE:DWORD, lpdwFunctionsList:DWORD
LOCAL PEMemMapPtr:DWORD
LOCAL dwExportCount:DWORD
LOCAL pExportNamePointerTable:DWORD
LOCAL pExportNamePointerTableEntry:DWORD
LOCAL dwHintNameTableRVA:DWORD
LOCAL pNameList:DWORD
LOCAL pNameListNextFunction:DWORD
LOCAL dwNameListSize:DWORD
LOCAL nExport:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
.IF lpdwFunctionsList == 0
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEMemMapPtr
mov PEMemMapPtr, eax
Invoke PE_ExportNameCount, hPE
.IF eax == NULL
ret
.ENDIF
mov dwExportCount, eax
Invoke PE_ExportNamePointerTable, hPE
.IF eax == NULL
ret
.ENDIF
mov pExportNamePointerTable, eax
mov pExportNamePointerTableEntry, eax
; calc max name list string size
mov eax, dwExportCount
inc eax
mov ebx, SIZEOF DWORD
mul ebx
mov dwNameListSize, eax
Invoke GlobalAlloc, GMEM_FIXED or GMEM_ZEROINIT, dwNameListSize
.IF eax == NULL
ret
.ENDIF
mov pNameList, eax
mov pNameListNextFunction, eax
mov ebx, pExportNamePointerTableEntry
mov nExport, 0
mov eax, 0
.WHILE eax < dwExportCount
mov eax, [ebx] ; get rva pointer to function string
Invoke PE_RVAToOffset, hPE, eax
.IF eax == 0
ret
.ENDIF
add eax, PEMemMapPtr ; eax is pointer to function string
mov ebx, pNameListNextFunction
mov [ebx], eax ; store pointer to function string in our array
add pNameListNextFunction, SIZEOF DWORD
add pExportNamePointerTableEntry, SIZEOF DWORD ; pointers are always 32bits
mov ebx, pExportNamePointerTableEntry
inc nExport
mov eax, nExport
.ENDW
mov ebx, lpdwFunctionsList
mov eax, pNameList
mov [ebx], eax
mov eax, dwExportCount
ret
PE_ExportFunctionNames ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_ExportFunctionNameToDef - Creates a .DEF file from export functions
;------------------------------------------------------------------------------
PE_ExportFunctionNameToDef PROC USES EBX hPE:DWORD, lpszDefFilename:DWORD, bUseFilename:DWORD, bRemoveUnderscore:DWORD
LOCAL pExportFunctionNamesList:DWORD
LOCAL pExportName:DWORD
LOCAL nExportName:DWORD
LOCAL dwExportNameCount:DWORD
LOCAL lpszExportName:DWORD
LOCAL lpszExportDllName:DWORD
LOCAL hDefFile:DWORD
LOCAL dwNumberOfBytesToWrite:DWORD
LOCAL dwNumberOfBytesWritten:DWORD
LOCAL szDefFilename[MAX_PATH]:BYTE
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
.IF lpszDefFilename == NULL
; create def file based on export name (usually a .dll)
lea ebx, szDefFilename
mov byte ptr [ebx], '.'
mov byte ptr [ebx+1], '\'
.IF bUseFilename == TRUE
Invoke PE_FileNameOnly, hPE, Addr szDefFilename+2
Invoke lstrcat, Addr szDefFilename, CTEXT(".def")
.ELSE
Invoke PE_ExportDLLName, hPE
Invoke lstrcpyn, Addr szDefFilename+2, eax, MAX_PATH
Invoke lstrlen, Addr szDefFilename
lea ebx, szDefFilename
add ebx, eax
sub ebx, 4
mov eax, [ebx]
.IF eax == 'LLD.' || eax == 'lld.' || eax == 'EXE.' || eax == 'exe.'
mov byte ptr [ebx+1], 'd'
mov byte ptr [ebx+2], 'e'
mov byte ptr [ebx+3], 'f'
mov byte ptr [ebx+4], 0
.ELSE
xor eax, eax
ret
.ENDIF
.ENDIF
.ENDIF
Invoke PE_ExportNameCount, hPE
.IF eax == 0
ret
.ENDIF
mov dwExportNameCount, eax
Invoke PE_ExportFunctionNames, hPE, Addr pExportFunctionNamesList
.IF eax == 0
ret
.ENDIF
mov eax, pExportFunctionNamesList
mov pExportName, eax
; Create DEF file
.IF lpszDefFilename == NULL
Invoke CreateFile, Addr szDefFilename, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.ELSE
Invoke CreateFile, lpszDefFilename, GENERIC_READ or GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
.ENDIF
.IF eax == INVALID_HANDLE_VALUE
xor eax, eax
ret
.ENDIF
mov hDefFile, eax
; Write out LIBRARY and EXPORTS to DEF file
Invoke WriteFile, hDefFile, Addr DEFLIBRARY, dwLenDEFLIBRARY, Addr dwNumberOfBytesWritten, NULL
.IF bUseFilename == TRUE
Invoke PE_FileNameOnly, hPE, Addr szDefFilename ; reuse szDefFilename buffer
Invoke lstrlen, Addr szDefFilename
mov dwNumberOfBytesToWrite, eax
Invoke WriteFile, hDefFile, Addr szDefFilename, dwNumberOfBytesToWrite, Addr dwNumberOfBytesWritten, NULL
.ELSE
Invoke PE_ExportDLLName, hPE
mov lpszExportDllName, eax
Invoke lstrlen, lpszExportDllName
mov dwNumberOfBytesToWrite, eax
Invoke WriteFile, hDefFile, lpszExportDllName, dwNumberOfBytesToWrite, Addr dwNumberOfBytesWritten, NULL
.ENDIF
Invoke WriteFile, hDefFile, Addr DEFCRLF, dwLenDEFCRLF, Addr dwNumberOfBytesWritten, NULL
Invoke WriteFile, hDefFile, Addr DEFEXPORTS, dwLenDEFEXPORTS, Addr dwNumberOfBytesWritten, NULL
mov nExportName, 0
mov eax, 0
.WHILE eax < dwExportNameCount
mov ebx, pExportName
mov eax, [ebx]
mov lpszExportName, eax
.IF bRemoveUnderscore == TRUE
mov ebx, lpszExportName
movzx eax, byte ptr [ebx]
.IF al == '_'
inc lpszExportName
.ENDIF
.ENDIF
; Write out function name to DEF file
Invoke WriteFile, hDefFile, Addr DEFINDENT, dwLenDEFINDENT, Addr dwNumberOfBytesWritten, NULL
Invoke lstrlen, lpszExportName
mov dwNumberOfBytesToWrite, eax
Invoke WriteFile, hDefFile, lpszExportName, dwNumberOfBytesToWrite, Addr dwNumberOfBytesWritten, NULL
Invoke WriteFile, hDefFile, Addr DEFCRLF, dwLenDEFCRLF, Addr dwNumberOfBytesWritten, NULL
add pExportName, SIZEOF DWORD
inc nExportName
mov eax, nExportName
.ENDW
; Close DEF File
Invoke CloseHandle, hDefFile
mov eax, TRUE
ret
PE_ExportFunctionNameToDef ENDP
;##############################################################################
; I N F O F U N C T I O N S
;##############################################################################
PE_ALIGN
;------------------------------------------------------------------------------
; PE_DLL - returns TRUE if DLL or FALSE otherwise
;------------------------------------------------------------------------------
PE_DLL PROC USES EBX hPE:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PEDLL
ret
PE_DLL ENDP
PE_ALIGN
;------------------------------------------------------------------------------
; PE_PE64 - returns TRUE if PE32+ (PE64) or FALSE if PE32
;------------------------------------------------------------------------------
PE_PE64 PROC USES EBX hPE:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PE64
ret
PE_PE64 ENDP
;##############################################################################
; E R R O R F U N C T I O N S
;##############################################################################
PE_ALIGN
;------------------------------------------------------------------------------
; PE_SetError
;------------------------------------------------------------------------------
PE_SetError PROC USES EBX hPE:DWORD, dwError:DWORD
.IF hPE != NULL && dwError != PE_ERROR_SUCCESS
mov ebx, hPE
mov ebx, [ebx].PEINFO.PEHandle
.IF ebx != 0
mov eax, 0 ; null out hPE handle if it exists
mov [ebx], eax
.ENDIF
.ENDIF
mov eax, dwError
mov PELIB_ErrorNo, eax
ret
PE_SetError ENDP
;##############################################################################
; H E L P E R F U N C T I O N S
;##############################################################################
PE_ALIGN
;------------------------------------------------------------------------------
; PE_RVAToOffset - convert Relative Virtual Address (RVA) to file offset
;------------------------------------------------------------------------------
PE_RVAToOffset PROC USES EBX EDX hPE:DWORD, dwRVA:DWORD
LOCAL nTotalSections:DWORD
LOCAL nCurrentSection:DWORD
LOCAL pCurrentSection:DWORD
LOCAL dwSectionSize:DWORD
LOCAL dwVirtualAddress:DWORD
LOCAL dwPointerToRawData:DWORD
.IF hPE == NULL
xor eax, eax
ret
.ENDIF
mov ebx, hPE
mov eax, [ebx].PEINFO.PESectionCount
mov nTotalSections, eax
mov eax, [ebx].PEINFO.PESectionTable
mov pCurrentSection, eax
mov ebx, pCurrentSection
mov edx, dwRVA
mov eax, 0
mov nCurrentSection, 0
.WHILE eax < nTotalSections
mov eax, [ebx].IMAGE_SECTION_HEADER.Misc.VirtualSize
.IF eax == 0
mov eax, [ebx].IMAGE_SECTION_HEADER.SizeOfRawData
.ENDIF
mov dwSectionSize, eax
mov eax, [ebx].IMAGE_SECTION_HEADER.VirtualAddress
.IF eax <= edx
mov dwVirtualAddress, eax
add eax, dwSectionSize
.IF eax > edx
mov eax, [ebx].IMAGE_SECTION_HEADER.PointerToRawData
mov dwPointerToRawData, eax
mov ebx, dwVirtualAddress
mov eax, edx
sub eax, ebx
mov edx, eax
mov ebx, dwPointerToRawData