DNS failover packet (python + scapy)

I am working on building a proxy using Python and scapy. TCP packets seem to be working fine, but I am running into some problems with UDP, in particular with DNS queries. Essentially, when a DNS request comes in, I take it into my script, prepare a DNS lookup, and try to return it to the person requesting the DNS request. The script successfully performs a preliminary lookup and returns a DNS response, however looking at wireshark it tells me "Malformed Packet". Can anyone tell me what I need to do to get the DNS response back correctly?

#!/usr/bin/env python

from tornado.websocket import WebSocketHandler
from tornado.httpserver import HTTPServer
from tornado.web import Application
from tornado.ioloop import IOLoop

from collections import defaultdict
from scapy.all import *
import threading    

outbound_udp = defaultdict(int)
connection = None

class PacketSniffer(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        global connection
        while (True):
            pkt = sniff(iface="eth0", count=1)

            if pkt[0].haslayer(DNS):
              print "Returning back has UDP"
              print pkt.summary()
              ipPacket = pkt[0][IP]
              dnsPacket = pkt[0][DNS]

              if outbound_udp[(ipPacket.src, dnsPacket.id)] > 0:
                  outbound_udp[(ipPacket.src, dnsPacket.id)] -= 1
                  print "Found in outbound_udp"
                  # Modify the destination address back to the address of the TUN on the host.
                  ipPacket.dst = "10.0.0.1"
                  try:
                    del ipPacket[TCP].chksum
                    del ipPacket[IP].chksum
                    del ipPacket[UDP].chksum
                  except IndexError:
                    print ""

                  ipPacket.show2() # Force recompute the checksum

                  if connection:
                      connection.write_message(str(ipPacket).encode('base64'))


sniffingThread = PacketSniffer()
sniffingThread.daemon = True
sniffingThread.start()

      

+3


source to share


1 answer


Several bugs have recently been fixed in Scapy around DNS (and other complex protocols, but DNS is most commonly seen):



Trying with the latest version of Scapy development from the Mercurial ( hg clone http://bb.secdev.org/scapy

) repository should fix this.

+1


source







All Articles