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.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
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.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.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:
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:
(This is more for reference, so no votes expected)
The Scapy list scapy.ml@secdev.org is well-monitored and tends to be very responsive. If you don't get answers here, try there as well.
To get the PicklabePacket class to work with scapy 3.0.0 you can use this class definition: