import struct

def parse_pcap(filename):
    with open(filename, 'rb') as f:
        f.read(24)  # Skip global header
        packets = []
        while True:
            packet_header = f.read(16)
            if len(packet_header) < 16:
                break
            _, _, incl_len, _ = struct.unpack('IIII', packet_header)
            packet_data = f.read(incl_len)
            if len(packet_data) < incl_len:
                break
            packets.append(packet_data)
        return packets

packets = parse_pcap('sniffed.pcap')

print(f"Total packets: {len(packets)}\n")
print("=" * 80)
print("ALL PACKETS - NO FILTERING")
print("=" * 80)

for i, pkt in enumerate(packets, 1):
    last_byte = pkt[-1]
    is_printable = 32 <= last_byte <= 126
    char = chr(last_byte) if is_printable else '.'
    print(f"Packet {i:3d}: 0x{last_byte:02x} ({last_byte:3d}) '{char}' {'✓' if is_printable else ''}")

print("\n" + "=" * 80)
print("EXTRACTING ALL PRINTABLE")
print("=" * 80)

all_printable = [(i+1, chr(pkt[-1])) for i, pkt in enumerate(packets) if 32 <= pkt[-1] <= 126]

print("\nAll printable characters:")
for num, char in all_printable:
    print(f"  Packet {num}: '{char}'")

full_message = ''.join([char for _, char in all_printable])
print(f"\nFull message: {full_message}")
print(f"\nLength: {len(full_message)} characters")

# Show different possible interpretations
print("\n" + "=" * 80)
print("POSSIBLE FLAGS")
print("=" * 80)
print(f"1. Full message as flag: HTB{{{full_message}}}")
print(f"2. Message without braces: {full_message}")
print(f"3. Looking for HTB{{ in message: {'HTB{' in full_message}")
