| 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' |