我正在使用python开发PCAP,并使用dpkt读取它。 PCAP文件中的数据是Linux Cooked Capture,SLL(朋友)。这是在Wireshark中看到的示例数据包:
:Frame 3: 578 bytes on wire (4624 bits), 578 bytes captured (4624 bits) Linux cooked capture Packet type: Unicast to another host (3) Link-layer address type: 1 Link-layer address length: 6 Source: VMware_ZZ:ZZ:ZZ (ZZ:ZZ:ZZ:ZZ:ZZ:ZZ) Unused: 0000 Protocol: IPv4 (0x0800) Internet Protocol Version 4, Src: XX.X.XX.XX, Dst: YYY.YY.YYY.YYY Transmission Control Protocol, Src Port: 65382, Dst Port: 443, Seq: 1, Ack: 1, Len: 522 Transport Layer Security
这是我用于获取TCP数据的代码
,该代码是字节编码的,但是就可以了,因为该程序的其余部分不在乎,我不需要人为地-可读:import dpkt pcap_path = 'example-packet.pcap' with open(pcap_path, 'rb') as fp: try: capture = dpkt.pcap.Reader(fp) except ValueError as e: raise Exception("Wrong file: %s" % e) for timestamp, buf in capture: eth = dpkt.sll.SLL(buf) print("Ethernet: ", eth) ip = eth.data print("IP: ", ip) tcp = ip.data # <-- This is line 15, for error reference print("TCP: ", tcp)
这就是该代码输出
。我转到另一个PCAP,仍然是Linux Cooked Capture,但是这个PCAP具有802.1Q VLAN上的IP]。所以这里有问题包:Ethernet: b'\x00\x03\x00\x01\x00\x06\x00PV\x9auT\x00\x00\x08\x00E\x00\x05\x8cT3@\x00... IP: b'E\x00\x05\x8cT3@\x00\x80\x06O\x1f\xac\x1f\xaed\xc6\x8f1\x06\x01\xbb%\x7f\xccz... TCP: b'\x01\xbb%\x7f\xccz\xdf\x9d\xe5\xbe\xb98\x80\x10\x01\xfd\x047\x00\x00\x01\x01...
现在,问题
:Frame 11: 577 bytes on wire (4616 bits), 577 bytes captured (4616 bits) Linux cooked capture Packet type: Unicast to another host (3) Link-layer address type: 1 Link-layer address length: 6 Source: Cisco_ZZ:ZZ:ZZ (ZZ:ZZ:ZZ:ZZ:ZZ:ZZ) Unused: 0000 Protocol: 802.1Q Virtual LAN (0x8100) 802.1Q Virtual LAN, PRI: 0, DEI: 0, ID: 1328 000. .... .... .... = Priority: Best Effort (default) (0) ...0 .... .... .... = DEI: Ineligible .... 0101 0011 0000 = ID: 1328 Type: IPv4 (0x0800) Internet Protocol Version 4, Src: XXX.XX.XXX.XX, Dst: YY.YYY.YYY.YY Transmission Control Protocol, Src Port: 54545, Dst Port: 443, Seq: 1, Ack: 1, Len: 517 Transport Layer Security
但是如果我运行相同代码
,这就是我得到的输出
Traceback (most recent call last): File "myproject.py", line 15, in <module> tcp = ip.data AttributeError: 'bytes' object has no attribute 'data' Ethernet: b'\x00\x03\x00\x01\x00\x06\x00PV\xa7(\x93\x00\x00\x81\x00\x050\x08\x00E\x00\x00... IP: b'\x050\x08\x00E\x00\x00\xb2\xba\xd6@\x004\x06y\xf7_\xf9H \xac\x15\xbdI...
现在,我认为问题在于将4字节802.1Q添加到数据包中:由于未识别IP报头,因此eth.data以字节的形式读取,因此,当我去做ip.data时,如前所述,没有属性“数据”。但是我不知道如何规避这个问题。这是另一个捕获,我知道dpkt有一个dpkt.ethernet.VLANTag8021Q类,但是就我尝试过的而言,它不适用于Linux Cooked Capture。我可以考虑移至dpkt以外的其他库,但我真的不愿意这样做,因为这是工作项目的一部分,期限非常严格。
因此,当TCP数据通过IP,802.1Q,通过dpkt在“ SLL”上时,如何访问TCP数据?
我正在使用python开发PCAP,并使用dpkt读取它。 PCAP文件中的数据是Linux Cooked Capture,SLL(朋友)。如Wireshark中所示,这是一个示例数据包:帧3:线路上的578个字节(...
我几乎感到I愧,我以前从未想到过。由于eth.data由于vlan标签而被视为字节,因此它是可切片的。因此:
for timestamp, buf in capture:
eth = dpkt.sll.SLL(buf)
print("Ethernet: ", eth)
ip = eth.data
print("IP: ", ip)
tcp = ip.data
print("TCP: ", tcp)