Ahmed Hussein
Ahmed Hussein

Reputation: 765

How to convert pyshark packet to binary value

I am able to read .pcap file with pyshark. Here is my code:

packets = pyshark.FileCapture(pcap_dir) # pcap_dir is the directory of my pcap file

To print a packet, I use print(packets[0]). My question is: how can I convert packets[0] to its binary value? For example, this can be useful if I want to send the packet again to the network

Upvotes: 0

Views: 2188

Answers (1)

Ahmed Hussein
Ahmed Hussein

Reputation: 765

I was able to solve the problem. I simply used:

packets = pyshark.FileCapture(
            input_file=pcap_dir,
            use_json=True,
            include_raw=True
          )._packets_from_tshark_sync() # pcap_dir is the directory of my pcap file

hex_packet = packets.__next__().frame_raw.value
print(hex_packet)

binary_packet = bytearray.fromhex(hex_packet)
print(binary_packet)

Also, it might be useful to check this

Upvotes: 1

Related Questions