在python中解析ERF(endace)捕获文件的最佳方法是什么我发现了一个用于python的libpcap包装器,但我不认为lipcap支持erf格式。
谢谢!

最佳答案

这里有一个简单的erf记录解析器,它返回每个包的dict(我只是把它放在一起,所以没有经过很好的测试)。并不是所有的标志字段都被解码,但那些不被解码的字段并不广泛适用):
注:
ERF记录类型:1=HDLC,2=以太网,3=ATM,4=重新组装的AAL5,5-7多通道变体,此处不处理额外的报头。
如果快照长度太短,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

09-25 21:18