python - 从未收到 Python 原始套接字帧
问题描述
我正在使用 RHEL6 计算机,并尝试通过 RAW 套接字与 Windows XP 计算机通信。
当我在 RHEL 计算机上收到特定帧时,使用 RAW 套接字的 Python 2 脚本会处理该帧并在将其发送到 Windows 计算机之前更改以下字段:
- 目标 MAC
- 目标IP
- IP ID
- 校验和
当我在 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 套接字数据包没有被目标正确处理吗?
解决方案
您需要在操作系统上启用混杂模式,否则它只会在数据包到达您的应用程序之前将其杀死。看起来像这样:
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/
推荐阅读
- java - 递归遍历数组并计算像素
- javascript - Node.js:意外省略 var 声明通过全局对象、错误或功能公开所有变量?
- swift - 领域崩溃,错误原因:'属性'人'被声明为'NSArray',即使我已经删除了数组
- c# - Rx - 方法在哪里导致内存泄漏?
- ethereum - Solidity 是否根据调用合约方法之前和之后使用的总存储量来优化 gas 使用?
- reactjs - 当超过 2 个 reducer 时,Webpack 4 不生成 webpackBootstrap 函数
- python-3.x - 获取在 Python turtle 中工作的键绑定
- msbuild - 自定义道具/目标的 MSBuild 设计实践
- c# - 填充有空实体的列表视图
- javascript - Why does push of local array mutate global array?