Scapy 数据包嗅探器触发对每个嗅探数据包的操作
Scapy packet sniffer triggering an action up on each sniffed packet
我正在使用 scapy
和 python
来嗅探实时流量。
capture=sniff(iface="<My Interface>", filter="tcp")
但这会嗅探每个数据包并将其添加到列表capture
,稍后可以处理。
我想处理一个数据包并在嗅探后立即显示数据包的几个字段。即在嗅探数据包时,它会触发一个函数,我可以在其中分析该数据包。这将持续几个数据包。
我已经准备好与捕获的数据包列表一起使用的功能。但是我无法将它用于每个实时数据包。
如何实现? scapy
是否可行,或者我是否需要安装任何其他软件包?
这可以通过 sniff
函数的 prn
参数来完成。 Scapy
的教程有一个简单的例子here. Scapy
's official API documentation指定:
sniff(prn=None, lfilter=None, count=0, store=1, offline=None, L2socket=None, timeout=None)
...
prn
: function to apply to each packet. If something is returned, it is displayed. For instance you can use prn = lambda x: x.summary()
.
...
编辑:
claims that the store
argument must be set to 0
for the prn
callback to be invoked. However, setting store=0
doesn't have any such effect. Scapy
's own examples don't set store=0
and the official API documentation 没有提到任何此类要求。事实上,检查 Scapy
的源代码会发现 store
和 prn
参数之间没有任何联系。以下是相关代码块的摘录:
...
if store:
lst.append(p)
c += 1
if prn:
r = prn(p)
if r is not None:
print r
...
执行一些简单的测试用例也支持这一发现。
嗅探函数的参数应该像下面的代码。:
from scapy.all import *
def pkt_callback(pkt):
pkt.show() # debug statement
sniff(iface="<My Interface>", prn=pkt_callback, filter="tcp", store=0)
store=0
表示不存储任何收到的数据包,prn
表示将 pkt
发送到 pkt_callback
。
如 Yoel 所述,如果只需要一个操作,lambda
可以与 prn
一起使用,而不是像这种情况下的新功能:
sniff(iface="<My Interface>", prn = lambda x: x.show(), filter="tcp", store=0)
我正在使用 scapy
和 python
来嗅探实时流量。
capture=sniff(iface="<My Interface>", filter="tcp")
但这会嗅探每个数据包并将其添加到列表capture
,稍后可以处理。
我想处理一个数据包并在嗅探后立即显示数据包的几个字段。即在嗅探数据包时,它会触发一个函数,我可以在其中分析该数据包。这将持续几个数据包。
我已经准备好与捕获的数据包列表一起使用的功能。但是我无法将它用于每个实时数据包。
如何实现? scapy
是否可行,或者我是否需要安装任何其他软件包?
这可以通过 sniff
函数的 prn
参数来完成。 Scapy
的教程有一个简单的例子here. Scapy
's official API documentation指定:
sniff(prn=None, lfilter=None, count=0, store=1, offline=None, L2socket=None, timeout=None)
...
prn
: function to apply to each packet. If something is returned, it is displayed. For instance you can useprn = lambda x: x.summary()
.
...
编辑:
store
argument must be set to 0
for the prn
callback to be invoked. However, setting store=0
doesn't have any such effect. Scapy
's own examples don't set store=0
and the official API documentation 没有提到任何此类要求。事实上,检查 Scapy
的源代码会发现 store
和 prn
参数之间没有任何联系。以下是相关代码块的摘录:
...
if store:
lst.append(p)
c += 1
if prn:
r = prn(p)
if r is not None:
print r
...
执行一些简单的测试用例也支持这一发现。
嗅探函数的参数应该像下面的代码。:
from scapy.all import *
def pkt_callback(pkt):
pkt.show() # debug statement
sniff(iface="<My Interface>", prn=pkt_callback, filter="tcp", store=0)
store=0
表示不存储任何收到的数据包,prn
表示将 pkt
发送到 pkt_callback
。
如 Yoel 所述,如果只需要一个操作,lambda
可以与 prn
一起使用,而不是像这种情况下的新功能:
sniff(iface="<My Interface>", prn = lambda x: x.show(), filter="tcp", store=0)