This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
virustotal.py
220 lines (176 loc) · 8.31 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
#!/usr/bin/env python3
""" VirusTotal File Scan
Required libraries:
- requests (can be installed manually or through pip)
"""
__author__ = "Xiaokui Shu"
__copyright__ = "Copyright 2013, Xiaokui Shu"
__license__ = "Apache"
__version__ = "2.0.0"
__maintainer__ = "Xiaokui Shu"
__email__ = "[email protected]"
__status__ = "Prototype"
import sys
import os
import hashlib
import argparse
import logging
import requests
import json
import time
def list_all_files(path):
"""
List all file paths
@param path: if it is a path, just return, if dir, return paths of files in it
Subdirectories not listed
No recursive search
"""
assert os.path.isfile(path) or os.path.isdir(path)
if os.path.isfile(path):
return [path]
else:
return filter(os.path.isfile, map(lambda x: '/'.join([os.path.abspath(path), x]), os.listdir(path)))
def sha256sum(filename):
"""
Efficient sha256 checksum realization
Take in 8192 bytes each time
The block size of sha256 is 512 bytes
"""
with open(filename, 'rb') as f:
m = hashlib.sha256()
while True:
data = f.read(8192)
if not data:
break
m.update(data)
return m.hexdigest()
class VirusTotal(object):
def __init__(self, apikey="", public=True, verbose=False, **requests_kwargs):
self.apikey = apikey
self.URL_BASE = "https://www.virustotal.com/vtapi/v2/"
self.HTTP_OK = 200
self.HTTP_NOCONTENT = 204
self.HTTP_FORBIDDEN = 403
# whether the API_KEY is a public API. limited to 4 per min if so.
self.is_public_api = public
# whether a retrieval request is sent recently
self.has_sent_retrieve_req = False
# if needed (public API), sleep this amount of time between requests
self.PUBLIC_API_SLEEP_TIME = 20
# user-supplied dict of kwargs supplied to requests calls
self.requests_kwargs = requests_kwargs
self.logger = logging.getLogger("virt-log")
self.logger.setLevel(logging.INFO)
self.scrlog = logging.StreamHandler()
self.scrlog.setFormatter(logging.Formatter("[%(levelname)s] %(message)s"))
self.logger.addHandler(self.scrlog)
self.is_verboselog = verbose
def send_files(self, filenames):
"""
Send files to scan
@param filenames: list of target files
"""
url = self.URL_BASE + "file/scan"
attr = {"apikey": self.apikey}
for filename in filenames:
files = {"file": open(filename, 'rb')}
res = requests.post(url, data=attr, files=files, **self.requests_kwargs)
if res.status_code == self.HTTP_OK:
resmap = json.loads(res.text)
if not self.is_verboselog:
self.logger.info("sent: %s, HTTP: %d, response_code: %d, scan_id: %s",
os.path.basename(filename), res.status_code, resmap["response_code"], resmap["scan_id"])
else:
self.logger.info("sent: %s, HTTP: %d, content: %s", os.path.basename(filename), res.status_code, res.text)
else:
self.logger.warning("sent: %s, HTTP: %d", os.path.basename(filename), res.status_code)
break
return res.status_code
def send_file(self,filename):
"""
Send single file to scan
:param filename:
:return json response or None if error:
"""
url = self.URL_BASE + "file/scan"
attr = {"apikey": self.apikey}
files = {"file": open(filename, 'rb')}
res = requests.post(url, data=attr, files=files, **self.requests_kwargs)
if res.status_code == self.HTTP_OK:
return json.loads(res.text)
return None
def retrieve_files_reports(self, filenames):
"""
Retrieve Report for file
@param filename: target file
"""
for filename in filenames:
res = self.retrieve_report(sha256sum(filename))
if res.status_code == self.HTTP_OK:
resmap = json.loads(res.text)
if not self.is_verboselog:
self.logger.info("retrieve report: %s, HTTP: %d, response_code: %d, scan_date: %s, positives/total: %d/%d",
os.path.basename(filename), res.status_code, resmap["response_code"], resmap["scan_date"], resmap["positives"], resmap["total"])
else:
self.logger.info("retrieve report: %s, HTTP: %d, content: %s", os.path.basename(filename), res.status_code, res.text)
else:
self.logger.warning("retrieve report: %s, HTTP: %d", os.path.basename(filename), res.status_code)
def retrieve_from_meta(self, filename):
"""
Retrieve Report for checksums in the metafile
@param filename: metafile, each line is a checksum, best use sha256
"""
with open(filename) as f:
for line in f:
checksum = line.strip()
res = self.retrieve_report(checksum)
if res.status_code == self.HTTP_OK:
resmap = json.loads(res.text)
if not self.is_verboselog:
self.logger.info("retrieve report: %s, HTTP: %d, response_code: %d, scan_date: %s, positives/total: %d/%d",
checksum, res.status_code, resmap["response_code"], resmap["scan_date"], resmap["positives"], resmap["total"])
else:
self.logger.info("retrieve report: %s, HTTP: %d, content: %s", os.path.basename(filename), res.status_code, res.text)
else:
self.logger.warning("retrieve report: %s, HTTP: %d", checksum, res.status_code)
def retrieve_report(self, chksum):
"""
Retrieve Report for the file checksum
4 retrieval per min if only public API used
@param chksum: sha256sum of the target file
"""
if self.has_sent_retrieve_req and self.is_public_api:
time.sleep(self.PUBLIC_API_SLEEP_TIME)
url = self.URL_BASE + "file/report"
params = {"apikey": self.apikey, "resource": chksum}
res = requests.post(url, data=params, **self.requests_kwargs)
self.has_sent_retrieve_req = True
return res
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Virustotal File Scan')
parser.add_argument("-p", "--private", help="the API key belongs to a private API service", action="store_true")
parser.add_argument("-a", "--apikey", help="the apikey to use for this transaction")
parser.add_argument("-v", "--verbose", help="print verbose log (everything in response)", action="store_true")
parser.add_argument("-s", "--send", help="send a file or a directory of files to scan", metavar="PATH")
parser.add_argument("-o", "--one", help="send a single file to scan", metavar="PATH")
parser.add_argument("-r", "--retrieve", help="retrieve reports on a file or a directory of files", metavar="PATH")
parser.add_argument("-m", "--retrievefrommeta", help="retrieve reports based on checksums in a metafile (one sha256 checksum for each line)", metavar="METAFILE")
parser.add_argument("-l", "--log", help="log actions and responses in file", metavar="LOGFILE")
args = parser.parse_args()
args.private = not args.private
vt = VirusTotal(args.apikey, args.private, args.verbose)
if args.log:
filelog = logging.FileHandler(args.log)
filelog.setFormatter(logging.Formatter("[%(asctime)s %(levelname)s] %(message)s", datefmt="%m/%d/%Y %I:%M:%S"))
vt.logger.addHandler(filelog)
# system init end, start to perform operations
api_comments = {True: 'Public', False: 'Private'}
vt.logger.info("API KEY loaded. %s API used.", api_comments[vt.is_public_api])
if args.one:
print(vt.send_file(args.one))
if args.send:
vt.send_files(list_all_files(args.send))
if args.retrieve:
vt.retrieve_files_reports(list_all_files(args.retrieve))
if args.retrievefrommeta:
vt.retrieve_from_meta(args.retrievefrommeta)