mirror of https://github.com/MISP/misp-modules
fixed spacing, addressed error handling for public api, added subdomains, and added context comment
parent
8bd9b46713
commit
bc1eab3520
|
@ -7,7 +7,8 @@ import os
|
||||||
|
|
||||||
misperrors = {'error': 'Error'}
|
misperrors = {'error': 'Error'}
|
||||||
mispattributes = {'input': ['hostname', 'domain', "ip-src", "ip-dst", "md5", "sha1", "sha256", "sha512"],
|
mispattributes = {'input': ['hostname', 'domain', "ip-src", "ip-dst", "md5", "sha1", "sha256", "sha512"],
|
||||||
'output':['domain', "ip-src", "ip-dst", "text", "md5", "sha1", "sha256", "sha512", "ssdeep", "authentihash", "filename"]
|
'output': ['domain', "ip-src", "ip-dst", "text", "md5", "sha1", "sha256", "sha512", "ssdeep",
|
||||||
|
"authentihash", "filename"]
|
||||||
}
|
}
|
||||||
|
|
||||||
# possible module-types: 'expansion', 'hover' or both
|
# possible module-types: 'expansion', 'hover' or both
|
||||||
|
@ -17,7 +18,9 @@ moduleinfo = {'version': '2', 'author': 'Hannah Ward',
|
||||||
|
|
||||||
# config fields that your code expects from the site admin
|
# config fields that your code expects from the site admin
|
||||||
moduleconfig = ["apikey", "event_limit"]
|
moduleconfig = ["apikey", "event_limit"]
|
||||||
limit = 5 #Default
|
limit = 5 # Default
|
||||||
|
comment = '%s: Enriched via VT'
|
||||||
|
|
||||||
|
|
||||||
def handler(q=False):
|
def handler(q=False):
|
||||||
global limit
|
global limit
|
||||||
|
@ -30,145 +33,169 @@ def handler(q=False):
|
||||||
limit = int(q["config"].get("event_limit", 5))
|
limit = int(q["config"].get("event_limit", 5))
|
||||||
|
|
||||||
r = {"results": []}
|
r = {"results": []}
|
||||||
|
|
||||||
if "ip-src" in q:
|
if "ip-src" in q:
|
||||||
r["results"] += getIP(q["ip-src"], key)
|
r["results"] += getIP(q["ip-src"], key)
|
||||||
if "ip-dst" in q:
|
if "ip-dst" in q:
|
||||||
r["results"] += getIP(q["ip-dst"], key)
|
r["results"] += getIP(q["ip-dst"], key)
|
||||||
if "domain" in q:
|
if "domain" in q:
|
||||||
r["results"] += getDomain(q["domain"], key)
|
r["results"] += getDomain(q["domain"], key)
|
||||||
if 'hostname' in q:
|
if 'hostname' in q:
|
||||||
r["results"] += getDomain(q['hostname'], key)
|
r["results"] += getDomain(q['hostname'], key)
|
||||||
if 'md5' in q:
|
if 'md5' in q:
|
||||||
r["results"] += getHash(q['md5'], key)
|
r["results"] += getHash(q['md5'], key)
|
||||||
if 'sha1' in q:
|
if 'sha1' in q:
|
||||||
r["results"] += getHash(q['sha1'], key)
|
r["results"] += getHash(q['sha1'], key)
|
||||||
if 'sha256' in q:
|
if 'sha256' in q:
|
||||||
r["results"] += getHash(q['sha256'], key)
|
r["results"] += getHash(q['sha256'], key)
|
||||||
if 'sha512' in q:
|
if 'sha512' in q:
|
||||||
r["results"] += getHash(q['sha512'], key)
|
r["results"] += getHash(q['sha512'], key)
|
||||||
|
|
||||||
uniq = []
|
uniq = []
|
||||||
for res in r["results"]:
|
for res in r["results"]:
|
||||||
if res not in uniq:
|
if res not in uniq:
|
||||||
uniq.append(res)
|
uniq.append(res)
|
||||||
r["results"] = uniq
|
r["results"] = uniq
|
||||||
return r
|
return r
|
||||||
|
|
||||||
def getHash(hash, key, do_not_recurse = False):
|
|
||||||
|
def getHash(hash, key, do_not_recurse=False):
|
||||||
global limit
|
global limit
|
||||||
toReturn = []
|
toReturn = []
|
||||||
req = requests.get("https://www.virustotal.com/vtapi/v2/file/report",
|
try:
|
||||||
params = { "allinfo":1, "apikey":key, 'resource': hash}
|
req = requests.get("https://www.virustotal.com/vtapi/v2/file/report",
|
||||||
).json()
|
params={"allinfo": 1, "apikey": key, 'resource': hash}
|
||||||
|
).json()
|
||||||
|
except:
|
||||||
|
return []
|
||||||
|
|
||||||
if req["response_code"] == 0:
|
if req["response_code"] == 0:
|
||||||
#Nothing found
|
# Nothing found
|
||||||
return []
|
return []
|
||||||
|
|
||||||
toReturn += getMoreInfo(req, key)
|
toReturn += getMoreInfo(req, key)
|
||||||
return toReturn
|
return toReturn
|
||||||
|
|
||||||
def getIP(ip, key, do_not_recurse = False):
|
|
||||||
|
def getIP(ip, key, do_not_recurse=False):
|
||||||
global limit
|
global limit
|
||||||
toReturn = []
|
toReturn = []
|
||||||
req = requests.get("https://www.virustotal.com/vtapi/v2/ip-address/report",
|
try:
|
||||||
params = {"ip":ip, "apikey":key}
|
req = requests.get("https://www.virustotal.com/vtapi/v2/ip-address/report",
|
||||||
).json()
|
params={"ip": ip, "apikey": key}
|
||||||
|
).json()
|
||||||
|
except:
|
||||||
|
return []
|
||||||
|
|
||||||
if req["response_code"] == 0:
|
if req["response_code"] == 0:
|
||||||
#Nothing found
|
# Nothing found
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if "resolutions" in req:
|
if "resolutions" in req:
|
||||||
for res in req["resolutions"][:limit]:
|
for res in req["resolutions"][:limit]:
|
||||||
toReturn.append( {"types":["domain"], "values":[res["hostname"]]})
|
toReturn.append({"types": ["domain"], "values": [res["hostname"]], "comment":comment % ip})
|
||||||
#Pivot from here to find all domain info
|
# Pivot from here to find all domain info
|
||||||
if not do_not_recurse:
|
if not do_not_recurse:
|
||||||
toReturn += getDomain(res["hostname"], key, True)
|
toReturn += getDomain(res["hostname"], key, True)
|
||||||
|
|
||||||
toReturn += getMoreInfo(req, key)
|
toReturn += getMoreInfo(req, key)
|
||||||
return toReturn
|
return toReturn
|
||||||
|
|
||||||
|
|
||||||
def getDomain(domain, key, do_not_recurse=False):
|
def getDomain(domain, key, do_not_recurse=False):
|
||||||
global limit
|
global limit
|
||||||
toReturn = []
|
toReturn = []
|
||||||
req = requests.get("https://www.virustotal.com/vtapi/v2/domain/report",
|
try:
|
||||||
params = {"domain":domain, "apikey":key}
|
req = requests.get("https://www.virustotal.com/vtapi/v2/domain/report",
|
||||||
).json()
|
params={"domain": domain, "apikey": key}
|
||||||
|
).json()
|
||||||
|
except:
|
||||||
|
return []
|
||||||
|
|
||||||
if req["response_code"] == 0:
|
if req["response_code"] == 0:
|
||||||
#Nothing found
|
# Nothing found
|
||||||
return []
|
return []
|
||||||
|
|
||||||
if "resolutions" in req:
|
if "resolutions" in req:
|
||||||
for res in req["resolutions"][:limit]:
|
for res in req["resolutions"][:limit]:
|
||||||
toReturn.append( {"types":["ip-dst", "ip-src"], "values":[res["ip_address"]]})
|
toReturn.append({"types": ["ip-dst", "ip-src"], "values": [res["ip_address"]], "comment":comment % domain})
|
||||||
#Pivot from here to find all info on IPs
|
# Pivot from here to find all info on IPs
|
||||||
if not do_not_recurse:
|
if not do_not_recurse:
|
||||||
toReturn += getIP(res["ip_address"], key, True)
|
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})
|
||||||
toReturn += getMoreInfo(req, key)
|
toReturn += getMoreInfo(req, key)
|
||||||
return toReturn
|
return toReturn
|
||||||
|
|
||||||
|
|
||||||
def findAll(data, keys):
|
def findAll(data, keys):
|
||||||
a = []
|
a = []
|
||||||
if isinstance(data, dict):
|
if isinstance(data, dict):
|
||||||
for key in data.keys():
|
for key in data.keys():
|
||||||
if key in keys:
|
if key in keys:
|
||||||
a.append(data[key])
|
a.append(data[key])
|
||||||
else:
|
else:
|
||||||
if isinstance(data[key], (dict, list)):
|
if isinstance(data[key], (dict, list)):
|
||||||
a += findAll(data[key], keys)
|
a += findAll(data[key], keys)
|
||||||
if isinstance(data, list):
|
if isinstance(data, list):
|
||||||
for i in data:
|
for i in data:
|
||||||
a += findAll(i, keys)
|
a += findAll(i, keys)
|
||||||
|
|
||||||
return a
|
return a
|
||||||
|
|
||||||
|
|
||||||
def getMoreInfo(req, key):
|
def getMoreInfo(req, key):
|
||||||
global limit
|
global limit
|
||||||
r = []
|
r = []
|
||||||
#Get all hashes first
|
# Get all hashes first
|
||||||
hashes = []
|
hashes = []
|
||||||
hashes = findAll(req, ["md5", "sha1", "sha256", "sha512"])
|
hashes = findAll(req, ["md5", "sha1", "sha256", "sha512"])
|
||||||
r.append({"types":["md5", "sha1", "sha256", "sha512"], "values":hashes})
|
r.append({"types": ["md5", "sha1", "sha256", "sha512"], "values": hashes})
|
||||||
for hsh in hashes[:limit]:
|
for hsh in hashes[:limit]:
|
||||||
#Search VT for some juicy info
|
# Search VT for some juicy info
|
||||||
data = requests.get("http://www.virustotal.com/vtapi/v2/file/report",
|
try:
|
||||||
params={"allinfo":1, "apikey":key, "resource":hsh}
|
data = requests.get("http://www.virustotal.com/vtapi/v2/file/report",
|
||||||
).json()
|
params={"allinfo": 1, "apikey": key, "resource": hsh}
|
||||||
|
).json()
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
# Go through each key and check if it exists
|
# Go through each key and check if it exists
|
||||||
if "submission_names" in data:
|
if "submission_names" in data:
|
||||||
r.append({'types':["filename"], "values":data["submission_names"]})
|
r.append({'types': ["filename"], "values": data["submission_names"], "comment":comment % hsh})
|
||||||
|
|
||||||
if "ssdeep" in data:
|
if "ssdeep" in data:
|
||||||
r.append({'types':["ssdeep"], "values":[data["ssdeep"]]})
|
r.append({'types': ["ssdeep"], "values": [data["ssdeep"]], "comment":comment % hsh})
|
||||||
|
|
||||||
if "authentihash" in data:
|
if "authentihash" in data:
|
||||||
r.append({"types":["authentihash"], "values":[data["authentihash"]]})
|
r.append({"types": ["authentihash"], "values": [data["authentihash"]], "comment":comment % hsh})
|
||||||
|
|
||||||
if "ITW_urls" in data:
|
if "ITW_urls" in data:
|
||||||
r.append({"types":["url"], "values":data["ITW_urls"]})
|
r.append({"types": ["url"], "values": data["ITW_urls"], "comment":comment % hsh})
|
||||||
|
|
||||||
#Get the malware sample
|
# Get the malware sample
|
||||||
sample = requests.get("https://www.virustotal.com/vtapi/v2/file/download",
|
sample = requests.get("https://www.virustotal.com/vtapi/v2/file/download",
|
||||||
params = {"hash":hsh, "apikey":key})
|
params={"hash": hsh, "apikey": key})
|
||||||
|
|
||||||
malsample = sample.content
|
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')
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
# 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')
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
return r
|
return r
|
||||||
|
|
||||||
|
|
||||||
def introspection():
|
def introspection():
|
||||||
return mispattributes
|
return mispattributes
|
||||||
|
|
||||||
|
|
||||||
def version():
|
def version():
|
||||||
moduleinfo['config'] = moduleconfig
|
moduleinfo['config'] = moduleconfig
|
||||||
return moduleinfo
|
return moduleinfo
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue