-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.py
511 lines (377 loc) · 16.5 KB
/
dns.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
import re, socket, untwisted
from untwisted import promise, udp
# TYPE fields are used in resource records. Note that these types are a subset
# of QTYPEs
A = 1
NS = 2
MD = 3
MF = 4
CNAME = 5
SOA = 6
MB = 7
MG = 8
MR = 9
NULL = 10
WKS = 11
PTR = 12
HINFO = 13
MINFO = 14
MX = 15
TXT = 16
# Here is the format of the SRV RR, whose DNS type code is 33
SRV = 33
# CLASS fields appear in resource records. The following CLASS mnemonics and
# values are defined
IN = 1
CS = 2
CH = 3
HS = 4
# The name server was unable to interpret the query
formatError = 1
# The name server was unable to process this query due to a problem with the
# name server
serverFailure = 2
# Meaningful only for responses from an authoritative name server, this code
# signifies that the domain name referenced in the query does not exist
nameError = 3
# The name server does not support the requested kind of query
notSupported = 4
# The name server refuses to perform the specified operation for policy reasons
refused = 5
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / QNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | QTYPE |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | QCLASS |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
class question:
__metaclass__ = type
def __init__(ctx, *args):
if args:
try:
ctx.qname, ctx.qtype, ctx.qclass = args
except ValueError:
try:
ctx.qname, ctx.qtype = args
except ValueError:
ctx.qname, = args
__delitem__ = object.__delattr__
__getitem__ = object.__getattribute__
__setitem__ = object.__setattr__
def __str__(ctx):
result = ''.join(chr(len(label)) + label for label in ctx.qname.split('.'))
if not ctx.qname.endswith('.'):
result += '\0'
return result + chr(ctx.qtype >> 8) + chr(ctx.qtype & 0xff) + chr(ctx.qclass >> 8) + chr(ctx.qclass & 0xff)
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / NAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | TYPE |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | CLASS |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | TTL |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | RDLENGTH |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / RDATA /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
class rr:
__metaclass__ = type
__delitem__ = object.__delattr__
__getitem__ = object.__getattribute__
__setitem__ = object.__setattr__
# +---------------------+
# | Header |
# +---------------------+
# | Question | Question for the name server
# +---------------------+
# | Answer | RRs answering question
# +---------------------+
# | Authority | RRs pointing toward an authority
# +---------------------+
# | Additional | RRs holding additional information
# +---------------------+
class message:
def __init__(ctx):
ctx.question = untwisted.oneMany()
ctx.answer = untwisted.oneMany()
ctx.authority = untwisted.oneMany()
ctx.additional = untwisted.oneMany()
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | ID |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# |QR| Opcode |AA|TC|RD|RA| Z | RCODE |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | QDCOUNT |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | ANCOUNT |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | NSCOUNT |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | ARCOUNT |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
def __str__(ctx):
qdcount = len(ctx.question)
ancount = len(ctx.answer)
nscount = len(ctx.authority)
arcount = len(ctx.additional)
return ctx.id + chr(ctx.qr << 7 | ctx.opcode << 3 | ctx.aa << 2 | ctx.tc << 1 | ctx.rd) + chr(ctx.ra << 7 | ctx.z << 4 | ctx.rcode) + chr(qdcount >> 8) + chr(qdcount & 0xff) + chr(ancount >> 8) + chr(ancount & 0xff) + chr(nscount >> 8) + chr(nscount & 0xff) + chr(arcount >> 8) + chr(arcount & 0xff) + ''.join(map(str, ctx.question)) + ''.join(map(str, ctx.answer)) + ''.join(map(str, ctx.authority)) + ''.join(map(str, ctx.additional))
server = []
resolvConf = open('/etc/resolv.conf').read()
pos = 0
while pos < len(resolvConf):
match = re.compile('^nameserver\s+([^\s#]+)', re.M).search(resolvConf, pos)
if not match:
break
server.append(match.group(1))
pos = match.end()
# Workaround PEP 3104 with class
class lookup:
class __metaclass__(type):
@promise.resume
def __call__(ctx, qname, qtype=A, qclass=IN, server=server[0]):
ctx = type.__call__(ctx)
transport = yield udp.connect(server, 'domain')()
query = message()
query.id = '\0\0'
query.qr = 0
query.opcode = 0
query.aa = 0
query.tc = 0
query.rd = 1
query.ra = 0
query.z = 0
query.rcode = 0
query.question.append(question(qname, qtype, qclass))
transport.write(str(query))
ctx.recv = yield transport.recv()
response = message()
response.id = ctx.recv[:2]
response.qr = ord(ctx.recv[2]) >> 7
response.opcode = ord(ctx.recv[2]) >> 3 & 0xf
response.aa = ord(ctx.recv[2]) >> 2 & 1
response.tc = ord(ctx.recv[2]) >> 1 & 1
response.rd = ord(ctx.recv[2]) & 1
response.ra = ord(ctx.recv[3]) >> 7
response.z = ord(ctx.recv[3]) >> 4 & 7
response.rcode = ord(ctx.recv[3]) & 0xf
qdcount = ord(ctx.recv[4]) << 8 | ord(ctx.recv[5])
ancount = ord(ctx.recv[6]) << 8 | ord(ctx.recv[7])
nscount = ord(ctx.recv[8]) << 8 | ord(ctx.recv[9])
arcount = ord(ctx.recv[10]) << 8 | ord(ctx.recv[11])
ctx.offset = 12
for _ in range(qdcount):
response.question.append(ctx.question(ctx.offset))
for _ in range(ancount):
response.answer.append(ctx.rr(ctx.offset))
for _ in range(nscount):
response.authority.append(ctx.rr(ctx.offset))
for _ in range(arcount):
response.additional.append(ctx.rr(ctx.offset))
if response.rcode:
raise response
#return ...
raise StopIteration(response)
def domainName(ctx, offset):
result = ''
while True:
length = ord(ctx.recv[offset])
# An entire domain name or a list of labels at the end of a domain name
# is replaced with a pointer to a prior occurance of the same name
if 0xbf < length:
try:
return result + ctx.domainName((length & 0x3f) << 8 | ord(ctx.recv[offset + 1]))
finally:
ctx.offset = offset + 2
offset += 1
if not length:
ctx.offset = offset
return result
result += ctx.recv[offset:offset + length] + '.'
offset += length
def question(ctx, offset):
result = question()
result.qname = ctx.domainName(offset)
result.qtype = ord(ctx.recv[ctx.offset]) << 8 | ord(ctx.recv[ctx.offset + 1])
result.qclass = ord(ctx.recv[ctx.offset + 2]) << 8 | ord(ctx.recv[ctx.offset + 3])
ctx.offset += 4
return result
def rr(ctx, offset):
result = rr()
result.name = ctx.domainName(offset)
result.type = ord(ctx.recv[ctx.offset]) << 8 | ord(ctx.recv[ctx.offset + 1])
result['class'] = ord(ctx.recv[ctx.offset + 2]) << 8 | ord(ctx.recv[ctx.offset + 3])
result.ttl = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset + 4:ctx.offset + 8]))
result.rdlength = ord(ctx.recv[ctx.offset + 8]) << 8 | ord(ctx.recv[ctx.offset + 9])
ctx.offset += 10
if A == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | ADDRESS |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.address = socket.inet_ntop(socket.AF_INET, ctx.recv[ctx.offset:ctx.offset + 4])
ctx.offset += 4
return result
if NS == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / NSDNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.nsdname = ctx.domainName(ctx.offset)
return result
if result.type in (MD, MF, MB):
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / MADNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.madname = ctx.domainName(ctx.offset)
return result
if CNAME == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / CNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.cname = ctx.domainName(ctx.offset)
return result
if SOA == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / MNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / RNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | SERIAL |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | REFRESH |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | RETRY |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | EXPIRE |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | MINIMUM |
# | |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.mname = ctx.domainName(ctx.offset)
result.rname = ctx.domainName(ctx.offset)
result.serial = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset:ctx.offset + 4]))
result.refresh = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset + 4:ctx.offset + 8]))
result.retry = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset + 8:ctx.offset + 12]))
result.expire = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset + 12:ctx.offset + 16]))
result.minimum = reduce(lambda result, itm: result << 8 | itm, map(ord, ctx.recv[ctx.offset + 16:ctx.offset + 20]))
ctx.offset += 20
return result
if MG == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / MGMNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.mgmname = ctx.domainName(ctx.offset)
return result
if MR == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / NEWNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.newname = ctx.domainName(ctx.offset)
return result
if PTR == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / PTRDNAME /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.ptrdname = ctx.domainName(ctx.offset)
return result
if HINFO == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / CPU /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / OS /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
length = ord(ctx.recv[ctx.offset])
ctx.offset += 1
result.cpu = ctx.recv[ctx.offset:ctx.offset + length]
ctx.offset += length
length = ord(ctx.recv[ctx.offset])
ctx.offset += 1
result.os = ctx.recv[ctx.offset:ctx.offset + length]
ctx.offset += length
return result
if MINFO == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / RMAILBX /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / EMAILBX /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.rmailbx = ctx.domainName(ctx.offset)
result.emailbx = ctx.domainName(ctx.offset)
return result
if MX == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# | PREFERENCE |
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / /
# / EXCHANGE /
# / /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
result.preference = ord(ctx.recv[ctx.offset]) << 8 | ord(ctx.recv[ctx.offset + 1])
ctx.offset += 2
result.exchange = ctx.domainName(ctx.offset)
return result
if TXT == result.type:
# 0 1 2 3 4 5 6 7 8 9 A B C D E F
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
# / TXT-DATA /
# +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
length = ord(ctx.recv[ctx.offset])
ctx.offset += 1
result.txtData = ctx.recv[ctx.offset:ctx.offset + length]
ctx.offset += length
return result
if SRV == result.type:
result.priority = ord(ctx.recv[ctx.offset]) << 8 | ord(ctx.recv[ctx.offset + 1])
result.weight = ord(ctx.recv[ctx.offset + 2]) << 8 | ord(ctx.recv[ctx.offset + 3])
result.port = ord(ctx.recv[ctx.offset + 4]) << 8 | ord(ctx.recv[ctx.offset + 5])
ctx.offset += 6
result.target = ctx.domainName(ctx.offset)
return result
ctx.offset += result.rdlength
return result