-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (60 loc) · 2.65 KB
/
Copy pathmain.py
File metadata and controls
81 lines (60 loc) · 2.65 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This is the main script to extract PE icons and header information
"""
__author__ = "Paolo Di Prodi"
__copyright__ = "Copyright 2017, LogstTotal Project"
__license__ = "Apache"
__version__ = "2.0"
__maintainer__ = "Paolo Di Prodi"
__email__ = "paolo@logstotal.com"
__status__ = "Experimental"
import pefile
import argparse
import os
from extracticon import ExtractIcon
from hashicons import hash_icons
import json
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Process PE file and extract information')
parser.add_argument('--pefile',dest='filepath',type=str,required=True)
parser.add_argument('-d','--dump_info', dest='dump_info', action='store_true')
parser.add_argument('-i','--image_hash', dest='image_hash', action='store_true')
parser.add_argument('-m', '--main_icon', dest='main_icon', action='store_true')
parser.add_argument('-s', '--save_icon', dest='save_icon', action='store_true')
parser.add_argument('-o', '--output_folder', dest='out_path', type=str)
args = parser.parse_args()
if args.filepath is not None:
fileid = os.path.basename(args.filepath)
fileid = fileid.replace(".","_")
if args.out_path:
os.makedirs(args.out_path, exist_ok=True)
try:
pe_parsed = pefile.PE(args.filepath, fast_load=True)
pe_parsed.parse_data_directories( directories=[
pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_IMPORT'],
pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_EXPORT'],
pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_TLS'],
pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_RESOURCE']])
if args.image_hash:
ico_extractor = ExtractIcon(pe_parsed)
if args.out_path is None:
icons_path = os.path.join(fileid)
os.makedirs(icons_path, exist_ok=True)
else:
icons_path = args.out_path
icon_report = hash_icons(ico_extractor,icons_path,args.main_icon,args.save_icon)
with open(os.path.join(icons_path,"icons.json"), "w") as rep:
json.dump(icon_report,rep)
if args.dump_info:
if args.out_path is None:
print(pe_parsed.dump_info())
else:
summary_path = os.path.join(args.out_path,"pemeta.txt")
with open(summary_path,"w") as rep:
rep.write(pe_parsed.dump_info())
except Exception as msg:
raise msg
else:
parser.print_help()