Skip to content

Instantly share code, notes, and snippets.

@ae-s
Created September 21, 2020 05:43
Show Gist options
  • Save ae-s/351cae19dc6262d821cc42d168c48da9 to your computer and use it in GitHub Desktop.
Save ae-s/351cae19dc6262d821cc42d168c48da9 to your computer and use it in GitHub Desktop.
pcap to png
#!/usr/local/bin/python3
from pcapfile import savefile
from PIL import Image
import sys
# turn a pcap into an image.
# each row of the image is one packet from the capture.
cap = open(sys.argv[1], 'rb')
capfile = savefile.load_savefile(cap, layers=0, verbose=True)
rows = len(capfile.packets)
width = 1024
img = Image.new('L', (width, rows,), 128)
px = img.load()
cur_row = 0
for packet in capfile.packets:
data = packet.raw()
cur_byte = 0
for byte in data:
cur_byte += 1
cur_bit = cur_byte * 8
if (cur_bit == width):
break
px[cur_bit+0, cur_row] = 255 if int(byte) & 0x80 else 0
px[cur_bit+1, cur_row] = 255 if int(byte) & 0x40 else 0
px[cur_bit+2, cur_row] = 255 if int(byte) & 0x20 else 0
px[cur_bit+3, cur_row] = 255 if int(byte) & 0x10 else 0
px[cur_bit+4, cur_row] = 255 if int(byte) & 0x08 else 0
px[cur_bit+5, cur_row] = 255 if int(byte) & 0x04 else 0
px[cur_bit+6, cur_row] = 255 if int(byte) & 0x02 else 0
px[cur_bit+7, cur_row] = 255 if int(byte) & 0x01 else 0
cur_row += 1
img.save("out.png")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment