2023-12-31 13:40:47 +02:00
|
|
|
#!/usr/bin/env python3
|
2023-12-29 02:26:24 +02:00
|
|
|
import argparse
|
|
|
|
import os
|
|
|
|
import subprocess
|
2023-12-25 23:09:03 +02:00
|
|
|
import numpy as np
|
|
|
|
from tqdm import tqdm
|
|
|
|
import pandas as pd
|
|
|
|
import pcapng
|
2023-12-26 15:34:22 +02:00
|
|
|
from struct import unpack
|
2023-12-25 23:09:03 +02:00
|
|
|
from PIL import Image
|
|
|
|
|
2023-12-29 02:26:24 +02:00
|
|
|
|
|
|
|
# Create the parser
|
|
|
|
parser = argparse.ArgumentParser(description="Process a pcap file.")
|
|
|
|
|
|
|
|
# Add an argument for the pcap file, with a default value
|
|
|
|
parser.add_argument('input_file', nargs='?', default='in.pcap', help='The pcap file to process')
|
|
|
|
|
|
|
|
# Parse the arguments
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Now use args.input_file as the file to process
|
|
|
|
input_file = args.input_file
|
|
|
|
basename = os.path.splitext(os.path.basename(input_file))[0]
|
|
|
|
|
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Read packets from a pcap file
|
2023-12-29 02:26:24 +02:00
|
|
|
scanner = pcapng.scanner.FileScanner(open(input_file, "rb"))
|
2023-12-31 13:40:47 +02:00
|
|
|
blocks = tqdm(scanner)
|
2023-12-25 23:09:03 +02:00
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Helper function to safely get an attribute from an object
|
2023-12-25 23:09:03 +02:00
|
|
|
def tryget(obj, att):
|
|
|
|
if hasattr(obj, att):
|
|
|
|
return getattr(obj, att)
|
|
|
|
return None
|
2023-12-26 15:34:22 +02:00
|
|
|
|
2023-12-25 23:09:03 +02:00
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
|
2023-12-31 13:40:47 +02:00
|
|
|
def rightsize(it):
|
|
|
|
for i, obj in enumerate(it):
|
|
|
|
if not hasattr(obj, 'packet_len'):
|
|
|
|
continue
|
|
|
|
len = obj.packet_len
|
|
|
|
if len != 6972:
|
|
|
|
continue
|
|
|
|
yield obj.packet_data
|
2023-12-26 16:35:32 +02:00
|
|
|
|
|
|
|
|
2023-12-31 13:40:47 +02:00
|
|
|
def removestart(it):
|
|
|
|
"Remove the UDP header from the packets"
|
|
|
|
for x in it:
|
|
|
|
yield x[0x2A:]
|
|
|
|
|
2023-12-25 23:09:03 +02:00
|
|
|
|
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Function to parse packet data
|
2023-12-25 23:09:03 +02:00
|
|
|
def parse(data):
|
2023-12-26 16:35:32 +02:00
|
|
|
hdr = 4 + 2 * 7 # Header length
|
|
|
|
# Unpack data into variables
|
2023-12-29 02:26:24 +02:00
|
|
|
c1, c2, part, a, ffaa, b, c, d = unpack(">Lhhhhhhh", data[:hdr])
|
2023-12-25 23:09:03 +02:00
|
|
|
ret = locals()
|
2023-12-29 02:26:24 +02:00
|
|
|
del ret["data"]
|
|
|
|
del ret["hdr"]
|
|
|
|
ret["data"] = data[hdr:]
|
2023-12-25 23:09:03 +02:00
|
|
|
return ret
|
2023-12-26 15:34:22 +02:00
|
|
|
|
|
|
|
|
2023-12-31 13:40:47 +02:00
|
|
|
def parsed(it):
|
|
|
|
for x in it:
|
|
|
|
yield parse(x)
|
2023-12-25 23:09:03 +02:00
|
|
|
|
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Function to group data into frames
|
2023-12-31 13:40:47 +02:00
|
|
|
def frames(it):
|
2023-12-25 23:09:03 +02:00
|
|
|
current = []
|
2023-12-31 13:40:47 +02:00
|
|
|
for obj in it:
|
|
|
|
if obj['part'] == 0:
|
2023-12-25 23:09:03 +02:00
|
|
|
if len(current) > 0:
|
2023-12-31 13:40:47 +02:00
|
|
|
yield b"".join(current)
|
2023-12-25 23:09:03 +02:00
|
|
|
current = []
|
2023-12-31 13:40:47 +02:00
|
|
|
current.append(obj["data"])
|
2023-12-25 23:09:03 +02:00
|
|
|
if len(current) > 0:
|
2023-12-31 13:40:47 +02:00
|
|
|
yield b"".join(current)
|
2023-12-25 23:09:03 +02:00
|
|
|
|
|
|
|
|
2023-12-31 13:40:47 +02:00
|
|
|
def iterimages(it, width, height, pixelformat=">H"):
|
|
|
|
for frame in it:
|
|
|
|
if len(frame) != width * height * 2: # 16 bpp
|
|
|
|
continue
|
|
|
|
yield Image.fromarray(np.frombuffer(frame, dtype=pixelformat).reshape(width, height))
|
2023-12-25 23:09:03 +02:00
|
|
|
|
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Get frames and convert them to images
|
2023-12-31 13:40:47 +02:00
|
|
|
frames = frames(parsed(removestart(rightsize(blocks))))
|
|
|
|
images = iterimages(it=frames, width=384, height=288)
|
2023-12-26 16:35:32 +02:00
|
|
|
|
2023-12-29 02:26:24 +02:00
|
|
|
# Create the directory for frames if not exists
|
|
|
|
frame_dir = f"frames/{basename}"
|
|
|
|
if not os.path.exists(frame_dir):
|
|
|
|
os.makedirs(frame_dir)
|
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Save each image as a PNG file
|
2023-12-31 13:40:47 +02:00
|
|
|
for i, img in enumerate(images):
|
2023-12-29 02:26:24 +02:00
|
|
|
img.save(f'frames/{basename}/{basename}_{i:04}.png')
|
2023-12-25 23:09:03 +02:00
|
|
|
|
2023-12-26 16:35:32 +02:00
|
|
|
# Produce a video from the saved images
|
2023-12-29 02:26:24 +02:00
|
|
|
ffmpeg_input = f"frames/{basename}/{basename}_%04d.png"
|
|
|
|
command = [
|
|
|
|
"ffmpeg",
|
|
|
|
"-y", # Overwrite output file without asking
|
|
|
|
"-hide_banner", # Hide banner
|
|
|
|
"-loglevel", "info", # Log level
|
|
|
|
"-f", "image2", # Input format
|
|
|
|
"-framerate", "25", # Framerate
|
|
|
|
"-i", ffmpeg_input, # Input file pattern
|
|
|
|
"-vf", "transpose=1", # Video filter for transposing
|
|
|
|
"-s", "384x288", # Size of one frame
|
|
|
|
"-vcodec", "libx264", # Video codec
|
|
|
|
"-pix_fmt", "yuv420p", # Pixel format: YUV 4:2:0
|
|
|
|
"thermal.mp4", # Output file in MP4 container
|
|
|
|
]
|
|
|
|
|
|
|
|
subprocess.run(command)
|
|
|
|
|
|
|
|
print("to play: ffplay thermal.mp4")
|