-
Notifications
You must be signed in to change notification settings - Fork 8
/
fortune_http_unseen.py
executable file
·54 lines (38 loc) · 1.48 KB
/
fortune_http_unseen.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
#!/usr/bin/env python3
"""Grab sites which will not listed in search engines"""
import re
from fire import Fire
from lib.files import LOCAL_DIR
from lib.net import HTTPConnection
from lib.scan import generate_ips, process_each
from lib.models import add_result
LOG_FILE = LOCAL_DIR / 'http_unseen.txt'
DISALLOW_RE = re.compile(
r'^User-agent:\s+\*$[\n\r]+^Disallow:\s+/$', re.IGNORECASE | re.MULTILINE)
TITLE_RE = re.compile(r'<title[^>]*>([^<]+)', re.IGNORECASE)
H1_RE = re.compile(r'<h1[^>]*>([^<]+)', re.IGNORECASE)
def check_host(ip, lock):
with HTTPConnection(ip, 80, timeout=1.5) as c:
response = c.get('/robots.txt')
if not response.ok:
return
if not DISALLOW_RE.findall(response.body):
return
# coz connection closes often after 1st request
with HTTPConnection(ip, 80) as c:
response = c.get('/')
page = response.body
t_match = TITLE_RE.findall(page)
h_match = H1_RE.findall(page)
title = t_match[0] if t_match else h_match[0] if h_match else '-'
title = title.strip().replace('\n', ' ').replace('\r', '')
with lock:
print(ip, title)
banner = response.headers_str
add_result(ip, 80, title, ['fortune', 'unseen'], banner)
with LOG_FILE.open('a') as f:
f.write('%s %s\n' % (ip, title))
def main(c=10_000_000, w=16):
process_each(check_host, generate_ips(c), w)
if __name__ == "__main__":
Fire(main)