-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFileLookup.py
294 lines (264 loc) · 9.53 KB
/
FileLookup.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
#!/usr/bin/env python
# FileLookup.py was created by Glenn P. Edwards Jr.
# http://hiddenillusion.blogspot.com
# @hiddenillusion
# Date: 10-23-2012
# version = 0.3.1
# Requirements:
# - Internet Access :)
# - VirusTotal API key if you want prettier reports with more information from the JSON object
# Optional:
# - SimpleJson module to print the pretty reports (optional but is nicer)
# To-Do:
# - Bit9 File Advisor
# - OpenMalware (http://oc.gtisc.gatech.edu:8080/search.cgi?search=)
# - Threading
# - Pretty up the code
# - This really would be easier if I used BS4 in the beginning :/
import os
import sys
from datetime import datetime
import argparse
import binascii
import re
import hashlib
import socket
import urllib
import urllib2
from time import localtime, strftime
try :
import simplejson
sjson = True
except ImportError:
sjson = False
pass
# Configure some user-specific info
vt_key = ""
if not re.match('\d+', vt_key):
print "[!] You must configure your VirusTotal API key"
sys.exit()
def main():
# Get program args
parser = argparse.ArgumentParser(description='Searches various online resources to try and get as much info about a file as possible without submitting it, requiring third party modules or performing any analysis on the file.')
parser.add_argument('-f', '--file', help='Path to directory/file(s) to be scanned')
parser.add_argument('-H', '--hash', help='MD5 hash to be queried')
args = vars(parser.parse_args())
md5 = ''
def doWork(obj):
if args['hash']:
md5 = args['hash']
else:
md5 = md5sum(file)
results = []
results.append(("#" * 80) + "\nFile:\t %s\n" % obj + ("#" * 80))
results.append("MD5:\t\t\t%s" % md5)
if args['file']:
results.append("Sha256:\t\t\t%s" % sha256sum(file))
results.append("VirusTotal:\t\t%s" % virustotal(md5))
results.append("Cymru:\t\t\t%s" % cymru(md5))
results.append("ShadowServer A/V:\t%s" % ss_av(md5))
results.append("ShadowServer Known:\t%s" % ss_known(md5))
results.append("ThreatExpert Known:\t%s" % threatexpert(md5))
results.append("")
print '\n'.join(results)
# Verify we have something to do
if not args['file'] and not args['hash']:
print "[!] I need something to do"
sys.exit()
# Verify supplied path exists or die
if args['file']:
if not os.path.exists(args['file']):
print "[!] The supplied path does not exist"
sys.exit()
else:
# Set the path to file(s)
file = args['file']
if os.path.isdir(file):
# Recursivly walk the supplied path and process files accordingly
for root, dirs, files in os.walk(file):
for name in files:
f = os.path.join(root, name)
doWork(f)
elif os.path.isfile(file):
doWork(file)
# Verify the hash is legit
if args['hash']:
if not re.findall(r"([a-fA-F\d]{32})", args['hash']):
print "[!] The supplied path does not exist"
sys.exit()
else:
doWork(args['hash'])
def md5sum(file):
try:
f = open(file, "rb")
data = f.read()
md5 = hashlib.md5(data).hexdigest()
f.close()
except Exception, msg:
print msg
return md5
def sha256sum(file):
try:
f = open(file, "rb")
data = f.read()
sha256 = hashlib.sha256(data).hexdigest()
f.close()
except Exception, msg:
print msg
return sha256
def virustotal(hash):
"""
Return percent of A/V hits from VirusTotal scan report of the file if one exists.
"""
url = "https://www.virustotal.com/vtapi/v2/file/report"
parameters = {"resource": hash, "apikey": vt_key}
data = urllib.urlencode(parameters)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
out = []
out.append('')
try:
if not sjson == False:
rpt = simplejson.loads(result)
date = rpt["scan_date"].split(' ')[0]
new_date = datetime.strptime(date, "%Y-%m-%d").strftime("%b %d %Y")
out.append("\tScan Date:\t %s" % new_date)
out.append("\tTotal Engines:\t %s" % rpt["total"])
out.append("\tDetected:\t %s" % rpt["positives"])
out.append('')
out.append("\tA/V Results:")
out.append("\t\t\tClamAV:\t\t %s" % rpt["scans"]["Microsoft"]["result"])
out.append("\t\t\tKaspersky:\t %s" % rpt["scans"]["Kaspersky"]["result"])
out.append("\t\t\tMcAfee:\t\t %s" % rpt["scans"]["McAfee"]["result"])
out.append("\t\t\tMicrosoft:\t %s" % rpt["scans"]["Microsoft"]["result"])
out.append("\t\t\tSophos:\t\t %s" % rpt["scans"]["Sophos"]["result"])
out.append("\t\t\tSymantec:\t %s" % rpt["scans"]["Symantec"]["result"])
out.append("\tLink: %s" % rpt["permalink"])
else:
# Still return VT results, just not as pretty without SimpleJson
col = result.split(',')
for line in col:
l = line.replace('\"', '')
if "scan_date:" in l:
date = l.replace('\"', '').replace(' scan_date: ', '').split(' ')[0]
new_date = datetime.strptime(date, "%Y-%m-%d").strftime("%b %d %Y")
out.append("\tScan Date:\t%s" % new_date)
elif "positives:" in l:
out.append("\tDetected:\t%s" % l.replace('\"', '').replace(' positives: ', ''))
elif "total:" in l:
out.append("\tTotal Engines:\t%s" % l.replace('\"', '').replace(' total: ', ''))
result = '\n'.join(out)
if result == None:
result = "No Match"
return result
except Exception:
result = "No Match"
return result
def ss_known(hash):
"""
Based off original by: Jose Nazario ([email protected])
site : http://bin-test.shadowserver.org
"""
url = "http://bin-test.shadowserver.org/api"
data = {}
data['md5'] = hash
url_vals = urllib.urlencode(data)
req = urllib2.Request(url, data)
full_url = url + '?' + url_vals
response = urllib2.urlopen(full_url)
result = response.read()
count = 0
for line in result.split('\n'):
count += 1
if count < 2 :
result = "No Match"
else:
l = line.split(' ', 1)
if len(l) == 2:
try: res[l[0]] = simplejson.loads(l[1])
except: pass
return result
def ss_av(hash):
"""
Based off original by: Jose Nazario ([email protected])
site : http://innocuous.shadowserver.org/api/?query=#md5-or-sha1#
"""
url = "http://innocuous.shadowserver.org/api/"
data = {}
data['query'] = hash
url_vals = urllib.urlencode(data)
req = urllib2.Request(url, data)
full_url = url + '?' + url_vals
response = urllib2.urlopen(full_url)
result = response.read()
if "No match" in result:
result = "No Match"
elif "Whitlisted" in result:
result = "Whitelisted"
else:
lines = result.split('\n')
out = []
col = lines[0].split(',')
av = lines[1].split(',')
out.append('')
fdate = col[2].replace('\"', '').split(' ')[0]
fnew_date = datetime.strptime(fdate, "%Y-%m-%d").strftime("%b %d %Y")
out.append("\tFirst Seen:\t%s" % fnew_date)
ldate = col[2].replace('\"', '').split(' ')[0]
lnew_date = datetime.strptime(ldate, "%Y-%m-%d").strftime("%b %d %Y")
out.append("\tLast Seen:\t%s" % lnew_date)
out.append('')
out.append("\tA/V Results:")
if len(av) > 1:
for i in av:
out.append("\t\t\t%s" % i.replace('\"','').replace('{', '').replace('}', ''))
else:
out.append("\t\t\tN/A")
result = '\n'.join(out)
return result
def cymru(hash):
"""
Return Team Cymru Malware Hash Database results.
source: http://code.google.com/p/malwarecookbook/
site : http://www.team-cymru.org/Services/MHR/
"""
request = '%s\r\n' % hash
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect(('hash.cymru.com', 43))
s.send('begin\r\n')
s.recv(1024)
s.send(request)
response = s.recv(1024)
s.send('end\r\n')
s.close()
if len(response) > 0:
resp_re = re.compile('\S+ (\d+) (\S+)')
match = resp_re.match(response)
result = "\n\tLast Seen:\t%s\n\tDetected:\t%s" % (strftime("%b %d %Y", localtime(int(match.group(1)))), match.group(2))
except socket.error:
result = "Error"
return result
def threatexpert(hash):
"""
Return existence of report in ThreatExpert database.
site : http://www.threatexpert.com
credit : Added 11/29/2012 by Keith Gilbert - @digital4rensics
note : Greatly increases time required
"""
result = []
url = 'http://threatexpert.com/report.aspx?md5=' + hash
try:
page = urllib2.urlopen(url).read()
for line in page.split('\n'):
if line.find('Submission Summary:'):
result.append("Report Found")
result.append("\tLink: %s" % url)
return '\n'.join(result)
else:
return "No Match"
except Exception:
return "Error"
if __name__ == "__main__":
main()