格式错误的DNS响应包(python + scapy)

时间:2015-01-09 18:01:28

标签: python tcp dns scapy

我正在使用Python和scapy创建代理服务器。 TCP数据包似乎工作正常,但我遇到了UDP的一些问题,特别是DNS请求。基本上当DNS请求进来时我会在我的脚本中捕获它,执行DNS查找,并尝试将其返回给请求DNS查询的人。该脚本成功执行查找并返回DNS响应,但是当查看wireshark时,它告诉我它是“格式错误的数据包”。有人可以告诉我我需要做什么才能正确返回DNS响应吗?

#!/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()

1 个答案:

答案 0 :(得分:1)

最近在Scapy中围绕DNS(和其他复杂的协议,但DNS是最常见的)修复了一些错误:

尝试使用Mercurial存储库(hg clone http://bb.secdev.org/scapy)中的最新Scapy开发版本来解决此问题。