#!/usr/bin/env python3

# The message extracted from last bytes
message = "3h2pHv1T!\"bOb|&4}C\\_V____``azI"

print("Original message:")
print(message)
print()

# Reverse it
reversed_msg = message[::-1]
print("Reversed:")
print(reversed_msg)
print()

# Looking at the pattern, if we reverse we get:
# Iza``____V_\C}4&|bOb"!T1vHp2h3

# This looks like it might spell: HTB{...} where the content is obfuscated
# Let's try to find if there's a pattern

# Actually, looking at it more carefully:
# Forward: 3h2pHv1T!"bOb|&4}C\_V____``azI
# The '}' suggests end of flag, so reading backwards from }:
# }4&|bOb"!T1vHp2h3

# Maybe we need to find { somewhere?
# Let's check if { appears as a different character

full_message_with_nonprint = []
import struct

def parse_pcap(filename):
    with open(filename, 'rb') as f:
        f.read(24)
        packets = []
        while True:
            ph = f.read(16)
            if len(ph) < 16:
                break
            _, _, incl_len, _ = struct.unpack('IIII', ph)
            pd = f.read(incl_len)
            if len(pd) < incl_len:
                break
            packets.append(pd)
        return packets

packets = parse_pcap('sniffed.pcap')
print("All last bytes (including non-printable):")
for i, pkt in enumerate(packets[:50]):
    b = pkt[-1]
    if b == 0x7b:  # Check for '{'
        print(f"  Packet {i+1}: Found '{{' !!")
    print(f"  Packet {i+1}: 0x{b:02x} = {b:3d} -> {repr(chr(b)) if 32 <= b <= 126 else '[non-print]'}")

# Maybe the flag format is different
# Let me check common CTF flag formats
print("\n" + "=" * 80)
print("ANALYZING THE SEQUENCE:")
print("=" * 80)
print("If we have: 3h2pHv1T!\"bOb|&4}...")
print("Reversed: ...}4&|bOb\"!T1vHp2h3")
print()
print("The challenge says 'Spell Sequence' - maybe it's:")
print("  HTB{Sp3ll_S3qu3nc3_w0w_y0u_f0und_1T!}")
print()
print("Or based on reversed pattern:")
reversed_clean = reversed_msg.replace('`', '').replace('_', '').replace('\\', '')
print(f"Cleaned reversed: {reversed_clean}")
print()

# The actual flag might be:
proposed_flag = "HTB{Sp3ll_C4st_w1th_p4ck3t_s1z3s!}"
print(f"Possible flag structure: {proposed_flag}")
