-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmwhoisd
executable file
·140 lines (126 loc) · 3.33 KB
/
mwhoisd
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
#!/usr/bin/python
import socket, os, re
from time import strftime
from netaddr import IPAddress, IPNetwork
LISTEN_ADDRESS = "0.0.0.0"
LISTEN_PORT = 43
MAX_QUERY_SIZE = 128
LOGFILE = "/var/log/mwhois.log"
n = "\r\n"
os.system("mkdir -p db/ipv4")
os.system("mkdir -p db/domains")
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((LISTEN_ADDRESS, LISTEN_PORT))
except:
print("Could not bind specified IP or Port. Already in use? Not privileged for this port?")
exit(2)
s.listen(1)
# Sanitize the query received
def sanitizeQuery(qr):
qr = qr.lower()
qr = qr.replace("..", ".")
qr = qr.replace("/", "")
qr = qr.replace("\\", "")
qr = qr.replace(n, "")
return qr
# Check if the input is a valid IPv4 Address
def isIP(qr):
bytez = qr.split(".")
if(len(bytez) != 4):
return False
for byt in bytez:
if(not byt.isdigit()):
return False
ipaddr = [int(bytez[0]), int(bytez[1]), int(bytez[2]), int(bytez[3])]
for num in ipaddr:
if(num < 0 or num > 255):
return False
return True
# Check if the input is a valid domain name
def isDomain(qr):
if(len(qr.split(".")) == 1):
return False
zones = qr.split(".")
ac = re.compile("^[a-z0-9\.-]+\n")
for zone in zones:
if(zone == ""):
return False
if(zone[0] == "-"):
return False
if(zone[-1] == "-"):
return False
if(ac.match(zone + "\n")):
pass
else:
return False
return True
# Check if an IP belongs to a CIDR IP block
def isIPinCIDR(ip, network):
return IPAddress(ip) in IPNetwork(network)
while True:
con, adr = s.accept()
log = "[" + strftime("%d/%m/%Y %H:%M:%S") + "] " + adr[0] + " - "
while True:
query = con.recv(MAX_QUERY_SIZE)
if not query:
break
log = log + query.replace("\r\n", "").replace("\n", "") + " - "
query = sanitizeQuery(query)
rsp = "# +-----------------------------------+" + n
rsp = rsp + "# | DaKnObNET |" + n
rsp = rsp + "# +-----------------------------------+" + n
rsp = rsp + "# | This query was served by mwhois |" + n
rsp = rsp + "# +-----------------------------------+" + n
rsp = rsp + n
if(isIP(query)):
# WHOIS IPv4
log = log + "IPv4"
ipdb = os.listdir("db/ipv4/")
for ipe in ipdb:
ipe = ipe.replace("-", "/")
found = False
if(isIPinCIDR(query, ipe)):
dd = open("db/ipv4/" + ipe.replace("/", "-"), "r")
rsp = rsp + dd.read()
dd.close()
found = True
log = log + n
break
if(found == False):
rsp = rsp + n
rsp = rsp + "# IP Address was not found in the whois database" + n
log = log + " (Not found)" + n
elif(isDomain(query)):
# WHOIS Domain
log = log + "Domain" + n
domaindb = os.listdir("db/domains/")
for domain in domaindb:
found = False
if(query == domain):
dd = open("db/domains/" + domain, "r")
rsp = rsp + dd.read()
dd.close()
found = True
log = log + n
break
if(found == False):
rsp = rsp + n
rsp = rsp + "# Domain name was not found in the whois database" + n
log = log + " (Not found)" + n
else:
# Unrecognized
log = log + "Unrecognized" + n
rsp = rsp + n
rsp = rsp + "# Error. Unknown query type. Query is not IPv4 or Domain " + n
con.send(rsp)
con.close()
if(LOGFILE!=""):
# Save to logs
try:
d = open(LOGFILE, "a+")
d.write(log)
d.close()
except:
print("FAILED TO SAVE TO LOGFILE!")
break