def handleDatagram(udp): ((src, sport), (dst, dport)) = udp.addr if sport != 53 and dport != 53: udp.stop() return try: o = DNSRecord.parse(udp.data) except KeyError as e: chop.prnt("Key error: %s" % str(e)) return except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpeced exception: %s" % str(e)) return chopp = ChopProtocol('dns') # Create the dictionary... f = [ o.header.aa and 'AA', o.header.tc and 'TC', o.header.rd and 'RD', o.header.ra and 'RA' ] try: d = { 'header': { 'id': o.header.id, 'type': QR[o.header.get_qr()], 'opcode': OPCODE[o.header.get_opcode()], 'flags': ",".join(filter(None, f)), 'rcode': RCODE[o.header.rcode] }, 'questions': o.questions } except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpeted exception: %s" % str(e)) return if OPCODE[o.header.opcode] == 'UPDATE': f1 = 'zo' f2 = 'pr' f3 = 'up' f4 = 'ad' else: f1 = 'q' f2 = 'a' f3 = 'ns' f4 = 'ar' dhdr = d['header'] dhdr[f1] = o.header.q dhdr[f2] = o.header.a dhdr[f3] = o.header.auth dhdr[f4] = o.header.ar d['questions'] = [] for q in o.questions: qname = str(q.get_qname()) # Strip trailing dot. if qname.endswith('.'): qname = qname[:-1] try: dq = { 'qname': qname, 'qtype': QTYPE[q.qtype], 'qclass': CLASS[q.qclass] } d['questions'].append(dq) except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpected exception: %s" % str(e)) return d['rr'] = [] for r in o.rr: rname = str(r.get_rname()) # Strip trailing dot. if rname.endswith('.'): rname = rname[:-1] rdata = str(r.rdata) # Strip trailing dot. if rdata.endswith('.'): rdata = rdata[:-1] try: dr = { 'rname': rname, 'rtype': QTYPE[r.rtype], 'rclass': CLASS[r.rclass], 'ttl': r.ttl, 'rdata': rdata } d['rr'].append(dr) except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpected exception: %s" % str(e)) return if sport == 53: chopp.serverData = d return chopp elif dport == 53: chopp.clientData = d return chopp return None
def handleDatagram(udp): ((src, sport), (dst, dport)) = udp.addr if sport != 53 and dport != 53: udp.stop() return try: o = DNSRecord.parse(udp.data) except KeyError as e: chop.prnt("Key error: %s" % str(e)) return except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpeced exception: %s" % str(e)) return chopp = ChopProtocol('dns') # Create the dictionary... f = [o.header.aa and 'AA', o.header.tc and 'TC', o.header.rd and 'RD', o.header.ra and 'RA'] try: d = {'header': {'id': o.header.id, 'type': QR[o.header.get_qr()], 'opcode': OPCODE[o.header.get_opcode()], 'flags': ",".join(filter(None, f)), 'rcode': RCODE[o.header.rcode]}, 'questions': o.questions} except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpeted exception: %s" % str(e)) return if OPCODE[o.header.opcode] == 'UPDATE': f1 = 'zo' f2 = 'pr' f3 = 'up' f4 = 'ad' else: f1 = 'q' f2 = 'a' f3 = 'ns' f4 = 'ar' dhdr = d['header'] dhdr[f1] = o.header.q dhdr[f2] = o.header.a dhdr[f3] = o.header.auth dhdr[f4] = o.header.ar d['questions'] = [] for q in o.questions: qname = str(q.get_qname()) # Strip trailing dot. if qname.endswith('.'): qname = qname[:-1] try: dq = {'qname': qname, 'qtype': QTYPE[q.qtype], 'qclass': CLASS[q.qclass]} d['questions'].append(dq) except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpected exception: %s" % str(e)) return d['rr'] = [] for r in o.rr: rname = str(r.get_rname()) # Strip trailing dot. if rname.endswith('.'): rname = rname[:-1] rdata = str(r.rdata) # Strip trailing dot. if rdata.endswith('.'): rdata = rdata[:-1] try: dr = {'rname': rname, 'rtype': QTYPE[r.rtype], 'rclass': CLASS[r.rclass], 'ttl': r.ttl, 'rdata': rdata} d['rr'].append(dr) except DNSError as e: chop.prnt("dnslib error: %s" % str(e)) return except Exception as e: chop.prnt("Unexpected exception: %s" % str(e)) return if sport == 53: chopp.serverData = d return chopp elif dport == 53: chopp.clientData = d return chopp return None
d['questions'] = [] for q in o.questions: dq = { 'qname': str(q.qname), 'qtype': QTYPE[q.qtype], 'qclass': QTYPE[q.qclass] } d['questions'].append(dq) d['rr'] = [] for r in o.rr: dr = { 'rname': str(r.rname), 'rtype': QTYPE.lookup(r.rtype,r.rtype), 'rclass': CLASS[r.rclass], 'ttl': r.ttl, 'rdata': str(r.rdata) } d['rr'].append(dr) if sport == 53: chopp.serverData = d return chopp elif dport == 53: chopp.clientData = d return chopp return None def shutdown(module_data): return