-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathpe.py
executable file
·152 lines (131 loc) · 4.41 KB
/
pe.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import pefile
import argparse
import sys
import hashlib
import datetime
"""Display infos about a PE file
Author : Tek <[email protected]>
Date : 5/10/2016
"""
def display_hashes(data, pe):
"""Display md5, sha1 and sh256 of the data given"""
for algo in ["md5", "sha1", "sha256"]:
m = getattr(hashlib, algo)()
m.update(data)
print("%s\t%s" % (algo.upper(), m.hexdigest()))
print("Imphash: %s" % pe.get_imphash())
def display_headers(pe):
"""Display header information"""
if pe.FILE_HEADER.IMAGE_FILE_DLL:
print("DLL File! ")
print("Compile Time: " + str(datetime.datetime.fromtimestamp(pe.FILE_HEADER.TimeDateStamp)))
def display_sections(pe):
"""Display information about the PE sections"""
print("Name\tVirtualSize\tVirtualAddress\tRawSize\t\tRawAddress")
for section in pe.sections:
print("%s\t%s\t\t%s\t\t%s\t\t%s" % (
section.Name,
hex(section.Misc_VirtualSize),
hex(section.VirtualAddress),
hex(section.PointerToRawData),
hex(section.SizeOfRawData)
))
def display_imports(pe):
"""Display imports"""
for entry in pe.DIRECTORY_ENTRY_IMPORT:
print(entry.dll)
for imp in entry.imports:
print('\t%s %s' % (hex(imp.address), imp.name))
def display_exports(pe):
"""Display exports"""
try:
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
print("%s %s %s" % (
hex(pe.OPTIONAL_HEADER.ImageBase + exp.address),
exp.name,
exp.ordinal
))
except AttributeError:
return
def display_debug(pe):
"""Display debug infos"""
if hasattr(pe, 'DIRECTORY_ENTRY_DEBUG'):
for i in pe.DIRECTORY_ENTRY_DEBUG:
if hasattr(i.entry, 'PdbFileName'):
print("Debug Information: %s" % i.entry.PdbFileName)
def resource(level, r):
"""Recursive printing of resources"""
if hasattr(r, "data"):
# resource
offset = r.data.struct.OffsetToData
size = r.data.struct.Size
data = pe.get_memory_mapped_image()[offset:offset+size]
m = hashlib.md5()
m.update(data)
print(" "*level + "-%s\t%i\t%i\t%s\t%s\t%s" % (
str(r.name),
r.id,
size,
m.hexdigest(),
pefile.LANG.get(r.data.lang, 'UNKNOWN'),
pefile.get_sublang_name_for_lang(r.data.lang, r.data.sublang)
)
)
else:
# directory
if r.name is None:
print(" "*level + "-" + str(r.id))
else:
print(" "*level + "-" + str(r.name))
for r2 in r.directory.entries:
resource(level+1, r2)
def display_resources(pe):
"""Display resources"""
if hasattr(pe, 'DIRECTORY_ENTRY_RESOURCE'):
if(len(pe.DIRECTORY_ENTRY_RESOURCE.entries) > 0):
print("Resources:")
for r in pe.DIRECTORY_ENTRY_RESOURCE.entries:
resource(0, r)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Display information about a PE file')
parser.add_argument('FILE', help='a PE file')
parser.add_argument('--sections', '-s', action='store_true', help='Only display sections')
parser.add_argument('--imports', '-i', action='store_true', help='Display imports only')
parser.add_argument('--exports', '-e', action='store_true', help='Display exports only')
parser.add_argument('--resources', '-r', action='store_true', help='Display resources only')
parser.add_argument('--full', '-f', action='store_true', help='Full dump of all pefile infos')
args = parser.parse_args()
fin = open(args.FILE, 'rb')
data = fin.read()
fin.close()
pe = pefile.PE(data=data)
if args.sections:
display_sections(pe)
sys.exit(0)
if args.imports:
display_imports(pe)
sys.exit(0)
if args.exports:
display_exports(pe)
sys.exit(0)
if args.resources:
display_resources(pe)
sys.exit(0)
if args.full:
print(pe.dump_info())
sys.exit(0)
display_hashes(data, pe)
print("")
display_headers(pe)
print("")
display_debug(pe)
print("")
display_sections(pe)
print("")
display_imports(pe)
print("")
display_exports(pe)
print("")
display_resources(pe)