-
Notifications
You must be signed in to change notification settings - Fork 301
/
pe_features.py
321 lines (262 loc) · 15 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
''' PE Features: This class pulls static features
out of a PE file using the python pefile module.
'''
import sys
import pefile
# Author: Brian Wylie
class PEFileFeatures():
''' Create instance of PEFileFeatures class. This class pulls static
features out of a PE file using the python pefile module.
'''
def __init__(self):
''' 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 = False
# 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
# 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}
# Now extract the various features using pefile
return self.extract_features_using_pefile(pefile_handle)
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
def open_using_pefile(self, input_name, input_bytes):
''' Open the PE File using the Python pefile module. '''
try:
pe = pefile.PE(data=input_bytes, fast_load=False)
except Exception, 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 (pe.PE_TYPE is None or pe.OPTIONAL_HEADER is None or len(pe.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 pe, None
# Extract various set of features using PEfile module
def extract_features_using_pefile(self, pe):
''' 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 pe.sections:
name = convertToAsciiNullTerm(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'] = pe.OPTIONAL_HEADER.DATA_DIRECTORY[6].Size
extracted_dense['major_version'] = pe.OPTIONAL_HEADER.MajorImageVersion
extracted_dense['minor_version'] = pe.OPTIONAL_HEADER.MinorImageVersion
extracted_dense['iat_rva'] = pe.OPTIONAL_HEADER.DATA_DIRECTORY[1].VirtualAddress
extracted_dense['export_size'] = pe.OPTIONAL_HEADER.DATA_DIRECTORY[0].Size
extracted_dense['check_sum'] = pe.OPTIONAL_HEADER.CheckSum
try:
extracted_dense['generated_check_sum'] = pe.generate_checksum()
except ValueError:
#self._logger.logMessage('warning', 'pe.generate_check_sum() threw an exception, setting to 0!')
extracted_dense['generated_check_sum'] = 0
if (len(pe.sections) > 0):
extracted_dense['virtual_address'] = pe.sections[0].VirtualAddress
extracted_dense['virtual_size'] = pe.sections[0].Misc_VirtualSize
extracted_dense['number_of_sections'] = pe.FILE_HEADER.NumberOfSections
extracted_dense['compile_date'] = pe.FILE_HEADER.TimeDateStamp
extracted_dense['number_of_rva_and_sizes'] = pe.OPTIONAL_HEADER.NumberOfRvaAndSizes
extracted_dense['total_size_pe'] = len(pe.__data__)
# Number of import and exports
if hasattr(pe,'DIRECTORY_ENTRY_IMPORT'):
extracted_dense['number_of_imports'] = len(pe.DIRECTORY_ENTRY_IMPORT)
num_imported_symbols = 0
for module in pe.DIRECTORY_ENTRY_IMPORT:
num_imported_symbols += len(module.imports)
extracted_dense['number_of_import_symbols'] = num_imported_symbols
if hasattr(pe, 'DIRECTORY_ENTRY_BOUND_IMPORT'):
extracted_dense['number_of_bound_imports'] = len(pe.DIRECTORY_ENTRY_BOUND_IMPORT)
num_imported_symbols = 0
for module in pe.DIRECTORY_ENTRY_BOUND_IMPORT:
num_imported_symbols += len(module.entries)
extracted_dense['number_of_bound_import_symbols'] = num_imported_symbols
if hasattr(pe,'DIRECTORY_ENTRY_EXPORT'):
extracted_dense['number_of_export_symbols'] = len(pe.DIRECTORY_ENTRY_EXPORT.symbols)
symbol_set = set()
for symbol in pe.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(convertToUTF8('%s'%(symbol_info)).lower())
# Now convert set to list and add to features
extracted_sparse['ExportedSymbols'] = list(symbol_set)
# Specific Import info (Note this will be a sparse field woo hoo!)
if hasattr(pe, 'DIRECTORY_ENTRY_IMPORT'):
symbol_set = set()
for module in pe.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(convertToUTF8('%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(pe.sections) >= 2):
extracted_dense['virtual_size_2'] = pe.sections[1].Misc_VirtualSize
extracted_dense['size_image'] = pe.OPTIONAL_HEADER.SizeOfImage
extracted_dense['size_code'] = pe.OPTIONAL_HEADER.SizeOfCode
extracted_dense['size_initdata'] = pe.OPTIONAL_HEADER.SizeOfInitializedData
extracted_dense['size_uninit'] = pe.OPTIONAL_HEADER.SizeOfUninitializedData
extracted_dense['pe_majorlink'] = pe.OPTIONAL_HEADER.MajorLinkerVersion
extracted_dense['pe_minorlink'] = pe.OPTIONAL_HEADER.MinorLinkerVersion
extracted_dense['pe_driver'] = 1 if pe.is_driver() else 0
extracted_dense['pe_exe'] = 1 if pe.is_exe() else 0
extracted_dense['pe_dll'] = 1 if pe.is_dll() else 0
extracted_dense['pe_i386'] = 1
if pe.FILE_HEADER.Machine != 0x014c:
extracted_dense['pe_i386'] = 0
extracted_dense['pe_char'] = pe.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(pe.OPTIONAL_HEADER.DATA_DIRECTORY) <= idx:
continue
directory = pe.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 pe.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 = convertToAsciiNullTerm(sec.Name).lower()
secname = secname.replace('.','')
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
# Register if there were any pe warnings
warnings = pe.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):
self.log('info: Feature: %s not found! Setting to %d' % (feature, feature_default_value))
self._warnings.append('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] = feature_default_value
if (self._verbose):
self.log('info: Feature: %s not found! Setting to %d' % (feature, feature_default_value))
self._warnings.append('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 convertToUTF8(s):
if (isinstance(s, unicode)):
return s.encode( "utf-8" )
try:
u = unicode( s, "utf-8" )
except:
return str(s)
utf8 = u.encode( "utf-8" )
return utf8
def convertToAsciiNullTerm(s):
s = s.split('\x00', 1)[0]
return s.decode('ascii', 'ignore')
# Unit test: Create the class, the proper input and run the execute() method for a test
def test():
''' pe_features.py: Unit test'''
my_extractor = PEFileFeatures()
with open('data/bad/033d91aae8ad29ed9fbb858179271232','rb') as f:
print my_extractor.execute(f.read())
if __name__ == "__main__":
test()