我正在使用 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()
最佳答案
最近在 Scapy 中围绕 DNS(和其他复杂的协议(protocol),但 DNS 是最常见的)修复了一些错误:
- https://bitbucket.org/secdev/scapy/issue/913/
- https://bitbucket.org/secdev/scapy/issue/5104/
- https://bitbucket.org/secdev/scapy/issue/5105/
尝试使用来自 Mercurial 存储库 (hg clone http://bb.secdev.org/scapy
) 的最新 Scapy 开发版本应该可以解决这个问题。
关于python - 格式错误的 DNS 响应数据包(python + scapy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27866478/