-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvtscan.py
More file actions
339 lines (258 loc) · 10 KB
/
vtscan.py
File metadata and controls
339 lines (258 loc) · 10 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
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
from __future__ import print_function
'''
vtscan - VirusTotal Scanner
Verifies a file using VirusTotal API
Shows virus total results and includes a QR code
for easy side channel validation of results
.: Get free Virus Total API Key :.
https://developers.virustotal.com/reference/getting-started
.: Store local copy of API Key in Env vars :.
export VT_API_KEY=<virus_total_api_key>
.: install dependency :.
python3 -m pip install vt-py
.: Sample :.
python3 vtscan.py <path_to_file>
.: deployment :.
Download vtscan.py to a folder on your system
% alias vtscan="python3 <path_to_vtscan_folder>/vtscan.py"
.: Other :.
Author: Timothy C. Quinn
Home: https://github.com/JavaScriptDude/vtscan
Licence: https://opensource.org/licenses/MIT
'''
import os
import sys
import hashlib
import traceback
import pathlib
import argparse
import vt
import qrcode
def main():
# Hack to handle naked '-' argument
bStdIn:bool=False
bFile:bool=False
for i, arg in enumerate(sys.argv):
if sys.argv[i] == '-':
sys.argv[i] = '--stdin'
bStdIn = True
argp = argparse.ArgumentParser(prog="vtscan")
argp.add_argument("--verbose", "-v", action='store_true')
argp.add_argument("--stdin", "-", action='store_true', help="Read file from stdin (can use '-' also. Eg %% curl https://foo.com/some_installer | vtscan - )")
argp.add_argument("--hash", "-m", action='store_true', help="sha1 or sha256 hash to scan")
if not bStdIn:
argp.add_argument("file", type=str, help="File to scan or hash (see --hash))")
args = argp.parse_args()
if bStdIn:
if args.hash:
raise Exception("Cannot use --hash with stdin")
else:
if args.hash:
args.hash = args.file
api_call_failed : bool = False
got_results : bool = False
warnings : list = []
# Check for Api key
if "VT_API_KEY" not in os.environ:
argp.print_help()
exit("\nMissing Virus total API Key. Please set VT_API_KEY environment variable!", 1)
API_KEY=os.environ["VT_API_KEY"]
if API_KEY.strip() == "":
argp.print_help()
exit("\nMissing Virus total API Key. Please set VT_API_KEY environment variable!", 1)
if args.stdin:
sb = []
for line in sys.stdin:
sb.append(line)
_stdin = "".join(sb)
_stdin_bytes = _stdin.encode('utf-8')
digest_md5 = hashlib.md5(_stdin_bytes).hexdigest()
digest_sha1 = hashlib.sha1(_stdin_bytes).hexdigest()
digest_sha256 = hashlib.sha256(_stdin_bytes).hexdigest()
fname = "(stdin)"
fpath = "-"
elif args.hash:
digest_md5 = "-"
digest_sha1 = "-"
digest_sha256 = "-"
fname = args.hash
fpath = "-"
else:
bFile = True
# Verify that file exists
if not os.path.isfile(args.file):
argp.print_help()
exit("\nPlease specify path to an existing file", 1)
# Get args.file (first arg)
fname, fpath = splitPath(args.file)
# Get sha1 checksum of file
digest_md5 = getChecksum(args.file, 'md5')
digest_sha1 = getChecksum(args.file, 'sha1')
digest_sha256 = getChecksum(args.file, 'sha256')
# print("digest_sha256 = " + digest_sha256)
_verb:str
if bFile:
_verb = f"File: {args.file}"
elif args.stdin:
_verb = f"Stdin (sha256 hash: {digest_sha256}))"
else:
_verb = f"Hash: {args.hash}"
with vt.Client(API_KEY) as client:
if args.hash:
_hash = args.hash
else:
_hash = digest_sha256
try:
res = client.get_object(f"/files/{_hash}")
except vt.APIError as ae:
if ae.code == 'NotFoundError':
warnings.append(f"{_verb} not found in VirusTotal database. Therefore its safety is unknown.")
warnings.append("Alternate verifications may be required")
else:
warnings.append(f"{_verb} - call failed: {ae}")
if len(warnings) == 0:
if bFile:
# Lets be paranoid and verify the checksums found
if not res.md5 == digest_md5:
warnings.append("MD5 Checksums do not match:\n - Original: {}\n - Virus Total: {}".format(digest_md5, res.sha256))
if not res.sha1 == digest_sha1:
warnings.append("SHA1 Checksums do not match:\n - Original: {}\n - Virus Total: {}".format(digest_sha1, res.sha256))
if not res.sha256 == digest_sha256:
warnings.append("SHA256 Checksums do not match:\n - Original: {}\n - Virus Total: {}".format(digest_sha256, res.sha256))
else:
digest_md5 = res.md5
digest_sha1 = res.sha1
digest_sha256 = res.sha256
got_results = True
if bFile:
print(f"\n.: File in :.\n File : {fname}\n Path : {fpath}")
print(f"""
.: Virus Total :.
sha1 : {digest_sha1}
sha256 : {digest_sha256}""")
permalink:str = None
qr:qrcode.QRCode = None
if got_results:
permalink = f"https://www.virustotal.com/gui/file/{digest_sha256}/details"
print(f" Permalink : {permalink}")
qr = qrcode.QRCode(
version=1, # Controls the size of the QR Code (1 is the smallest)
error_correction=qrcode.constants.ERROR_CORRECT_L, # Error correction level
box_size=10, # Size of each box in the QR code grid
border=3,
)
# Add data to the QR Code
qr.add_data(permalink)
qr.make(fit=True)
elif args.stdin:
print(f"\n.: Stdin :.\n sha256 : {digest_sha256}")
else:
print(f"\n.: Hash :.\n Hash arg : {args.hash}")
total:int=None
if got_results:
harmless = res.last_analysis_stats['harmless']
suspicious = res.last_analysis_stats['suspicious']
malicious = res.last_analysis_stats['malicious']
undetected = res.last_analysis_stats['undetected']
detected = malicious + suspicious
total = harmless + suspicious + malicious + undetected
print("\n.: VirusTotal Details :.")
items = []
if hasattr(res, 'creation_date'):
items.append( ("Creation", res.creation_date) )
else:
if hasattr(res, 'first_submission_date'):
items.append( ("First Submission", res.first_submission_date) )
if hasattr(res, 'dot_net_assembly'):
asy = res.dot_net_assembly
items.append( (".Net Name", getattr(asy, 'assembly_name', '-')) )
items.append( (".Net CLR Ver", getattr(asy, 'clr_version', '-')) )
else:
items.append( ("Names", res.names[:5]) )
if hasattr(res, 'signature_info'):
sig = res.signature_info
items.append( ("Description", getattr(sig, 'description', '-')) )
items.append( ("Version", getattr(sig, 'file version', '-')) )
if not hasattr(res, 'dot_net_assembly'):
items.append( ("Original Name", getattr(sig, 'original name', '-')) )
items.append( ("Comments", getattr(sig, 'comments', '-')) )
items.append( ("Magic", res.magic) )
items.append( ("Type", res.type) )
items.append( ("Size", f"{int(res.size/1024):,}kb") )
_print_items(items)
print("\n.: Virus Total Summary :.")
if detected == 0:
print(" Detections : 0 out of {} (100% pass)".format(total))
else:
print(" Detections : suspicious: {0} and malicious {1} out of {2} (Go to VirusTotal for more details)".format(suspicious, malicious, total))
if len(warnings) > 0:
print("\n.: Warnings :.")
for warning in warnings:
print("- {}".format(warning))
if not api_call_failed and not got_results:
print("""
If this is an installer, executable, or other file that does not contain
personal information (for example not a zip archive of personal files),
you may want to consider uploading to VirusTotal to do a deep scan at:
- https://www.virustotal.com/gui/home/upload""")
if got_results:
# Generate the QR Code as ASCII art and print to terminal
qr.print_ascii()
print('--- vtscan end ---')
class VTData():
def __init__(self):
pass
def _print_items(items):
iMax:int = 0
for k, v in items:
if len(k) > iMax:
iMax = len(k)
for k, v in items:
print(" {0:{1}}: {2}".format(k, iMax+1, v))
def getChecksum(path, csumtype):
if csumtype == 'md5':
h = hashlib.md5()
elif csumtype == 'sha1':
h = hashlib.sha1()
elif csumtype == 'sha256':
h = hashlib.sha256()
else:
raise Exception("Unexpected csumtype: {}".format(csumtype))
b = bytearray(128*1024)
mv = memoryview(b)
with open(path, 'rb', buffering=0) as f:
for n in iter(lambda : f.readinto(mv), 0):
h.update(mv[:n])
return h.hexdigest()
def exit(s, exitCode=1):
if not s is None:
print(s)
print('~')
sys.stdout.flush()
sys.stderr.flush()
sys.exit(exitCode)
def splitPath(s):
f = os.path.basename(s)
p = s[:-(len(f))-1]
p = toPosixPath(getAbsPath(p))
return f, p
def toPosixPath(s:str, strip_slash:bool=False, ensure_slash:bool=False):
s = s.strip().replace('\\', '/')
if strip_slash and s[-1:] == '/':
return s[:-1]
if ensure_slash and not s[-1:] == '/':
return '%s/' % s
return s
def getAbsPath(s:str):
return os.path.abspath( pathlib.Path(s).expanduser() )
def noop(*args, **kwargs):
pass
if __name__ == '__main__':
iExit = 0
try:
main()
except Exception:
exc_type, exc_value, exc_traceback = sys.exc_info()
aTB = traceback.format_tb(exc_traceback)
exit("Program Exception:\nStack:\n{}\n Error: {} - {}".format('\n'.join(aTB), exc_type.__name__, exc_value), exitCode=1)
sys.exit(iExit)