2020-05-30 02:21:20 +02:00
|
|
|
import json
|
2020-06-22 21:15:28 +02:00
|
|
|
import pymisp
|
2020-08-07 06:59:13 +02:00
|
|
|
from base64 import b64encode
|
|
|
|
from collections import OrderedDict
|
2020-07-28 11:47:53 +02:00
|
|
|
from . import check_input_attribute, checking_error, standard_error_message
|
2020-05-30 02:21:20 +02:00
|
|
|
from pymisp import MISPAttribute, MISPEvent, MISPObject
|
2020-08-10 05:29:37 +02:00
|
|
|
from trustar import TruStar, Indicator
|
2020-08-07 06:59:13 +02:00
|
|
|
from urllib.parse import quote
|
2020-05-30 02:21:20 +02:00
|
|
|
|
|
|
|
misperrors = {'error': "Error"}
|
2020-06-22 04:52:17 +02:00
|
|
|
mispattributes = {
|
|
|
|
'input': ["btc", "domain", "email-src", "filename", "hostname", "ip-src", "ip-dst", "malware-type", "md5", "sha1",
|
|
|
|
"sha256", "url"], 'format': 'misp_standard'}
|
2020-05-30 02:21:20 +02:00
|
|
|
|
|
|
|
moduleinfo = {'version': "0.1", 'author': "Jesse Hedden",
|
|
|
|
'description': "Enrich data with TruSTAR",
|
|
|
|
'module-type': ["hover", "expansion"]}
|
|
|
|
|
2020-06-22 04:52:17 +02:00
|
|
|
moduleconfig = ["user_api_key", "user_api_secret", "enclave_ids"]
|
|
|
|
|
2020-06-25 19:54:34 +02:00
|
|
|
MAX_PAGE_SIZE = 100 # Max allowable page size returned from /1.3/indicators/summaries endpoint
|
2020-06-22 23:54:37 +02:00
|
|
|
|
2020-06-22 04:52:17 +02:00
|
|
|
|
|
|
|
class TruSTARParser:
|
|
|
|
ENTITY_TYPE_MAPPINGS = {
|
|
|
|
'BITCOIN_ADDRESS': "btc",
|
|
|
|
'CIDR_BLOCK': "ip-src",
|
|
|
|
'CVE': "vulnerability",
|
|
|
|
'URL': "url",
|
|
|
|
'EMAIL_ADDRESS': "email-src",
|
|
|
|
'SOFTWARE': "filename",
|
|
|
|
'IP': "ip-src",
|
|
|
|
'MALWARE': "malware-type",
|
|
|
|
'MD5': "md5",
|
|
|
|
'REGISTRY_KEY': "regkey",
|
|
|
|
'SHA1': "sha1",
|
|
|
|
'SHA256': "sha256"
|
|
|
|
}
|
|
|
|
|
2020-08-10 16:53:24 +02:00
|
|
|
# Relevant fields from each TruSTAR endpoint
|
2020-08-10 05:29:37 +02:00
|
|
|
SUMMARY_FIELDS = ["severityLevel", "source", "score", "attributes"]
|
2020-11-11 02:20:03 +01:00
|
|
|
METADATA_FIELDS = ["sightings", "firstSeen", "lastSeen", "tags"]
|
2020-08-07 06:59:13 +02:00
|
|
|
|
2020-06-22 04:52:17 +02:00
|
|
|
REPORT_BASE_URL = "https://station.trustar.co/constellation/reports/{}"
|
|
|
|
|
2020-08-07 06:59:13 +02:00
|
|
|
CLIENT_METATAG = f"MISP-{pymisp.__version__}"
|
2020-06-22 21:15:28 +02:00
|
|
|
|
2020-06-22 04:52:17 +02:00
|
|
|
def __init__(self, attribute, config):
|
2020-06-25 02:47:41 +02:00
|
|
|
config['enclave_ids'] = config.get('enclave_ids', "").strip().split(',')
|
2020-06-22 21:15:28 +02:00
|
|
|
config['client_metatag'] = self.CLIENT_METATAG
|
2020-06-22 04:52:17 +02:00
|
|
|
self.ts_client = TruStar(config=config)
|
|
|
|
|
|
|
|
self.misp_event = MISPEvent()
|
|
|
|
self.misp_attribute = MISPAttribute()
|
|
|
|
self.misp_attribute.from_dict(**attribute)
|
|
|
|
self.misp_event.add_attribute(**self.misp_attribute)
|
|
|
|
|
|
|
|
def get_results(self):
|
2020-06-22 22:47:25 +02:00
|
|
|
"""
|
|
|
|
Returns the MISP Event enriched with TruSTAR indicator summary data.
|
|
|
|
"""
|
2020-08-10 05:29:37 +02:00
|
|
|
try:
|
|
|
|
event = json.loads(self.misp_event.to_json())
|
|
|
|
results = {key: event[key] for key in ('Attribute', 'Object') if (key in event and event[key])}
|
|
|
|
return {'results': results}
|
|
|
|
except Exception as e:
|
|
|
|
misperrors['error'] += f" -- Encountered issue serializing enrichment data -- {e}"
|
|
|
|
return misperrors
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-08-07 06:59:13 +02:00
|
|
|
def generate_trustar_link(self, entity_type, entity_value):
|
2020-06-22 04:52:17 +02:00
|
|
|
"""
|
2020-08-07 06:59:13 +02:00
|
|
|
Generates link to TruSTAR report of entity.
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
:param entity_type: <str> Type of entity.
|
2020-06-22 04:52:17 +02:00
|
|
|
:param entity_value: <str> Value of entity.
|
2020-08-10 05:29:37 +02:00
|
|
|
:return: <str> Link to indicator report in TruSTAR platform.
|
2020-06-22 04:52:17 +02:00
|
|
|
"""
|
2020-08-07 06:59:13 +02:00
|
|
|
report_id = b64encode(quote(f"{entity_type}|{entity_value}").encode()).decode()
|
|
|
|
|
|
|
|
return self.REPORT_BASE_URL.format(report_id)
|
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
@staticmethod
|
|
|
|
def extract_tags(enrichment_report):
|
|
|
|
"""
|
|
|
|
Extracts tags from the enrichment report in order to add them
|
|
|
|
to the TruSTAR MISP Object. Removes tags from report to avoid
|
|
|
|
redundancy.
|
|
|
|
|
|
|
|
:param: <OrderedDict> Enrichment data.
|
2020-08-10 05:41:52 +02:00
|
|
|
:return: <list> List of tags.
|
2020-08-10 05:29:37 +02:00
|
|
|
"""
|
|
|
|
if enrichment_report and enrichment_report.get('tags'):
|
|
|
|
return [tag.get('name') for tag in enrichment_report.pop('tags')]
|
|
|
|
return None
|
|
|
|
|
2020-08-07 06:59:13 +02:00
|
|
|
def generate_enrichment_report(self, summary, metadata):
|
|
|
|
"""
|
|
|
|
Extracts desired fields from summary and metadata reports and
|
|
|
|
generates an enrichment report.
|
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
:param summary: <trustar.IndicatorSummary> Indicator summary report.
|
|
|
|
:param metadata: <trustar.Indicator> Indicator metadata report.
|
2020-08-07 06:59:13 +02:00
|
|
|
:return: <str> Enrichment report.
|
|
|
|
"""
|
2020-08-10 05:36:47 +02:00
|
|
|
# Preserve order of fields as they exist in SUMMARY_FIELDS and METADATA_FIELDS
|
2020-08-07 06:59:13 +02:00
|
|
|
enrichment_report = OrderedDict()
|
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
if summary:
|
|
|
|
summary_dict = summary.to_dict()
|
|
|
|
enrichment_report.update(
|
|
|
|
{field: summary_dict[field] for field in self.SUMMARY_FIELDS if summary_dict.get(field)})
|
2020-08-07 06:59:13 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
if metadata:
|
|
|
|
metadata_dict = metadata.to_dict()
|
|
|
|
enrichment_report.update(
|
|
|
|
{field: metadata_dict[field] for field in self.METADATA_FIELDS if metadata_dict.get(field)})
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-08-07 06:59:13 +02:00
|
|
|
return enrichment_report
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
def parse_indicator_summary(self, indicator, summary, metadata):
|
2020-06-22 22:47:25 +02:00
|
|
|
"""
|
2020-08-10 05:29:37 +02:00
|
|
|
Pulls enrichment data from the TruSTAR /indicators/summaries and /indicators/metadata endpoints
|
|
|
|
and creates a MISP trustar_report.
|
2020-06-22 22:47:25 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
:param indicator: <str> Value of the attribute
|
|
|
|
:summary: <trustar.IndicatorSummary> Indicator summary response object.
|
|
|
|
:metadata: <trustar.Indicator> Indicator response object.
|
2020-06-22 22:47:25 +02:00
|
|
|
"""
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
# Verify that the indicator type is supported by TruSTAR
|
|
|
|
if summary and summary.indicator_type in self.ENTITY_TYPE_MAPPINGS:
|
|
|
|
indicator_type = summary.indicator_type
|
|
|
|
elif metadata and metadata.type in self.ENTITY_TYPE_MAPPINGS:
|
|
|
|
indicator_type = metadata.type
|
|
|
|
else:
|
|
|
|
misperrors['error'] += " -- Attribute not found or not supported"
|
|
|
|
raise Exception
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Extract most relevant fields from indicator summary and metadata responses
|
|
|
|
enrichment_report = self.generate_enrichment_report(summary, metadata)
|
|
|
|
tags = self.extract_tags(enrichment_report)
|
|
|
|
|
|
|
|
if enrichment_report:
|
2020-08-10 16:53:24 +02:00
|
|
|
# Create MISP trustar_report object and populate it with enrichment data
|
2020-08-10 05:29:37 +02:00
|
|
|
trustar_obj = MISPObject('trustar_report')
|
|
|
|
trustar_obj.add_attribute(indicator_type, attribute_type=self.ENTITY_TYPE_MAPPINGS[indicator_type],
|
|
|
|
value=indicator)
|
|
|
|
trustar_obj.add_attribute("INDICATOR_SUMMARY", attribute_type="text",
|
|
|
|
value=json.dumps(enrichment_report, indent=4))
|
2020-08-10 16:53:24 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
report_link = self.generate_trustar_link(indicator_type, indicator)
|
|
|
|
trustar_obj.add_attribute("REPORT_LINK", attribute_type="link", value=report_link)
|
2020-08-10 16:53:24 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
self.misp_event.add_object(**trustar_obj)
|
|
|
|
elif not tags:
|
2020-08-10 05:36:47 +02:00
|
|
|
# If enrichment report is empty and there are no tags, nothing to add to attribute
|
2020-08-10 05:29:37 +02:00
|
|
|
raise Exception("No relevant data found")
|
|
|
|
|
|
|
|
if tags:
|
|
|
|
for tag in tags:
|
|
|
|
self.misp_event.add_attribute_tag(tag, indicator)
|
2020-08-10 05:36:47 +02:00
|
|
|
|
2020-08-10 05:29:37 +02:00
|
|
|
except Exception as e:
|
|
|
|
misperrors['error'] += f" -- Error enriching attribute {indicator} -- {e}"
|
|
|
|
raise e
|
2020-06-22 04:52:17 +02:00
|
|
|
|
|
|
|
|
2020-06-26 00:22:54 +02:00
|
|
|
def handler(q=False):
|
|
|
|
"""
|
|
|
|
MISP handler function. A user's API key and secret will be retrieved from the MISP
|
|
|
|
request and used to create a TruSTAR API client. If enclave IDs are provided, only
|
|
|
|
those enclaves will be queried for data. Otherwise, all of the enclaves a user has
|
|
|
|
access to will be queried.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
|
|
|
|
request = json.loads(q)
|
|
|
|
|
|
|
|
config = request.get('config', {})
|
|
|
|
if not config.get('user_api_key') or not config.get('user_api_secret'):
|
|
|
|
misperrors['error'] = "Your TruSTAR API key and secret are required for indicator enrichment."
|
|
|
|
return misperrors
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-07-28 11:47:53 +02:00
|
|
|
if not request.get('attribute') or not check_input_attribute(request['attribute'], requirements=('type', 'value')):
|
|
|
|
return {'error': f'{standard_error_message}, {checking_error}.'}
|
2020-06-26 00:22:54 +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.'}
|
2020-06-26 00:22:54 +02:00
|
|
|
trustar_parser = TruSTARParser(attribute, config)
|
2020-08-10 05:36:47 +02:00
|
|
|
metadata = None
|
|
|
|
summary = None
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-06-26 00:22:54 +02:00
|
|
|
try:
|
2020-08-10 05:29:37 +02:00
|
|
|
metadata = trustar_parser.ts_client.get_indicators_metadata([Indicator(value=attribute['value'])])[0]
|
2020-12-04 20:59:57 +01:00
|
|
|
except IndexError:
|
|
|
|
misperrors['error'] += f" -- No metadata found for indicator {attribute['value']}"
|
2020-06-26 00:22:54 +02:00
|
|
|
except Exception as e:
|
2020-08-10 05:29:37 +02:00
|
|
|
misperrors['error'] += f" -- Could not retrieve indicator metadata from TruSTAR {e}"
|
|
|
|
|
|
|
|
try:
|
|
|
|
summary = list(
|
|
|
|
trustar_parser.ts_client.get_indicator_summaries([attribute['value']], page_size=MAX_PAGE_SIZE))[0]
|
2020-12-04 20:59:57 +01:00
|
|
|
except IndexError:
|
|
|
|
misperrors['error'] += f" -- No summary data found for indicator {attribute['value']}"
|
2020-08-10 05:29:37 +02:00
|
|
|
except Exception as e:
|
|
|
|
misperrors['error'] += f" -- Unable to retrieve TruSTAR summary data: {e}"
|
|
|
|
|
|
|
|
try:
|
|
|
|
trustar_parser.parse_indicator_summary(attribute['value'], summary, metadata)
|
|
|
|
except Exception:
|
2020-06-26 00:22:54 +02:00
|
|
|
return misperrors
|
2020-06-22 04:52:17 +02:00
|
|
|
|
2020-06-26 00:22:54 +02:00
|
|
|
return trustar_parser.get_results()
|
2020-06-22 23:54:37 +02:00
|
|
|
|
|
|
|
|
2020-06-26 00:22:54 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
2020-06-22 04:52:17 +02:00
|
|
|
|
|
|
|
|
2020-06-26 00:22:54 +02:00
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
|
|
|
return moduleinfo
|