forked from Erethon/vta.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vta.py
103 lines (89 loc) · 3.21 KB
/
vta.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
#Created by Erethon, erethon.com, <[email protected]>
#A simple Python implementation of the VirusTotal public API
#https://www.virustotal.com/en/documentation/public-api/
#License is the MIT License, see LICENSE and README.md files for more info
#Copyright (C) 2013 Erethon
import requests
import json
try:
from colors import red, green
except ImportError:
def nothing(val):
return val
global red
global green
red = green = nothing
class vtapi():
def __init__(self, verbose=False):
self.verbose = verbose
#self.api = "ASWGFHAHJGASDAGHHKHEGWARJLQGEIQYEQWIUAGHDASD"
self.api = "INSERT YOUR VIRUS TOTAL PUBLIC API HERE"
self.baseurl = "https://www.virustotal.com/vtapi/v2/"
#Print results from a file/url
def print_scan_results(self, results):
if results['response_code'] == 0:
print "Url/file not found, or scanned yet. Try again later"
else:
print ("""Permalink: %s \nScandate: %s \n"""
% (results['permalink'], results['scan_date']))
for i in results['scans']:
print("%s: " % i),
if (str(results['scans'][i]['detected']) == "False"):
print green("Clean")
else:
print (red("Malicious -- %s"
% str(results['scans'][i]['result'])))
if self.verbose:
print
print results
#Print reply for a url scan request
def print_url_scan(self, results):
print ("""Permalink: %s \nURL: %s \nDate: %s \nID: %s"""
% (results['permalink'], results['resource'],
results['scan_date'], results['scan_id']))
if self.verbose:
print
print results
#Print reply for a file scan request
def print_file_scan(self, results):
print results['verbose_msg']
print "Permalink: %s" % results['permalink']
if self.verbose:
print
print results
#Checking if any `networking` related errors occured
def check_results(self, r):
try:
results = r.json()
except ValueError:
print "URL not found, malformed URL or invalid API token"
exit(1)
return results
#Function to get results of a scanned file/url
def results(self, mode, resource):
url = self.baseurl + "%s/report" % mode
values = {"resource": resource,
"apikey": self.api}
r = requests.post(url, values)
results = self.check_results(r)
return results
#Scan a url
def scanurl(self, resource):
url = self.baseurl + "url/scan"
values = {"url": resource,
"apikey": self.api}
r = requests.post(url, values)
results = self.check_results(r)
return results
#Scan a file
def sendfile(self, filename):
url = self.baseurl + "file/scan"
try:
f = open(filename, "rb")
except:
print "Could not open file"
files = {"file": f}
values = {"apikey": self.api}
r = requests.post(url, values, files=files)
results = self.check_results(r)
return results