2016-08-12 18:40:00 +02:00
|
|
|
import json
|
|
|
|
import requests
|
2017-03-05 18:59:36 +01:00
|
|
|
from requests import HTTPError
|
2016-08-12 18:40:00 +02:00
|
|
|
import base64
|
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
2017-02-10 14:16:39 +01:00
|
|
|
mispattributes = {'input': ['hostname', 'domain', "ip-src", "ip-dst", "md5", "sha1", "sha256", "sha512"],
|
2017-03-01 04:04:24 +01:00
|
|
|
'output': ['domain', "ip-src", "ip-dst", "text", "md5", "sha1", "sha256", "sha512", "ssdeep",
|
|
|
|
"authentihash", "filename"]
|
2016-08-12 18:40:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# possible module-types: 'expansion', 'hover' or both
|
2017-02-10 14:16:39 +01:00
|
|
|
moduleinfo = {'version': '2', 'author': 'Hannah Ward',
|
2016-08-12 18:40:00 +02:00
|
|
|
'description': 'Get information from virustotal',
|
2016-08-15 12:09:40 +02:00
|
|
|
'module-type': ['expansion']}
|
2016-08-12 18:40:00 +02:00
|
|
|
|
|
|
|
# config fields that your code expects from the site admin
|
2016-08-17 14:01:11 +02:00
|
|
|
moduleconfig = ["apikey", "event_limit"]
|
2017-03-01 04:04:24 +01:00
|
|
|
limit = 5 # Default
|
|
|
|
comment = '%s: Enriched via VT'
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
|
|
|
|
def handler(q=False):
|
2016-08-17 14:01:11 +02:00
|
|
|
global limit
|
2016-08-12 18:40:00 +02:00
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
|
|
|
|
q = json.loads(q)
|
|
|
|
|
|
|
|
key = q["config"]["apikey"]
|
2016-08-17 14:01:11 +02:00
|
|
|
limit = int(q["config"].get("event_limit", 5))
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
r = {"results": []}
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
if "ip-src" in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getIP(q["ip-src"], key)
|
2016-08-12 18:40:00 +02:00
|
|
|
if "ip-dst" in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getIP(q["ip-dst"], key)
|
2016-08-12 18:40:00 +02:00
|
|
|
if "domain" in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getDomain(q["domain"], key)
|
2016-08-17 14:01:11 +02:00
|
|
|
if 'hostname' in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getDomain(q['hostname'], key)
|
2017-02-10 14:16:39 +01:00
|
|
|
if 'md5' in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getHash(q['md5'], key)
|
2017-02-10 14:16:39 +01:00
|
|
|
if 'sha1' in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getHash(q['sha1'], key)
|
2017-02-10 14:16:39 +01:00
|
|
|
if 'sha256' in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getHash(q['sha256'], key)
|
2017-02-10 14:16:39 +01:00
|
|
|
if 'sha512' in q:
|
2017-03-01 04:04:24 +01:00
|
|
|
r["results"] += getHash(q['sha512'], key)
|
2016-08-12 18:40:00 +02:00
|
|
|
|
2016-08-17 10:30:15 +02:00
|
|
|
uniq = []
|
|
|
|
for res in r["results"]:
|
2017-03-01 04:04:24 +01:00
|
|
|
if res not in uniq:
|
|
|
|
uniq.append(res)
|
2016-08-17 10:30:15 +02:00
|
|
|
r["results"] = uniq
|
2016-08-12 18:40:00 +02:00
|
|
|
return r
|
|
|
|
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
def getHash(hash, key, do_not_recurse=False):
|
2017-03-05 18:59:36 +01:00
|
|
|
req = requests.get("https://www.virustotal.com/vtapi/v2/file/report",
|
|
|
|
params={"allinfo": 1, "apikey": key, 'resource': hash})
|
2017-03-01 04:04:24 +01:00
|
|
|
try:
|
2017-03-05 18:59:36 +01:00
|
|
|
req.raise_for_status()
|
|
|
|
req = req.json()
|
|
|
|
except HTTPError as e:
|
|
|
|
misperrors['error'] = str(e)
|
|
|
|
return misperrors
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2017-02-10 14:16:39 +01:00
|
|
|
if req["response_code"] == 0:
|
2017-03-01 04:04:24 +01:00
|
|
|
# Nothing found
|
|
|
|
return []
|
2017-02-10 14:16:39 +01:00
|
|
|
|
2017-03-05 18:59:36 +01:00
|
|
|
return getMoreInfo(req, key)
|
2017-02-10 14:16:39 +01:00
|
|
|
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
def getIP(ip, key, do_not_recurse=False):
|
2016-08-17 14:01:11 +02:00
|
|
|
global limit
|
2016-08-12 18:40:00 +02:00
|
|
|
toReturn = []
|
2017-03-05 18:59:36 +01:00
|
|
|
req = requests.get("https://www.virustotal.com/vtapi/v2/ip-address/report",
|
|
|
|
params={"ip": ip, "apikey": key})
|
2017-03-01 04:04:24 +01:00
|
|
|
try:
|
2017-03-05 18:59:36 +01:00
|
|
|
req.raise_for_status()
|
|
|
|
req = req.json()
|
|
|
|
except HTTPError as e:
|
|
|
|
misperrors['error'] = str(e)
|
|
|
|
return misperrors
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
if req["response_code"] == 0:
|
2017-03-01 04:04:24 +01:00
|
|
|
# Nothing found
|
|
|
|
return []
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
if "resolutions" in req:
|
2017-03-01 04:04:24 +01:00
|
|
|
for res in req["resolutions"][:limit]:
|
2017-03-05 18:59:36 +01:00
|
|
|
toReturn.append({"types": ["domain"], "values": [res["hostname"]], "comment": comment % ip})
|
2017-03-01 04:04:24 +01:00
|
|
|
# Pivot from here to find all domain info
|
|
|
|
if not do_not_recurse:
|
|
|
|
toReturn += getDomain(res["hostname"], key, True)
|
2016-08-12 18:40:00 +02:00
|
|
|
|
|
|
|
toReturn += getMoreInfo(req, key)
|
|
|
|
return toReturn
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
|
2016-08-15 12:09:40 +02:00
|
|
|
def getDomain(domain, key, do_not_recurse=False):
|
2016-08-17 14:01:11 +02:00
|
|
|
global limit
|
2016-08-12 18:40:00 +02:00
|
|
|
toReturn = []
|
2017-03-05 18:59:36 +01:00
|
|
|
req = requests.get("https://www.virustotal.com/vtapi/v2/domain/report",
|
|
|
|
params={"domain": domain, "apikey": key})
|
2017-03-01 04:04:24 +01:00
|
|
|
try:
|
2017-03-05 18:59:36 +01:00
|
|
|
req.raise_for_status()
|
|
|
|
req = req.json()
|
|
|
|
except HTTPError as e:
|
|
|
|
misperrors['error'] = str(e)
|
|
|
|
return misperrors
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
if req["response_code"] == 0:
|
2017-03-01 04:04:24 +01:00
|
|
|
# Nothing found
|
|
|
|
return []
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
if "resolutions" in req:
|
2017-03-01 04:04:24 +01:00
|
|
|
for res in req["resolutions"][:limit]:
|
2017-03-05 18:59:36 +01:00
|
|
|
toReturn.append({"types": ["ip-dst", "ip-src"], "values": [res["ip_address"]], "comment": comment % domain})
|
2017-03-01 04:04:24 +01:00
|
|
|
# Pivot from here to find all info on IPs
|
|
|
|
if not do_not_recurse:
|
|
|
|
toReturn += getIP(res["ip_address"], key, True)
|
|
|
|
if "subdomains" in req:
|
|
|
|
for subd in req["subdomains"]:
|
|
|
|
toReturn.append({"types": ["domain"], "values": [subd], "comment": comment % domain})
|
2016-08-12 18:40:00 +02:00
|
|
|
toReturn += getMoreInfo(req, key)
|
|
|
|
return toReturn
|
|
|
|
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
def findAll(data, keys):
|
2017-03-01 04:04:24 +01:00
|
|
|
a = []
|
|
|
|
if isinstance(data, dict):
|
|
|
|
for key in data.keys():
|
|
|
|
if key in keys:
|
|
|
|
a.append(data[key])
|
|
|
|
else:
|
|
|
|
if isinstance(data[key], (dict, list)):
|
|
|
|
a += findAll(data[key], keys)
|
|
|
|
if isinstance(data, list):
|
|
|
|
for i in data:
|
|
|
|
a += findAll(i, keys)
|
|
|
|
|
|
|
|
return a
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
|
|
|
|
def getMoreInfo(req, key):
|
2016-08-17 14:01:11 +02:00
|
|
|
global limit
|
2016-08-12 18:40:00 +02:00
|
|
|
r = []
|
2017-03-01 04:04:24 +01:00
|
|
|
# Get all hashes first
|
2016-08-12 18:40:00 +02:00
|
|
|
hashes = []
|
|
|
|
hashes = findAll(req, ["md5", "sha1", "sha256", "sha512"])
|
2018-02-20 14:08:14 +01:00
|
|
|
r.append({"types": ["freetext"], "values": hashes})
|
2016-08-17 14:01:11 +02:00
|
|
|
for hsh in hashes[:limit]:
|
2017-03-01 04:04:24 +01:00
|
|
|
# Search VT for some juicy info
|
|
|
|
try:
|
|
|
|
data = requests.get("http://www.virustotal.com/vtapi/v2/file/report",
|
|
|
|
params={"allinfo": 1, "apikey": key, "resource": hsh}
|
|
|
|
).json()
|
|
|
|
except:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Go through each key and check if it exists
|
|
|
|
if "submission_names" in data:
|
2017-03-05 18:59:36 +01:00
|
|
|
r.append({'types': ["filename"], "values": data["submission_names"], "comment": comment % hsh})
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
if "ssdeep" in data:
|
2017-03-05 18:59:36 +01:00
|
|
|
r.append({'types': ["ssdeep"], "values": [data["ssdeep"]], "comment": comment % hsh})
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
if "authentihash" in data:
|
2017-03-05 18:59:36 +01:00
|
|
|
r.append({"types": ["authentihash"], "values": [data["authentihash"]], "comment": comment % hsh})
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
if "ITW_urls" in data:
|
2017-03-05 18:59:36 +01:00
|
|
|
r.append({"types": ["url"], "values": data["ITW_urls"], "comment": comment % hsh})
|
2017-03-01 04:04:24 +01:00
|
|
|
|
|
|
|
# Get the malware sample
|
|
|
|
sample = requests.get("https://www.virustotal.com/vtapi/v2/file/download",
|
|
|
|
params={"hash": hsh, "apikey": key})
|
|
|
|
|
|
|
|
malsample = sample.content
|
|
|
|
|
|
|
|
# It is possible for VT to not give us any submission names
|
|
|
|
if "submission_names" in data:
|
|
|
|
r.append({"types": ["malware-sample"],
|
|
|
|
"categories": ["Payload delivery"],
|
|
|
|
"values": data["submission_names"],
|
|
|
|
"data": str(base64.b64encode(malsample), 'utf-8')
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
return r
|
|
|
|
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
2017-03-01 04:04:24 +01:00
|
|
|
|
2016-08-12 18:40:00 +02:00
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|