opexxx

pyminifakeDNS.py

Mar 10th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. import socket
  2.  
  3. class DNSQuery:
  4.   def __init__(self, data):
  5.     self.data=data
  6.     self.dominio=''
  7.  
  8.     tipo = (ord(data[2]) >> 3) & 15   # Opcode bits
  9.     if tipo == 0:                     # Standard query
  10.       ini=12
  11.       lon=ord(data[ini])
  12.       while lon != 0:
  13.         self.dominio+=data[ini+1:ini+lon+1]+'.'
  14.         ini+=lon+1
  15.         lon=ord(data[ini])
  16.  
  17.   def respuesta(self, ip):
  18.     packet=''
  19.     if self.dominio:
  20.       packet+=self.data[:2] + "\x81\x80"
  21.       packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00'   # Questions and Answers Counts
  22.       packet+=self.data[12:]                                         # Original Domain Name Question
  23.       packet+='\xc0\x0c'                                             # Pointer to domain name
  24.       packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04'             # Response type, ttl and resource data length -> 4 bytes
  25.       packet+=str.join('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
  26.     return packet
  27.  
  28. if __name__ == '__main__':
  29.   ip='192.168.1.1'
  30.   print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
  31.  
  32.   udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  33.   udps.bind(('',53))
  34.  
  35.   try:
  36.     while 1:
  37.       data, addr = udps.recvfrom(1024)
  38.       p=DNSQuery(data)
  39.       udps.sendto(p.respuesta(ip), addr)
  40.       print 'Respuesta: %s -> %s' % (p.dominio, ip)
  41.   except KeyboardInterrupt:
  42.     print 'Finalizando'
  43.     udps.close()
Add Comment
Please, Sign In to add comment