-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhaveIBeenPwned.py
More file actions
56 lines (52 loc) · 1.88 KB
/
haveIBeenPwned.py
File metadata and controls
56 lines (52 loc) · 1.88 KB
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
51
52
53
54
55
56
#HaveIBeenPwned:
import json, csv, sys, time
from urllib import request, parse
from urllib.error import HTTPError, URLError
headers = {"Accept": "application/vnd.haveibeenpwned.v2+json",
"User-Agent": "Pwnage-Checker-Python"}
uri = "https://haveibeenpwned.com/api/v2/breachedaccount/{0}"
#Output the following: mail, pwnedCount, pwnedDomains, pwnedDates, rawJSON
def prepareRow(mail, pwnedList):
rowDict = {'email' : mail}
rowDict['pwnedCount'] = len(pwnedList)
rowDict['rawJSON'] = pwnedList
pwnedDomains = []
pwnedDates = []
for pwnedItem in pwnedList:
pwnedDomains.append(pwnedItem['Domain'])
pwnedDates.append(pwnedItem['BreachDate'])
rowDict['pwnedDomains'] = '[{0}]'.format(','.join(pwnedDomains))
rowDict['pwnedDates'] = '[{0}]'.format(','.join(pwnedDates))
return rowDict
def checkPwned(mail, output=None):
pwnedRequest = request.Request(uri.format(mail), headers=headers)
try:
response = request.urlopen(pwnedRequest)
pwnedInfo = json.loads(response.read().decode())
print ("Pwned count for ({0}): {1} ".format(mail, len(pwnedInfo)))
if (output != None):
rowVal = prepareRow(mail, pwnedInfo)
output.writerow(rowVal)
except HTTPError as err:
if (err.code == 404):
print("Pwned count for ({0}): 0".format(mail))
else:
print("Error while retrieving data for {0}".format(mail))
if (len(sys.argv) < 2):
print("Usage: python haveIBeenPwned.py <email>")
print(" python haveIBeenPwned.py -f <list.csv>")
sys.exit()
if (sys.argv[1] != '-f'):
mail = sys.argv[1]
checkPwned(mail)
else:
with open(sys.argv[2]) as csvfile:
csvfileOut = open("output.csv", 'w')
csvreader = csv.reader(csvfile, delimiter=',', quotechar='"')
fieldNames = ['email', 'pwnedCount', 'pwnedDomains', 'pwnedDates', 'rawJSON']
csvwriter = csv.DictWriter(csvfileOut, fieldNames)
csvwriter.writeheader()
for row in csvreader:
mail = row[0]
checkPwned(mail, output=csvwriter)
time.sleep(2)