1
2
3
4
5
6
7 from SocketServer import *
8
9 FAKEADDRESS="111.111.111.111"
10 PORT=53
11
12 class DNSQuery:
13
14 def __init__(self, data):
15 self.data=data
16 self.dominio=''
17 tipo = (ord(data[2]) >> 3) & 15
18 if tipo == 0:
19 ini=12
20 lon=ord(data[ini])
21 while lon != 0:
22 self.dominio+=data[ini+1:ini+lon+1]+'.'
23 ini+=lon+1
24 lon=ord(data[ini])
25
26 def respuesta(self, ip):
27 packet=''
28 if self.dominio:
29 packet+=self.data[:2] + "\x81\x80"
30 packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'
31 packet+=self.data[12:]
32 packet+='\xc0\x0c'
33 packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'
34 packet+=''.join(chr(int(x)) for x in ip.split('.'))
35 return packet
36
37
38 class DNSRequestHandler (DatagramRequestHandler):
39 def handle(self):
40 DNSRequest=self.rfile.read()
41 q=DNSQuery(DNSRequest)
42 DNSDatagram=q.respuesta(FAKEADDRESS)
43 self.wfile.write(DNSDatagram)
44
45
46
47 class FakeDNSServer (ThreadingMixIn, UDPServer): pass
48
49 dummyDNS=FakeDNSServer(('',PORT),DNSRequestHandler)
50
51
52 going=True
53 while going:
54 try:
55 dummyDNS.serve_forever()
56 except KeyboardInterrupt:
57 going=False
58 except:
59 pass