如何腌制 scapy 包?
我需要腌制一个 scapy 数据包。大多数时候这是有效的,但有时pickler会抱怨函数对象。根据经验:ARP 数据包可以很好地腌制。某些 UDP 数据包有问题。
I need to pickle a scapy
packet. Most of the time this works, but sometimes the pickler complains about a function object. As a rule of thumb: ARP packets pickle fine. Some UDP packets are problematic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我的解决方案(受到 scapy 邮件列表的启发)如下:
任何我希望通过
Queue
传递scapy
Packet
的地方,我只需包装它在PicklablePacket
中,然后__call__
。我不知道没有以这种方式保留的数据。然而,这种方法仅适用于以太网数据包。 (在常规 NIC(而非 WLAN)上嗅探的所有数据包都是以太网。)它也可能扩展到其他类型。My solution (as inspired by the scapy mailing list) is as follows:
Anywhere I wish to pass a
scapy
Packet
through aQueue
I simply wrap it in aPicklablePacket
and__call__
it afterwards. I am not aware of data that is not retained this way. However this approach only works withEthernet
packets. (All packets sniffed on a regular NIC (not WLAN) are Ethernet.) It could probably be extended to work for other types, too.如果 pickle 指的是一般序列化,则始终可以使用 pcap 导入/导出方法: rdpcap和wrpcap。
或者您可以启动您的进程并在另一个进程中抓取数据包。如果有某种模式可以匹配,比如已知端口或源 IP tcpdump 就可以工作:
然后您可以读取生成的 pcap,如上所示:
If by pickle you mean generically serialize you can always use the pcap import/export methods: rdpcap and wrpcap.
Or you could start up your process and grab the packets in another process. If there is some pattern you can match, say a known port or source IP tcpdump will work:
You can then read the generated pcap in as above:
(这仅供参考,因此预计不会投票)
Scapy 列表 [email protected]< /a> 受到良好监控并且往往反应非常灵敏。如果您在这里没有得到答案,也可以在那里尝试。
(This is more for reference, so no votes expected)
The Scapy list [email protected] is well-monitored and tends to be very responsive. If you don't get answers here, try there as well.
受此问题的启发,可以使用dill 库(或其他类似 sPickle 等 - 请参阅 pypi search pickle)来保存 scapy 数据包。例如,使用 sudo easy_install dill 或 sudo pip install dill 安装 dill。这是一个基本的使用场景:
当然,如果只有一组数据包,也可以使用 scapy 的本机函数将数据包转储到 pcap 文件(可由 tcpdump/wireshark 等读取):
As inspired by this question one can use the dill library (or others like sPickle etc - see pypi search pickle) to save scapy packets. E.g. Install dill using
sudo easy_install dill
orsudo pip install dill
. Here's a basic usage scenario:Also one can of course just use scapy's native functions to dump the packets to a pcap file (readable by tcpdump/wireshark etc) - if one just has an array of packets:
您可以对
Packet
类进行 Monkeypatch,并注入__getstate__
和__setstate__
方法,将对象中的函数从可选取表示形式转换为可选取表示形式。有关详细信息,请参阅此处。You can monkeypatch the
Packet
class and inject__getstate__
and__setstate__
methods that convert the function in the object from and to a picklable representation. See here for details.要使 PicklabePacket 类与 scapy 3.0.0 一起使用,您可以使用此类定义:
To get the PicklabePacket class to work with scapy 3.0.0 you can use this class definition: