我正在使用 Scapy 通过以下代码嗅探网络流量:
from scapy.all import *
import sys
filter = sys.argv[1]
def Responder():
def getPacket(pkt):
if Raw in pkt: print pkt[Raw]
return getPacket
sniff(filter=filter, prn=Responder())
如果我只是浏览一个网站,这段代码可以正常工作。
问题是,当我打开连接共享并尝试运行此脚本时,出现以下错误:
bash-3.2# sudo python sniffy.py "tcp port 80"
Traceback (most recent call last):
File "sniffy.py", line 1, in <module>
from scapy.all import *
File "/Library/Python/2.7/site-packages/scapy/all.py", line 25, in <module>
from route import *
File "/Library/Python/2.7/site-packages/scapy/route.py", line 162, in <module>
conf.route=Route()
File "/Library/Python/2.7/site-packages/scapy/route.py", line 22, in __init__
self.resync()
File "/Library/Python/2.7/site-packages/scapy/route.py", line 31, in resync
self.routes = read_routes()
File "/Library/Python/2.7/site-packages/scapy/arch/unix.py", line 87, in read_routes
ifaddr = scapy.arch.get_if_addr(netif)
File "/Library/Python/2.7/site-packages/scapy/arch/__init__.py", line 36, in get_if_addr
return socket.inet_ntoa(get_if_raw_addr(iff))
File "/Library/Python/2.7/site-packages/scapy/arch/pcapdnet.py", line 188, in get_if_raw_addr
return i.get(ifname)["addr"].data
File "dnet.pyx", line 990, in dnet.intf.get
OSError: Device not configured
我尝试在脚本顶部添加 "conf.iface=en1"
。这给出了同样的错误。 en1 是一个有效的接口(interface)。
我也听从了 here 的建议,并将第 34 行的 arch/unix.py 从 "os.popen("netstat -rn")
"更改为 "os.popen("netstat -rn | grep -v vboxnet")
”。这并没有解决问题。
最佳答案
我正在运行 OSX 10.5.9 - 一直在尝试让 scapy 工作 - 在安装 dnet/pcap 库之后,我也得到了“OSError:Device not configured”。尝试用
替换 unix.py 中的第 34 行“netstat -rn | grep -v vboxnet”
还是一样的错误。但是当我在 if block 的“else”部分更改第 37 行时:
def read_routes():
if scapy.arch.SOLARIS:
f=os.popen("netstat -rvn") # -f inet
elif scapy.arch.FREEBSD:
f=os.popen("netstat -rnW") # -W to handle long interface names
else:
# f=os.popen("netstat -rn") # -f inet
f=os.popen("netstat -rn | grep -v vboxnet") # -f inet
像魅力一样工作!
关于python - OSX 连接共享和 Scapy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23241244/