我将尝试用一个简化的例子来证明我的问题。
以下是一个非常简单的(单线程)数据包嗅探器(ICMP):
from scapy.all import *
m_iface = "wlan0"
m_dst = "192.168.0.1"
def print_summary(pkt):
print pkt.summary()
def plain_sniff():
sniff(iface = m_iface, count = 10, filter = "icmp and src {0}".format(m_dst), prn = print_summary)
这个嗅探器工作得很好,我得到了输出:
WARNING: No route found for IPv6 destination :: (no default route?)
Ether / IP / ICMP 192.168.0.1 > 192.168.0.9 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.0.1 > 192.168.0.9 echo-reply 0 / Raw
Ether / IP / ICMP 192.168.0.1 > 192.168.0.9 echo-reply 0 / Raw
...
接下来,我创建一个单独的线程来嗅探数据包,并使用队列在嗅探器线程和主线程之间传递捕获的数据包:
from threading import Thread
from Queue import Queue, Empty
from scapy.all import *
m_iface = "wlan0"
m_finished = False
m_dst = "192.168.0.1"
def print_summary(pkt):
print pkt.summary()
def threaded_sniff_target(q):
global m_finished
sniff(iface = m_iface, count = 10, filter = "icmp and src {0}".format(m_dst), prn = lambda x : q.put(x))
m_finished = True
def threaded_sniff():
q = Queue()
sniffer = Thread(target = threaded_sniff_target, args = (q,))
sniffer.daemon = True
sniffer.start()
while (not m_finished):
try:
pkt = q.get(timeout = 1)
print_summary(pkt)
except Empty:
pass
这个嗅探器也可以正常工作,我得到与上面相同的输出。但是,当我稍微修改主线程以便它使用send()
从数据包队列读取之间的函数时,如下所示:
def threaded_sniff_with_send():
q = Queue()
sniffer = Thread(target = threaded_sniff_target, args = (q,))
sniffer.daemon = True
sniffer.start()
while (not m_finished):
send(IP(dst = m_dst) / ICMP()) # Here
try:
pkt = q.get(timeout = 1)
print_summary(pkt)
except Empty:
pass
然后我得到以下奇怪的输出(过滤器似乎不起作用):
WARNING: No route found for IPv6 destination :: (no default route?)
Sent 1 packets.
Ether / ARP who has 192.168.0.1 says 192.168.0.9
Sent 1 packets.
Ether / ARP is at a0:21:b7:1a:7a:db says 192.168.0.1
Sent 1 packets.
Ether / IP / ICMP 192.168.0.9 > 192.168.0.1 echo-request 0
Sent 1 packets.
Ether / IP / ICMP 192.168.0.1 > 192.168.0.9 echo-reply 0
...
三个嗅探器的脚本可以从这里下载。
我目前的系统配置如下:
Python: 2.7.3
Scapy: 2.2.0
OS: Fedora 18
有趣的是,这三个嗅探器都可以在我的旧电脑上正常工作:
Python: 2.6.4
Scapy: 2.0.0.10 (beta)
OS: Fedora 13
首先我认为它可能是 Scapy / Python 版本。但即使我在新计算机上安装了完全相同的版本,这种行为仍然存在。
我不完全确定这是否适合 SO(可能是 Scapy 的错误报告?)。在那种情况下请原谅我。