I have the following two PY
(Python
) files that represent one project (in PyCharm
):
antivirus.py
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 api():
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
wrapper.py
from antivirus import api
import argparse
import hashlib
def parse_options():
parser = argparse.ArgumentParser()
parser.add_argument("-u", "--url", dest="url",
help="URL to scan")
parser.add_argument("-F", "--results-file", dest="sfile",
help="Get report of previously scanned file. If the "
"given filename cannot be found/opened, we'll assume "
"it's a hash.")
parser.add_argument("-U", "--results", dest="url_res",
help="Get report of previously scanned url")
parser.add_argument("-f", "--file", dest="file",
help="Scan file")
parser.add_argument("-v", "--verbose", default=False, action="store_true",
dest="verbose", help="Print complete reply")
return parser.parse_args()
def main():
arg = parse_options()
vt = api(arg.verbose)
#Scan url
if arg.url:
vt.print_url_scan(vt.scanurl(arg.url))
#Get results of file
elif arg.sfile:
try:
f = open(arg.sfile, "r")
fhash = hashlib.sha256()
fhash.update(str(f.read()))
value = fhash.hexdigest()
except:
value = arg.sfile
vt.print_scan_results(vt.results("file", value))
#Get results of url
elif arg.url_res:
vt.print_scan_results(vt.results("url", arg.url_res))
#Send file for scan
elif arg.file:
vt.print_file_scan(vt.sendfile(arg.file))
if __name__ == "__main__":
exit(main())
When I run one of these files (e.g. wrapper.py), nothing happens. There are no errors, but nothing happens.
How can I test the functionality of this project?
That is, how to run this program?
Is it possible to run it as a console application?
Also, I can't type in console. How to enable this? I am using Windows 7.