-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicroDNSSrv.py
168 lines (147 loc) · 5.51 KB
/
MicroDNSSrv.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
from _thread import start_new_thread
from re import match
import socket
import gc
class MicroDNSSrv :
def Create(domainsList):
print("starting dns ...")
mds = MicroDNSSrv()
print("lel")
mds.SetDomainsList(domainsList)
print("domainliset")
mds.Start()
print("setting records ...")
return mds
return None
def _tryStartThread(func, args=()) :
for x in range(10) :
try :
gc.collect()
start_new_thread(func, args)
return True
except :
global _dns_thread_id
try :
_dns_thread_id += 1
except :
_dns_thread_id = 0
try :
start_new_thread('DNS_THREAD_%s' % _dns_thread_id, func, args)
return True
except :
pass
return False
def _ipV4StrToBytes(ipStr) :
try :
parts = ipStr.split('.')
if len(parts) == 4 :
return bytes( [ int(parts[0]),
int(parts[1]),
int(parts[2]),
int(parts[3]) ] )
except :
pass
return None
def _getAskedDomainName(packet) :
try :
queryType = (packet[2] >> 3) & 15
qCount = (packet[4] << 8) | packet[5]
if queryType == 0 and qCount == 1 :
pos = 12
domName = ''
while True :
domPartLen = packet[pos]
if (domPartLen == 0) :
break
domName += ('.' if len(domName) > 0 else '') \
+ packet[ pos+1 : pos+1+domPartLen ].decode()
pos += 1+domPartLen
return domName
except :
pass
return None
def _getPacketAnswerA(packet, ipV4Bytes) :
try :
queryEndPos = 12
while True :
domPartLen = packet[queryEndPos]
if (domPartLen == 0) :
break
queryEndPos += 1 + domPartLen
queryEndPos += 5
return b''.join( [
packet[:2], # Query identifier
b'\x85\x80', # Flags and codes
packet[4:6], # Query question count
b'\x00\x01', # Answer record count
b'\x00\x00', # Authority record count
b'\x00\x00', # Additional record count
packet[12:queryEndPos], # Query question
b'\xc0\x0c', # Answer name as pointer
b'\x00\x01', # Answer type A
b'\x00\x01', # Answer class IN
b'\x00\x00\x00\x1E', # Answer TTL 30 secondes
b'\x00\x04', # Answer data length
ipV4Bytes ] ) # Answer data
except :
pass
return None
def __init__(self) :
self._domList = { }
self._started = False
def _serverProcess(self) :
self._started = True
while True :
try :
packet, cliAddr = self._server.recvfrom(256)
domName = MicroDNSSrv._getAskedDomainName(packet)
if domName :
domName = domName.lower()
ipB = self._domList.get(domName, None)
if not ipB :
for domChk in self._domList.keys() :
if domChk.find('*') >= 0 :
r = domChk.replace('.', '\.').replace('*', '.*') + '$'
if match(r, domName) :
ipB = self._domList.get(domChk, None)
break
if not ipB :
ipB = self._domList.get('*', None)
if ipB :
packet = MicroDNSSrv._getPacketAnswerA(packet, ipB)
if packet :
self._server.sendto(packet, cliAddr)
except :
if not self._started :
break
def Start(self) :
if not self._started:
self._server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self._server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self._server.bind(('0.0.0.0', 53))
self._server.setblocking(True)
self._serverProcess() # Directly call the server process
return True
return False
def Stop(self) :
if self._started :
self._started = False
self._server.close()
return True
return False
def IsStarted(self) :
return self._started
def SetDomainsList(self, domainsList) :
if domainsList and isinstance(domainsList, dict) :
o = { }
for dom, ip in domainsList.items() :
if isinstance(dom, str) and len(dom) > 0 :
ipB = MicroDNSSrv._ipV4StrToBytes(ip)
if ipB :
o[dom.lower()] = ipB
continue
break
if len(o) == len(domainsList) :
self._domList = o
return True
return False