-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathPE.asm
909 lines (821 loc) · 32.6 KB
/
PE.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
;==============================================================================
;
; PE LIBRARY
;
; Copyright (c) 2019 by fearless
;
; http://github.com/mrfearless
;
;
; This software is provided 'as-is', without any express or implied warranty.
; In no event will the author be held liable for any damages arising from the
; use of this software.
;
; Permission is granted to anyone to use this software for any non-commercial
; program. If you use the library in an application, an acknowledgement in the
; application or documentation is appreciated but not required.
;
; You are allowed to make modifications to the source code, but you must leave
; the original copyright notices intact and not misrepresent the origin of the
; software. It is not allowed to claim you wrote the original software.
; Modified files must have a clear notice that the files are modified, and not
; in the original state. This includes the name of the person(s) who modified
; the code.
;
; If you want to distribute or redistribute any portion of this package, you
; will need to include the full package in it's original state, including this
; license and all the copyrights.
;
; While distributing this package (in it's original state) is allowed, it is
; not allowed to charge anything for this. You may not sell or include the
; package in any commercial package without having permission of the author.
; Neither is it allowed to redistribute any of the package's components with
; commercial applications.
;
;==============================================================================
.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 PE.inc
;------------------------------------------------------------------------------
; Prototypes for internal use
;------------------------------------------------------------------------------
PESignature PROTO :DWORD
PEJustFname PROTO :DWORD, :DWORD
PEIncreaseFileSize PROTO :DWORD, :DWORD
PEDecreaseFileSize 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
.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
.IF dwNumberOfRvaAndSizes > 1 ; Import Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEImportDirectoryTable, eax
mov pImportDirectoryTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 2 ; Resource Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEResourceDirectoryTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 3 ; Exception Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEExceptionTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 4 ; Certificate Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PECertificateTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 5 ; Base Relocation Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEBaseRelocationTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 6 ; Debug Data
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEDebugData, eax
.ENDIF
.ENDIF
;.IF dwNumberOfRvaAndSizes > 7 ; Data Directory Architecture
;mov ebx, pDataDirectories
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
;.IF eax != 0
; add eax, PEMemMapPtr
; mov pDataDirArchitecture, eax
;.ENDIF
;.ENDIF
.IF dwNumberOfRvaAndSizes > 8 ; Global Ptr
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEGlobalPtr, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 9 ; TLS Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PETLSTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 10 ; Load Config Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PELoadConfigTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 11 ; Bound Import Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEBoundImportTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 12 ; Import Address Table
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEImportAddressTable, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 13 ; Delay Import Descriptor
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PEDelayImportDescriptor, eax
.ENDIF
.ENDIF
.IF dwNumberOfRvaAndSizes > 14 ; CLR Runtime Header
mov ebx, pDataDirectories
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
add ebx, SIZEOF IMAGE_DATA_DIRECTORY
mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
.IF eax != 0
Invoke PE_RVAToOffset, hPE, eax
add eax, PEMemMapPtr
mov [edx].PEINFO.PECLRRuntimeHeader, eax
.ENDIF
.ENDIF
;.IF dwNumberOfRvaAndSizes > 15 ; DataDir Reserved
;mov ebx, pDataDirectories
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;add ebx, SIZEOF IMAGE_DATA_DIRECTORY
;mov eax, [ebx].IMAGE_DATA_DIRECTORY.VirtualAddress
;.IF eax != 0
; add eax, PEMemMapPtr
; mov pDataDirReserved, eax
;.ENDIF
;.ENDIF
.ENDIF
;--------------------------------------------------------------------------
; Import
;--------------------------------------------------------------------------
.IF pImportDirectoryTable != 0
mov eax, 0
mov ebx, pImportDirectoryTable
.WHILE [ebx].IMAGE_IMPORT_DESCRIPTOR.Characteristics != 0
inc eax
add ebx, SIZEOF IMAGE_IMPORT_DESCRIPTOR
.ENDW
mov edx, hPE
mov [edx].PEINFO.PEImportDirectoryCount, eax
.ENDIF
IFDEF DEBUG32
mov eax, dwNumberOfSections
mov ebx, SIZEOF IMAGE_SECTION_HEADER
mul ebx
DbgDump pSectionTable, eax
ENDIF
Invoke PE_HeaderRich, hPE
.IF eax != 0
mov edx, hPE
mov [edx].PEINFO.PERichHeader, eax
Invoke PE_RichSignatureCompIDs, hPE
.IF eax != 0
mov edx, hPE
mov [edx].PEINFO.PERichCompIDs, eax
.ENDIF
.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
PE_LIBEND