-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_dumper.py
91 lines (68 loc) · 2.57 KB
/
resource_dumper.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
#!/usr/bin/env python3
''' PE File Resource content identification and extraction of contents '''
import argparse
import pefile
import magic
__author__ = "Ben Mason"
__copyright__ = "Copyright 2022"
__version__ = "0.1.0"
__email__ = "[email protected]"
__status__ = "Development"
EXPORT_DATA = False
IGNORE_NONE_NAMES = False
FILE_SUFFIX = '.bin'
def write_file(filename, bin_data):
''' Write Data to a file '''
with open(filename, 'wb') as file_handle:
file_handle.write(bin_data)
return 0
def main(filename):
''' Main function '''
none_name_counter = 0
# Open PE file and read contents
thepefile = pefile.PE(filename)
all_data = thepefile.get_memory_mapped_image()
print(thepefile.DIRECTORY_ENTRY_RESOURCE.struct)
print ("-----------------------")
print()
for entry in thepefile.DIRECTORY_ENTRY_RESOURCE.entries:
if entry.name is None and IGNORE_NONE_NAMES:
print ("Skipping Directory entry named None")
continue
print("Directory Entry: ", entry.name)
print(entry.struct)
print ("")
for item in entry.directory.entries:
print("Resource Item: ", item.name)
for item_dir in item.directory.entries:
print(item_dir.data.struct)
data_rva = item_dir.data.struct.OffsetToData
size = item_dir.data.struct.Size
data = all_data[data_rva:data_rva+size]
print()
print("Data type: " + magic.from_buffer(data[0:1024]))
print("First 20 bytes:" , end='')
print(data[0:20])
if EXPORT_DATA:
if item.name is None:
filename = 'Unknown_' + str(none_name_counter) + FILE_SUFFIX
none_name_counter += 1
else:
filename = str(item.name) + FILE_SUFFIX
print("Exporting content to: " + filename)
write_file(filename, data)
print ("")
print ("-----------------------")
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument("file", help="Filename")
parser.add_argument("-d", "--dump", help="Dump Contents to Files",
action="store_true")
parser.add_argument("-i", "--ignore", help="Ignore Sections named None",
action="store_true")
args = parser.parse_args()
if args.dump:
EXPORT_DATA = True
if args.ignore:
IGNORE_NONE_NAMES = True
main(args.file)