-
Notifications
You must be signed in to change notification settings - Fork 7
/
DnsByTcp.py
62 lines (51 loc) · 1.91 KB
/
DnsByTcp.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
import os, sys
import socket
import struct
import threading
import SocketServer
import random
# DNS Server List
DNS_SERVS = ['8.8.8.8',
'8.8.4.4',
'208.67.222.222',
'208.67.220.220',
]
DNS_PORT = 53 # default dns port 53
TIMEOUT = 20 # set timeout 5 second
def QueryDnsByTcp(dns_ip, dns_port, query_data):
# make TCP DNS Frame
tcp_frame = struct.pack('!h', len(query_data)) + query_data
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT) # set socket timeout
s.connect((dns_ip, dns_port))
s.send(tcp_frame)
data = s.recv(2048)
except:
if s: s.close()
return
if s: s.close()
return data
class ThreadedUDPServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
# Ctrl-C will cleanly kill all spawned threads
daemon_threads = True
# much faster rebinding
allow_reuse_address = True
def __init__(self, s, t):
SocketServer.UDPServer.__init__(self, s, t)
class ThreadedUDPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
query_data = self.request[0]
udp_sock = self.request[1]
addr = self.client_address
dns_ip = DNS_SERVS[random.randint(0, len(DNS_SERVS) - 1)]
response = QueryDnsByTcp(dns_ip, DNS_PORT, query_data)
if response:
# udp dns packet no length
udp_sock.sendto(response[2:], addr)
if __name__ == "__main__":
print "---------------------------------------------------------------"
print "| To Use this tool, you must set your dns server to 127.0.0.1 |"
print "---------------------------------------------------------------"
dns_server = ThreadedUDPServer(('127.0.0.1', 53), ThreadedUDPRequestHandler)
dns_server.serve_forever()