首页 > 解决方案 > 从未收到 Python 原始套接字帧

问题描述

我正在使用 RHEL6 计算机,并尝试通过 RAW 套接字与 Windows XP 计算机通信。

当我在 RHEL 计算机上收到特定帧时,使用 RAW 套接字的 Python 2 脚本会处理该帧并在将其发送到 Windows 计算机之前更改以下字段:

当我在 Wireshark 中看到数据包时,数据包到达了我的 Windows XP 计算机,但它从未到达应用层,因为需要数据包的软件没有反应。

这就是我创建发送数据包的方式:

import socket, binascii, optparse
s=socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(3))
s.bind(('eth1',0))
while True:

    result =  s.recv(65535)
    if binascii.hexlify(result[30:34]).decode() == "<WANTED FRAME IP>":
        result2 = "<DEST_MAC>".decode("hex") +  result[6:18] + "<IP_ID>".decode("hex") + result[20:24] + "<CHECKSUM>".decode("hex") + result[26:30] + "<DEST_IP>".decode("hex") + result[34:]
    s.send(result2)

当我尝试使用“经典”套接字时,目标软件正确接收数据包,但这不是我想要的行为,因为我必须使用 RAW 套接字来发送它们。

我尝试使用相同的代码发送其他简单的UDP数据包,结果我得到了相同的行为,在wireshark上正确看到了数据包,但从未到达我的Windows XP上的应用程序层。

知道为什么我的 RAW 套接字数据包没有被目标正确处理吗?

标签: pythonwindows-xpwiresharkrhelraw-sockets

解决方案


您需要在操作系统上启用混杂模式,否则它只会在数据包到达您的应用程序之前将其杀死。看起来像这样:

sniffer = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket_protocol)
sniffer.bind((host, 0))
if os.name == “nt”:
    sniffer.ioctl(socket.SIO_RCVALL, socket.RCVALL_ON)

这是有关如何在 linux 和 Windows 上使用 python 启用它的教程:https ://codingsec.net/2016/05/packet-sniffing-windows-linux-using-python/


推荐阅读