-
Notifications
You must be signed in to change notification settings - Fork 0
/
pe_class.py
483 lines (387 loc) · 13.1 KB
/
pe_class.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
__author__ = 'dmt101'
import sys
import distorm3
import os
import string
import re
#import magic
try:
import pefile
import peutils
except ImportError:
print 'pefile not installed, see http://code.google.com/p/pefile/'
sys.exit()
def is_pe(filename):
try:
global pe
pe = pefile.PE(filename)
return True
except:
return False
#Obtain the time that the sample was compiled
def getTimeInfo(pe):
if hasattr(pe,"FILE_HEADER"):
return int(pe.FILE_HEADER.TimeDateStamp)
else:
return None
#Obtain number of sections with file header
def getNumSections(pe):
if hasattr(pe,"FILE_HEADER"):
return int(pe.FILE_HEADER.NumberOfSections)
else:
return None
#Determine the section that a given RVA is found within
def findSection(pe,address):
sec=0
for s in pe.sections:
if hex(address) >= hex(s.VirtualAddress):
if hex(address) <= hex(s.VirtualAddress+s.Misc):
break
sec+=1
return sec
#Determine what type of system the sample can be run on
#or needs to be able to emulate
def getMachineType(pe):
if hasattr(pe,'DOS_HEADER'):
if pe.FILE_HEADER.Machine == 332:
return "x86"
elif pe.FILE_HEADER.Machine == 512:
return "Intel Itanium"
elif pe.FILE_HEADER.Machine == 34404:
return "x64"
else:
return None
#Use peutil signature database to guess compiler/packer type
def compiler_or_packer_type(pe):
if hasattr(pe,'DOS_HEADER'):
signature= peutils.SignatureDatabase("/Users/temp/OneDrive/bin/UserDB.TXT")
check=signature.match_all(pe,ep_only=True)
#print "Compiler/Packer:",check
return check
else:
return None
#Determine the file type of the given sample
#Figure out why python Magic does not work!!!!
def get_filetype_data(data):
if sys.modules.has_key('magic'):
try:
return magic.from_buffer(data)
except magic.MagicException:
magic_custom = magic.Magic(magic_file='/Users/temp/OneDrive/bin/share/miscmagic')
return magic_custom.from_buffer(data)
return None
#Use magic module to determine the file type
def get_filetype_file(f):
if sys.modules.has_key('magic'):
try:
return magic.from_file(file)
except magic.MagicException:
magic_custom = magic.Magic(magic_file='/Users/temp/OneDrive/bin/share/miscmagic')
return magic_custom.from_file(f)
return None
#Find resources found within file
def check_rsrc(pe):
ret = {}
if hasattr(pe, 'DIRECTORY_ENTRY_RESOURCE'):
i = 0
for resource_type in pe.DIRECTORY_ENTRY_RESOURCE.entries:
if resource_type.name is not None:
name = "%s" % resource_type.name
else:
name = "%s" % pefile.RESOURCE_TYPE.get(resource_type.struct.Id)
if name == None:
name = "%d" % resource_type.struct.Id
if hasattr(resource_type, 'directory'):
for resource_id in resource_type.directory.entries:
if hasattr(resource_id, 'directory'):
for resource_lang in resource_id.directory.entries:
data = pe.get_data(resource_lang.data.struct.OffsetToData, resource_lang.data.struct.Size)
filetype = get_filetype_data(data)
lang = pefile.LANG.get(resource_lang.data.lang, '*unknown*')
sublang = pefile.get_sublang_name_for_lang( resource_lang.data.lang, resource_lang.data.sublang )
ret[i] = (name, resource_lang.data.struct.OffsetToData, resource_lang.data.struct.Size, filetype, lang, sublang)
#ret[i] = (name, resource_lang.data.struct.OffsetToData, resource_lang.data.struct.Size, lang, sublang)
i += 1
if not ret:
return None
else:
return ret
else:
return None
def get_num_rsrc(pe):
if hasattr(pe,'DIRECTORY_ENTRY_RESOURCE'):
return len(pe.DIRECTORY_ENTRY_RESOURCE.entries)
else:
return None
#TODO: Find better measure of code complexity to identify programmer traits
"""
def getCodeComplexity(pe,path):
if len(getImportedFunctions(pe))>0:
print int(getSizeOfCode(pe)),len(getImportedFunctions(pe))
return int(getSizeOfCode(pe))/len(getImportedFunctions(pe))
else:
return 0
"""
#TODO: Develop way of determining the average function size with a sample
#def avgFuncSize()
#TODO: Find metric for Graph Complexity
#def graphComplexity()
#Get size of Sample
def fileSize(path):
return os.stat(path).st_size
#Get name of sample
def fileName(path):
return os.path.basename(path)
#Find all imported Dlls for sample
def getImportedDLLs(pe):
if hasattr(pe,'DIRECTORY_ENTRY_IMPORT'):
dict=[]
for entry in pe.DIRECTORY_ENTRY_IMPORT:
#print entry.dll
dict.append(entry.dll)
return dict
else:
return None
#Find all imported functions for sample
def getImportedFunctions(pe):
if hasattr(pe,'DIRECTORY_ENTRY_IMPORT'):
list=[]
for entry in pe.DIRECTORY_ENTRY_IMPORT:
for imp in entry.imports:
if imp.name:
list.append(imp.name)
else:
list.append(imp.ordinal)
return list
else:
return None
#Check to see if the sample has a low number of imports
#as well as if the sample has LoadLibrary & GetProcAddress
def checkIfPacked(pe):
if hasattr(pe,'DIRECTORY_ENTRY_IMPORT'):
list={}
for entry in pe.DIRECTORY_ENTRY_IMPORT:
for imp in entry.imports:
list.setdefault(imp.name)
try:
if len(list) <= 10:
if list.keys().index('GetProcAddress'):
if list.keys().index('LoadLibraryA') or list.keys().index('LoadLibrary'):
return True
except:
return False
return None
def checkIfPacked2(pe):
if peutils.is_probably_packed(pe):
return 1
else:
return 0
#Find all exported functions in sample
def getExportedFunctions(pe):
if hasattr(pe,'DIRECTORY_ENTRY_EXPORT'):
list=[]
for exports in pe.DIRECTORY_ENTRY_EXPORT.symbols:
list.append(exports.name)
if not list:
return None
else:
return list
else:
return None
#Produces all FileVersion information associated with the file
def getFileInfo(pe):
list=[]
if hasattr(pe,'FileInfo'):
for entry in pe.FileInfo:
if hasattr(entry, 'StringTable'):
for st_entry in entry.StringTable:
for str_entry in st_entry.entries.items():
list.append(str_entry)
return list
if not list:
return None
#Displays all strings from a file
def strings(filename, min=4):
with open(filename, "rb") as f:
result = ""
for c in f.read():
if c in string.printable:
result += c
continue
if len(result) >= min:
yield result
result = ""
#All section under PE Optional Header
#? What happens to linker if size of optional header is zero?
def getLinkerVersion(pe):
if hasattr(pe,'OPTIONAL_HEADER'):
return str(pe.OPTIONAL_HEADER.MajorLinkerVersion)+"."+str(pe.OPTIONAL_HEADER.MinorLinkerVersion)
else:
return None
def getSectionAlignment(pe):
if hasattr(pe,'OPTIONAL_HEADER'):
return pe.OPTIONAL_HEADER.SectionAlignment
else:
return None
def getSizeOfCode(pe):
if hasattr(pe,'OPTIONAL_HEADER'):
return pe.OPTIONAL_HEADER.SizeOfCode
else:
return None
def checkSectionSizeZero(pe):
if hasattr(pe,'DOS_HEADER'):
list=[]
for section in pe.sections:
#print section.Name,section.SizeOfRawData
if section.SizeOfRawData == 0 and section.Misc != 0:
list.append(re.sub('\W', '',str(section.Name)))
if list:
return 1
else:
return 0
else:
None
def checkRawVsVirtualSection(pe):
if hasattr(pe,'DOS_HEADER'):
list=[]
for section in pe.sections:
#print section.Name
#print "Virt",section.Misc,"Raw",section.SizeOfRawData,"align",getSectionAlignment(pe)
if section.Misc-section.SizeOfRawData > getSectionAlignment(pe):
list.append(re.sub('\W', '',str(section.Name)))
if list:
return 1
else:
return 0
else:
None
def checkEntropy(pe):
if hasattr(pe,'DOS_HEADER'):
list=[]
for section in pe.sections:
#print section.Name,section.get_entropy()
#print section
if (section.get_entropy() > 0 and section.get_entropy() < 1) or section.get_entropy() > 7:
#print section.Name
list.append(re.sub('\W', '',str(section.Name)))
if list:
return 1
else:
return 0
else:
None
#Get Entry Point Address
def getEntryPointAddress(pe):
if hasattr(pe,'OPTIONAL_HEADER'):
return pe.OPTIONAL_HEADER.AddressOfEntryPoint
else:
return None
def getMagicNumber(pe):
if hasattr(pe,'OPTIONAL_HEADER'):
return hex(pe.OPTIONAL_HEADER.Magic)
else:
return None
def numStrangeSectionName(pe):
if hasattr(pe,'DOS_HEADER'):
list=[]
section_list=["arch","bss","data","debug","debug$S","debug$T","edata","idata",
"pdata","rdata","reloc","rsrc",'text',"tls",".xdata","drectve","code"]
c=0
for section in pe.sections:
string=re.sub('\W', '',str(section.Name))
if string.lower() not in section_list:
#print section.Name
list.append(re.sub('\W', '',str(section.Name)))
c+=1
if list:
return c
else:
return c
else:
return None
#Get opcode for the given malware sample
def getOpcodes(pe,file_path):
if hasattr(pe,'DOS_HEADER'):
l=[]
sec=findSection(pe,getEntryPointAddress(pe))
#print len(pe.sections)
if sec < len(pe.sections):
#print sec
#print "Inside",pe.sections[sec].Name
#Grab instructions within section that contains the entry point
instructions=distorm3.Decode(pe.OPTIONAL_HEADER.ImageBase, open(file_path,'rb').read(),distorm3.Decode32Bits)
for i in instructions:
#print i[0],i[1],i[2],i[3]
#print "0x%08x (%02x) %-20s %s" % (i[0], i[1], i[3], i[2])
#Make sure instruction is within after entry point but before the end of the section
if i[0] >= (getEntryPointAddress(pe)+pe.OPTIONAL_HEADER.ImageBase):
if i[0] < (getEntryPointAddress(pe)+pe.sections[sec].SizeOfRawData+pe.OPTIONAL_HEADER.ImageBase):
#Grab opcode for the instruction being used
l.append("".join(list(i[3])[:2]))
return l
else:
return None
else:
return None
#Get the list of the opcode sets for a given malware sample
def getOpcodeSets(opCodeList,setSize):
s1=0
s2=setSize
opcodeSet=[]
#l= getOpcodes(pe,file_path)
if opCodeList:
while s2 <= len(opCodeList):
opcodeSet.append(opCodeList[s1:s2])
s1 +=1
s2 +=1
return opcodeSet
else:
return None
def getByteSets(path,pe,setSize):
#Determine the section the RVA is found in
sec=findSection(pe,pe.OPTIONAL_HEADER.BaseOfCode)
#Check if valid section is found
if sec < len(pe.sections):
#Formula for file_offset = RVA - VirtualAddress +RawDataPointer
file_offset = pe.OPTIONAL_HEADER.BaseOfCode-pe.sections[sec].VirtualAddress+pe.sections[sec].PointerToRawData
s1=file_offset
s2=file_offset+setSize
byte_ngram=[]
with open (path,'rb') as f:
file = f.read()
#Find byte n-grams until the end of the section is reached
while s2 <= int(file_offset+pe.sections[sec].SizeOfRawData):
byte_ngram.append(file[s1:s2])
s1+=1
s2+=1
return byte_ngram
else:
return None
def printable_strings(path):
#process = subprocess.Popen(["strings",path], stdout=subprocess.PIPE, shell=True)
#(out,err)=process.communicate()
strings_output=[]
for s in strings(path,4):
strings_output.append(s)
"""
if s in d:
d[s]+=1
else:
d[s]=1
for s in out:
print ""
if s in d:
d[s]+=1
else:
d[s]=1
"""
"""
p_strings = set(string.printable)
d =dict()
with open(path,'rb') as f:
for data in f.read():
if data in string.printable:
if len(data) > 3:
print data
"""
return strings_output