【发布时间】: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 文件来捕获那些格式错误的数据包吗?您确定校验和有效吗?