2019-04-02 15:30:11 +02:00
|
|
|
import requests
|
|
|
|
import json
|
|
|
|
|
|
|
|
misperrors = {'error': 'Error'}
|
2019-04-02 16:01:33 +02:00
|
|
|
mispattributes = {'input': ['email-dst', 'email-src'], 'output': ['text']} # All mails as input
|
2019-04-02 15:30:11 +02:00
|
|
|
moduleinfo = {'version': '0.1', 'author': 'Aurélien Schwab', 'description': 'Module to access haveibeenpwned.com API.', 'module-type': ['hover']}
|
2019-04-02 16:01:33 +02:00
|
|
|
moduleconfig = ['user-agent'] # TODO take this into account in the code
|
2019-04-02 15:30:11 +02:00
|
|
|
|
|
|
|
haveibeenpwned_api_url = 'https://api.haveibeenpwned.com/api/v2/breachedaccount/'
|
2019-04-02 16:01:33 +02:00
|
|
|
default_user_agent = 'MISP-Module' # User agent (must be set, requiered by API))
|
|
|
|
|
2019-04-02 15:30:11 +02:00
|
|
|
|
|
|
|
def handler(q=False):
|
|
|
|
if q is False:
|
|
|
|
return False
|
|
|
|
request = json.loads(q)
|
|
|
|
for input_type in mispattributes['input']:
|
|
|
|
if input_type in request:
|
|
|
|
email = request[input_type]
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
misperrors['error'] = "Unsupported attributes type"
|
|
|
|
return misperrors
|
|
|
|
|
2019-04-02 16:01:33 +02:00
|
|
|
r = requests.get(haveibeenpwned_api_url + email, headers={'user-agent': default_user_agent}) # Real request
|
|
|
|
if r.status_code == 200: # OK (record found)
|
2019-04-02 15:30:11 +02:00
|
|
|
breaches = json.loads(r.text)
|
|
|
|
if breaches:
|
|
|
|
return {'results': [{'types': mispattributes['output'], 'values': breaches}]}
|
2019-04-02 16:01:33 +02:00
|
|
|
elif r.status_code == 404: # Not found (not an error)
|
2019-04-02 15:30:11 +02:00
|
|
|
return {'results': [{'types': mispattributes['output'], 'values': 'OK (Not Found)'}]}
|
2019-04-02 16:01:33 +02:00
|
|
|
else: # Real error
|
2019-04-02 15:30:11 +02:00
|
|
|
misperrors['error'] = 'haveibeenpwned.com API not accessible (HTTP ' + str(r.status_code) + ')'
|
|
|
|
return misperrors['error']
|
|
|
|
|
2019-04-02 16:01:33 +02:00
|
|
|
|
2019-04-02 15:30:11 +02:00
|
|
|
def introspection():
|
|
|
|
return mispattributes
|
|
|
|
|
2019-04-02 16:01:33 +02:00
|
|
|
|
2019-04-02 15:30:11 +02:00
|
|
|
def version():
|
|
|
|
moduleinfo['config'] = moduleconfig
|
2019-04-02 15:39:27 +02:00
|
|
|
return moduleinfo
|