在 Python 2.6 中解析 PCAP

发布于 2025-01-06 23:58:03 字数 413 浏览 2 评论 0原文

我试图简单地解析数据包捕获中的数据。我举了一些例子只是为了看看是否可以编译,但最终出现了错误。下面是代码。

import dpkt
import sys

f = open('test.pcap')
pcap = dpkt.pcap.Reader(f)

for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    ip = eth.data
    tcp = ip.data


f.close()

我得到的错误如下:File "inspection.py", line 15, in tcp = ip.data

AttributeError: 'str' object has no attribute 'data'

任何帮助将不胜感激。

I am trying to simply parse through data in a packet capture. I've taken examples just to see if I could compile and I end up with an error. Below is the code.

import dpkt
import sys

f = open('test.pcap')
pcap = dpkt.pcap.Reader(f)

for ts, buf in pcap:
    eth = dpkt.ethernet.Ethernet(buf)
    ip = eth.data
    tcp = ip.data


f.close()

The error I get is the following:File "inspection.py", line 15, in tcp = ip.data

AttributeError: 'str' object has no attribute 'data'

Any help would be appreciated.

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

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

发布评论

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

评论(2

梦纸 2025-01-13 23:58:03

dpkt.ethernet.Ethernet(buf) 的调用返回了一个字符串,因为以太网类无法解压 buf。造成这种情况的可能原因是您的 pcap 文件没有以太网作为其第 2 层协议。您可以将 pcap 加载到 Wireshark 中来确认这一点。

以下脚本尝试检查 pcap 文件的数据链路字段并使用适当的第 2 层 dpkt 类来解码帧:

import dpkt
import sys

f = open('test.pcap')
pcap = dpkt.pcap.Reader(f)

for ts, buf in pcap:
    if pcap.datalink() == dpkt.pcap.DLT_LINUX_SLL:
        l2 = dpkt.sll.SLL(raw_pkt)
    else:
        l2 = dpkt.ethernet.Ethernet(buf)
    ip = l2.data
    tcp = ip.data

The call to dpkt.ethernet.Ethernet(buf) returned a string because the Ethernet class was unable to unpack buf. A likely cause for this is that your pcap file does not have ethernet as its layer 2 protocol. You can load the pcap into Wireshark to confirm this.

The following script attempts to check the datalink field of the pcap file and use an appropriate layer 2 dpkt class to decode the frame:

import dpkt
import sys

f = open('test.pcap')
pcap = dpkt.pcap.Reader(f)

for ts, buf in pcap:
    if pcap.datalink() == dpkt.pcap.DLT_LINUX_SLL:
        l2 = dpkt.sll.SLL(raw_pkt)
    else:
        l2 = dpkt.ethernet.Ethernet(buf)
    ip = l2.data
    tcp = ip.data
任谁 2025-01-13 23:58:03

我为解决该问题所做的事情是:

        if ip.p == 6:
           tcp = dpkt.tcp.TCP(ip.data)

What I did to solve the problem was:

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