Skip to main content
 首页 » 编程设计

python之Scapy之如何嗅探数据包并将其作为封装数据发送

2025年05月04日10zhujiabin

我需要做以下事情:

  • a) 使用 Scapy 在 eth0 上嗅探()。这会给我一个 scapy 包.. 说 X。
  • b) 将 X 作为二进制数据存储到某个变量... Y。
  • c) 使用 zeromq IPC 套接字将 Y 发送到另一个进程。我必须使用 IPC, 不是 tcp/udp。

代码:

def handler(x): 
  x.show() #or do something ..  
  s = .. a zmq socket = ipc://myipcendpoint 
 
  y = convert x to binary <== how to do this?  
  s.send(y)  
 
 
sniff(prn=handler, iface='eth0') 

关于其他进程:

while 1:  
  y = s.recv() 
  x = recover scapy packet from y 
  ... do something with x ...  

请您参考如下方法:

Scapy 实现的 str() 表示对你来说应该足够了。

y = str(x) 

你可以让 Scapy 在接收端将它解析回正确的数据包,就像这样 -

x = Ether(y) 

来自2nd code example in the Stacking-Layers Scapy 文档的一部分。