-
Notifications
You must be signed in to change notification settings - Fork 0
/
PEparser.py
456 lines (357 loc) · 17.3 KB
/
PEparser.py
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
import os
#This function converts numbers stored in little endian to human-readable format
def convert_little_endian(hex, l):
retval = ""
for i in range(0, l, 2):
retval += hex[-(i+2)] + hex[-(i+1)]
return '0x' + retval
#This function is used to print data stored in fields of headers
def print_line(name, size, index):
output = "\t" + name + ":"
for i in range(26 - len(name)):
output += " "
print(output + convert_little_endian(text[index:index + size], size))
return index + size
#This function is used to find a section where import directories reside
def where_is_import_table(sec, rva):
retval = ""
for k in sec:
if rva < sec[k]:
return retval
retval = k
return retval
#This funciton does the same work as where_is_import_table, but I am using it to find the export table, hence the name
def where_is_export_table(secrva, rva):
return where_is_import_table(secrva, rva)
#This function also does the same work as where_is_import_table, but the name implies that it is used to find any RVA
def where_is_address(secrva, rva):
return where_is_import_table(secrva, rva)
#This function calculates physical offset of RVA based on following formula:
#PHYSICAL OFFSET = RVA - SECTION RVA + POINTER TO RAW DATA
#In my code I have been multiplying calcualted physical offset by two because I haven't been reading text of a file
#as an array of bytes, but as a string where every nibble is one character.
def calculate_physical_offset(rva, secrva, ptr):
return rva - secrva + ptr
print("Enter the name or a full path of a file you want to parse if it is in the same folder as this program:", end=" ")
while True:
try:
file_to_parse = open(input(), "rb")
break
except FileNotFoundError:
print("File not found. Please try again.")
continue
text = file_to_parse.read()
text = ''.join('{:02X}'.format(c) for c in text)
index = 0
############################################################################
# MZ header #
############################################################################
print("MZ header\n=========")
index = print_line("Magic", 4, index)
index = print_line("BytesOnLastPageOfFile", 4, index)
index = print_line("PagesInFile", 4, index)
index = print_line("Relocations", 4, index)
index = print_line("SizeOfHeaderInParagraphs", 4, index)
index = print_line("MinimumExtraParagraphs", 4, index)
index = print_line("MaximumExtraParagraphs", 4, index)
index = print_line("InitialSS", 4, index)
index = print_line("InitialSP", 4, index)
index = print_line("Checksum", 4, index)
index = print_line("InitialIP", 4, index)
index = print_line("InitialCS", 4, index)
index = print_line("OffsetToRelocationTable", 4, index)
index = print_line("OverlayNumber", 4, index)
for i in range(4):
index = print_line("Reserved", 4, index)
index = print_line("OEMIdentifier", 4, index)
index = print_line("OEMInformation", 4, index)
for i in range(10):
index = print_line("Reserved", 4, index)
print_line("OffsetToNewExeHeader", 8, index)
index = 2*int((convert_little_endian(text[index:index+8], 8)[2:]), 16)
############################################################################
# PE header #
############################################################################
print("\nPE header\n=========")
index = print_line("Magic", 8, index)
index = print_line("Machine", 4, index)
number_of_section_headers = int(convert_little_endian(text[index:index+4],4)[2:],16)
index = print_line("NumberOfSections", 4, index)
index = print_line("TimeDateStamp", 8, index)
index = print_line("PointerToSymbolTable", 8, index)
index = print_line("NumberOfSymbols", 8, index)
index = print_line("SizeOfOptionalHeader", 4, index)
index = print_line("Characteristics", 4, index)
############################################################################
# Optional header #
############################################################################
print("\nOptional header\n===============")
optional_header_magic = convert_little_endian(text[index:index+4], 4)
index = print_line("Magic", 4, index)
index = print_line("MajorLinkerVersion", 2, index)
index = print_line("MinorLinkerVersion", 2, index)
index = print_line("SizeOfCode", 8, index)
index = print_line("SizeOfInitializedData", 8, index)
index = print_line("SizeOfUninitializedData", 8, index)
index = print_line("AddressOfEntryPoint", 8, index)
index = print_line("BaseOfCode", 8, index)
index = print_line("BaseOfData", 8, index)
image_base = int(convert_little_endian(text[index:index+8],8)[2:], 16)
index = print_line("ImageBase", 8, index)
index = print_line("SectionAlignment", 8, index)
index = print_line("FileAlignment", 8, index)
index = print_line("MajorOSVersion", 4, index)
index = print_line("MinorOSVersion", 4, index)
index = print_line("MajorImageVersion", 4, index)
index = print_line("MinorImageVersion", 4, index)
index = print_line("MajorSubsystemVersion", 4, index)
index = print_line("MinorSubsystemVersion", 4, index)
index = print_line("Win32VersionValue", 8, index)
index = print_line("SizeOfImage", 8, index)
index = print_line("SizeOfHeaders", 8, index)
index = print_line("Checksum", 8, index)
index = print_line("Subsystem", 4, index)
index = print_line("DLLCharacteristics", 4, index)
index = print_line("SizeOfStackReserve", 8, index)
index = print_line("SizeOfStackCommit", 8, index)
index = print_line("SizeOfHeapReserve", 8, index)
index = print_line("SizeOfHeapCommit", 8, index)
index = print_line("LoaderFlags", 8, index)
index = print_line("NumberOfDataDirectories", 8, index)
print("\tEXPORT Table")
export_table_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("\tRVA", 8, index)
export_table_size = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("\tSize", 8, index)
print("\tIMPORT Table")
import_table_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tRESOURCE Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tEXCEPTION Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tCERTIFICATE Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tBASE RELOCATION Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tDEBUG Directory")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tArchitecture Specific Data")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tGLOBAL POINTER Register")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tTLS Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tLOAD CONFIGURATION Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tBOUND IMPORT Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tIMPORT Address Table")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tDELAY IMPORT Descriptors")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print("\tCLI Header")
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
print()
index = print_line("\tRVA", 8, index)
index = print_line("\tSize", 8, index)
############################################################################
# Section headers #
############################################################################
print("Section headers\n===============")
pointers_to_raw_data = {}
section_rvas = {}
for i in range(number_of_section_headers):
print("\tName:", end="")
for j in range(22):
print(end=" ")
name = bytearray.fromhex(text[index:index + 16].lstrip("0")).decode()
print(name)
index += 16
index = print_line("VirtualSize", 8, index)
section_rvas[name] = int(convert_little_endian(text[index:index+8],8)[2:], 16)
index = print_line("RVA", 8, index)
index = print_line("SizeOfRawData", 8, index)
pointers_to_raw_data[name] = int(convert_little_endian(text[index:index+8],8)[2:], 16)
index = print_line("PointerToRawData", 8, index)
index = print_line("PointerToRelocations", 8, index)
index = print_line("PointerToLineNumbers", 8, index)
index = print_line("NumberOfRelocations", 4, index)
index = print_line("NumberOfLineNumbers", 4, index)
index = print_line("Characteristics", 8, index)
print()
############################################################################
# Import table #
############################################################################
if optional_header_magic == "0x020B":
print("File is 64-bit, therefore only limited view is provided.\n")
os.system('PAUSE')
exit(0)
there = where_is_import_table(section_rvas, import_table_rva)
physical_offset = calculate_physical_offset(import_table_rva, section_rvas[there], pointers_to_raw_data[there])
index = 2 * physical_offset
print("Import table\n============")
while convert_little_endian(text[index:index+8], 8) != "0x00000000":
print("\n\tImport directory\n\t================")
index = print_line("\tImportNameTableRVA", 8, index)
index = print_line("\tTimeDateStamp", 8, index)
index = print_line("\tForwarderChain", 8, index)
name_rva = int(convert_little_endian(text[index:index+8],8)[2:], 16)
name_physical_offset = 2 * calculate_physical_offset(name_rva, section_rvas[there], pointers_to_raw_data[there])
index = print_line("\tNameRVA", 8, index)
print("\t\tLibraryName:", end = "")
for j in range(14):
print(end=" ")
i = name_physical_offset
while True:
hexcharacter = text[i:i+2]
if hexcharacter == "00":
break
character = bytearray.fromhex(hexcharacter).decode()
print(character, end = "")
i += 2
print()
import_address_table_rva = int(convert_little_endian(text[index:index + 8], 8)[2:], 16)
import_address_table_physical_offset = 2 * calculate_physical_offset(import_address_table_rva, section_rvas[there], pointers_to_raw_data[there])
index = print_line("\tImportAddressTableRVA", 8, index)
print()
print("\t\tImport Thunks\n\t\t================")
i = import_address_table_physical_offset
while True:
output_string = "\t\t\tApi: "
if convert_little_endian(text[i:i+8], 8)[2] != "0":
print("\t\t\tApi: ", end="")
print(convert_little_endian(text[i:i+8], 8), end="")
print(" Ordinal: " + convert_little_endian(text[i:i+4], 4))
i += 8
continue
function_name_rva = convert_little_endian(text[i:i + 8], 8)
output_string += function_name_rva + " "
function_name_rva = int(function_name_rva, 16)
function_name_physical_offset = calculate_physical_offset(function_name_rva, section_rvas[there], pointers_to_raw_data[there])
output_string += "(phys: " + str.format('0x{:08X}', function_name_physical_offset) + ") hint: "
function_name_physical_offset *= 2
j = function_name_physical_offset
if text[j:j+8] == "00000000" or j <= 0:
break
hint = convert_little_endian(text[j:j+4], 4)
output_string += hint + ", Name: "
j += 4
function_name = ""
while text[j:j+2] != "00" and j > 0:
function_name += bytearray.fromhex(text[j:j+2]).decode()
j += 2
output_string += function_name + " "
print(output_string)
i += 8
############################################################################
# Export table #
############################################################################
print("Export Table\n============")
if export_table_size == 0:
print("\tExport table does not exist\n")
os.system('PAUSE')
exit(0)
there = where_is_export_table(section_rvas, export_table_rva)
physical_offset = calculate_physical_offset(export_table_rva, section_rvas[there], pointers_to_raw_data[there])
index = 2 * physical_offset
print("\tExport Directory\n\t================")
index = print_line("Characteristics", 8, index)
index = print_line("TimeDateStamp:", 8, index)
index = print_line("MinorVersion", 4, index)
index = print_line("MajorVersion", 4, index)
name_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("NameRVA", 8, index)
name_physical_offset = 2 * calculate_physical_offset(name_rva, section_rvas[there], pointers_to_raw_data[there])
i = name_physical_offset
print("\tLibraryName: ", end="")
while text[i:i+2] != "00":
print(bytearray.fromhex(text[i:i+2]).decode(), end="")
i += 2
function_names = i + 2
print()
ordinal_base = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("OrdinalBase", 8, index)
number_of_functions = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("NumberOfFunctions", 8, index)
number_of_names = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("NumberOfNames", 8, index)
address_table_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
index = print_line("AddressTableRVA", 8, index)
name_pointer_table_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
name_pointer_table_physical_offset = 2 * calculate_physical_offset(name_pointer_table_rva, section_rvas[there], pointers_to_raw_data[there])
index = print_line("NamePointerTableRVA", 8, index)
ordinal_table_rva = int(convert_little_endian(text[index:index+8], 8)[2:], 16)
ordinal_table_offset = 2 * calculate_physical_offset(ordinal_table_rva, section_rvas[there], pointers_to_raw_data[there])
index = print_line("OrdinalTableRVA", 8, index)
address_table_physical_offset = 2 * calculate_physical_offset(address_table_rva, section_rvas[there], pointers_to_raw_data[there])
k = name_pointer_table_physical_offset
ordinals_and_names = {}
ordinals_and_function_name_addresses = {}
ordinals_and_physical_offsets = {}
ordinals_index = ordinal_table_offset
for i in range (number_of_names):
ordinal = ordinal_base + int(convert_little_endian(text[ordinals_index:ordinals_index+4],4),16)
function_name_rva = int(convert_little_endian(text[k:k+8], 8), 16)
ordinals_and_function_name_addresses[hex(ordinal)] = convert_little_endian(text[k:k+8], 8)
function_name_physical_offset = 2 * calculate_physical_offset(function_name_rva, section_rvas[there], pointers_to_raw_data[there])
ordinals_and_physical_offsets[hex(ordinal)] = function_name_physical_offset
l = function_name_physical_offset
name = ""
while text[l:l+2] != "00":
name += bytearray.fromhex(text[l:l+2]).decode()
l += 2
ordinals_and_names[hex(ordinal)] = name
ordinals_index += 4
k += 8
print("\n\t\tExport address table\n\t\t====================")
j = address_table_physical_offset
for i in range(number_of_functions):
ordinal = hex(ordinal_base + i)
output_string = "\t\t\tApi: "
function_rva = convert_little_endian(text[j:j+8], 8)
output_string += convert_little_endian(text[j:j+8], 8)
here = where_is_address(section_rvas, int(function_rva[2:], 16))
output_string += " (phys: "
output_string += str.format('0x{:08X}', calculate_physical_offset(int(function_rva[2:], 16), section_rvas[here], pointers_to_raw_data[here]))
output_string += ") "
output_string += "--> Ordinal: " + str.format('0x{:04X}', i)
output_string += " Name: "
if ordinal in ordinals_and_names:
output_string += ordinals_and_names[ordinal]
print(output_string)
j += 8
print("\n\t\tFunction name table\n\t\t====================")
for i in range(number_of_functions):
ordinal = hex(ordinal_base + i)
if ordinal not in ordinals_and_function_name_addresses:
continue
print("\t\t\tApi: ", ordinals_and_function_name_addresses[ordinal], end = "")
offset = str.format('0x{:08X}', ordinals_and_physical_offsets[ordinal])
print(" (phys: " + offset + ") ", end = "")
print("Name: " + ordinals_and_names[ordinal])
print("\n\t\tFunction name table\n\t\t====================")
for i in range(number_of_functions):
print("\t\t\tValue: " + str(hex(i)), end = "")
decoded_ordinal = str.format('0x{:04X}', int(i + ordinal_base))
print("(decoded ordinal: " + decoded_ordinal + ") ", end = "")
print("Name: ", end = "")
if hex(i + ordinal_base) not in ordinals_and_names:
continue
print(ordinals_and_names[hex(i + ordinal_base)])
print()
os.system('PAUSE')