我正在使用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字段似乎有时都包含似乎无法解析到ip地址(如_daQ )的数据( _daQ是如何解析为地址的?)或者RT\x00\x125\x02 (什么是RT?)或者33\x00\x01\x00\x03 (开头的33是什么,为什么这看起来像5个字节而不是4个字节?)
发布于 2014-08-18 20:25:13
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)https://stackoverflow.com/questions/25370010
复制相似问题