【问题标题】:Malformed DNS response packet (python + scapy)格式错误的 DNS 响应数据包(python + scapy)
【发布时间】:2015-01-09 18:01:27
【问题描述】:

我正在使用 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()

【问题讨论】:

  • 你能提供一个 pcap 文件来捕获那些格式错误的数据包吗?您确定校验和有效吗?

标签: python tcp dns scapy


【解决方案1】:

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

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

【讨论】:

  • 升级似乎解决了这个问题,谢谢!但是有一个问题 - 在 Wireshark 中,我看到一个 DNS 查询,后面跟着正确的答案并且没有错误,但是 nslookup 从未收到答案并显示 connection timed out; no servers could be reached。知道为什么/如何正确返回响应但 nslookup 超时吗?
  • 这听起来不熟悉,我不知道,对不起。您可能应该提出一个新问题并提供一个 PCAP 文件来帮助人们弄清楚发生了什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2011-05-07
  • 2022-07-20
  • 2016-10-22
相关资源
最近更新 更多