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

2024-04-25 00:23:59 发布

您现在位置:Python中文网/ 问答频道 /正文

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

Tags: thefromimportselfifdnsoutboundconnection