/
exescan.py
336 lines (293 loc) · 9.04 KB
/
exescan.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
#!/usr/bin/env python
import sys,pefile,re,peutils,os
from hashlib import md5,sha1,sha256
'''
Author: Amit Malik
E-Mail: m.amit30@gmail.com
(C)2011
Version: 2.6
Last Update: 16-09-2012
'''
def help():
print
print "Usage: prog [option] file/Directory"
print "For eg: exescan.py -a malware.exe/malware"
print "-a","advanced scan with anomaly detection"
print "-b","display basic information"
print "-m","scan for commonly known malware APIs"
print "-i","display import/export table"
print "-p","display PE header"
print
def greet():
log("\t\t**********************************************************")
log("\t\t** Author: Amit Malik (m.amit30@gmail.com) **")
log("\t\t** http://www.SecurityXploded.com **")
log("\t\t** **")
log("\t\t**********************************************************")
def log(data):
global handle
print data
data = data
nextline = "\n"
handle.write(data)
handle.write(nextline)
return
class ExeScan():
def __init__(self,pe,file):
self.pe = pe
self.file = file
self.MD5 = None
self.SHA1 = None
self.SHA256 = None
self.data = None
def hashes(self):
f = open(self.file,"rb")
self.data = f.read()
self.MD5 = md5(self.data).hexdigest()
self.SHA1 = sha1(self.data).hexdigest()
self.SHA256 = sha256(self.data).hexdigest()
f.close()
return (self.MD5,self.SHA1,self.SHA256,self.data)
def header(self):
#header information check
file_header = self.pe.FILE_HEADER.dump()
log("\n")
for i in file_header:
log(i)
nt_header = self.pe.NT_HEADERS.dump()
log("\n")
for i in nt_header:
log(i)
optional_header = self.pe.OPTIONAL_HEADER.dump()
log("\n")
for i in optional_header:
log(i)
log("\n")
for i in self.pe.OPTIONAL_HEADER.DATA_DIRECTORY:
i = i.dump()
log("\n")
for t in i:
log(t)
log("\n")
for section in self.pe.sections:
log("Name: %s\n" % section.Name)
log('\tVirtual Size: 0x%.8x' % section.Misc_VirtualSize)
log('\tVirtual Address: 0x%.8x' % section.VirtualAddress)
log('\tSize of Raw Data: 0x%.8x' % section.SizeOfRawData)
log('\tPointer To Raw Data: 0x%.8x' % section.PointerToRawData)
log('\tPointer To Relocations: 0x%.8x' % section.PointerToRelocations)
log('\tPointer To Linenumbers: 0x%.8x' % section.PointerToLinenumbers)
log('\tNumber Of Relocations: 0x%.8x' % section.NumberOfRelocations)
log('\tNumber Of Linenumbers: 0x%.8x' % section.NumberOfLinenumbers)
log('\tCharacteristics: 0x%.8x\n' % section.Characteristics)
def anomalis(self):
log("\n[+] Anomalies Check\n")
# Entropy based check.. imported from peutils
pack = peutils.is_probably_packed(self.pe)
if pack == 1:
log("\t[*] Based on the sections entropy check! file is possibly packed")
# SizeOfRawData Check.. some times size of raw data value is used to crash some debugging tools.
nsec = self.pe.FILE_HEADER.NumberOfSections
for i in range(0,nsec-1):
if i == nsec-1:
break
else:
nextp = self.pe.sections[i].SizeOfRawData + self.pe.sections[i].PointerToRawData
currp = self.pe.sections[i+1].PointerToRawData
if nextp != currp:
log("\t[*] The Size Of Raw data is valued illegal! Binary might crash your disassembler/debugger")
break
else:
pass
# Non-Ascii or empty section name check
for sec in self.pe.sections:
if not re.match("^[.A-Za-z][a-zA-Z]+",sec.Name):
log("\t[*] Non-ascii or empty section names detected")
break;
# Size of optional header check
if self.pe.FILE_HEADER.SizeOfOptionalHeader != 224:
log("\t[*] Illegal size of optional Header")
# Zero checksum check
if self.pe.OPTIONAL_HEADER.CheckSum == 0:
log("\t[*] Header Checksum is zero!")
# Entry point check
enaddr = self.pe.OPTIONAL_HEADER.AddressOfEntryPoint
vbsecaddr = self.pe.sections[0].VirtualAddress
ensecaddr = self.pe.sections[0].Misc_VirtualSize
entaddr = vbsecaddr + ensecaddr
if enaddr > entaddr:
log("\t[*] Enrty point is outside the 1st(.code) section! Binary is possibly packed")
# Numeber of directories check
if self.pe.OPTIONAL_HEADER.NumberOfRvaAndSizes != 16:
log("\t[*] Optional Header NumberOfRvaAndSizes field is valued illegal")
# Loader flags check
if self.pe.OPTIONAL_HEADER.LoaderFlags != 0:
log("\t[*] Optional Header LoaderFlags field is valued illegal")
# TLS (Thread Local Storage) callback function check
if hasattr(self.pe,"DIRECTORY_ENTRY_TLS"):
log("\t[*] TLS callback functions array detected at 0x%x" % self.pe.DIRECTORY_ENTRY_TLS.struct.AddressOfCallBacks)
callback_rva = self.pe.DIRECTORY_ENTRY_TLS.struct.AddressOfCallBacks - self.pe.OPTIONAL_HEADER.ImageBase
log("\t[*] Callback Array RVA 0x%x" % callback_rva)
def base(self,check):
log("\n[+] Signature [Compiler/Packer]\n")
if check:
for i in check:
log('\t%s' % i)
else:
log("\t[*] No match found.\n")
log("\n[+] Address of entry point : 0x%.8x\n" % self.pe.OPTIONAL_HEADER.AddressOfEntryPoint)
log("[+] Image Base Address : 0x%.8x\n" % self.pe.OPTIONAL_HEADER.ImageBase)
log("[+] Sections")
for section in self.pe.sections:
log("\tName: %s\t" % section.Name.strip() + "Virtual Address: 0x%.8x\t" % section.VirtualAddress + "Size: 0x%.8x\t" % section.Misc_VirtualSize + "Entropy: %f" % section.get_entropy())
def importtab(self):
if hasattr(self.pe,"DIRECTORY_ENTRY_IMPORT"):
log("\n[+] Imports\n")
for entry in self.pe.DIRECTORY_ENTRY_IMPORT:
log('\n[-] %s\n' % entry.dll)
for imp in entry.imports:
log('\t0x%.8x\t%s' % (imp.address, imp.name))
def exporttab(self):
if hasattr(self.pe,"DIRECTORY_ENTRY_EXPORT"):
log("\n[+] Exports\n")
for entry in self.pe.DIRECTORY_ENTRY_EXPORT.symbols:
log('\t0x%.8x\t%s' % (entry.address, entry.name))
def malapi(self,MALAPI,str):
dict = {}
log("\n[+] Following expected Malware APIs are Detected\n")
log("\n\t[-] Import Table\n")
for entry in self.pe.DIRECTORY_ENTRY_IMPORT:
for imp in entry.imports:
dict[imp.name] = imp.address
for m in MALAPI:
m = m.strip()
if m in dict.keys():
log("\t\tIA: 0x%08x\t%s" % (dict[m],m))
log("\n\t[-] Entire Executable\n")
for m in MALAPI:
i = 0
m = m.strip()
try:
for s in str:
if re.search(m,s):
d = m
i = i + 1
if d == m:
log("\t\t %d times\t%s" % (i,d))
except:
pass
class StringAndThreat():
'''
extract strings from binary.
'''
def __init__(self,MD5,data):
self.MD5 = MD5
self.data = data
self.handle = None
def StringE(self):
name = "strings_"+self.MD5+".txt"
name = os.path.join(self.MD5,name)
if os.path.exists(name):
return
self.handle = open(name,'a')
headline = "\t\t\tStrings-%s\n\n" % self.MD5
self.handle.write(headline)
for m in re.finditer("([\x20-\x7e]{3,})", self.data):
self.handle.write(m.group(1))
self.handle.write("\n")
return
def main_s(pe,ch,f,name):
global handle
exescan = ExeScan(pe,name)
(MD5,SHA1,SHA256,data) = exescan.hashes()
stx = StringAndThreat(MD5,data)
# store reports in folders
if os.path.exists(MD5):
report_name = str(MD5)+".txt"
report_name = os.path.join(MD5,report_name)
else:
os.mkdir(MD5)
report_name = str(MD5)+".txt"
report_name = os.path.join(MD5,report_name)
handle = open(report_name,'a')
greet()
log("\n\n[+] File: %s" % name)
log("\n\t[*] MD5 : %s" % MD5)
log("\t[*] SHA-1 : %s" % SHA1)
log("\t[*] SHA-256 : %s" % SHA256)
#check file type (exe, dll)
if pe.is_exe():
log("\n[+] File Type: EXE")
elif pe.is_dll():
log("\n[+] File Type: DLL")
strings = f.readlines()
mf = open("API.txt","r")
MALAPI = mf.readlines()
signature = peutils.SignatureDatabase("userdb.txt")
check = signature.match_all(pe,ep_only = True)
if ch == "-i":
exescan.base(check)
exescan.importtab()
exescan.exporttab()
elif ch == "-b":
exescan.base(check)
elif ch == "-m":
exescan.base(check)
exescan.malapi(MALAPI,strings)
elif ch == "-p":
exescan.base(check)
exescan.header()
elif ch == "-a":
exescan.base(check)
exescan.anomalis()
exescan.malapi(MALAPI,strings)
stx.StringE()
else:
print
mf.close()
handle.close()
return MD5
def main():
if len(sys.argv) < 3:
help()
sys.exit(0)
ch = sys.argv[1]
fname = sys.argv[2]
if os.path.isdir(fname):
filelist = os.listdir(fname)
for name in filelist:
try:
name = os.path.join(fname,name)
pe = pefile.PE(name)
f = open(name,"rb")
new_name = main_s(pe,ch,f,name)
f.close()
pe.__data__.close()
try:
new_name = new_name + ".bin"
new_name = os.path.join(fname,new_name)
os.rename(name,new_name)
except:
pass
except:
pass
else:
try:
fname = os.path.realpath(fname)
print fname
pe = pefile.PE(fname)
f = open(fname,"rb")
new_name = main_s(pe,ch,f,fname)
f.close()
pe.__data__.close()
try:
new_name = new_name + ".bin"
os.rename(fname,new_name)
except:
pass
except Exception, WHY:
print "\nInvalid file\n"
print "Verbose: %s" % WHY
sys.exit(0)
if __name__ == '__main__':
main()