Kua*_*aly 2 python pcap packet-sniffers dpkt
我实际上是想用python嗅探数据包(使用pypcap和dpkt).
我尝试了以下方法:
import dpkt, pcap
pc = pcap.pcap() # construct pcap object
pc.setfilter('src host X.X.X.X or dst host X.X.X.X')
for timestamp, packet in pc:
print dpkt.ethernet.Ethernet(packet)
Run Code Online (Sandbox Code Playgroud)
但是当我启动脚本时没有任何反应......我错过了什么吗?
在OS X Yosemite上使用Python 2.7(10.10)
问题是陈旧的,但对于那些可能会遇到这种情况的新人来说 github'chain'项目使用pypcap和dpkt来完成这种事情(免责声明:我参与了所有三个项目:) https://github.com/SuperCowPowers/chains
对于那些只想使用pypcap/dpkt的人来说,这是一个有效的代码片段:
import pcap
import dpkt
sniffer = pcap.pcap(name=None, promisc=True, immediate=True)
for timestamp, raw_buf in sniffer:
output = {}
# Unpack the Ethernet frame (mac src/dst, ethertype)
eth = dpkt.ethernet.Ethernet(raw_buf)
output['eth'] = {'src': eth.src, 'dst': eth.dst, 'type':eth.type}
# It this an IP packet?
if not isinstance(eth.data, dpkt.ip.IP):
print 'Non IP Packet type not supported %s\n' % eth.data.__class__.__name__
continue
# Grab ip packet
packet = eth.data
# Pull out fragment information
df = bool(packet.off & dpkt.ip.IP_DF)
mf = bool(packet.off & dpkt.ip.IP_MF)
offset = packet.off & dpkt.ip.IP_OFFMASK
# Pulling out src, dst, length, fragment info, TTL, checksum and Protocol
output['ip'] = {'src':packet.src, 'dst':packet.dst, 'p': packet.p,
'len':packet.len, 'ttl':packet.ttl,
'df':df, 'mf': mf, 'offset': offset,
'checksum': packet.sum}
print output
Run Code Online (Sandbox Code Playgroud)