Created
November 30, 2011 12:39
-
-
Save waawal/1408932 to your computer and use it in GitHub Desktop.
Python DNS server with Redis backend
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# A naive dns server with a Redis backend | |
# Set keys in Redis you want to be authoritative for (set google.com. 127.0.0.1) | |
# Tip: Use Redis's ttl functions to have temporary names | |
# Currently only does A records, feel free to fix that | |
# | |
# Licensed under the PSF License | |
# Thanks to: http://code.activestate.com/recipes/491264-mini-fake-dns-server/ | |
# Author: @Kaerast <[email protected]> | |
import socket | |
import redis # https://github.com/andymccurdy/redis-py | |
PORT = 5053 # requires root if < 1024 | |
class DNSQuery(object): | |
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 and ip is not None: | |
packet = [ | |
self.data[:2], "\x81\x80", | |
self.data[4:6], self.data[4:6], '\x00\x00\x00\x00', # Questions and Answers Counts | |
self.data[12:], # Original Domain Name Question | |
'\xc0\x0c', # Pointer to domain name | |
'\x00\x01\x00\x01\x00\x00\x00\x3c\x00\x04', # Response type, ttl and resource data length -> 4 bytes | |
socket.inet_aton(ip), # 4bytes of IP | |
] | |
elif ip is None: | |
packet = [ | |
self.data[:2], "\x81\x80", | |
self.data[4:6], "\x00\x00", '\x00\x00\x00\x00', # Questions and Answers Counts | |
self.data[12:], # Original Domain Name Question | |
'\xc0\x0c', # Pointer to domain name | |
] | |
return ''.join(packet) | |
if __name__ == '__main__': | |
r = redis.StrictRedis(host='localhost', port=6379, db=0, password='mypassword') | |
print 'pyDNS:: dom.query. 60 IN A IP; port {0}'.format(PORT) | |
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
udps.bind(('',PORT)) | |
try: | |
while 1: | |
data, addr = udps.recvfrom(1024) | |
p=DNSQuery(data) | |
ip=r.get(p.dominio) | |
print 'Respuesta: %s -> %s' % (p.dominio, ip) | |
udps.sendto(p.respuesta(ip), addr) | |
except KeyboardInterrupt: | |
print 'Finalizando' | |
udps.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment