python 如何发送带有损坏 FCS 的以太网帧?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/723635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you send an Ethernet frame with a corrupt FCS?
提问by user83753
I'm not sure if this is even possible since this might be handled in hardware, but I need to send some Ethernet frames with errors in them. I'd like to be able to create runts, jabber, misalignment, and bad FCS errors. I'm working in Python.
我不确定这是否可行,因为这可能是在硬件中处理的,但我需要发送一些有错误的以太网帧。我希望能够创建矮小、jabber、未对齐和错误的 FCS 错误。我在 Python 中工作。
回答by Charles Duffy
It canbe handled in hardware, but isn't always -- and even if it is, you can turn that off; see the ethtooloffload parameters.
它可以在硬件中处理,但并不总是——即使是,你也可以关闭它;请参阅ethtool卸载参数。
With regard to getting full control over the frames you create -- look into PF_PACKET(for one approach) or the tap driver(for another).
关于完全控制您创建的帧 - 查看PF_PACKET(对于一种方法)或tap 驱动程序(对于另一种方法)。
Here's an article on using PF_PACKET to send hand-crafted frames from Python.
这是一篇关于使用 PF_PACKET 从 Python 发送手工制作的帧的文章。
回答by brice
First, you disable your ethernet card's checksumming:
首先,禁用以太网卡的校验和:
sudo ethtool -K eth1 tx off
Then, you send the corrupt frames from python:
然后,您从 python 发送损坏的帧:
#!/usr/bin/env python
from socket import *
#
# Ethernet Frame:
# [
# [ Destination address, 6 bytes ]
# [ Source address, 6 bytes ]
# [ Ethertype, 2 bytes ]
# [ Payload, 40 to 1500 bytes ]
# [ 32 bit CRC chcksum, 4 bytes ]
# ]
#
s = socket(AF_PACKET, SOCK_RAW)
s.bind(("eth1", 0))
src_addr = "\x01\x02\x03\x04\x05\x06"
dst_addr = "\x01\x02\x03\x04\x05\x06"
payload = ("["*30)+"PAYLOAD"+("]"*30)
checksum = "\x00\x00\x00\x00"
ethertype = "\x08\x01"
s.send(dst_addr+src_addr+ethertype+payload+checksum)
看到一个类似的问题
回答by vilanova
try using scapy for python, there are examples to generate jumbo frames, a runt frames too. http://www.dirk-loss.de/scapy-doc/usage.html
尝试使用 scapy for python,有一些示例可以生成巨型帧,还有一个矮帧。http://www.dirk-loss.de/scapy-doc/usage.html
回答by deagol
The program did not work as intended for me to generate FCS errors.
该程序没有按照我的预期运行以生成 FCS 错误。
The network driver added the correct checksum at the end of the generated frame again. Of course it's quite possible that the solution is working for some cards, but I'm sure not with any from Intel. (It's also working without any ethtool changes for me.)
网络驱动程序再次在生成的帧的末尾添加了正确的校验和。当然,该解决方案很可能适用于某些卡,但我确定英特尔的任何卡都不会。(它也可以在没有任何 ethtool 更改的情况下工作。)
With at least an Intel e1000e network card you need a small change to the code above: Add the following line after "s = socket(AF_PACKET, SOCK_RAW)":
至少使用 Intel e1000e 网卡,您需要对上面的代码稍作更改:在“s = socket(AF_PACKET, SOCK_RAW)”之后添加以下行:
s.setsockopt(SOL_SOCKET,43,1)
This tell the NIC driver to use the "SO_NOFCS" option defined in socket.h and send the frame out without calculating and adding the FCS.
这告诉 NIC 驱动程序使用 socket.h 中定义的“SO_NOFCS”选项并发送帧而不计算和添加 FCS。
You may also be interested in the following C-programm, which did show me how to do it: http://markmail.org/thread/eoquixklsjgvvaom
您可能还对以下 C 程序感兴趣,它确实向我展示了如何操作:http: //markmail.org/thread/eoquixklsjgvvaom
But be aware that the program will not work on recent kernels without a small change. The SOL_SOCKET seems to have changed the numeric ID from 42 to 43 at some time in the past.
但请注意,如果没有小的更改,该程序将无法在最近的内核上运行。SOL_SOCKET 似乎在过去的某个时间将数字 ID 从 42 更改为 43。
According to the original author the feature should be available for at least the following drivers: e100, e1000, and e1000e. A quick grep in the kernel sources of 3.16.0 is indicating that ixgbe igb and i40e should also work. If you are not using any of these cards this socket option will probably not be available.
根据原作者的说法,该功能至少应可用于以下驱动程序:e100、e1000 和 e1000e。3.16.0 内核源代码中的快速 grep 表明 ixgbe igb 和 i40e 也应该可以工作。如果您没有使用这些卡中的任何一个,则此插槽选项可能不可用。