2019-07-11 22:59:07 +02:00
|
|
|
import json
|
2021-05-28 21:53:35 +02:00
|
|
|
import logging
|
2019-07-11 22:59:07 +02:00
|
|
|
import requests
|
2020-07-28 11:47:53 +02:00
|
|
|
from . import check_input_attribute, standard_error_message
|
2021-05-28 21:53:35 +02:00
|
|
|
from urllib.parse import urlparse
|
2020-07-28 11:47:53 +02:00
|
|
|
from pymisp import MISPAttribute, MISPEvent, MISPObject
|
2019-07-11 22:59:07 +02:00
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
2020-02-07 12:20:12 +01:00
|
|
|
mispattributes = {'input': ['hostname', 'domain', "ip-src", "ip-dst", "md5", "sha1", "sha256", "url"],
|
2019-07-11 22:59:07 +02:00
|
|
|
'format': 'misp_standard'}
|
|
|
|
moduleinfo = {'version': '1', 'author': 'Christian Studer',
|
2020-02-07 12:20:12 +01:00
|
|
|
'description': 'Get information from VirusTotal public API v2.',
|
2019-07-12 10:59:19 +02:00
|
|
|
'module-type': ['expansion', 'hover']}
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2021-05-28 21:53:35 +02:00
|
|
|
moduleconfig = ['apikey', 'proxy_host', 'proxy_port', 'proxy_username', 'proxy_password']
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2021-05-28 21:53:35 +02:00
|
|
|
LOGGER = logging.getLogger('virus_total_public')
|
|
|
|
LOGGER.setLevel(logging.INFO)
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2021-06-11 14:54:07 +02:00
|
|
|
|
2019-07-11 22:59:07 +02:00
|
|
|
class VirusTotalParser():
|
|
|
|
def __init__(self):
|
|
|
|
super(VirusTotalParser, self).__init__()
|
|
|
|
self.misp_event = MISPEvent()
|
2021-05-28 21:53:35 +02:00
|
|
|
self.proxies = None
|
2019-07-11 22:59:07 +02:00
|
|
|
|
|
|
|
def declare_variables(self, apikey, attribute):
|
|
|
|
self.attribute = MISPAttribute()
|
|
|
|
self.attribute.from_dict(**attribute)
|
|
|
|
self.apikey = apikey
|
|
|
|
|
|
|
|
def get_result(self):
|
2019-08-05 11:33:04 +02:00
|
|
|
event = json.loads(self.misp_event.to_json())
|
2019-07-11 22:59:07 +02:00
|
|
|
results = {key: event[key] for key in ('Attribute', 'Object') if (key in event and event[key])}
|
|
|
|
return {'results': results}
|
|
|
|
|
2019-07-15 23:44:25 +02:00
|
|
|
def parse_urls(self, query_result):
|
|
|
|
for feature in ('detected_urls', 'undetected_urls'):
|
2019-07-22 09:16:04 +02:00
|
|
|
if feature in query_result:
|
|
|
|
for url in query_result[feature]:
|
2019-07-22 11:39:46 +02:00
|
|
|
value = url['url'] if isinstance(url, dict) else url[0]
|
2019-07-22 09:16:04 +02:00
|
|
|
self.misp_event.add_attribute('url', value)
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2019-07-17 20:40:56 +02:00
|
|
|
def parse_resolutions(self, resolutions, subdomains=None, uuids=None):
|
2019-07-11 22:59:07 +02:00
|
|
|
domain_ip_object = MISPObject('domain-ip')
|
2020-09-28 12:34:00 +02:00
|
|
|
if self.attribute.type in ('domain', 'hostname'):
|
2019-07-12 10:59:19 +02:00
|
|
|
domain_ip_object.add_attribute('domain', type='domain', value=self.attribute.value)
|
|
|
|
attribute_type, relation, key = ('ip-dst', 'ip', 'ip_address')
|
|
|
|
else:
|
|
|
|
domain_ip_object.add_attribute('ip', type='ip-dst', value=self.attribute.value)
|
|
|
|
attribute_type, relation, key = ('domain', 'domain', 'hostname')
|
2019-07-11 22:59:07 +02:00
|
|
|
for resolution in resolutions:
|
2019-07-12 10:59:19 +02:00
|
|
|
domain_ip_object.add_attribute(relation, type=attribute_type, value=resolution[key])
|
2019-07-11 22:59:07 +02:00
|
|
|
if subdomains:
|
|
|
|
for subdomain in subdomains:
|
|
|
|
attribute = MISPAttribute()
|
|
|
|
attribute.from_dict(**dict(type='domain', value=subdomain))
|
|
|
|
self.misp_event.add_attribute(**attribute)
|
|
|
|
domain_ip_object.add_reference(attribute.uuid, 'subdomain')
|
2019-07-17 20:40:56 +02:00
|
|
|
if uuids:
|
|
|
|
for uuid in uuids:
|
|
|
|
domain_ip_object.add_reference(uuid, 'sibling-of')
|
2019-07-11 22:59:07 +02:00
|
|
|
self.misp_event.add_object(**domain_ip_object)
|
|
|
|
|
|
|
|
def parse_vt_object(self, query_result):
|
2019-10-30 16:34:15 +01:00
|
|
|
if query_result['response_code'] == 1:
|
|
|
|
vt_object = MISPObject('virustotal-report')
|
|
|
|
vt_object.add_attribute('permalink', type='link', value=query_result['permalink'])
|
|
|
|
detection_ratio = '{}/{}'.format(query_result['positives'], query_result['total'])
|
|
|
|
vt_object.add_attribute('detection-ratio', type='text', value=detection_ratio)
|
|
|
|
self.misp_event.add_object(**vt_object)
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2019-07-22 09:53:19 +02:00
|
|
|
def get_query_result(self, query_type):
|
2019-07-11 22:59:07 +02:00
|
|
|
params = {query_type: self.attribute.value, 'apikey': self.apikey}
|
2021-05-28 21:53:35 +02:00
|
|
|
return requests.get(self.base_url, params=params, proxies=self.proxies)
|
|
|
|
|
|
|
|
def set_proxy_settings(self, config: dict) -> dict:
|
|
|
|
"""Returns proxy settings in the requests format.
|
|
|
|
If no proxy settings are set, return None."""
|
|
|
|
proxies = None
|
|
|
|
host = config.get('proxy_host')
|
|
|
|
port = config.get('proxy_port')
|
|
|
|
username = config.get('proxy_username')
|
|
|
|
password = config.get('proxy_password')
|
|
|
|
|
|
|
|
if host:
|
|
|
|
if not port:
|
|
|
|
misperrors['error'] = 'The virustotal_public_proxy_host config is set, ' \
|
|
|
|
'please also set the virustotal_public_proxy_port.'
|
|
|
|
raise KeyError
|
|
|
|
parsed = urlparse(host)
|
|
|
|
if 'http' in parsed.scheme:
|
|
|
|
scheme = 'http'
|
|
|
|
else:
|
|
|
|
scheme = parsed.scheme
|
|
|
|
netloc = parsed.netloc
|
|
|
|
host = f'{netloc}:{port}'
|
|
|
|
|
|
|
|
if username:
|
|
|
|
if not password:
|
|
|
|
misperrors['error'] = 'The virustotal_public_proxy_username config is set, ' \
|
|
|
|
'please also set the virustotal_public_proxy_password.'
|
|
|
|
raise KeyError
|
|
|
|
auth = f'{username}:{password}'
|
|
|
|
host = auth + '@' + host
|
|
|
|
|
|
|
|
proxies = {
|
|
|
|
'http': f'{scheme}://{host}',
|
|
|
|
'https': f'{scheme}://{host}'
|
|
|
|
}
|
2021-06-11 14:54:07 +02:00
|
|
|
self.proxies = proxies
|
2021-05-28 21:53:35 +02:00
|
|
|
return True
|
2019-07-11 22:59:07 +02:00
|
|
|
|
2021-06-11 14:54:07 +02:00
|
|
|
|
2019-07-11 22:59:07 +02:00
|
|
|
class DomainQuery(VirusTotalParser):
|
|
|
|
def __init__(self, apikey, attribute):
|
|
|
|
super(DomainQuery, self).__init__()
|
|
|
|
self.base_url = "https://www.virustotal.com/vtapi/v2/domain/report"
|
|
|
|
self.declare_variables(apikey, attribute)
|
|
|
|
|
|
|
|
def parse_report(self, query_result):
|
|
|
|
hash_type = 'sha256'
|
|
|
|
whois = 'whois'
|
2019-07-22 11:42:52 +02:00
|
|
|
for feature_type in ('referrer', 'downloaded', 'communicating'):
|
2019-07-18 21:38:17 +02:00
|
|
|
for feature in ('undetected_{}_samples', 'detected_{}_samples'):
|
2019-07-22 11:43:35 +02:00
|
|
|
for sample in query_result.get(feature.format(feature_type), []):
|
2019-07-18 21:38:17 +02:00
|
|
|
self.misp_event.add_attribute(hash_type, sample[hash_type])
|
2019-07-11 22:59:07 +02:00
|
|
|
if query_result.get(whois):
|
2019-07-12 10:59:19 +02:00
|
|
|
whois_object = MISPObject(whois)
|
|
|
|
whois_object.add_attribute('text', type='text', value=query_result[whois])
|
|
|
|
self.misp_event.add_object(**whois_object)
|
2020-01-17 11:26:35 +01:00
|
|
|
if 'domain_siblings' in query_result:
|
|
|
|
siblings = (self.parse_siblings(domain) for domain in query_result['domain_siblings'])
|
|
|
|
if 'subdomains' in query_result:
|
|
|
|
self.parse_resolutions(query_result['resolutions'], query_result['subdomains'], siblings)
|
2019-07-15 23:44:25 +02:00
|
|
|
self.parse_urls(query_result)
|
2019-07-16 22:39:35 +02:00
|
|
|
|
2019-07-22 09:16:04 +02:00
|
|
|
def parse_siblings(self, domain):
|
2019-07-16 22:39:35 +02:00
|
|
|
attribute = MISPAttribute()
|
2019-07-22 09:16:04 +02:00
|
|
|
attribute.from_dict(**dict(type='domain', value=domain))
|
2019-07-16 22:39:35 +02:00
|
|
|
self.misp_event.add_attribute(**attribute)
|
|
|
|
return attribute.uuid
|
2019-07-11 22:59:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
class HashQuery(VirusTotalParser):
|
|
|
|
def __init__(self, apikey, attribute):
|
|
|
|
super(HashQuery, self).__init__()
|
|
|
|
self.base_url = "https://www.virustotal.com/vtapi/v2/file/report"
|
|
|
|
self.declare_variables(apikey, attribute)
|
|
|
|
|
|
|
|
def parse_report(self, query_result):
|
|
|
|
file_attributes = []
|
|
|
|
for hash_type in ('md5', 'sha1', 'sha256'):
|
2019-07-12 10:59:19 +02:00
|
|
|
if query_result.get(hash_type):
|
2019-07-11 22:59:07 +02:00
|
|
|
file_attributes.append({'type': hash_type, 'object_relation': hash_type,
|
2019-07-12 10:59:19 +02:00
|
|
|
'value': query_result[hash_type]})
|
2019-07-11 22:59:07 +02:00
|
|
|
if file_attributes:
|
2019-07-12 10:59:19 +02:00
|
|
|
file_object = MISPObject('file')
|
2019-07-11 22:59:07 +02:00
|
|
|
for attribute in file_attributes:
|
|
|
|
file_object.add_attribute(**attribute)
|
|
|
|
self.misp_event.add_object(**file_object)
|
|
|
|
self.parse_vt_object(query_result)
|
|
|
|
|
|
|
|
|
|
|
|
class IpQuery(VirusTotalParser):
|
|
|
|
def __init__(self, apikey, attribute):
|
|
|
|
super(IpQuery, self).__init__()
|
|
|
|
self.base_url = "https://www.virustotal.com/vtapi/v2/ip-address/report"
|
|
|
|
self.declare_variables(apikey, attribute)
|
|
|
|
|
|
|
|
def parse_report(self, query_result):
|
|
|
|
if query_result.get('asn'):
|
|
|
|
asn_mapping = {'network': ('ip-src', 'subnet-announced'),
|
2019-07-12 10:59:19 +02:00
|
|
|
'country': ('text', 'country')}
|
2019-07-11 22:59:07 +02:00
|
|
|
asn_object = MISPObject('asn')
|
|
|
|
asn_object.add_attribute('asn', type='AS', value=query_result['asn'])
|
|
|
|
for key, value in asn_mapping.items():
|
2019-07-12 10:59:19 +02:00
|
|
|
if query_result.get(key):
|
|
|
|
attribute_type, relation = value
|
|
|
|
asn_object.add_attribute(relation, type=attribute_type, value=query_result[key])
|
2019-07-11 22:59:07 +02:00
|
|
|
self.misp_event.add_object(**asn_object)
|
2019-07-15 23:44:25 +02:00
|
|
|
self.parse_urls(query_result)
|
2019-07-11 22:59:07 +02:00
|
|
|
if query_result.get('resolutions'):
|
|
|
|
self.parse_resolutions(query_result['resolutions'])
|
|
|
|
|
|
|
|
|
|
|
|
class UrlQuery(VirusTotalParser):
|
|
|
|
def __init__(self, apikey, attribute):
|
|
|
|
super(UrlQuery, self).__init__()
|
|
|
|
self.base_url = "https://www.virustotal.com/vtapi/v2/url/report"
|
|
|
|
self.declare_variables(apikey, attribute)
|
|
|
|
|
|
|
|
def parse_report(self, query_result):
|
|
|
|
self.parse_vt_object(query_result)
|
|
|
|
|
|
|
|
|
|
|
|
domain = ('domain', DomainQuery)
|
|
|
|
ip = ('ip', IpQuery)
|
|
|
|
file = ('resource', HashQuery)
|
|
|
|
misp_type_mapping = {'domain': domain, 'hostname': domain, 'ip-src': ip,
|
|
|
|
'ip-dst': ip, 'md5': file, 'sha1': file, 'sha256': file,
|
2020-02-07 12:20:12 +01:00
|
|
|
'url': ('resource', UrlQuery)}
|
2019-07-11 22:59:07 +02:00
|
|
|
|
|
|
|
|
|
|
|
def parse_error(status_code):
|
|
|
|
status_mapping = {204: 'VirusTotal request rate limit exceeded.',
|
|
|
|
400: 'Incorrect request, please check the arguments.',
|
|
|
|
403: 'You don\'t have enough privileges to make the request.'}
|
|
|
|
if status_code in status_mapping:
|
|
|
|
return status_mapping[status_code]
|
|
|
|
return "VirusTotal may not be accessible."
|
|
|
|
|
|
|
|
|
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
if not request.get('config') or not request['config'].get('apikey'):
|
|
|
|
misperrors['error'] = "A VirusTotal api key is required for this module."
|
|
|
|
return misperrors
|
2020-07-28 11:47:53 +02:00
|
|
|
if not request.get('attribute') or not check_input_attribute(request['attribute']):
|
|
|
|
return {'error': f'{standard_error_message}, which should contain at least a type, a value and an uuid.'}
|
2019-07-11 22:59:07 +02:00
|
|
|
attribute = request['attribute']
|
2020-07-28 11:47:53 +02:00
|
|
|
if attribute['type'] not in mispattributes['input']:
|
|
|
|
return {'error': 'Unsupported attribute type.'}
|
2019-07-11 22:59:07 +02:00
|
|
|
query_type, to_call = misp_type_mapping[attribute['type']]
|
|
|
|
parser = to_call(request['config']['apikey'], attribute)
|
2021-05-28 21:53:35 +02:00
|
|
|
parser.set_proxy_settings(request.get('config'))
|
2019-07-22 09:53:19 +02:00
|
|
|
query_result = parser.get_query_result(query_type)
|
2019-07-11 22:59:07 +02:00
|
|
|
status_code = query_result.status_code
|
|
|
|
if status_code == 200:
|
|
|
|
parser.parse_report(query_result.json())
|
|
|
|
else:
|
|
|
|
misperrors['error'] = parse_error(status_code)
|
|
|
|
return misperrors
|
|
|
|
return parser.get_result()
|
|
|
|
|
|
|
|
|
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
|
|
|
|
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|