Skip to content

Instantly share code, notes, and snippets.

@yukkeorg
Last active Mar 23, 2018
Embed
What would you like to do?
Terateil 118442
# VirusTotalのScan URL API使ってみる検証プログラム
# 実行方法:
# for Linux/Mac(Bash):
# $ VT_APIKEY=<apikey> python3 vt_scanurl.py <URL>
# for Windows(cmd.exe)
# > set VT_APIKEY=<apikey>
# > python3 vt_scanurl.py <URL>
import sys
import os
import urllib.request
import urllib.parse
import json as _json
import pprint
import time
def main():
if len(sys.argv) != 2:
print("Not specified URL")
return 1
url = sys.argv[1]
APIKEY = os.environ.get("VT_APIKEY")
if APIKEY is None:
print("Can not read VT_APIKEY environment variable")
return 2
# Call /url/scan API
print("Waiting response 'POST /url/scan'")
params = urllib.parse.urlencode({"apikey": APIKEY, "url": url}).encode('utf-8')
response = urllib.request.urlopen("https://www.virustotal.com/vtapi/v2/url/scan", data=params)
json = _json.loads(response.read().decode('utf-8'))
pprint.pprint(json)
if not json:
print("Error in calling /url/scan API")
return 3
# Save scan_id from /url/scan API response
scan_id = json["scan_id"]
# wait 10 secs
print("Wait 10 seconds...")
time.sleep(10)
# Call /url/report API
print("Waiting response 'GET /url/report'")
query = urllib.parse.urlencode({"apikey": APIKEY, "resource": scan_id})
response = urllib.request.urlopen("https://www.virustotal.com/vtapi/v2/url/report?" + query)
json = _json.loads(response.read().decode('utf-8'))
pprint.pprint(json)
if not json:
print("Error in calling /url/report API")
return 4
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment