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

77 lines
2.3 KiB
Python
Raw Normal View History

2016-04-28 14:21:39 +02:00
# -*- coding: utf-8 -*-
import json
from pyeupi import PyEUPI
misperrors = {'error': 'Error'}
mispattributes = {'input': ['hostname', 'domain', 'url'], 'output': ['freetext']}
moduleinfo = {'version': '0.1', 'author': 'Raphaël Vinot',
'description': 'Query the Phishing Initiative service (https://phishing-initiative.lu)',
'module-type': ['expansion', 'hover']}
moduleconfig = ['apikey', 'url']
def handler(q=False):
if q is False:
return False
request = json.loads(q)
if request.get('hostname'):
toquery = request['hostname']
elif request.get('domain'):
toquery = request['domain']
elif request.get('url'):
toquery = request['url']
else:
misperrors['error'] = "Unsupported attributes type"
return misperrors
if not request.get('config') and not (request['config'].get('apikey') and request['config'].et('url')):
2016-06-01 16:19:41 +02:00
misperrors['error'] = 'EUPI authentication is missing'
2016-04-28 14:21:39 +02:00
return misperrors
2016-06-01 16:19:41 +02:00
pyeupi = PyEUPI(request['config']['apikey'], request['config']['url'])
if 'event_id' in request:
return handle_expansion(pyeupi, toquery)
else:
return handle_hover(pyeupi, toquery)
def handle_expansion(pyeupi, url):
results = pyeupi.search_url(url=url)
2016-04-28 14:21:39 +02:00
if results.get('results'):
to_return = ''
for r in results['results']:
if r['tag_label'] != 'phishing':
continue
2016-04-28 16:31:55 +02:00
to_return += ' {} {} {} '.format(r['url'], r['domain'], r['ip_address'])
2016-05-13 11:44:35 +02:00
if to_return:
return {'results': [{'types': mispattributes['output'], 'values': to_return}]}
2016-05-13 11:56:12 +02:00
else:
2016-06-01 16:19:41 +02:00
misperrors['error'] = 'Unknown in the EUPI service'
2016-05-13 11:56:12 +02:00
return misperrors
2016-06-01 16:19:41 +02:00
else:
return {'results': [{'types': mispattributes['output'], 'values': ''}]}
2016-06-01 16:19:41 +02:00
def handle_hover(pyeupi, url):
try:
result = pyeupi.lookup(url=url)['results'][0]
except (KeyError, IndexError):
misperrors['error'] = 'Error in EUPI lookup'
return misperrors
return {'results': [{'types': mispattributes['output'],
'values': result['tag_label'].title()}]}
2016-04-28 14:21:39 +02:00
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo