-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
396 lines (308 loc) · 11.1 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/python
from time import sleep
from secrets import SystemRandom
from re import compile as regex_compile, IGNORECASE as regex_IGNORECASE, MULTILINE as regex_MULTILINE
from requests import Session, packages, adapters
from requests.adapters import TimeoutSauce
from requests.exceptions import Timeout
MAX_TIMEOUT_SECONDS = 6
DOMAINS_FILE = 'domains.txt'
INCLUDE_SUBDOMAINS = False
class CustomTimeout(TimeoutSauce):
def __init__(self, *args, **kwargs):
if kwargs["connect"] is None:
kwargs["connect"] = MAX_TIMEOUT_SECONDS
if kwargs["read"] is None:
kwargs["read"] = MAX_TIMEOUT_SECONDS
super().__init__(*args, **kwargs)
adapters.TimeoutSauce = CustomTimeout
#stackoverflow.com/questions/45267003/python-requests-hanging-freezing
def rand_int(min_:int, max_:int):
rng = SystemRandom()
return rng.randrange(min_, max_ + 1)
def get_headers():
# Try to get the most white-listed user-agent
random_platform = rand_int(1, 3)
is_mobile = False
if random_platform == 1:
platform = 'Windows'
elif random_platform == 2:
platform = 'macOS'
else:
platform = 'iOS'
is_mobile = True
if platform == 'Windows':
rand_win_vers = rand_int(1, 3)
if rand_win_vers == 1: #8.1
win_ver = '6.3'
platform_version = '0.3.0'
elif rand_win_vers == 2: #10
win_ver = '10.0'
r_major = rand_int(1, 3)
if r_major == 1:
platform_version = '7.0.0' #Windows 10 1809
elif r_major == 2:
platform_version = '8.0.0' #Windows 10 1903 | 1909
elif r_major == 3:
platform_version = '10.0.0' #Windows 10 2004 | 20H2 | 21H1
elif rand_win_vers == 3: #11
win_ver = '10.0'
platform_version = '15.0.0'
else:
win_ver = '10.0' #11
platform_version = '15.0.0'
chrome_agent = f"Mozilla/5.0 (Windows NT {win_ver}; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
elif platform == 'macOS':
r_macos = rand_int(1, 4)
if r_macos == 1:
macos_version = '10_15_7'
r_big = rand_int(1, 2)
if r_big == 1:
platform_version = '12.5.1' #monterey
else:
platform_version = '11.6.2' #big sur
elif r_macos == 2:
macos_version = '10_15_6' #Catalina
platform_version = '10.15.6'
elif r_macos == 3:
macos_version = '10_14_6' #Mojave
platform_version = '10.14.6'
elif r_macos == 4:
macos_version = '10_13_6' #High Sierra
platform_version = '10.13.6'
else:
macos_version = '10_15_7'
platform_version = '12.5.1'
chrome_agent = f"Mozilla/5.0 (Macintosh; Intel Mac OS X {macos_version}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36"
else:
r_device = rand_int(1, 2)
if r_device == 1:
ios_device = 'iPhone; CPU iPhone'
else:
ios_device = 'iPad; CPU'
r_ios = rand_int(1, 4)
if r_ios == 1:
ios_version = '16_1'
elif r_ios == 2:
ios_version = '16_0'
elif r_ios == 3:
ios_version = '15_7'
elif r_ios == 4:
ios_version = '15_6'
else:
ios_version = '16_1'
chrome_agent = f"Mozilla/5.0 ({ios_device} OS {ios_version} like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/119.0.6045.109 Mobile/15E148 Safari/604.1"
if not is_mobile:
c_headers = {'Upgrade-Insecure-Requests': '1',
'User-Agent': chrome_agent,
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
'sec-ch-ua': '"Google Chrome";v="109", "Chromium";v="109", "Not=A?Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-full-version': '"109.0.5414.94"',
'sec-ch-ua-arch': '"x86"',
'sec-ch-ua-platform': f'"{platform}"',
'sec-ch-ua-platform-version': f'"{platform_version}"',
'sec-ch-ua-model': '""',
'sec-ch-ua-bitness': '"64"',
'sec-ch-ua-wow64': '?0',
'sec-ch-ua-full-version-list': '"Google Chrome";v="109.0.5414.94", "Chromium";v="109.0.5414.94", "Not=A?Brand";v="24.0.0.0"',
'sec-ch-prefers-color-scheme': 'light',
'Sec-Fetch-Site': 'none',
'Sec-Fetch-Mode': 'navigate',
'Sec-Fetch-User': '?1',
'Sec-Fetch-Dest': 'document',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'en-US,en;q=0.9'}
else:
c_headers = {'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate',
'User-Agent': chrome_agent}
return c_headers
def do_get(session, get_url:str, proxy:dict, headers:dict):
if headers:
session.headers.update(headers)
try:
resp = session.get(get_url, verify=False, allow_redirects=True, proxies=proxy)
if resp.status_code == 429: #Too Many Requests
retry_after = int(resp.headers.get('Retry-After'))
print(f'[-] Retry-After: {str(retry_after)} - {get_url}')
sleep(retry_after + 10)
resp = session.get(get_url, verify=False, allow_redirects=True, proxies=proxy)
if resp.status_code == 200:
return resp
except ConnectionError:
print(f'[-] {get_url} (ConnectionError)')
except Timeout:
print(f'[-] {get_url} (Timeout)')
except Exception as e:
print(f'[-] {str(e)} on: {get_url}')
return False
def get_page_links(base_url:str, domain:str, resp, disallowed:tuple, regex_href):
# Search only on html pages.
content_type = resp.headers.get('Content-Type')
if content_type is None or content_type.startswith('text/html') is False:
return False
links = set()
regex_links = regex_href.findall(resp.text)
for href in regex_links:
href = href[1]
if href == "" or href is None or href == '#':
continue
#Full link
if href.startswith((base_url)):
links.add(href)
continue
#Absolute link
if href.startswith("/"):
#Check if it's Disallowed in robots.txt
if href.startswith(disallowed):
continue
links.add(base_url + href)
continue
if INCLUDE_SUBDOMAINS:
#Check if it's really current domain subdomain
sp1 = href.split('://')
if len(sp1) != 1:
sp2 = sp1[1].split('/')
if len(sp2) != 1:
hostname = sp2[0]
sp3 = hostname.split('.' + domain)
if len(sp3) != 1: #subdomain = sp3[0]
links.add(href)
return list(links)
def extract_emails(file, regex_emails, text:str, domain_emails:set, tlds):
all_emails = set(regex_emails.findall(text))
parsend_emails = set()
for email in all_emails:
email = email.lower()
if email not in domain_emails:
if tlds:
last_dot = email.rfind('.')
if not last_dot:
continue
tld = email[last_dot + 1:]
if tld not in tlds:
continue
file.write(email + '\n')
parsend_emails.add(email)
file.flush()
return parsend_emails
def parse_domain(domain:str, regex_emails, regex_robots, regex_href, headers:dict, tlds):
session = Session()
#Firstly try to crawl via HTTP, as it's faster
base_url = f'http://{domain}'
main_resp = do_get(session, base_url, None, headers)
if main_resp:
if main_resp.url.startswith('https://'):
base_url = f'https://{domain}'
else:
base_url = f'https://{domain}'
main_resp = do_get(session, base_url, None, headers)
if main_resp is False:
return False
resp = do_get(session, base_url + '/robots.txt', None, headers)
disallowed = tuple()
if resp:
#Parse robots.txt
r_entries = regex_robots.findall(resp.text)
for e in r_entries:
url = e.strip()
if url.endswith('?') or url.endswith('$') or url.endswith('*'):
url = url[:-1]
if url not in disallowed:
disallowed += (url, )
resp = None
links_visited = set()
domain_emails = set()
links = get_page_links(base_url, domain, main_resp, disallowed, regex_href)
try:
f = open(f'{domain}_emails.txt', 'w')
except Exception as e:
print(f'[-] {str(e)}')
return False
if links:
for link in links:
if link in links_visited:
continue
print(f'[+] {link}')
resp = do_get(session, link, None, headers)
links_visited.add(link)
if resp:
new_list = get_page_links(base_url, domain, resp, disallowed, regex_href)
if new_list:
links.extend(new_list)
domain_emails.update(extract_emails(f, regex_emails, resp.text, domain_emails, tlds))
else:
#If page has no href links and it's an html page then search in it too.
content_type = resp.headers.get('Content-Type')
if content_type and content_type.startswith('text/html'):
domain_emails.update(extract_emails(f, regex_emails, resp.text, domain_emails, tlds))
else:
#If main page has no href links and it's an html page then search in it too.
content_type = main_resp.headers.get('Content-Type')
if content_type and content_type.startswith('text/html'):
domain_emails.update(extract_emails(f, regex_emails, main_resp.text, domain_emails, tlds))
f.close()
print(f'[+] Extracted {len(domain_emails)} emails from {domain}')
return True
def get_iana_tlds(regex_href):
session = Session()
iana_path = '/domains/root/db'
resp = do_get(session, f'http://www.iana.org{iana_path}', None, get_headers())
if not resp:
return False
regex_links = regex_href.findall(resp.text)
iana_len = len(iana_path) + 1
if not regex_links:
return False
tlds = set()
for href in regex_links:
href = href[1]
if href == "" or href is None or href == '#':
continue
if not href.startswith(iana_path + '/'):
continue
tld_html = href[iana_len:]
# just to be sure
if not tld_html.endswith('.html'):
continue
# 5 = len('.html')
tld = tld_html[:-5]
tlds.add(tld)
if tlds:
return tlds
return False
def main():
try:
with open(DOMAINS_FILE, 'r') as f:
domains = f.read().splitlines()
except FileNotFoundError:
print(f'[-] File {DOMAINS_FILE} not found!')
return
except PermissionError:
print(f'[-] Insufficient permission to read {DOMAINS_FILE}!')
return
if len(domains) == 0:
print(f'[-] File {DOMAINS_FILE} is empty!')
return
unique_domains = set()
for domain in domains:
if '.' in domain:
unique_domains.add(domain)
if len(unique_domains) == 0:
print(f'[-] File {DOMAINS_FILE} contains no domains!')
return
packages.urllib3.disable_warnings()
regex_pattern = regex_compile("\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\b", regex_IGNORECASE) #regular-expressions.info/email.html
regex_robots = regex_compile("^Disallow[ ]*:(.*)", regex_MULTILINE)
regex_href = regex_compile("<a\\s+(?:[^>]*?\\s+)?href=([\"'])(.*?)\\1")
tlds = get_iana_tlds(regex_href)
for domain in unique_domains:
parse_domain(domain, regex_pattern, regex_robots, regex_href, get_headers(), tlds)
return
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
exit("Bye")