You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Scapy Python3制作Ethernet与IEEE802.3帧及问题排查

以太网帧制作与识别问题解决

原始代码

import scapy.all as scapy

#Here i define some arguments i want to modify at layer 2
payload = "hello, here is my first message!"
destinationMAC = "ff:ff:ff:ff:ff:ff"
sourceMAC = "78:dd:08:cf:fb:8a"
etherTypeHex = "0x0800"
etherType = int(etherTypeHex, 16)

#Here creating a ethernet2 frame 
myEtherFrame = scapy.Ether(
    src=sourceMAC,
    dst=destinationMAC,
    type=etherType) #there is no further details like FCS, or payload!

问题解答

1. 为什么你的Ethernet帧被识别为IEEE802.3帧?

Ethernet II和IEEE802.3帧的核心区别在于帧头第13-14字节的用途:

  • Ethernet II用这两个字节表示协议类型(Type),取值≥0x0600(1536)
  • IEEE802.3用这两个字节表示载荷长度(Length),取值≤0x05DC(1500)

你当前创建的帧未添加payload,实际长度远小于1536,Wireshark会自动将该字段判定为长度字段,因此识别为IEEE802.3帧。另外Scapy默认会自动添加FCS,但帧结构不符合Ethernet II判定规则时,依然会被误识别。

2. 制作可正常发送的完整Ethernet2帧

要生成符合标准且可正常发送的Ethernet2帧,需添加payload,让Type字段的数值符合Ethernet II判定逻辑,Scapy会自动处理FCS的计算与添加。修改代码如下:

import scapy.all as scapy

payload = "hello, here is my first message!"
destinationMAC = "ff:ff:ff:ff:ff:ff"
sourceMAC = "78:dd:08:cf:fb:8a"
etherType = 0x0800  # 直接使用十六进制值,无需额外转换

# 创建完整Ethernet2帧并挂载payload
myEtherFrame = scapy.Ether(src=sourceMAC, dst=destinationMAC, type=etherType) / payload

# 发送二层帧(替换为你的实际网卡名称,比如"eth0"或"Wi-Fi")
scapy.sendp(myEtherFrame, iface="你的网卡名称")

3. 制作IEEE802.3帧

IEEE802.3帧需使用Dot3层对应MAC层,并配合LLC层逻辑链路控制层标识上层协议,示例代码如下:

import scapy.all as scapy

payload = "hello, here is my first message!"
destinationMAC = "ff:ff:ff:ff:ff:ff"
sourceMAC = "78:dd:08:cf:fb:8a"

# 构建IEEE802.3帧:Dot3(MAC层) + LLC(逻辑链路控制) + payload
dot3_frame = scapy.Dot3(src=sourceMAC, dst=destinationMAC, len=len(payload)) / scapy.LLC() / payload

# 发送二层帧(替换为你的实际网卡名称)
scapy.sendp(dot3_frame, iface="你的网卡名称")

内容的提问来源于stack exchange,提问作者Abolfazl

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 15:31:58