如何从pcap文件中提取以太网级数据?

发布于 2024-12-20 21:24:30 字数 225 浏览 3 评论 0原文

我有一些pcap文件,之前我使用tshark结合python来提取源IP地址、时间戳等。

但是,现在我在 Wireshark 中打开这些 pcap 文件。它还包含 VLAN 信息,VID 是我现在要提取的内容。

我在终端中使用tshark -r xx.pcap,它只能显示tcp级别信息,我无法获取这个VLAN ID。有谁知道如何用Python 来做这件事吗?使用一些库或工具?

I have some pcap files, previously I used tshark combined python to extract source IP address, timestamp, ect.

However, now I open these pcap files in Wireshark. It also contains the VLAN info, VID is the thing I want to extract right now.

I use tshark -r xx.pcap in the terminal, it can only show the tcp level info, I can not get this VLAN ID. Does anyone one know how to do it in Python? use some library or tool?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗心人 2024-12-27 21:24:30

完美的解决方案是 Scapy

在这个例子中,我创建一个带有 vlan 的数据包,然后打印 vlan ID

from scapy.all import *
pkt=Ether()/Dot1Q(vlan=0x32)/IP(dst="192.168.1.66")/ICMP()
print pkt[Dot1Q].vlan

这个例子展示了如何读取 pcap 文件并打印

from scapy.all import *
from scapy.utils import *
pkts=rdpcap("filename.pcap")
for pkt in pkts:
    if pkt.haslayer(Dot1Q):
        print pkt[Dot1Q].vlan 

经过测试的 VLAN ID,效果非常好。

the perfect solution is Scapy

In this example i create a packet with vlan and then print the vlan ID

from scapy.all import *
pkt=Ether()/Dot1Q(vlan=0x32)/IP(dst="192.168.1.66")/ICMP()
print pkt[Dot1Q].vlan

and this example shows how to read a pcap file and print the VLAN ID

from scapy.all import *
from scapy.utils import *
pkts=rdpcap("filename.pcap")
for pkt in pkts:
    if pkt.haslayer(Dot1Q):
        print pkt[Dot1Q].vlan 

tested and works perfect.

只有一腔孤勇 2024-12-27 21:24:30

您可以使用 Scapy 来实现:

from scapy.all import *

recs = rdpcap("yourpcap.pcap")

# extract vlan id from first record
for rec in recs:
    dot1q = rec.get_layer(Dot1Q)
    if dot1q is None:
        # not vlan here, skip.
        continue
    print 'Vlanid found:', dot1q.vlan, 'on packet', rec

未经测试,但可以工作。要学习 scapy,最好是启动它,并使用自动完成功能,并阅读教程:)

You could use Scapy for that:

from scapy.all import *

recs = rdpcap("yourpcap.pcap")

# extract vlan id from first record
for rec in recs:
    dot1q = rec.get_layer(Dot1Q)
    if dot1q is None:
        # not vlan here, skip.
        continue
    print 'Vlanid found:', dot1q.vlan, 'on packet', rec

Not tested, but could work. To learn scappy, best is to launch it, and play with auto completion, and read tutorials :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文