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

Malformed DNS response packet (python + scapy)

我正在使用 Python 和 scapy 创建代理服务器。 TCP 数据包似乎工作正常,但我 运行 遇到 UDP 的一些问题,特别是 DNS 请求。本质上,当收到 DNS 请求时,我会在脚本中捕获它,执行 DNS 查找,然后尝试 return 将其返回给请求 DNS 查询的人。该脚本成功执行查找和 returns DNS 响应,但是在查看 wireshark 时它告诉我它是 "Malformed Packet"。有人可以告诉我我需要做什么才能正确 return 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()

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

尝试使用来自 Mercurial 存储库 (hg clone http://bb.secdev.org/scapy) 的最新 Scapy 开发版本应该可以解决这个问题。