我正在使用dpkt来解析pcap文件,但我对如何提取目标IP地址感到困惑。我使用eth = dpkt.ethernet.Ethernet(buf)
解析数据包,它返回一个如下所示的以太网对象:
Ethernet(src='\x00\x1a\xa0kUf', dst='\x00\x13I\xae\x84,', data=IP(src='\xc0\xa8\n\n',
off=16384, dst='C\x17\x030', sum=25129, len=52, p=6, id=51105, data=TCP(seq=9632694,
off_x2=128, ack=3382015884, win=54, sum=65372, flags=17, dport=80, sport=56145)))
我对两件事感到困惑。
我尝试了像Convert "little endian" hex string to IP address in Python这样的解决方案,但两个dst字段似乎有时包含似乎无法解析为_daQ
的IP地址的数据(如何解析_daQ以解决?)或{ {1}}(什么是RT?)或RT\x00\x125\x02
(开头的33是什么,为什么这看起来像5个字节而不是4?)
答案 0 :(得分:7)
eth.dst
字段将包含目标MAC地址(例如01:23:45:67:89:ab
),而不是目标IP地址。你需要ip.dst字段。试试这个:
ip_hdr = eth.data
ip_hdr.dst # will contain your destination IP address in BINARY
# adapted from http://www.commercialventvac.com/dpkt.html#mozTocId303989
import socket
dst_ip_addr_str = socket.inet_ntoa(ip_hdr.dst)