forked from MISP/misp-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hibp.py
50 lines (40 loc) · 1.74 KB
/
hibp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# -*- coding: utf-8 -*-
import requests
import json
misperrors = {'error': 'Error'}
mispattributes = {'input': ['email-dst', 'email-src'], 'output': ['text']}
moduleinfo = {'version': '0.2', 'author': 'Corsin Camichel, Aurélien Schwab', 'description': 'Module to access haveibeenpwned.com API (v3).', 'module-type': ['hover']}
moduleconfig = ['api_key']
haveibeenpwned_api_url = 'https://haveibeenpwned.com/api/v3/breachedaccount/'
API_KEY = "" # details at https://www.troyhunt.com/authentication-and-the-have-i-been-pwned-api/
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
if request.get('config') is None or request['config'].get('api_key') is None:
misperrors['error'] = 'Have I Been Pwned authentication is incomplete (no API key)'
return misperrors
else:
API_KEY = request['config'].get('api_key')
r = requests.get(haveibeenpwned_api_url + email, headers={'hibp-api-key': API_KEY})
if r.status_code == 200:
breaches = json.loads(r.text)
if breaches:
return {'results': [{'types': mispattributes['output'], 'values': breaches}]}
elif r.status_code == 404:
return {'results': [{'types': mispattributes['output'], 'values': 'OK (Not Found)'}]}
else:
misperrors['error'] = f'haveibeenpwned.com API not accessible (HTTP {str(r.status_code)})'
return misperrors['error']
def introspection():
return mispattributes
def version():
moduleinfo['config'] = moduleconfig
return moduleinfo