Linux cooked-mode capture 格式转换

本文详细介绍了如何使用TCPdump进行网络数据包抓取,并重点讲解了-i选项与-y选项的功能区别。当使用any作为-i参数时,抓取的数据包将以Linuxcookedcapture头部形式出现,而非以太网头部。文章还提供了将Linuxcookedcapture格式转换为Ethernet格式的多种方法,包括使用tcprewrite工具的快速转换技巧。
部署运行你感兴趣的模型镜像

 

tcpdump抓包时,如果-i选项指定为一个网卡地址,那么抓取的数据包数据链路层是以太网头部;如果指定any,则以太网头部将被替换为linux cooked capture头部

 # tcpdump -i any -w linux_sll.pcap
tcpdump: listening on any, link-type LINUX_SLL (Linux cooked), capture size 96 bytes

 

 # tcpdump -i eth1 -w enet.pcap
tcpdump: listening on eth1, link-type EN10MB (Ethernet), capture size 96 bytes

 

tcpdump抓包时可以通过 -y 选项来指定data link type,不过测试发现 -i 选项指定 any 时,不支持抓获的包的data link type 为以太网 :

 # tcpdump -i any -w test.pcap -y EN10MB
tcpdump: EN10MB is not one of the DLTs supported by this device
 # tcpdump -i eth1 -w test.pcap -y EN10MB
tcpdump: data link type EN10MB
 # 

 

这时,若需要将linux cooked capture格式的包转换为Ethernet格式,有那么几种方法:

1. 写代码读出每一个包后再改写到新文件(使用libpcap或者基于pcap头部结构体偏移);

2. tcpdump 3.0+ 版本下,可以用tcprewrite直接改写,这应该是最快捷的方法;

DLT Plugins
As of 3.0, tcprewrite uses plugins to support different DLT/Layer 2 types. This not only makes the 
code easier to maintain, but also helps make things clearer for users regarding what is and isn't
supported. Each plugin may support reading and/or writing packets. By default, the plugin used to
read packets is also used for output, but you can override the output plugin using the --dlt option.
Changing the DLT plugin allows you to convert the packets from one DLT/Layer 2 type to another type.
This allows you for example to capture traffic on say an Ethernet interface and replay over Cisco
HDLC or capture on a BSD Loopback interface and replay over Ethernet. Plugins supported in output mode: Ethernet (enet) Cisco HDLC (hdlc) User defined Layer 2 (user) Plugins supported in input mode: Ethernet Cisco HDLC Linux SLL BSD Loopback BSD Null Raw IP 802.11 Juniper Ethernet (version >= 4.0) Hence, if you have a pcap in one of the supported input DLT types, you can convert it to one of the
supported output DLT type by using the --dlt=<output> option. Depending on the input DLT you may
need to provide additional DLT plugin flags.

 

tcprewrite转换命令如下:

 # tcpdump -r linux_sll.pcap
reading from file linux_sll.pcap, link-type LINUX_SLL (Linux cooked)
 # tcprewrite --dlt=enet --infile=linux_sll.pcap  --outfile=enet.pcap
 # tcpdump -r enet.pcap
reading from file enet.pcap, link-type EN10MB (Ethernet)
 #

 

唯一有点问题的,是转换后的数据的Destination-Mac为空, 对这个字段有需求的要注意下:

 

可以参考的网址:

https://wiki.wireshark.org/SLL

http://www.tcpdump.org/linktypes.html

http://tcpreplay.synfin.net/wiki/tcprewrite

 

其它:

# tips 删除vlan
# tcprewrite --enet-vlan=del --infile=enet.pcap --outfile=output.pcap

  

转载于:https://www.cnblogs.com/aios/p/9545378.html

您可能感兴趣的与本文相关的镜像

HunyuanVideo-Foley

HunyuanVideo-Foley

语音合成

HunyuanVideo-Foley是由腾讯混元2025年8月28日宣布开源端到端视频音效生成模型,用户只需输入视频和文字,就能为视频匹配电影级音效

### Linux Cooked Capture v1 在网络协议栈中的层级 Linux Cooked Capture v1 是一种特殊的链路层封装格式,主要用于在 Linux 系统中进行抓包操作时,处理那些无法直接提供标准以太网帧头的接口数据。它在网络协议栈中处于**链路层**(OSI模型的第二层)[^1]。 该格式并非标准的以太网帧结构,而是 Linux 内核在捕获数据包时,对原始帧头进行替换的一种“烹饪”格式。这种格式通常出现在使用 `tcpdump` 并指定 `-i any` 参数进行抓包的场景中。由于 `-i any` 会捕获所有网卡上的流量,系统无法保留原始的以太网帧头,因此使用 Linux Cooked Capture v1 来模拟链路层信息[^2]。 Linux Cooked Capture v1 的结构中包含了一些元数据,如链路层地址类型、源地址长度、源地址(通常为虚假 MAC 地址)以及协议类型等。这些信息在链路层处理中用于标识数据包的来源和协议类型,但与标准的以太网帧不同,它并不包含完整的帧头信息,例如目标 MAC 地址或 VLAN 标签[^3]。 Wireshark 等工具在解析此类数据包时,会识别出 Linux Cooked Capture v1 并将其标记为一种“伪协议”(pseudo-protocol),因为它并非实际的物理链路层协议,而是为了便于分析而引入的封装格式[^4]。 ### 示例代码:解析 Linux Cooked Capture v1 数据包 以下是一个使用 Python 的 `scapy` 库解析 Linux Cooked Capture v1 数据包的示例: ```python from scapy.all import rdpcap, Ether # 读取 pcap 文件 packets = rdpcap('capture.pcap') # 遍历所有数据包 for packet in packets: if packet.haslayer(Ether): # 打印以太网帧信息 print("Source MAC:", packet[Ether].src) print("Destination MAC:", packet[Ether].dst) print("Protocol Type:", hex(packet[Ether].type)) else: # 如果是 Linux Cooked Capture 格式,打印 SLL 信息 print("Linux Cooked Capture Packet") print("Packet Type:", packet.packet_type) print("Source MAC:", packet.src) print("Protocol Type:", hex(packet.protocol)) ``` 该代码展示了如何使用 `scapy` 来读取一个包含 Linux Cooked Capture v1 数据包的 `.pcap` 文件,并根据数据包类型判断其链路层信息。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值