我在Scapy中构建了一个网络嗅探器,但它不能处理我嗅探的数据包的速率(它增加了15-20分钟的延迟,这是不可接受的)。我以前以这种速度成功使用过Pcapy,但这一次为了避免重写所有使用Scapy的解析代码,我想将Pcapy接收到的数据包转换为Scapy IP对象。问题是,当我尝试这样做时,我得到的IP和协议号是混乱的/不可用的,就像Scapy读取了错误的数据包部分一样。
下面是一些示例代码:
#!/usr/bin/python
from pcapy import findalldevs, open_live
from impacket import ImpactDecoder, ImpactPacket
from scapy.all import *
def sniff():
interface = "eth3"
print "Listening on: %s" % interface
# Open a live capture
reader = open_live(interface, 65535, 1, 100)
# Set a filter to be notified only for TCP packets
reader.setfilter('ip proto \\tcp')
# Run the packet capture loop
reader.loop(0, callback)
def callback(hdr, data):
pkt = IP(data)
if IP in pkt:
print pkt[IP].dst
# Parse the Ethernet packet
#decoder = ImpactDecoder.EthDecoder()
#ether = decoder.decode(data)
# Parse the IP packet inside the Ethernet packet
#iphdr = ether.child()
# Parse the TCP packet inside the IP packet
#tcphdr = iphdr.child()
# Only process SYN packets
#if tcphdr.get_SYN() and not tcphdr.get_ACK():
# # Get the source and destination IP addresses
# src_ip = iphdr.get_ip_src()
# dst_ip = iphdr.get_ip_dst()
# # Print the results
# print "Connection attempt %s -> %s" % (src_ip, dst_ip)
def main():
sniff()
if __name__ == "__main__":
main()输出的示例如下:
30.184.113.84
0.120.231.205
30.184.113.91
5.64.113.97
0.120.231.206
21.248.113.98
0.120.231.207
0.120.231.208
0.120.231.209
0.120.231.210
0.120.231.211
0.48.243.73正如你所看到的,这些IP没有意义,你认为我哪里错了?Eth3连接到NetGear镜像端口。
耽误您时间,实在对不起。
发布于 2015-05-05 17:18:34
不要紧,我是个笨蛋,我责怪银行假日星期一。我试图检测来自错误层的数据包。将raw转换为Ether,Scapy会替我完成剩下的工作。
def callback(hdr, data):
pkt = Ether(data)
if IP in pkt:
print pkt[IP].dst
else:
print list(pkt)干杯
https://stackoverflow.com/questions/30048578
复制相似问题