-
Notifications
You must be signed in to change notification settings - Fork 2
/
dns-server.py
executable file
·252 lines (213 loc) · 8.83 KB
/
dns-server.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env python3
import argparse
import re
import socket
import select
import sqlite3
import threading
import dns.zone
import dns.name
import dns.message
import dns.rcode
import dns.rdatatype
import dns.rdataclass
import dns.query
from datetime import datetime
SERVER = '' # for all addresses
PORT = 53
DEBUG_MODE = False
zonefile = './test.com.zone'
zone = dns.zone.from_file(zonefile, relativize=False)
def process_command_line_args():
global SERVER, PORT, DEBUG_MODE, zone
parser = argparse.ArgumentParser(
description='This is a simple authoritative dns.')
parser.add_argument('-s', '--server',
help='IP address for listen. Default: all addresses')
parser.add_argument('-p', '--port',
help='Port for listen. Default: 53')
parser.add_argument('-v', '--verbose', action='store_true',
help='Print verbose output')
parser.add_argument('-d', '--debug', action='store_true',
help='Same as "--verbose"')
parser.add_argument('-f', '--file',
help='The zone file to use. Default: "./test.com.zone"')
args = parser.parse_args()
if args.server:
SERVER = args.server
if args.port:
PORT = int(args.port)
if args.debug or args.verbose:
DEBUG_MODE = True
print('running in DEBUG MODE')
if args.file:
zone = dns.zone.from_file(args.file, relativize=False)
def setup_sockets(server, port):
sock_udp4 = create_udp4_socket(server, port)
sock_udp6 = create_udp6_socket(server, port)
sock_tcp4 = create_tcp4_socket(server, port)
sock_tcp6 = create_tcp6_socket(server, port)
fd_udp4 = sock_udp4.fileno()
fd_udp6 = sock_udp6.fileno()
fd_tcp4 = sock_tcp4.fileno()
fd_tcp6 = sock_tcp6.fileno()
fd_read = [fd_udp4, fd_udp6, fd_tcp4, fd_tcp6]
sockets = {fd_udp4: sock_udp4, fd_udp6: sock_udp6,
fd_tcp4: sock_tcp4, fd_tcp6: sock_tcp6}
is_tcp_dict = {fd_udp4: False, fd_udp6: False, fd_tcp4: True, fd_tcp6: True}
return fd_read, sockets, is_tcp_dict
def create_udp4_socket(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((host, port))
return sock
def create_udp6_socket(host, port):
sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
sock.bind((host, port))
return sock
def create_tcp4_socket(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
return sock
def create_tcp6_socket(host, port):
sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
return sock
def handle_connection(sock, is_tcp):
if is_tcp:
conn, client_addrport = sock.accept()
query, received_time = dns.query.receive_tcp(conn)
else:
query, received_time, client_addrport = dns.query.receive_udp(sock)
if DEBUG_MODE:
transport = "TCP" if is_tcp else "UDP"
qname = query.question[0].name
qtype = dns.rdatatype.to_text(query.question[0].rdtype)
qclass = dns.rdataclass.to_text(query.question[0].rdclass)
addr, port = client_addrport
print('\n{} query: {} {} {} from {}#{}, id: {}'.format(
transport, qname, qtype, qclass, addr, port, query.id))
qname = query.question[0].name
queried_name = str(qname).split(".")[0].lower()
# We need to match only UUIDs, like this one: e33ed847-5421-40a9-bb77-e570bfdfd74b
uuid = re.search("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", queried_name)
# We extract the DB from the queried name.
database = str(qname).split(".")[1].lower()
if uuid is not None:
dt = datetime.now()
ts = datetime.timestamp(dt)
print("The uuid is {}".format(uuid.group()))
print(client_addrport)
addr = client_addrport[0]
to_insert = "INSERT into queries (uuid, ip_address, query_timestamp) VALUES ('{}', '{}', '{}')".format(uuid.group(), addr, ts)
con = sqlite3.connect(f'{database}.sql', check_same_thread=False)
con.execute(to_insert)
con.commit()
con.close()
response = resolve_query(query)
# Check the response size, and truncate if it exceeds 512bytes.
if not is_tcp:
try:
response.to_wire(max_size=512)
except dns.exception.TooBig:
response.flags |= dns.flags.TC
response.answer = []
response.authority = []
response.additional = []
if DEBUG_MODE:
print("Response for", response)
# Response the answer. If response is None, it means query is dropped.
if response is not None:
if is_tcp:
dns.query.send_tcp(conn, response)
conn.close()
else:
dns.query.send_udp(sock, response, client_addrport)
def resolve_query(query):
response = dns.message.make_response(query)
qname = query.question[0].name
qtype = query.question[0].rdtype
qclass = query.question[0].rdclass
# Refuse query if qclass is not IN
if qclass != dns.rdataclass.IN:
response.set_rcode(dns.rcode.REFUSED)
return response
# Refuse query for a different Zone
if not qname.is_subdomain(zone.origin):
response.set_rcode(dns.rcode.REFUSED)
return response
# Return NOTIMP for RR Types which SimpleAuthDNS does not support
# https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-4
if qtype >= 99:
response.set_rcode(dns.rcode.NOTIMP)
return response
def add_additional(response, target):
for rdtype in (dns.rdatatype.A, dns.rdatatype.AAAA):
additional_rrset = zone.get_rrset(target, rdtype)
if additional_rrset:
response.additional.append(additional_rrset)
return response
# Check for delegation
for i in range(len(qname.labels) - len(zone.origin.labels)):
check_name = dns.name.Name(labels=qname.labels[i:])
ns_rdataset = zone.get_rdataset(check_name, dns.rdatatype.NS)
if ns_rdataset:
# do referral response
response.authority.append(
zone.get_rrset(check_name, dns.rdatatype.NS))
for rdata in ns_rdataset:
# check if ns target is in-Bailiwick or out-of-Bailiwick
ns_target = rdata.target
if ns_target.is_subdomain(check_name):
# if ns target is in-Bailiwick, add glue record
response = add_additional(response, ns_target)
return response
# Look for qname
node = zone.get_node(qname)
if node is None:
# NXDOMAIN response
response.set_rcode(dns.rcode.NXDOMAIN)
soa_rrset = zone.get_rrset(zone.origin, dns.rdatatype.SOA)
response.authority.append(soa_rrset)
response.flags |= dns.flags.AA
return response
# Look for (qname, qtype)
rrset = zone.get_rrset(qname, qtype)
if rrset is None:
# NODATA response
response.set_rcode(dns.rcode.NOERROR)
soa_rrset = zone.get_rrset(zone.origin, dns.rdatatype.SOA)
response.authority.append(soa_rrset)
response.flags |= dns.flags.AA
else:
response.set_rcode(dns.rcode.NOERROR)
response.answer.append(rrset)
response.flags |= dns.flags.AA
# If needed, add additional section
if qtype == dns.rdatatype.NS:
for ns_rdata in rrset.to_rdataset():
response = add_additional(response, ns_rdata.target)
elif qtype == dns.rdatatype.MX:
for mx_rdata in rrset.to_rdataset():
response = add_additional(response, mx_rdata.exchange)
elif qtype == dns.rdatatype.SRV:
for srv_rdata in rrset.to_rdataset():
response = add_additional(response, srv_rdata.target)
return response
def main():
process_command_line_args()
print("SimpleAuthDNS: running")
fd_read, sockets, is_tcp_dict = setup_sockets(SERVER, PORT)
print("Listening on UDP and TCP port {}".format(PORT))
while True:
fd_read_ready, _, _ = select.select(fd_read, [], [], 5)
for fd in fd_read_ready:
threading.Thread(target=handle_connection,
args=(sockets[fd], is_tcp_dict[fd])).start()
if __name__ == '__main__':
main()