-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultidns.py
264 lines (219 loc) · 9.1 KB
/
multidns.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/usr/bin/env python
# Author:
# --= Mohammad Razavi =--
# mrazavi64-at-gmail-dot-com
"""\
multidns will relay your DNS requsts to several DNS servers and
returns the first answer.
You can also specify an invalid address e.g. 10.10.34.34. If the
answer was 10.10.34.34 the program will continue to try other DNS
servers to find an answer that is not 10.10.34.34.
If you put an `x' character before the address of a DNS server
e.g. `x8.8.8.8:53' the request and its response will be encrypted with
a symmetrical encyption algorithm--that applying the same encryption
algorithm twice will decode to the first input. So if you relay your
DNS requst twice through two instances of this program with `x'
prefixes, the result will be a normal DNS server. But the traffic
between the two program instances will be encrypted. The encryption
algorithm is not secure at all but it is highly possible that it can
fool your government censorship devices.
"""
from __future__ import print_function
import sys, time, re, copy, socket, struct
from threading import Thread
from optparse import OptionParser
from dnslib import DNSRecord, DNSError
from dnslib.server import DNSServer, DNSHandler, BaseResolver, DNSLogger
from dnslib.dns import A
try:
from socketserver import ThreadingUDPServer
except ImportError:
from SocketServer import ThreadingUDPServer
try:
import queue as Queue
except ImportError:
import Queue
__version__ = "1.0.6"
class UDPServer(ThreadingUDPServer):
allow_reuse_address = True
def encrypt(data):
char_encrypt = lambda x: 31 - x if x < 32 else \
x if x == 32 else \
78 - x if x < 46 else \
x if x == 46 else \
173 - x if x < 127 else \
127 if x == 127 else \
383 - x
return "".join([chr(char_encrypt(ord(ch))) for ch in data])
def EncryptDNSRecord(record):
for q in record.questions:
qname = str(q.get_qname())
encrypted_qname = encrypt(qname)
q.set_qname(encrypted_qname)
for rr in record.rr:
rname = str(rr.get_rname())
encrypted_rname = encrypt(rname)
rr.set_rname(encrypted_rname)
for auth in record.auth:
rname = str(auth.get_rname())
encrypted_rname = encrypt(rname)
auth.set_rname(encrypted_rname)
for ar in record.ar:
rname = str(ar.get_rname())
encrypted_rname = encrypt(rname)
ar.set_rname(encrypted_rname)
def IsAcceptable(reply):
global OPTIONS
try:
result = reply.get_a().rdata
except AttributeError:
return False
else:
if result == None:
return False
return not bool(re.match(OPTIONS.invalid_resolve, str(result)))
def request_send(self, dest, port = 53, tcp = False, timeout = None, \
bind_address = None):
"""\
Send packet to nameserver and return response
This is dnslib DNSRecord.send in dns.py but with a "bind_address"
extra parameter
"""
data = self.pack()
if tcp:
if len(data) > 65535:
raise ValueError("Packet length too long: %d" % len(data))
data = struct.pack("!H",len(data)) + data
sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
if timeout is not None:
sock.settimeout(timeout)
if bind_address:
sock.bind((bind_address, 0))
sock.connect((dest,port))
sock.sendall(data)
response = sock.recv(8192)
length = struct.unpack("!H",bytes(response[:2]))[0]
while len(response) - 2 < length:
response += sock.recv(8192)
sock.close()
response = response[2:]
else:
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
if timeout is not None:
sock.settimeout(timeout)
if bind_address:
sock.bind((bind_address, 0))
sock.sendto(self.pack(),(dest,port))
response,server = sock.recvfrom(8192)
sock.close()
return response
class ProxyResolver(BaseResolver):
def __init__(self, addresses, request_bind_address = None):
self.addresses = addresses
self.request_bind_address = request_bind_address
def resolve(self, request, handler):
global OPTIONS
queue = Queue.Queue()
reply = None
for i in range(OPTIONS.retry):
for addr in self.addresses:
request2 = copy.deepcopy(request)
encrypted = addr[0][:1] in ["x", "X"]
if encrypted:
addr = (addr[0][1:],) + addr[1:]
EncryptDNSRecord(request2)
def put(request, encrypted, *send_args, **send_kwargs):
try:
if self.request_bind_address:
result = request_send(request, *send_args, \
bind_address = \
self.request_bind_address, \
**send_kwargs)
else:
result = request.send(*send_args, **send_kwargs)
except socket.timeout:
pass
else:
queue.put((encrypted, result))
t = Thread(target = put, \
args = (request2, encrypted,) + addr, \
kwargs = {"timeout": OPTIONS.timeout})
t.daemon = True
t.start()
for _ in self.addresses:
try:
encrypted, r = queue.get(timeout = OPTIONS.timeout)
except Queue.Empty:
continue
reply = DNSRecord.parse(r)
if encrypted:
EncryptDNSRecord(reply)
if IsAcceptable(reply):
return reply
if not reply:
raise DNSError("No reply received for the request")
return reply
def main():
global OPTIONS, ARGS
parser = OptionParser()
parser.add_option("-b", "--bind", dest = "bind", \
type = "string", default = "127.0.0.7:53", \
help = "set bind address/port to IP[:PORT]. " \
"Default value is `127.0.0.7:53'.", \
metavar = "IP[:PORT]")
parser.add_option("-t", "--timeout", dest = "timeout", \
type = "int", default = "5", \
help = "set DNS resolving timeout to SECONDS", \
metavar = "SECONDS")
parser.add_option("-r", "--retry", dest = "retry", \
type = "int", default = "3", \
help = "set retry count to COUNT", metavar = "COUNT")
parser.add_option("-i", "--invalid-resolve", dest = "invalid_resolve", \
type = "string", default = r"10\.10\.34\.\d+", \
help = "REGEX is a an IP address or a regular " \
"expression that will not be prefered on DNS " \
"resolving. Default value is `10.10.34.\\d+'.", \
metavar = "REGEX")
parser.add_option("--request-bind", dest = "request_bind", \
type = "string", default = "", \
help = "bind to IP address before sending DNS " \
"requests.", metavar = "IP")
parser.add_option("-q", "--quiet", dest = "quiet", \
action = "store_true", default = False, \
help = "do not print any log")
parser.set_usage("%s [OPTION]... DNS_SERVER[:PORT]..." % \
sys.argv[0])
parser.set_description(__doc__)
OPTIONS, ARGS = parser.parse_args()
if len(ARGS) < 1:
parser.print_help()
exit(1)
def get_address_port(arg):
address_port = arg if ":" in arg else "%s:53" % arg
address_port = address_port.split(":")
return address_port[0], int(address_port[1])
bind_address, bind_port = get_address_port(OPTIONS.bind)
dns_servers = [get_address_port(arg) for arg in ARGS]
resolver = ProxyResolver(dns_servers, request_bind_address = \
(OPTIONS.request_bind or None))
logger = DNSLogger("request,reply,truncated,error", False) \
if not OPTIONS.quiet else \
DNSLogger("-request,-reply,-truncated,-error,-log_recv," \
"-log_send,-log_data", False)
udp_server = DNSServer(resolver,
port = bind_port,
address = bind_address,
logger = logger if not OPTIONS.quiet else None,
handler = DNSHandler,
server = UDPServer)
udp_server.start_thread()
try:
while udp_server.isAlive():
time.sleep(1)
except KeyboardInterrupt:
if not OPTIONS.quiet:
print("Shutting down the server with user request...")
udp_server.stop()
exit(0)
if __name__ == "__main__":
main()