#!/bin/python # JPEG (JFIF) has the following header: FF D8 FF E0 00 10 4A 46 49 46 00 01 correct_header = [0xff, 0xd8, 0xff, 0xe0, 0x00, 0x10, 0x4a, 0x46, 0x49, 0x46, 0x00, 0x01] # The file we got has this header: (Inspected using xxd / emacs hexl-mode) # false_header = [0xb9, 0x14, 0x06, 0x45, 0x71, 0xe0, 0xb5, 0xf7, 0x37, 0x07, 0xcb, 0x85] # A: Correct header, B: Wrong header, X: de/encryption key # Get the XOR key to decrypt the file (A ^ X = B => X = A ^ B) xor = [] with open("my_magic_bytes.jpg.enc", "rb") as f: for i in range(len(correct_header)): xor.append(correct_header[i] ^ int(f.read(1).hex(), 16)) # read binary data from file, write xored binary data to file i = 0 with open("my_magic_bytes.jpg.enc", "rb") as f, open("mep.jpg", "wb") as o: while (cb := f.read(1)): xored = int(cb.hex(), 16) ^ xor[i] o.write(bytes([xored])) i = (i + 1) % len(xor) # FLAG: 247CTF{ca4e3b7f913ca7ca8f33fb0504f2947f}