Scapy生成Dot11元素时添加'c2'字节的异常原因及解决方法问询
Why Scapy Adds a
c2 Byte Between 0f and ac in Dot11 Elements & How to Fix It The Root Cause
That extra c2 byte is a result of unintended UTF-8 encoding in Scapy. Here's the breakdown:
- When you pass a standard Python string (like
"\\xac") as theinfovalue for aDot11Eltobject, Scapy assumes it's text that needs UTF-8 encoding. - The byte
0xacfalls outside the 7-bit ASCII range (0x00-0x7F). In UTF-8, values above 0x7F get converted into multi-byte sequences—specifically,0xacbecomesc2 ac, which is why you see that extrac2inserted.
The Fix: Use Raw Byte Strings
To stop Scapy from encoding your binary data, you need to pass a byte string (prefixed with b) instead of a regular string for the info field. This tells Scapy to treat the data as raw binary, not text that needs formatting.
Wrong Approach (Causes the c2 Byte)
from scapy.layers.dot11 import Dot11, Dot11Elt, Dot11Beacon from scapy.sendrecv import sendp # Regular string triggers UTF-8 encoding bad_element = Dot11Elt(ID=0x0f, info="\xac") beacon_frame = Dot11(type=0, subtype=8, addr1="ff:ff:ff:ff:ff:ff", addr2="00:11:22:33:44:55", addr3="00:11:22:33:44:55") / Dot11Beacon(cap="ESS") / bad_element sendp(beacon_frame, iface="wlan0mon", loop=1)
Correct Approach (Eliminates the c2 Byte)
from scapy.layers.dot11 import Dot11, Dot11Elt, Dot11Beacon from scapy.sendrecv import sendp # Byte string preserves raw binary data good_element = Dot11Elt(ID=0x0f, info=b"\xac") beacon_frame = Dot11(type=0, subtype=8, addr1="ff:ff:ff:ff:ff:ff", addr2="00:11:22:33:44:55", addr3="00:11:22:33:44:55") / Dot11Beacon(cap="ESS") / good_element sendp(beacon_frame, iface="wlan0mon", loop=1)
Quick Check
After updating your code, capture the beacon frame with Wireshark or a similar tool. You should see the 0f element ID followed directly by ac—no extra c2 byte will be present.
内容的提问来源于stack exchange,提问作者I'm Shyan
相关产品推荐
相关产品推荐

