-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhibp_check.py
executable file
·196 lines (176 loc) · 6.21 KB
/
hibp_check.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/env python3
#
# author: https://github.com/m0nkeyplay
# August 26, 2019 - original script written
# Updated April 23, 2020 - see README for updates
# Updated January 12, 2020 - because I saw typos and managed to change the way text is printee
# Free to use
# Free to modify and make better
# ./hibp_check.py -h for help
# Use of the have i been pwned API now requires a key that must be paid for
# I am not requiring a key, but the script won't run without it.
# Get a key here and learn about why it's required here:
# https://haveibeenpwned.com/API/Key
# Paste it in below headers['hibp-api-key']=''
import requests
import json
import os
import time
import datetime
import signal
import argparse
ap = argparse.ArgumentParser()
ap.add_argument("-b", "--breach", action="store_true")
ap.add_argument("-p", "--paste", action="store_true")
ap.add_argument("-e", "--email", required=False, help="Search for just one email")
ap.add_argument("-f", "--file", required=False, help="grab emails from a list of files -f /path/to/file")
args = vars(ap.parse_args())
# CTRL+C handler - from https:/gist.github.com/mikerr/6389549
def handler(signum, frame):
print("\n^^^^^^Task aborted by user. Some cleanup may be necessary.")
exit(0)
signal.signal(signal.SIGINT,handler)
# Banner Info
def show_banner():
print('####################################################################')
print('### have i been pwned Quick Check ###')
print('### ###')
print('### Usage: hibp_check.py --breach|--paste -e email|-f textFile ###')
print('### ###')
print('### Use and modify at will. ###')
print('### All data from: ###')
print('### https://haveibeenpwned.com/ ###')
print('### ###')
print('### *Note that using a file will take some time since the ###')
print('### API we are calling is rate limited. ###')
print('####################################################################')
# Short Help Menu
def show_help():
print('\n::Help with argument usage::\n')
print('Which check do you want to do? breach or paste')
print('Are you searching one or many emails?')
print('-e or -f with textFile having one email per line')
print('$: Breach+Email: hibp_check_.py -b -e [email protected]')
print('$: Breach+List of emails: hibp_check_.py -b -f ./path/to/file')
# Text we are going to use
headerError = """
ERROR: Setup still required.
Please register an API key to start using this script.
Register @ %s
"""
fileError = """
We can't find/open %s.
Please check that it's a valid file.
"""
dataError = "Error: <%s> %s"
breachData = """
Account: %s
Breach: %s
Sensitive: %s
Domain: %sBreach Date:%s
"""
pasteData = """
Paste Source: %s
ID: %s
Date: %s
"""
if args['breach']:
hibpCheck = 'breachaccount'
elif args['paste']:
hibpCheck = 'pasteaccount'
else:
show_banner()
show_help()
exit()
if args['email']:
chkType = 'email'
chkIt = args['email']
elif args['file']:
chkType = 'file'
chkIt = args['file']
else:
show_banner()
show_help()
exit()
# Hopefully got through the arg checks, let's build this check
headers = {}
headers['content-type']= 'application/json'
headers['api-version']= '3'
headers['User-Agent']='the-monkey-playground-script'
# Place that API key here
headers['hibp-api-key']='https://haveibeenpwned.com/API/Key'
# Check Breach
def check_breach(eml):
url = 'https://haveibeenpwned.com/api/v3/breachedaccount/'+eml+'?truncateResponse=false'
r = requests.get(url, headers=headers)
if r.status_code == 404:
print("%s not found in a breach."%eml)
elif r.status_code == 200:
data = r.json()
print('Breach Check for: %s'%eml)
for d in data:
# Simple info
breach = d['Name']
domain = d['Domain']
breachDate = d['BreachDate']
sensitive = d['IsSensitive']
print(breachData%(eml,breach,sensitive,domain,breachDate))
# or to print out the whole shebang comment above and uncomment below
#for k,v in d.items():
# print(k+":"+str(v))
else:
data = r.json()
print(dataError%(str(r.status_code),data['message']))
exit()
# Check Paste
def check_paste(eml):
url = 'https://haveibeenpwned.com/api/v3/pasteaccount/'+eml
print(url)
r = requests.get(url, headers=headers)
if r.status_code == 404:
print("%s not found in a breach."%eml)
elif r.status_code == 200:
data = r.json()
print('Paste Check for: %s'%eml)
for d in data:
source = d['Source']
id = str(d['Id'])
pasteDate = d['Date']
# Uncomment and add these if you like
#title = str(d['Title'])
#EmailCount = str(d['EmailCount'])
print(pasteData%(source,id,pasteDate))
else:
data = r.json()
print(dataError%(str(r.status_code),data['message']))
exit()
# Get started
if __name__ == '__main__':
show_banner()
# Single Checks
if headers['hibp-api-key']=='https://haveibeenpwned.com/API/Key':
print(headerError%headers['hibp-api-key'])
exit()
if chkType == 'email':
if hibpCheck == 'breachaccount':
check_breach(chkIt)
else:
check_paste(chkIt)
# File Checks
elif chkType == 'file':
if not os.path.isfile(chkIt):
print(fileError%chkIt)
else:
get_emails = open(chkIt, 'r')
for line in get_emails:
cleanEmail = line.strip()
if hibpCheck == 'breachaccount':
check_breach(cleanEmail)
time.sleep(2)
else:
check_paste(cleanEmail)
time.sleep(2)
get_emails.close()
# Something really interesting happened
else:
print('We in trouble. We should not be here.')