2019-04-16 17:55:16 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding:utf-8 -*-
|
|
|
|
|
|
|
|
import utils
|
|
|
|
from time import sleep
|
|
|
|
from urllib import parse
|
|
|
|
from requests import codes, exceptions
|
|
|
|
|
|
|
|
def haveibeenpwned(string, entities):
|
|
|
|
emails = []
|
2019-04-27 07:12:04 +03:00
|
|
|
|
2019-04-16 17:55:16 +03:00
|
|
|
for item in entities:
|
|
|
|
if item['entity'] == 'email':
|
|
|
|
emails.append(item['resolution']['value'])
|
2019-04-27 07:12:04 +03:00
|
|
|
|
2019-04-16 17:55:16 +03:00
|
|
|
if not emails:
|
|
|
|
emails = utils.config('emails')
|
2019-04-27 07:12:04 +03:00
|
|
|
|
2019-04-16 17:55:16 +03:00
|
|
|
if not emails:
|
2019-04-17 14:00:54 +03:00
|
|
|
return utils.output('end', 'no-email', utils.translate('no-email'))
|
2019-04-27 07:12:04 +03:00
|
|
|
|
2019-04-17 14:00:54 +03:00
|
|
|
utils.output('inter', 'checking', utils.translate('checking'))
|
2019-04-16 17:55:16 +03:00
|
|
|
|
|
|
|
for index, email in enumerate(emails):
|
|
|
|
isLastEmail = index == len(emails) - 1
|
|
|
|
breached = checkForBreach(email)
|
|
|
|
data = { 'email': email }
|
|
|
|
|
2019-04-27 07:12:04 +03:00
|
|
|
# Have I Been Pwned API returns a 403 when accessed by unauthorized/banned clients
|
2019-04-17 17:40:16 +03:00
|
|
|
if breached == 403:
|
2019-04-27 07:12:04 +03:00
|
|
|
return utils.output('end', 'blocked', utils.translate('blocked', { 'website_name': 'Have I Been Pwned' }))
|
2019-05-01 11:00:01 +03:00
|
|
|
elif breached == 503:
|
|
|
|
return utils.output('end', 'blocked', utils.translate('unavailable', { 'website_name': 'Have I Been Pwned' }))
|
2019-04-17 17:40:16 +03:00
|
|
|
elif not breached:
|
2019-04-16 17:55:16 +03:00
|
|
|
if isLastEmail:
|
2019-04-17 14:00:54 +03:00
|
|
|
return utils.output('end', 'no-pwnage', utils.translate('no-pwnage', data))
|
2019-04-16 17:55:16 +03:00
|
|
|
else:
|
|
|
|
utils.output('inter', 'no-pwnage', utils.translate('no-pwnage', data))
|
|
|
|
else:
|
2019-04-27 07:12:04 +03:00
|
|
|
data['result'] = ''
|
|
|
|
|
|
|
|
for index, b in enumerate(breached):
|
|
|
|
data['result'] += utils.translate('list_element', {
|
|
|
|
'url': 'http://' + b['Domain'],
|
|
|
|
'name': b['Name'],
|
|
|
|
'total': b['PwnCount']
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-04-16 17:55:16 +03:00
|
|
|
if isLastEmail:
|
2019-04-17 14:00:54 +03:00
|
|
|
return utils.output('end', 'pwned', utils.translate('pwned', data))
|
2019-04-16 17:55:16 +03:00
|
|
|
else:
|
|
|
|
utils.output('inter', 'pwned', utils.translate('pwned', data))
|
|
|
|
|
|
|
|
def checkForBreach(email):
|
|
|
|
# Delay for 2 seconds before making request to accomodate API usage policy
|
|
|
|
sleep(2)
|
|
|
|
truncate = '?truncateResponse=true'
|
2019-04-27 07:12:04 +03:00
|
|
|
url = 'https://haveibeenpwned.com/api/v2/breachedaccount/' + parse.quote_plus(email)
|
|
|
|
|
2019-04-16 17:55:16 +03:00
|
|
|
try:
|
|
|
|
response = utils.http('GET', url)
|
|
|
|
|
|
|
|
if response.status_code == 404:
|
|
|
|
return None
|
2019-04-17 17:40:16 +03:00
|
|
|
elif response.status_code == 200:
|
2019-04-27 07:12:04 +03:00
|
|
|
return response.json()
|
|
|
|
|
2019-04-17 17:40:16 +03:00
|
|
|
return response.status_code
|
2019-04-16 17:55:16 +03:00
|
|
|
except exceptions.RequestException as e:
|
2019-04-27 07:12:04 +03:00
|
|
|
return utils.output('end', 'down', utils.translate('errors', { 'website_name': 'Have I Been Pwned' }))
|