-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminidns.py
128 lines (104 loc) · 3.55 KB
/
minidns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python
import sys
import socket
import fcntl
import struct
# 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 respuesta(self, ip):
packet=''
if self.domain:
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('',map(lambda x: chr(int(x)), ip.split('.'))) # 4bytes of IP
return packet
# get_ip_address code from http://code.activestate.com/recipes/439094-get-the-ip-address-associated-with-a-network-inter/
def get_ip_address(ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
except:
return None
def usage():
print ""
print "Usage:"
print ""
print "\t# minidns [ip | interface]"
print ""
print "Description:"
print ""
print "\tMiniDNS will respond to all DNS queries with a single IPv4 address."
print ""
print "\tYou may specify the IP address to be returned as the first argument on the command line:\n"
print "\t\t# minidns 1.2.3.4\n"
print "\tAlternatively, you may specify an interface name and MiniDNS will use the IP address currently assigned to that interface:\n"
print "\t\t# minidns eth0\n"
print "\tIf no interface or IP address is specified, the IP address of eth0 will be used."
print ""
sys.exit(1)
if __name__ == '__main__':
ip = None
iface = 'eth0'
if len(sys.argv) == 2:
if sys.argv[-1] == '-h' or sys.argv[-1] == '--help':
usage()
else:
if len(sys.argv[-1].split('.')) == 4:
ip=sys.argv[-1]
else:
iface = sys.argv[-1]
if ip is None:
ip = get_ip_address(iface)
if ip is None:
print "ERROR: Invalid IP address or interface name specified!"
usage()
try:
udps = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udps.bind(('',53))
except Exception, e:
print "Failed to create socket on UDP port 53:", e
sys.exit(1)
print 'miniDNS :: * 60 IN A %s\n' % ip
try:
while 1:
data, addr = udps.recvfrom(1024)
p=DNSQuery(data)
indomain=''
tipo = (ord(data[2]) >> 3) & 15 # Opcode bits
if tipo == 0: # Standard query
ini=12
lon=ord(data[ini])
while lon != 0:
indomain+=data[ini+1:ini+lon+1]+'.'
ini+=lon+1
lon=ord(data[ini])
print "incoming domain ", indomain
if indomain == "www.fun.com.":
ip = "10.0.0.2"
print "match fun.com"
if indomain == "www.work.com.":
ip = "10.0.0.3"
print ip
udps.sendto(p.respuesta(ip), addr)
print 'Request: %s -> %s' % (p.domain, ip)
except KeyboardInterrupt:
print '\nBye!'
udps.close()