-
Notifications
You must be signed in to change notification settings - Fork 0
/
domainchecker.py
executable file
·227 lines (189 loc) · 5.99 KB
/
domainchecker.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
#!/usr/bin/python3
from http.client import RemoteDisconnected
import argparse
import json
import multiprocessing
import os
import socket
import re
import ssl
import sys
import urllib
import urllib.request
from urllib.error import HTTPError, URLError
from urllib.parse import urlparse
def progressbar(width, _min, _max, current, text=""):
hashes = int(current / ((_max - _min) / width))
fmt = "[%-" + str(width) + "s]"
sys.stdout.write("\r")
sys.stdout.write("\033[K")
sys.stdout.write(fmt % ('#' * hashes))
sys.stdout.write("\n")
sys.stdout.write("\033[K")
sys.stdout.write(text)
sys.stdout.write("\033[1A")
sys.stdout.write("\r")
sys.stdout.flush()
def get_file(filename):
try:
with open(filename, "r") as f:
return f.read().split("\n")
except IOError as e:
print("> " + e.strerror + " [" + e.filename + "]")
parser.print_usage()
def parse_inline_domains(urls):
return urls.split(",")
# help title
parser = argparse.ArgumentParser(
prog='./domainchecker.py',
formatter_class=argparse.RawDescriptionHelpFormatter,
description='''\
_ _
___ ___ _| |___| |_ _ _ ___ ___ ___
| _| . | . | -_| . | | |- _| -_| |
|___|___|___|___|___|_ |___|___|_|_|
https://dsda.ru|___|
''')
parser.add_argument('-l', dest="list", type=get_file, help='domain list file (each domain from new line)')
parser.add_argument('-d', dest="domains", type=parse_inline_domains, help='domain list string (separates by ",")')
parser.add_argument('-crt', dest="crt", action='store_true', help='get additional domains from crt.sh')
parser.add_argument('-crto', dest="crt_log_file", type=argparse.FileType('w'), help='save all domains from crt.sh')
parser.add_argument('-c', dest="codes", action='store_true', help='return [return code] domain name')
parser.add_argument('-g', dest="good", action='store_true', help='return only good domains')
parser.add_argument('-o', dest="log_file", type=argparse.FileType('w'), help='file to write result')
if len(sys.argv) <= 1:
parser.print_usage(sys.stderr)
sys.exit(1)
args = parser.parse_args()
def get_data_from_crt(_domain):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
ctx.ua = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1'
domain_name = _domain.strip()
matches = re.search("http[s]?:\/\/", domain_name)
if matches is not None:
purl = urlparse(domain_name)
domain_name = purl.netloc
data = urllib.parse.urlencode({'q': domain_name, 'output': 'json'})
data = data.encode('ascii')
ret = False
try:
print("Fetch domains from crt.sh...")
with urllib.request.urlopen("https://crt.sh", data, context=ctx) as f:
raw = f.read().decode('utf-8')
except HTTPError as e:
ret = "HTTPError >> " + e.reason
except URLError as e:
if isinstance(e.reason, socket.gaierror):
ret = "GAIError >> " + str(e.reason.errno) + " " + e.reason.strerror
else:
ret = "URLError >> " + e.reason
except socket.timeout:
ret = "TIMEOUT"
if ret:
print("Sorry crt.sh return " + str(ret) + " try use proxy or try later...")
exit(-1)
json_data = json.loads(raw)
urls_array = []
for i in json_data:
if i['name_value']:
url = i['name_value'].split("\n")
urls_array = urls_array + url
clean_domains = []
for i in urls_array:
if not re.match(".*@.*|\*\.", i):
clean_domains.append(i)
# insert the list to the set
list_set = set(clean_domains)
# convert the set to the list
clean_domains = (list(list_set))
if args.crt_log_file:
for clean_domains_item in clean_domains:
args.crt_log_file.write('%s\n' % clean_domains_item)
return clean_domains
def clean_list(domains_list):
dp_list = []
for _i in domains_list:
url = _i.strip()
if not url:
continue
matches = re.search("http[s]?:\/\/", url)
if matches is None:
dp_list.append("http://" + url)
dp_list.append("https://" + url)
else:
purl = urlparse(url)
dp_list.append(purl.scheme + "://" + purl.netloc)
my_set = set(dp_list)
dp_list = list(my_set)
# ret_list = []
# items_counter = 1
# for item in dp_list:
# ret_list.append([items_counter, item])
# items_counter = items_counter + 1
return dp_list
def try_connect(url, number):
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
domain_flag = False
try:
with urllib.request.urlopen(url, timeout=3, context=ctx) as f:
ret = f.code
domain_flag = True
except UnicodeError as e:
ret = "TooLong Domain >> " + url
except HTTPError as e:
ret = "HTTPError >> " + e.reason
except RemoteDisconnected as e:
ret = "RemoteDisconnected >> "
except URLError as e:
if isinstance(e.reason, socket.timeout):
ret = "TIMEOUT >> "
# elif isinstance(e.reason, socket.ConnectionRefusedError):
# ret = "ConnectionRefusedError >> "
elif isinstance(e.reason, socket.gaierror):
ret = "GAIError >> " + str(e.reason.errno) + " " + e.reason.strerror
else:
ret = "URLError >> " + e.reason
except socket.timeout:
ret = "TIMEOUT >>"
ret_str = url
if args.codes and not args.good:
ret_str = str(ret) + "\t" + ret_str
if args.good:
if domain_flag:
return number, ret_str
else:
return number, False
return number, ret_str
# count the arguments
if args.list is None:
domains = args.domains
else:
domains = args.list
if args.crt:
all_domains = []
for i in domains:
all_domains = all_domains + get_data_from_crt(i)
domains = all_domains
print("CPU count: " + str(multiprocessing.cpu_count()))
domains = clean_list(domains)
print("Total %d domain(s)" % len(domains))
def update(i):
progressbar(40, 0, len(domains), i[0], text=str(i))
# note: input comes from async `try_connect`
res[i[0]] = i[1] # put answer into correct index of result list
res = [None] * len(domains)
pool = multiprocessing.Pool(multiprocessing.cpu_count() + 1)
for domain, iter in zip(domains, range(len(domains))):
pool.apply_async(try_connect, args=(domain, iter,), callback=update)
pool.close()
pool.join()
for i in res:
if i is not None and i is not False:
if args.log_file:
args.log_file.write(i + os.linesep)
else:
print(i)