I'm new to pyshark. I'm trying to write a parser for custom UDP packets. I'm using the FileCapture
object to read packets from a file.
>>> cap = pyshark.FileCapture('sample.pcap')
>>> pkt = cap.next()
>>> pkt
<UDP/DATA Packet>
>>> pkt.data.data
'01ca00040500a4700500a22a5af20f830000b3aa000110da5af20f7c000bde1a000006390000666e000067f900000ba7000026ce000001d00000000100001726000100000000000000000000000017260500a4700500a22a608600250500a8c10500a22a608601310500a8c10500a22b608601200500a8cc0500a22a6086000c'
>>> dir(pkt.udp)
['DATA_LAYER', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__format__', '__getattr__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_all_fields', '_field_prefix', '_get_all_field_lines', _get_all_fields_with_alternates', '_get_field_or_layer_repr', '_get_field_repr', '_layer_name', '_sanitize_field_name', 'checksum', 'checksum_status', 'dstport', 'field_names', 'get', 'get_field', 'get_field_by_showname', get_field_value', 'layer_name', 'length', 'port', 'pretty_print', raw_mode', 'srcport', 'stream']
I need a method to simply access the packet's UDP payload. The only method I found to access raw packet data is pkt.data.data
, but this returns the entire content of the packet while I'm only interested to UDP portion. Something like pkt.udp.data
. Is there a way to simply do that or I need to use pkt.data.data
and calculate the offset at which my data are placed?
Correct.
Incorrect. The
.data.data
attribute is a hex string representation of just the UDP payload itself.For example if your UDP payload is the ASCII string "hello", you can simply retrieve it as such with:
bytearray.fromhex(pkt.data.data).decode()
(You can easily test this yourself from a Bash console, e.g., with
echo -n hello >/dev/udp/localhost/12345
while doing a pyshark capture on lo:12345.)pyshark_parser might help you out: https://github.com/jlents/pyshark_parser/blob/master/pyshark_parser/
I was looking at their code and what you might be looking for here: https://github.com/jlents/pyshark_parser/blob/master/pyshark_parser/packet_util.py
and