Skip to content

Instantly share code, notes, and snippets.

@bitrot-sh
Created March 16, 2018 16:22
Show Gist options
  • Save bitrot-sh/7cded46207eb28a28491d9b9429930c4 to your computer and use it in GitHub Desktop.
Save bitrot-sh/7cded46207eb28a28491d9b9429930c4 to your computer and use it in GitHub Desktop.
DNS A record lookups resolve to the host making the request.
#!/usr/bin/env python3
"""
Tiny Python DNS server that replies to all A record requests with the requester's IP.
"""
import socket
import sys
try:
socket.SO_REUSEPORT
except AttributeError:
socket.SO_REUSEPORT = 15
# DNSQuery class from http://code.activestate.com/recipes/491264-mini-fake-dns-server/
class DNSQuery:
def __init__(self, data):
self.data=data
self.domain=''
tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
if tipo == 0: # Standard query
ini=12
lon=ord(data[ini])
while lon != 0:
self.domain+=data[ini+1:ini+lon+1]+'.'
ini+=lon+1
lon=ord(data[ini])
def build_reply(self, ip):
packet=''
if ip == '': # Taken from crypt0s (https://github.com/Crypt0s/FakeDns/blob/master/fakedns.py)
# Build the response packet
packet+=self.data[:2] + "\x81\x83" # Reply Code: No Such Name
#0 answer rrs 0 additional, 0 auth
packet+=self.data[4:6] + '\x00\x00' + '\x00\x00\x00\x00' # Questions and Answers Counts
packet+=self.data[12:] # Original Domain Name Question
if self.domain and packet == '':
packet+=self.data[:2] + "\x81\x80"
packet+=self.data[4:6] + self.data[4:6] + '\x00\x00\x00\x00' # Questions and Answers Counts
packet+=self.data[12:] # Original Domain Name Question
packet+='\xc0\x0c' # Pointer to domain name
packet+='\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04' # Response type, ttl and resource data length -> 4 bytes
packet+=str.join('',[chr(int(x)) for x in ip.split('.')]) # 4bytes of IP
return packet
def main():
print('[+] PyEchoDNS')
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
#SO_REUSEPORT option allows multiple threads to bind to one port.
# kernel >= 3.9 https://lwn.net/Articles/542629/
try:
udps.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
except socket.error:
print('[!] SO_REUSEPORT not supported by your system.')
udps.bind(('',53))
try:
while 1:
data, src_addr = udps.recvfrom(1024)
ip = src_addr[0]
p=DNSQuery(data)
udps.sendto(p.build_reply(ip), src_addr)
print(('[+] Request from %s for %s' % (ip, p.domain)))
except KeyboardInterrupt:
print('[+] Closing')
udps.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment