Skip to content

Instantly share code, notes, and snippets.

@cute
Created October 25, 2018 09:02
Show Gist options
  • Save cute/f2697163023cac5f5f29484d660f3942 to your computer and use it in GitHub Desktop.
Save cute/f2697163023cac5f5f29484d660f3942 to your computer and use it in GitHub Desktop.
#source http://code.activestate.com/recipes/491264/
#edited by Guangming Li
import socket
class DNSQuery:
def __init__(self, data):
self.data = data
self.dominio = ''
tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
if tipo == 0: # Standard query
ini = 12
lon = ord(data[ini])
while lon != 0:
self.dominio += data[ini+1:ini+lon+1]+'.'
ini += lon+1
lon = ord(data[ini])
def respuesta(self, ip):
packet = ''
if self.dominio:
packet += self.data[:2] + "\x81\x80"
packet += self.data[4:6] + '\x00\x01\x00\x00\x00\x00' # Questions and Answers Counts
pos = self.data.find('\x00', 12) + 5
packet += self.data[12:pos] # Original Domain Name Question
packet += '\xc0\x0c' # Pointer to domain name
packet += '\x00\x01\x00\x01\x00\x00\x00\x00\x00\x04' # Response type, ttl and resource data length -> 4 bytes
packet += str.join('', map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
return packet
if __name__ == '__main__':
ip = '192.168.1.1'
print 'pyminifakeDNS:: dom.query. 60 IN A %s' % ip
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('', 5300))
try:
while 1:
data, addr = udps.recvfrom(1024)
p = DNSQuery(data)
udps.sendto(p.respuesta(ip), addr)
print 'Respuesta: %s -> %s' % (p.dominio, ip)
except KeyboardInterrupt:
print 'Finalizando'
udps.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment