misp-modules/misp_modules/modules/expansion/onyphe.py

101 lines
3.2 KiB
Python
Raw Normal View History

2018-06-08 16:38:41 +02:00
# -*- coding: utf-8 -*-
import json
2021-04-22 15:05:29 +02:00
2021-04-23 16:02:21 +02:00
from pymisp import MISPEvent, MISPObject
2021-04-22 15:05:29 +02:00
2018-06-08 16:38:41 +02:00
try:
from onyphe import Onyphe
except ImportError:
print("pyonyphe module not installed.")
misperrors = {'error': 'Error'}
2018-12-11 15:29:09 +01:00
mispattributes = {'input': ['ip-src', 'ip-dst', 'hostname', 'domain'],
2021-04-22 15:05:29 +02:00
'output': ['hostname', 'domain', 'ip-src', 'ip-dst', 'url'],
'format': 'misp_standard'}
2018-06-08 16:38:41 +02:00
# possible module-types: 'expansion', 'hover' or both
2021-04-22 15:05:29 +02:00
moduleinfo = {'version': '2', 'author': 'Sebastien Larinier @sebdraven',
2018-06-08 16:38:41 +02:00
'description': 'Query on Onyphe',
'module-type': ['expansion', 'hover']}
# config fields that your code expects from the site admin
moduleconfig = ['apikey']
2021-04-22 15:05:29 +02:00
class OnypheClient:
def __init__(self, api_key, attribute):
self.onyphe_client = Onyphe(api_key=api_key)
self.attribute = attribute
self.misp_event = MISPEvent()
self.misp_event.add_attribute(**attribute)
def get_results(self):
event = json.loads(self.misp_event.to_json())
results = {key: event[key] for key in ('Attribute', 'Object') if key in event}
return results
2021-04-23 16:02:21 +02:00
def get_query_onyphe(self):
2021-04-23 16:11:10 +02:00
if self.attribute['type'] == 'ip-src' or self.attribute['type'] == 'ip-dst':
2021-04-23 16:02:21 +02:00
self.__summary_ip()
def __summary_ip(self):
results = self.onyphe_client.summary_ip(self.attribute['value'])
if 'results' in results:
for r in results['results']:
2021-04-23 16:13:32 +02:00
print(r)
2021-04-23 16:02:21 +02:00
domain = r['domain']
if type(domain) == list:
for d in domain:
self.__get_object_domain_ip(d, 'domain')
elif type(domain) == str:
self.__get_object_domain_ip(domain, 'domain')
def __get_object_domain_ip(self, obs, relation):
objet_domain_ip = MISPObject('domain-ip')
objet_domain_ip.add_attribute(relation, obs)
relation_attr = self.__get_relation_attribute()
if relation_attr:
objet_domain_ip.add_attribute(relation, self.attribute['value'])
objet_domain_ip.add_reference(self.attribute['uuid'], 'related-to')
self.misp_event.add_object(objet_domain_ip)
def __get_relation_attribute(self):
if self.attribute['type'] == 'ip-src':
return 'ip'
elif self.attribute['type'] == 'ip-dest':
return 'ip'
elif self.attribute['type'] == 'domain':
return 'domain'
elif self.attribute['type'] == 'hostname':
return 'domain'
2021-04-22 15:05:29 +02:00
2018-06-08 16:38:41 +02:00
def handler(q=False):
2018-06-11 13:34:45 +02:00
if q:
2018-06-08 16:38:41 +02:00
2018-06-11 13:34:45 +02:00
request = json.loads(q)
2021-04-22 15:05:29 +02:00
attribute = request['attribute']
2018-06-08 16:38:41 +02:00
if not request.get('config') or not request['config'].get('apikey'):
2018-06-11 13:34:45 +02:00
misperrors['error'] = 'Onyphe authentication is missing'
return misperrors
2018-06-08 16:38:41 +02:00
2021-04-23 16:02:21 +02:00
api_key = request['config'].get('apikey')
onyphe_client = OnypheClient(api_key, attribute)
onyphe_client.get_query_onyphe()
results = onyphe_client.get_results()
return {'results': results}
2018-06-08 16:38:41 +02:00
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
2018-11-11 15:49:14 +01:00
return moduleinfo