forked from MobSF/Mobile-Security-Framework-MobSF
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirusTotal.py
More file actions
executable file
·181 lines (165 loc) · 6.24 KB
/
VirusTotal.py
File metadata and controls
executable file
·181 lines (165 loc) · 6.24 KB
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
import logging
import requests
from django.conf import settings
from mobsf.MobSF.proxy import upstream_proxy
from mobsf.MobSF.utils import (
append_scan_status,
file_size,
get_config_loc,
)
logger = logging.getLogger(__name__)
class VirusTotal:
API_KEY_ERROR = 'VirusTotal Permission denied, wrong api key?'
API_KEY_ERROR_SHORT = 'VirusTotal API error'
API_CONN_ERROR = 'VirusTotal Connection Error'
def __init__(self, checksum):
self.base_url = settings.VIRUS_TOTAL_BASE_URL
self.checksum = checksum
def get_report(self):
"""
Get Report from VT.
:param file_hash: md5/sha1/sha256
:return: json response / None
"""
try:
file_hash = self.checksum
url = self.base_url + 'report'
params = {
'apikey': settings.VT_API_KEY,
'resource': file_hash}
headers = {'Accept-Encoding': 'gzip, deflate'}
try:
proxies, verify = upstream_proxy('https')
except Exception as exp:
msg = 'Setting upstream proxy'
logger.exception(msg)
append_scan_status(file_hash, msg, repr(exp))
try:
response = requests.get(
url,
timeout=5,
params=params,
headers=headers,
proxies=proxies,
verify=verify)
if response.status_code == 403:
logger.error(self.API_KEY_ERROR)
append_scan_status(
file_hash,
self.API_KEY_ERROR,
self.API_KEY_ERROR_SHORT)
return None
except Exception as exp:
logger.error(self.API_CONN_ERROR)
append_scan_status(
file_hash,
self.API_CONN_ERROR,
repr(exp))
return None
try:
json_response = response.json()
return json_response
except ValueError:
return None
except Exception as exp:
msg = 'Failed to get report from VirusTotal'
logger.exception(msg)
append_scan_status(file_hash, msg, repr(exp))
return None
def upload_file(self, file_path):
"""
Upload File to VT.
:param file_path: file path to upload
:return: json response / None
"""
try:
url = self.base_url + 'scan'
if file_size(file_path) > 31:
logger.warning('VirusTotal Public API does '
'not support files above 32 MB')
return None
files = {'file': open(file_path, 'rb')}
headers = {'apikey': settings.VT_API_KEY}
try:
proxies, verify = upstream_proxy('https')
except Exception as exp:
msg = 'Setting upstream proxy'
logger.exception(msg)
append_scan_status(self.checksum, msg, repr(exp))
try:
response = requests.post(
url,
timeout=5,
files=files,
data=headers,
proxies=proxies,
verify=verify)
if response.status_code == 403:
logger.error(self.API_KEY_ERROR)
append_scan_status(
self.checksum,
self.API_KEY_ERROR,
self.API_KEY_ERROR_SHORT)
return None
except Exception as exp:
logger.error(self.API_CONN_ERROR)
append_scan_status(
self.checksum,
self.API_CONN_ERROR,
repr(exp))
return None
json_response = response.json()
return json_response
except Exception as exp:
msg = 'Failed to upload file to VirusTotal'
logger.exception(msg)
append_scan_status(self.checksum, msg, repr(exp))
return None
def get_result(self, file_path):
"""
Get Results from VT.
Uploading a file and getting the approval msg from VT
or fetching existing report
:param file_path: file's path
:return: VirusTotal result json / None upon error
"""
try:
file_hash = self.checksum
msg = 'VirusTotal: Check for existing report'
logger.info(msg)
append_scan_status(file_hash, msg)
report = self.get_report()
# Check for existing report
if report:
if report['response_code'] == 1:
msg = f'VirusTotal: {report["verbose_msg"]}'
logger.info(msg)
append_scan_status(file_hash, msg)
return report
if settings.VT_UPLOAD:
msg = 'VirusTotal: file upload'
logger.info(msg)
append_scan_status(file_hash, msg)
upload_response = self.upload_file(file_path)
if upload_response:
msg = f'VirusTotal: {upload_response["verbose_msg"]}'
logger.info(msg)
append_scan_status(file_hash, msg)
return upload_response
else:
msg = ('VirusTotal Scan not performed as file '
f'upload is disabled in {get_config_loc()}. '
'To enable file upload, set VT_UPLOAD to True.')
logger.info(msg)
append_scan_status(file_hash, msg)
message = ('Scan not performed, VirusTotal file '
f'upload disabled in {get_config_loc()}')
report = {
'verbose_msg': message,
'positives': 0,
'total': 0}
return report
except Exception as exp:
msg = 'VirusTotal: Error getting result'
logger.exception(msg)
append_scan_status(file_hash, msg, repr(exp))