-
Notifications
You must be signed in to change notification settings - Fork 2
/
job-virustotal.py
103 lines (79 loc) · 3.05 KB
/
job-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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import logging
import time
import json
from pymongo import MongoClient
import gridfs
import requests
from utils import clean_data, Config
JOBNAME = 'VIRUSTOTAL'
SLEEPTIME = 1
# create logger
logger = logging.getLogger(JOBNAME)
logger.setLevel(logging.DEBUG)
# create console handler with a higher log level
logch = logging.StreamHandler()
logch.setLevel(logging.ERROR)
# create formatter and add it to the handlers
formatter = \
logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logch.setFormatter(formatter)
# add the handlers to the logger
logger.addHandler(logch)
client = MongoClient(host=Config().database.dbhost, port=Config().database.dbport)
db = client.vxcage
fs = gridfs.GridFS(db)
url = 'https://www.virustotal.com/vtapi/v2/file/report'
api_key = Config().virustotal.api_key
if Config().virustotal.proxy:
proxy = {'http': Config().virustotal.proxy,
'https': Config().virustotal.proxy}
else:
proxy = {}
while True:
try:
for (sampleno, sample) in \
enumerate(db.fs.files.find({'virustotal': {'$exists': False}},
timeout=False)):
try:
logger.info('[%s] Processing sample %s' % (sampleno,
sample['sha256']))
sample_key = {'sample_id': sample['_id']}
job_key = {'md5': sample['md5']}
parameters = {'resource': sample['sha256'],
'apikey': api_key}
logger.debug('[%s] Analysing' % sampleno)
r = requests.post(url, data=parameters, proxies=proxy)
VTjson = None
logger.debug('[%s] Response headers: %s' % (sampleno,
r.headers))
logger.debug('[%s] Response content: %s' % (sampleno,
r.content))
try:
VTjson = clean_data(r.json())
except Exception:
try:
VTjson = clean_data(json.loads(r.text))
except Exception:
logger.debug('[%s] Unknown response: %s'
% (sampleno, r.content))
if VTjson:
if VTjson['response_code'] == 1:
logger.debug('[%s] Storing results into MongoDB'
% sampleno)
db.fs.files.update(sample_key,
{'$set': {'virustotal': VTjson}},
upsert=True)
logger.info('[%s] Metadata updated' % sampleno)
else:
logger.warn('[%s] File Does Not Exist in VirusTotal'
% sampleno)
except Exception, e:
logger.exception(e)
pass
except Exception, e:
logger.exception(e)
logger.info('Sleeping %s minutes' % SLEEPTIME)
time.sleep(SLEEPTIME * 60)