python - 在 python 中解析 ERF 捕获文件

标签 python

在 python 中解析 ERF (endace) 捕获文件的最佳方法是什么?我找到了 python 的 libpcap 包装器,但我认为 lipcap 不支持 ERF 格式。

谢谢!

最佳答案

这是一个简单的 ERF 记录解析器,它为每个数据包返回一个字典(我只是把它拼凑在一起,所以没有经过很好的测试。并不是所有的标志字段都被解码,但那些没有被解码的,并不广泛适用):

注意:

  • ERF 记录类型:1 = HDLC、2 = 以太网、3 = ATM、4 = 重新组装的 AAL5、5-7 种带有额外 header 的多 channel 变体,此处未处理。
  • 如果快照长度太短,
  • rlen 可以小于 wlen+len(header)
  • 间隙丢失计数器是 Dag 数据包处理器在其输入队列溢出时记录的此数据包与先前捕获的数据包之间丢失的数据包数。
  • 如果您不想使用 scapy,请注释掉这两行 scapy。

代码:

import scapy.layers.all as sl

def erf_records( f ):
    """
    Generator which parses ERF records from file-like ``f``
    """
    while True:
        # The ERF header is fixed length 16 bytes
        hdr = f.read( 16 )
        if hdr:
            rec = {}
            # The timestamp is in Intel byte-order
            rec['ts'] = struct.unpack( '<Q', hdr[:8] )[0]
            # The rest is in network byte-order
            rec.update( zip( ('type',  # ERF record type
                              'flags', # Raw flags bit field
                              'rlen',  # Length of entire record
                              'lctr',  # Interstitial loss counter
                              'wlen'), # Length of packet on wire
                             struct.unpack( '>BBHHH', hdr[8:] ) ) )
            rec['iface']  = rec['flags'] & 0x03
            rec['rx_err'] = rec['flags'] & 0x10 != 0
            rec['pkt'] = f.read( rec['rlen'] - 16 )
            if rec['type'] == 2:
                # ERF Ethernet has an extra two bytes of pad between ERF header
                # and beginning of MAC header so that IP-layer data are DWORD
                # aligned.  From memory, none of the other types have pad.
                rec['pkt'] = rec['pkt'][2:]
                rec['pkt'] = sl.Ether( rec['pkt'] )
            yield rec
        else:
            return

关于python - 在 python 中解析 ERF 捕获文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10672215/

相关文章:

python - 在 Python 3.4 中使用 for 循环从数组中删除特定元素

python - 根据 python 列表中的出现情况移动文件

python - GUI 按钮调用函数的问题,提示用户在 GUI 中输入然后显示输出

python - POST 请求不起作用

python - 在 python 中,如何获取 gtk 标签的大小(宽度,高度)?

python - 有没有办法获取通过 werkzeug(flask) 请求发送的 Immutabledict 中嵌套字典的值?

python - 如何在python中执行shell命令?

python - 如何在 Mac OS X 上强制使用 64 位 python?

Python 语法错误 : Non-UTF-8

python - 为什么网络服务器提示西里尔字母,而命令行则不然?