Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Commit 82ce7b6

Browse files
committed
Merge branch 'Drewsif-master'
2 parents 62ce19a + 438db87 commit 82ce7b6

File tree

1 file changed

+67
-34
lines changed

1 file changed

+67
-34
lines changed

modules/dns.py

Lines changed: 67 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,70 @@
2121
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2222
# SUCH DAMAGE.
2323

24-
from dnslib import DNSRecord, QR, OPCODE, RCODE, QTYPE, CLASS, DNSError
24+
from dnslib import (DNSRecord,
25+
QR,
26+
OPCODE,
27+
RCODE,
28+
QTYPE,
29+
CLASS,
30+
DNSError)
2531

2632
from ChopProtocol import ChopProtocol
2733

2834
moduleName = "dns"
2935
moduleVersion = '0.1'
3036
minimumChopLib = '4.0'
3137

38+
3239
def module_info():
3340
return "Parse DNS"
3441

42+
3543
def init(module_data):
36-
module_options = { 'proto': [ { 'udp': 'dns' } ] }
44+
module_options = {'proto': [{'udp': 'dns'}]}
3745
return module_options
3846

47+
3948
def handleDatagram(udp):
4049
((src, sport), (dst, dport)) = udp.addr
4150
if sport != 53 and dport != 53:
42-
#chop.tsprnt("STOP: %s:%s->%s:%s (%i:%i)" % (src, sport, dst, dport, len(udp.data), len(udp.ip)))
4351
udp.stop()
4452
return
4553

4654
try:
4755
o = DNSRecord.parse(udp.data)
48-
except KeyError, e:
56+
except KeyError as e:
4957
chop.prnt("Key error: %s" % str(e))
5058
return
51-
except DNSError, e:
59+
except DNSError as e:
5260
chop.prnt("dnslib error: %s" % str(e))
5361
return
62+
except Exception as e:
63+
chop.prnt("Unexpeced exception: %s" % str(e))
64+
return
5465

5566
chopp = ChopProtocol('dns')
5667

5768
# Create the dictionary...
58-
f = [ o.header.aa and 'AA',
59-
o.header.tc and 'TC',
60-
o.header.rd and 'RD',
61-
o.header.ra and 'RA' ]
62-
d = { 'header': {
63-
'id': o.header.id,
64-
'type': QR[o.header.get_qr()],
65-
'opcode': OPCODE[o.header.get_opcode()],
66-
'flags': ",".join(filter(None, f)),
67-
'rcode': RCODE[o.header.rcode],
68-
},
69-
'questions': o.questions
70-
}
69+
f = [o.header.aa and 'AA',
70+
o.header.tc and 'TC',
71+
o.header.rd and 'RD',
72+
o.header.ra and 'RA']
73+
74+
try:
75+
d = {'header': {'id': o.header.id,
76+
'type': QR[o.header.get_qr()],
77+
'opcode': OPCODE[o.header.get_opcode()],
78+
'flags': ",".join(filter(None, f)),
79+
'rcode': RCODE[o.header.rcode]},
80+
'questions': o.questions}
81+
except DNSError as e:
82+
chop.prnt("dnslib error: %s" % str(e))
83+
return
84+
except Exception as e:
85+
chop.prnt("Unexpeted exception: %s" % str(e))
86+
return
87+
7188
if OPCODE[o.header.opcode] == 'UPDATE':
7289
f1 = 'zo'
7390
f2 = 'pr'
@@ -78,23 +95,32 @@ def handleDatagram(udp):
7895
f2 = 'a'
7996
f3 = 'ns'
8097
f4 = 'ar'
98+
8199
dhdr = d['header']
82100
dhdr[f1] = o.header.q
83101
dhdr[f2] = o.header.a
84102
dhdr[f3] = o.header.auth
85-
dhdr[f4]= o.header.ar
103+
dhdr[f4] = o.header.ar
104+
86105
d['questions'] = []
87106
for q in o.questions:
88107
qname = str(q.get_qname())
89108
# Strip trailing dot.
90109
if qname.endswith('.'):
91110
qname = qname[:-1]
92-
dq = {
93-
'qname': qname,
94-
'qtype': QTYPE[q.qtype],
95-
'qclass': CLASS[q.qclass]
96-
}
97-
d['questions'].append(dq)
111+
112+
try:
113+
dq = {'qname': qname,
114+
'qtype': QTYPE[q.qtype],
115+
'qclass': CLASS[q.qclass]}
116+
d['questions'].append(dq)
117+
except DNSError as e:
118+
chop.prnt("dnslib error: %s" % str(e))
119+
return
120+
except Exception as e:
121+
chop.prnt("Unexpected exception: %s" % str(e))
122+
return
123+
98124
d['rr'] = []
99125
for r in o.rr:
100126
rname = str(r.get_rname())
@@ -105,14 +131,20 @@ def handleDatagram(udp):
105131
# Strip trailing dot.
106132
if rdata.endswith('.'):
107133
rdata = rdata[:-1]
108-
dr = {
109-
'rname': rname,
110-
'rtype': QTYPE[r.rtype],
111-
'rclass': CLASS[r.rclass],
112-
'ttl': r.ttl,
113-
'rdata': rdata
114-
}
115-
d['rr'].append(dr)
134+
135+
try:
136+
dr = {'rname': rname,
137+
'rtype': QTYPE[r.rtype],
138+
'rclass': CLASS[r.rclass],
139+
'ttl': r.ttl,
140+
'rdata': rdata}
141+
d['rr'].append(dr)
142+
except DNSError as e:
143+
chop.prnt("dnslib error: %s" % str(e))
144+
return
145+
except Exception as e:
146+
chop.prnt("Unexpected exception: %s" % str(e))
147+
return
116148

117149
if sport == 53:
118150
chopp.serverData = d
@@ -122,6 +154,7 @@ def handleDatagram(udp):
122154
return chopp
123155

124156
return None
125-
157+
158+
126159
def shutdown(module_data):
127160
return

0 commit comments

Comments
 (0)