2016-05-01 12:09:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import json
|
2019-01-21 13:31:52 +01:00
|
|
|
from pyipasnhistory import IPASNHistory
|
2020-01-10 15:02:59 +01:00
|
|
|
from pymisp import MISPAttribute, MISPEvent, MISPObject
|
2016-05-01 12:09:33 +02:00
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
2020-01-10 15:02:59 +01:00
|
|
|
mispattributes = {'input': ['ip-src', 'ip-dst'], 'format': 'misp_standard'}
|
2020-01-10 15:37:54 +01:00
|
|
|
moduleinfo = {'version': '0.2', 'author': 'Raphaël Vinot',
|
2016-05-01 12:09:33 +02:00
|
|
|
'description': 'Query an IP ASN history service (https://github.com/CIRCL/IP-ASN-history.git)',
|
|
|
|
'module-type': ['expansion', 'hover']}
|
|
|
|
|
|
|
|
|
2020-01-10 15:02:59 +01:00
|
|
|
def parse_result(attribute, values):
|
|
|
|
event = MISPEvent()
|
|
|
|
initial_attribute = MISPAttribute()
|
|
|
|
initial_attribute.from_dict(**attribute)
|
|
|
|
event.add_attribute(**initial_attribute)
|
|
|
|
mapping = {'asn': ('AS', 'asn'), 'prefix': ('ip-src', 'subnet-announced')}
|
|
|
|
print(values)
|
|
|
|
for last_seen, response in values['response'].items():
|
|
|
|
asn = MISPObject('asn')
|
|
|
|
asn.add_attribute('last-seen', **{'type': 'datetime', 'value': last_seen})
|
|
|
|
for feature, attribute_fields in mapping.items():
|
|
|
|
attribute_type, object_relation = attribute_fields
|
|
|
|
asn.add_attribute(object_relation, **{'type': attribute_type, 'value': response[feature]})
|
|
|
|
asn.add_reference(initial_attribute.uuid, 'related-to')
|
|
|
|
event.add_object(**asn)
|
|
|
|
event = json.loads(event.to_json())
|
|
|
|
return {key: event[key] for key in ('Attribute', 'Object')}
|
|
|
|
|
|
|
|
|
2016-05-01 12:09:33 +02:00
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
2020-01-10 15:02:59 +01:00
|
|
|
if request.get('attribute') and request['attribute'].get('type') in mispattributes['input']:
|
|
|
|
toquery = request['attribute']['value']
|
2016-05-01 12:09:33 +02:00
|
|
|
else:
|
|
|
|
misperrors['error'] = "Unsupported attributes type"
|
|
|
|
return misperrors
|
|
|
|
|
2019-01-21 13:31:52 +01:00
|
|
|
ipasn = IPASNHistory()
|
|
|
|
values = ipasn.query(toquery)
|
2016-05-04 12:52:01 +02:00
|
|
|
|
2016-05-01 12:09:33 +02:00
|
|
|
if not values:
|
|
|
|
misperrors['error'] = 'Unable to find the history of this IP'
|
|
|
|
return misperrors
|
2020-01-10 15:02:59 +01:00
|
|
|
return {'results': parse_result(request['attribute'], values)}
|
2016-05-01 12:09:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
|
|
|
|
|
|
|
def version():
|
|
|
|
return moduleinfo
|