This repository has been archived by the owner on Mar 15, 2021. It is now read-only.
/
pe_features.py
366 lines (306 loc) · 16.9 KB
/
pe_features.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
''' PE Features worker. This class pulls static features
out of a PE file using the python pefile module.
'''
import pefile
import pprint
class PEFileWorker(object):
''' Create instance of PEFileWorker class. This class pulls static
features out of a PE file using the python pefile module.
'''
dependencies = ['sample', 'tags']
def __init__(self, verbose=False):
''' Init method '''
# Dense feature list: this only functions to ensure that all of these
# features get extracted with a sanity check at the end.
self._dense_feature_list = None
self._dense_features = None
# Okay now the sparse fields
self._sparse_feature_list = None
self._sparse_features = None
# Verbose
self._verbose = verbose
# Warnings handle
self._warnings = []
# Set the features that I'm expected PE File to extract, note this is just
# for sanity checking, meaning that if you don't get some of these features
# the processing will spit out warnings for each feature not extracted.
self.set_dense_features(['check_sum', 'generated_check_sum', 'compile_date', 'debug_size', 'export_size',
'iat_rva', 'major_version', 'minor_version', 'number_of_bound_import_symbols',
'number_of_bound_imports', 'number_of_export_symbols', 'number_of_import_symbols',
'number_of_imports', 'number_of_rva_and_sizes', 'number_of_sections', 'pe_warnings',
'std_section_names', 'total_size_pe', 'virtual_address', 'virtual_size',
'virtual_size_2', 'datadir_IMAGE_DIRECTORY_ENTRY_BASERELOC_size',
'datadir_IMAGE_DIRECTORY_ENTRY_RESOURCE_size',
'datadir_IMAGE_DIRECTORY_ENTRY_IAT_size', 'datadir_IMAGE_DIRECTORY_ENTRY_IMPORT_size',
'pe_char', 'pe_dll', 'pe_driver', 'pe_exe', 'pe_i386', 'pe_majorlink',
'pe_minorlink', 'sec_entropy_data', 'sec_entropy_rdata',
'sec_entropy_reloc', 'sec_entropy_text', 'sec_entropy_rsrc', 'sec_rawptr_rsrc',
'sec_rawsize_rsrc', 'sec_vasize_rsrc', 'sec_raw_execsize', 'sec_rawptr_data',
'sec_rawptr_text', 'sec_rawsize_data', 'sec_rawsize_text', 'sec_va_execsize',
'sec_vasize_data', 'sec_vasize_text', 'size_code', 'size_image', 'size_initdata',
'size_uninit'])
self.set_sparse_features(['imported_symbols', 'section_names', 'pe_warning_strings'])
def execute(self, input_data):
''' Process the input bytes with pefile '''
raw_bytes = input_data['sample']['raw_bytes']
# Have the PE File module process the file
pefile_handle, error_str = self.open_using_pefile('unknown', raw_bytes)
if not pefile_handle:
return {'error': error_str, 'dense_features': [], 'sparse_features': []}
# Now extract the various features using pefile
dense_features, sparse_features = self.extract_features_using_pefile(pefile_handle)
# Okay set my response
return {'dense_features': dense_features, 'sparse_features': sparse_features, 'tags': input_data['tags']['tags']}
def set_dense_features(self, dense_feature_list):
''' Set the dense feature list that the Python pefile module should extract.
This is really just sanity check functionality, meaning that these
are the features you are expecting to get, and a warning will spit
out if you don't get some of these. '''
self._dense_feature_list = dense_feature_list
def get_dense_features(self):
''' Set the dense feature list that the Python pefile module should extract. '''
return self._dense_features
def set_sparse_features(self, sparse_feature_list):
''' Set the sparse feature list that the Python pefile module should extract.
This is really just sanity check functionality, meaning that these
are the features you are expecting to get, and a warning will spit
out if you don't get some of these. '''
self._sparse_feature_list = sparse_feature_list
def get_sparse_features(self):
''' Set the sparse feature list that the Python pefile module should extract. '''
return self._sparse_features
# Make sure pe can parse this file
@staticmethod
def open_using_pefile(input_name, input_bytes):
''' Open the PE File using the Python pefile module. '''
try:
pef = pefile.PE(data=input_bytes, fast_load=False)
except (AttributeError, pefile.PEFormatError), error:
print 'warning: pe_fail (with exception from pefile module) on file: %s' % input_name
error_str = '(Exception):, %s' % (str(error))
return None, error_str
# Now test to see if the features are there/extractable if not return FAIL flag
if pef.PE_TYPE is None or pef.OPTIONAL_HEADER is None or len(pef.OPTIONAL_HEADER.DATA_DIRECTORY) < 7:
print 'warning: pe_fail on file: %s' % input_name
error_str = 'warning: pe_fail on file: %s' % input_name
return None, error_str
# Success
return pef, None
# Extract various set of features using PEfile module
def extract_features_using_pefile(self, pef):
''' Process the PE File using the Python pefile module. '''
# Store all extracted features into feature lists
extracted_dense = {}
extracted_sparse = {}
# Now slog through the info and extract the features
feature_not_found_flag = -99
feature_default_value = 0
self._warnings = []
# Set all the dense features and sparse features to 'feature not found'
# value and then check later to see if it was found
for feature in self._dense_feature_list:
extracted_dense[feature] = feature_not_found_flag
for feature in self._sparse_feature_list:
extracted_sparse[feature] = feature_not_found_flag
# Check to make sure all the section names are standard
std_sections = ['.text', '.bss', '.rdata', '.data', '.rsrc', '.edata', '.idata',
'.pdata', '.debug', '.reloc', '.stab', '.stabstr', '.tls',
'.crt', '.gnu_deb', '.eh_fram', '.exptbl', '.rodata']
for i in range(200):
std_sections.append('/'+str(i))
std_section_names = 1
extracted_sparse['section_names'] = []
for section in pef.sections:
name = convert_to_ascii_null_term(section.Name).lower()
extracted_sparse['section_names'].append(name)
if name not in std_sections:
std_section_names = 0
extracted_dense['std_section_names'] = std_section_names
extracted_dense['debug_size'] = pef.OPTIONAL_HEADER.DATA_DIRECTORY[6].Size
extracted_dense['major_version'] = pef.OPTIONAL_HEADER.MajorImageVersion
extracted_dense['minor_version'] = pef.OPTIONAL_HEADER.MinorImageVersion
extracted_dense['iat_rva'] = pef.OPTIONAL_HEADER.DATA_DIRECTORY[1].VirtualAddress
extracted_dense['export_size'] = pef.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size
extracted_dense['check_sum'] = pef.OPTIONAL_HEADER.CheckSum
try:
extracted_dense['generated_check_sum'] = pef.generate_checksum()
except ValueError:
extracted_dense['generated_check_sum'] = 0
if len(pef.sections) > 0:
extracted_dense['virtual_address'] = pef.sections[0].VirtualAddress
extracted_dense['virtual_size'] = pef.sections[0].Misc_VirtualSize
extracted_dense['number_of_sections'] = pef.FILE_HEADER.NumberOfSections
extracted_dense['compile_date'] = pef.FILE_HEADER.TimeDateStamp
extracted_dense['number_of_rva_and_sizes'] = pef.OPTIONAL_HEADER.NumberOfRvaAndSizes
extracted_dense['total_size_pe'] = len(pef.__data__)
# Number of import and exports
if hasattr(pef, 'DIRECTORY_ENTRY_IMPORT'):
extracted_dense['number_of_imports'] = len(pef.DIRECTORY_ENTRY_IMPORT)
num_imported_symbols = 0
for module in pef.DIRECTORY_ENTRY_IMPORT:
num_imported_symbols += len(module.imports)
extracted_dense['number_of_import_symbols'] = num_imported_symbols
if hasattr(pef, 'DIRECTORY_ENTRY_BOUND_IMPORT'):
extracted_dense['number_of_bound_imports'] = len(pef.DIRECTORY_ENTRY_BOUND_IMPORT)
num_imported_symbols = 0
for module in pef.DIRECTORY_ENTRY_BOUND_IMPORT:
num_imported_symbols += len(module.entries)
extracted_dense['number_of_bound_import_symbols'] = num_imported_symbols
if hasattr(pef, 'DIRECTORY_ENTRY_EXPORT'):
try:
extracted_dense['number_of_export_symbols'] = len(pef.DIRECTORY_ENTRY_EXPORT.symbols)
symbol_set = set()
for symbol in pef.DIRECTORY_ENTRY_EXPORT.symbols:
symbol_info = 'unknown'
if not symbol.name:
symbol_info = 'ordinal=' + str(symbol.ordinal)
else:
symbol_info = 'name=' + symbol.name
symbol_set.add(convert_to_utf8('%s' % (symbol_info)).lower())
# Now convert set to list and add to features
extracted_sparse['ExportedSymbols'] = list(symbol_set)
except AttributeError:
extracted_sparse['ExportedSymbols'] = ['AttributeError']
# Specific Import info (Note this will be a sparse field woo hoo!)
if hasattr(pef, 'DIRECTORY_ENTRY_IMPORT'):
symbol_set = set()
for module in pef.DIRECTORY_ENTRY_IMPORT:
for symbol in module.imports:
symbol_info = 'unknown'
if symbol.import_by_ordinal is True:
symbol_info = 'ordinal=' + str(symbol.ordinal)
else:
symbol_info = 'name=' + symbol.name
# symbol_info['hint'] = symbol.hint
if symbol.bound:
symbol_info += ' bound=' + str(symbol.bound)
symbol_set.add(convert_to_utf8('%s:%s' % (module.dll, symbol_info)).lower())
# Now convert set to list and add to features
extracted_sparse['imported_symbols'] = list(symbol_set)
# Do we have a second section
if len(pef.sections) >= 2:
extracted_dense['virtual_size_2'] = pef.sections[1].Misc_VirtualSize
extracted_dense['size_image'] = pef.OPTIONAL_HEADER.SizeOfImage
extracted_dense['size_code'] = pef.OPTIONAL_HEADER.SizeOfCode
extracted_dense['size_initdata'] = pef.OPTIONAL_HEADER.SizeOfInitializedData
extracted_dense['size_uninit'] = pef.OPTIONAL_HEADER.SizeOfUninitializedData
extracted_dense['pe_majorlink'] = pef.OPTIONAL_HEADER.MajorLinkerVersion
extracted_dense['pe_minorlink'] = pef.OPTIONAL_HEADER.MinorLinkerVersion
extracted_dense['pe_driver'] = 1 if pef.is_driver() else 0
extracted_dense['pe_exe'] = 1 if pef.is_exe() else 0
extracted_dense['pe_dll'] = 1 if pef.is_dll() else 0
extracted_dense['pe_i386'] = 1
if pef.FILE_HEADER.Machine != 0x014c:
extracted_dense['pe_i386'] = 0
extracted_dense['pe_char'] = pef.FILE_HEADER.Characteristics
# Data directory features!!
datadirs = {
0: 'IMAGE_DIRECTORY_ENTRY_EXPORT', 1: 'IMAGE_DIRECTORY_ENTRY_IMPORT',
2: 'IMAGE_DIRECTORY_ENTRY_RESOURCE', 5: 'IMAGE_DIRECTORY_ENTRY_BASERELOC',
12: 'IMAGE_DIRECTORY_ENTRY_IAT'}
for idx, datadir in datadirs.items():
datadir = pefile.DIRECTORY_ENTRY[idx]
if len(pef.OPTIONAL_HEADER.DATA_DIRECTORY) <= idx:
continue
directory = pef.OPTIONAL_HEADER.DATA_DIRECTORY[idx]
extracted_dense['datadir_%s_size' % datadir] = directory.Size
# Section features
section_flags = ['IMAGE_SCN_MEM_EXECUTE', 'IMAGE_SCN_CNT_CODE', 'IMAGE_SCN_MEM_WRITE', 'IMAGE_SCN_MEM_READ']
rawexecsize = 0
vaexecsize = 0
for sec in pef.sections:
if not sec:
continue
for char in section_flags:
# does the section have one of our attribs?
if hasattr(sec, char):
rawexecsize += sec.SizeOfRawData
vaexecsize += sec.Misc_VirtualSize
break
# Take out any weird characters in section names
secname = convert_to_ascii_null_term(sec.Name).lower()
secname = secname.replace('.', '')
if secname in std_sections:
extracted_dense['sec_entropy_%s' % secname] = sec.get_entropy()
extracted_dense['sec_rawptr_%s' % secname] = sec.PointerToRawData
extracted_dense['sec_rawsize_%s' % secname] = sec.SizeOfRawData
extracted_dense['sec_vasize_%s' % secname] = sec.Misc_VirtualSize
extracted_dense['sec_va_execsize'] = vaexecsize
extracted_dense['sec_raw_execsize'] = rawexecsize
# Imphash (implemented in pefile 1.2.10-139 or later)
try:
extracted_sparse['imp_hash'] = pef.get_imphash()
except AttributeError:
extracted_sparse['imp_hash'] = 'Not found: Install pefile 1.2.10-139 or later'
# Register if there were any pe warnings
warnings = pef.get_warnings()
if warnings:
extracted_dense['pe_warnings'] = 1
extracted_sparse['pe_warning_strings'] = warnings
else:
extracted_dense['pe_warnings'] = 0
# Issue a warning if the feature isn't found
for feature in self._dense_feature_list:
if extracted_dense[feature] == feature_not_found_flag:
extracted_dense[feature] = feature_default_value
if (self._verbose):
print 'info: Feature: %s not found! Setting to %d' % (feature, feature_default_value)
# Issue a warning if the feature isn't found
for feature in self._sparse_feature_list:
if extracted_sparse[feature] == feature_not_found_flag:
extracted_sparse[feature] = [] # For sparse data probably best default
if (self._verbose):
print 'info: Feature: %s not found! Setting to %d' % (feature, feature_default_value)
# Set the features for the class var
self._dense_features = extracted_dense
self._sparse_features = extracted_sparse
return self.get_dense_features(), self.get_sparse_features()
# Helper functions
def convert_to_utf8(string):
''' Convert string to UTF8 '''
if (isinstance(string, unicode)):
return string.encode('utf-8')
try:
u = unicode(string, 'utf-8')
except TypeError:
return str(string)
utf8 = u.encode('utf-8')
return utf8
def convert_to_ascii_null_term(string):
''' Convert string to Null terminated ascii '''
string = string.split('\x00', 1)[0]
return string.decode('ascii', 'ignore')
# Unit test: Create the class, the proper input and run the execute() method for a test
def test():
''' pe_features.py: Test'''
# This worker test requires a local server running
import zerorpc
workbench = zerorpc.Client(timeout=300, heartbeat=60)
workbench.connect('tcp://127.0.0.1:4242')
# Generate 3 different inputs for the worker (better coverage)
import os
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'../data/pe/bad/033d91aae8ad29ed9fbb858179271232')
md5 = workbench.store_sample(open(data_path, 'rb').read(), 'bad_pe', 'exe')
data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'../data/pe/good/4be7ec02133544cde7a580875e130208')
md5_2 = workbench.store_sample(open(data_path, 'rb').read(), 'good_pe', 'exe')
input_data = workbench.get_sample(md5)
input_data.update(workbench.work_request('tags', md5))
input_data_2 = workbench.get_sample(md5_2)
input_data_2.update(workbench.work_request('tags', md5_2))
input_data_3 = {'sample': {'raw_bytes': 'invalid pe file to hit exception code'}}
# Execute the worker (unit test)
worker = PEFileWorker()
output = worker.execute(input_data)
print '\n<<< Unit Test >>>'
pprint.pprint(output)
# For code coverage
output = worker.execute(input_data_2)
output = worker.execute(input_data_3)
# Execute the worker (server test)
output = workbench.work_request('pe_features', md5)
print '\n<<< Server Test >>>'
pprint.pprint(output)
if __name__ == "__main__":
test()