Italiano English
Modifica History Actions

fakeDNS.py

   1 #
   2 # WARNING: See dnsmasq --address=/#/1.2.3.4 before!!!!!
   3 #
   4 
   5 # Fake dns multithread
   6 
   7 from SocketServer import *
   8 
   9 FAKEADDRESS="111.111.111.111"
  10 PORT=53 #udp
  11 
  12 class DNSQuery:
  13   #da http://preachermm.blogspot.com/2006/04/servidor-fake-dns-en-python.html (single thread)
  14   def __init__(self, data):
  15     self.data=data
  16     self.dominio=''
  17     tipo = (ord(data[2]) >> 3) & 15   # Opcode bits
  18     if tipo == 0:                     # Standard query
  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'   # Questions and Answers Counts
  31       packet+=self.data[12:]                                         # Original Domain Name Question
  32       packet+='\xc0\x0c'                                             # Pointer to domain name
  33       packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'             # Response type, ttl and resource data length -> 4 bytes
  34       packet+=''.join(chr(int(x)) for x in ip.split('.'))            # 4bytes of IP
  35     return packet
  36 #DNSQuery
  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 #DNSRequestHandler
  45 
  46 #crea una classe di server udp threadizzato
  47 class FakeDNSServer (ThreadingMixIn, UDPServer): pass
  48 
  49 dummyDNS=FakeDNSServer(('',PORT),DNSRequestHandler)
  50 
  51 #per prevenire attacchi, l'unico modo x fermare il server e' killarlo (SIGTERM, SIGKILL) o premere ctrl-C
  52 going=True
  53 while going:
  54 	try:
  55 		dummyDNS.serve_forever()
  56  	except KeyboardInterrupt:
  57  		going=False
  58 	except:
  59 		pass