from pprint import pprint import socket import re import codecs import

 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
from pprint import pprint
import socket
import re
import codecs
import logging
RE_ROOT_REFER = re.compile(r'refer:\s*(\S+)', re.M)
RE_REFER = re.compile(
r'^(refer|whois server|referral url|whois server|registrar whois)'
r':\s*(\S+\.\S+)',
re.M | re.I
)
def query_whois_server(server, domain):
logging.debug(
'Quering whois server [%s] for domain [%s]' % (server, domain)
)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server, 43))
sock.send(b'%s\r\n' % domain.encode('utf-8'))
chunks = []
while True:
chunk = sock.recv(1024)
if not chunk:
break
else:
chunks.append(chunk)
res = b''.join(chunks)
return res.decode('utf-8')
def get_root_server(domain):
res = query_whois_server('whois.iana.org', domain)
match = RE_ROOT_REFER.search(res)
if match:
return match.group(1)
else:
raise Exception('No root whois server found for %s' % domain)
def get_whois_data(domain, server=None, res_chain=None, server_chain=None):
if res_chain is None:
res_chain = []
if server_chain is None:
server_chain = []
# rfc3490
domain = codecs.encode(domain, 'idna').decode()
if server is None:
target_server = get_root_server(domain)
else:
target_server = server
res = query_whois_server(target_server, domain)
new_res_chain = res_chain + [res]
new_server_chain = server_chain + [target_server]
for match in RE_REFER.finditer(res):
ref = match.group(1)
if ref != target_server and '://' not in ref:
return get_whois_data(
domain, server=ref,
res_chain=new_res_chain,
server_chain=new_server_chain,
)
return (new_res_chain, new_server_chain)
def main(**kwargs):
socket.setdefaulttimeout(10)
domain = 'dumpz.org'
server = 'whois.iana.org'
res_chain, server_chain = get_whois_data(domain)
print(res_chain[-1])