-
Notifications
You must be signed in to change notification settings - Fork 61
/
virustotal.py
389 lines (301 loc) Β· 11.3 KB
/
virustotal.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
#!/usr/bin/env python
__author__ = "Gawen Arab"
__copyright__ = "Copyright 2012, Gawen Arab"
__credits__ = ["Gawen Arab"]
__license__ = "MIT"
__version__ = "1.0.3"
__maintainer__ = "Gawen Arab"
__email__ = "[email protected]"
__status__ = "Production"
# Snippet from http://code.activestate.com/recipes/146306/
import httplib, mimetypes
import urlparse
import urllib
import urllib2
import hashlib
import json
import time
import re
import logging
import threading
logger = logging.getLogger("virustotal")
FILE_SIZE_LIMIT = 30 * 1024 * 1024 # 30MB
class postfile:
@staticmethod
def post_multipart(host, selector, fields, files):
"""
Post fields and files to an http host as multipart/form-data.
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return the server's response page.
"""
content_type, body = postfile.encode_multipart_formdata(fields, files)
h = httplib.HTTPS(host)
h.putrequest('POST', selector)
h.putheader('content-type', content_type)
h.putheader('content-length', str(len(body)))
h.endheaders()
h.send(body)
errcode, errmsg, headers = h.getreply()
return h.file.read()
@staticmethod
def encode_multipart_formdata(fields, files):
"""
fields is a sequence of (name, value) elements for regular form fields.
files is a sequence of (name, filename, value) elements for data to be uploaded as files
Return (content_type, body) ready for httplib.HTTP instance
"""
BOUNDARY = '----------ThIs_Is_tHe_bouNdaRY_$'
CRLF = '\r\n'
L = []
for (key, value) in fields:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"' % key)
L.append('')
L.append(value)
for (key, filename, value) in files:
L.append('--' + BOUNDARY)
L.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (key, filename))
L.append('Content-Type: %s' % postfile.get_content_type(filename))
L.append('')
L.append(value)
L.append('--' + BOUNDARY + '--')
L.append('')
body = CRLF.join((bytes(i) for i in L))
content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
return content_type, body
@staticmethod
def get_content_type(filename):
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
class VirusTotal(object):
_SCAN_ID_RE = re.compile(r"^[a-fA-F0-9]{64}-[0-9]{10}$")
class EntityTooLarge(Exception):
pass
class ApiError(Exception):
pass
def __init__(self, api_key, limit_per_min = None):
limit_per_min = limit_per_min if limit_per_min is not None else 4
super(VirusTotal, self).__init__()
self.api_key = api_key
self.limit_per_min = limit_per_min
self.limits = []
self.limit_lock = threading.Lock()
def __repr__(self):
return "<VirusTotal proxy>"
def _limit_call_handler(self):
with self.limit_lock:
if self.limit_per_min <= 0:
return
now = time.time()
self.limits = [l for l in self.limits if l > now]
self.limits.append(now + 60)
if len(self.limits) >= self.limit_per_min:
wait = self.limits[0] - now
logger.info("Wait for %.2fs because of quotat limit." % (self.limits[0] - now, ))
time.sleep(self.limits[0] - now)
@classmethod
def _fileobj_to_fcontent(cls, anything, filename = None):
# anything can be:
# - A MD5, SHA1, SHA256
# - A scan id
# - A filepath or URL
# - A file object
if isinstance(anything, basestring):
# Is MD5, SHA1, SHA256?
if all(i in "1234567890abcdef" for i in anything.lower()) and len(anything) in [32, 40, 64]:
return ["resource", anything, filename]
if cls._SCAN_ID_RE.match(anything):
return ["resource", anything, filename]
# Is URL ?
if urlparse.urlparse(anything).scheme:
fh = urllib2.urlopen(anything)
else:
# it's file
fh = file(anything, "rb")
with fh as f:
return cls._fileobj_to_fcontent(f)
assert hasattr(anything, "read")
content = anything.read()
if hasattr(anything, "name") and isinstance(anything.name, basestring):
filename = anything.name
return ["file", filename, content]
def get(self, anything, filename = None):
logger.info("Get report of %r" % (anything, ))
o = self._fileobj_to_fcontent(anything, filename)
if o[0] == "file":
o = (
"resource",
hashlib.sha256(o[2]).hexdigest(),
o[2],
)
data = urllib.urlencode({
"apikey": self.api_key,
"resource": o[1],
})
self._limit_call_handler()
req = urllib2.urlopen(urllib2.Request(
"http://www.virustotal.com/vtapi/v2/file/report",
data,
)).read()
report = Report(req, self)
return report
def scan(self, anything, filename = None, reanalyze = None):
reanalyze = reanalyze if reanalyze is not None else False
if not reanalyze:
# Check if already exists
report = self.get(anything, filename)
if report is not None:
return report
logger.info("Analyze %r" % (anything, ))
o = self._fileobj_to_fcontent(anything, filename)
assert o[0] == "file"
if o[1] is None:
o[1] = "file"
if len(o[2]) > FILE_SIZE_LIMIT:
raise self.EntityTooLarge()
self._limit_call_handler()
ret_json = postfile.post_multipart(
host = "www.virustotal.com",
selector = "https://www.virustotal.com/vtapi/v2/file/scan",
fields = {
"apikey": self.api_key,
}.items(),
files = [
o,
],
)
report = Report(ret_json, self)
if report:
report.update()
return report
class Report(object):
def __new__(cls, r, parent):
if isinstance(r, basestring):
try:
r = json.loads(r)
except ValueError:
raise VirusTotal.ApiError()
assert isinstance(r, dict)
if r["response_code"] == 0:
return None
self = super(Report, cls).__new__(cls)
self.parent = parent
self.update(r)
return self
def update(self, data = None):
data = data if data is not None else self.parent.get(self.scan_id)._report
self._report = data
def __getattr__(self, attr):
# Aliases
item = {
"id": "resource",
"status": "verbose_msg",
}.get(attr, attr)
try:
return self._report[item]
except KeyError:
raise AttributeError(attr)
def __repr__(self):
return "<VirusTotal report %s (%s)>" % (
self.id,
self.status,
)
@property
def state(self):
return {
-2: "analyzing",
1: "ok",
0: "ko",
}.get(self.response_code, "unknown")
@property
def done(self):
return self.state == "ok"
def __iter__(self):
for antivirus, report in self.scans.iteritems():
yield (
(antivirus, report["version"], report["update"]),
report["result"],
)
def join(self, timeout = None, interval = None):
interval = interval if interval is not None else 60
if timeout is not None:
timeout = time.time() + timeout
self.update()
while self.state != "ok" and (timeout is None or time.time() < timeout):
time.sleep(interval)
self.update()
def main():
import sys
import optparse
import threading
import Queue
import glob
parser = optparse.OptionParser(usage = """%prog [-k API_KEY] (scan|get) RESOURCE ...
'scan' asks virustotal to scan the file, even if a report
is available. The resource must be a file.
'get' asks virustotal if a report is available for the given
resource.
A resource can be:
- a hash (md5, sha1, sha256)
- a scan ID
- a filepath or URL""")
parser.add_option("-k", "--key", dest = "api_key", default = None, help = "Set VirusTotal API key.")
parser.add_option("-l", "--limit", dest = "limit_per_min", default = "4", help = "Set limit per minute API call. VirusTotal specifies no more 4 API calls must be done per minute. You can change this value, but VirusTotal maybe ignores some calls and may make this script bug.")
parser.add_option("-v", "--verbose", dest = "verbose", action = "store_true", default = False, help = "Verbose.")
(options, arguments) = parser.parse_args()
logging.getLogger().setLevel(logging.DEBUG if options.verbose else logging.WARNING)
logging.basicConfig()
# This is my API key. Please use it only for examples, not for any production stuff
# You can get an API key signing-up on VirusTotal. It takes 2min.
API_KEY = "XXX"
api_key = options.api_key or API_KEY
if len(sys.argv) < 3:
parser.print_usage()
return -1
action = arguments.pop(0)
if action.lower() not in ("scan", "get", ):
print "ERROR: unknown action"
return -1
resources = []
for argument in arguments:
for resource in glob.glob(argument):
resources.append(resource)
v = VirusTotal(API_KEY, limit_per_min = int(options.limit_per_min))
q = Queue.Queue()
def analyze(resource):
try:
if action.lower() == "scan":
report = v.scan(resource, reanalyze = True)
print "%s: Scan started: %s" % (resource, report, )
report.join()
q.put((resource, report))
print "%s: Scan finished: %s" % (resource, report, )
elif action.lower() == "get":
report = v.get(resource)
q.put((resource, report))
except VirusTotal.ApiError:
print "VirusTotal returned a non correct response. It may be because the script does too many requests at the minute. See the parameter -l"
threads = []
for resource in resources:
thread = threading.Thread(target = analyze, args = (resource, ))
threads.append(thread)
thread.daemon = True
thread.start()
for thread in threads:
while thread.is_alive():
try:
thread.join(0.1)
except KeyboardInterrupt:
return
while not q.empty():
resource, report = q.get()
print "=== %s ===" % (resource, )
if report is None:
print "No report is available."
return 0
print "Report:"
for antivirus, virus in report:
print "- %s (%s, %s):\t%s" % (antivirus[0], antivirus[1], antivirus[2], virus, )
print
if __name__ == "__main__":
main()