赞
踩
我试了 dpkt 是可以的。
解析文件 2019_0416_1558_38.pcap 第一个报文是成功的,与 WireShark 展示一致。
#coding=utf-8
import os
import sys
from dpkt.ip import IP
from dpkt.pcap import Reader as PReader
from dpkt.ethernet import Ethernet
from socket import inet_ntop
from socket import AF_INET
curpath = os.path.dirname(os.path.realpath(__file__))
import dpkt
print("dpkt.__version__= {}".format(dpkt.__version__))
def main():
p = "2019_0416_1558_38.pcap"
p = os.path.join(curpath, p)
cnt = 0
with open(p, "rb") as fr:
rd = PReader(fr)
for pkt in rd:
cnt+=1
# bs is str
bs = pkt[1]
eth = Ethernet(bs)
ip = eth.ip.gre.ethernet.ip
tcp = ip.tcp
print("[{}]{}->{} {}->{}".format(
cnt,
inet_ntop(AF_INET, ip.src),
inet_ntop(AF_INET, ip.dst),
tcp.sport, tcp.dport))
if cnt > 10:
break
print("main exit")
if __name__ == '__main__':
main()
因为要解析 GRE.proto = (ETH_TYPE_ERSPAN1 = 0x88be),现有dpkt (1.9.2) 不支持此协议,因此要仿照此次 commit https://github.com/kbandla/dp... 增加 patch
ETH_TYPE_ERSPAN1 = 0x88be
...
Ethernet.set_type(ETH_TYPE_ERSPAN1, Ethernet)
然后就成功解析了。
输出
dpkt.__version__= 1.9.2
[1]10.126.44.15->10.120.50.5 63343->7191
[2]10.103.17.176->10.120.101.14 44589->80
[3]10.126.41.14->10.120.101.14 21632->80
[4]10.103.32.111->10.120.13.23 43223->9088
[5]10.120.182.22->10.120.91.61 26951->9088
[6]10.136.18.18->10.120.89.46 14273->4242
[7]10.120.175.36->10.120.186.25 39582->9504
[8]10.120.166.32->10.120.191.22 35683->9089
[9]10.103.32.112->10.120.163.33 52748->9089
[10]10.120.180.17->10.120.91.50 11881->9000
[11]10.126.51.16->10.120.91.17 31950->8193
main exit
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。