Corrupted file

Published: April 2, 2026

Description

A JPEG file has corrupted magic bytes. Fix the header to view the flag image.

Download the corrupted file from the challenge page.

file corrupted

Solution

  1. Step 1Diagnose the corruption
    Run the file command - it reports 'data' instead of JPEG because the magic bytes are wrong. Inspect the raw bytes with xxd to see what the first three bytes currently are.
    file corrupted
    xxd corrupted | head
    Learn more

    Magic bytes (also called file signatures) are specific byte sequences at the start of a file that identify its format. The file command reads these bytes and matches them against a database of known signatures rather than trusting the file extension. This is why renaming a PNG to .jpg still produces "PNG image data" in the output - the content determines the type, not the name.

    xxd produces a hex dump of a file - two hex characters per byte on the left, with an ASCII representation on the right. The first few bytes of the dump reveal exactly what is currently stored at the file's start. Comparing those bytes against a table of known signatures (e.g., JPEG = FF D8 FF, PNG = 89 50 4E 47, PDF = 25 50 44 46) immediately tells you what the file should be and what needs fixing.

    In forensics challenges, file corruption is a very common technique: magic bytes are intentionally altered so the file appears unreadable at first glance. The solution almost always involves identifying the correct signature for the detected file type and restoring those bytes using a hex editor or a command-line tool.

  2. Step 2Restore the JPEG magic bytes
    A valid JPEG must start with FF D8 FF. Use printf and dd to overwrite only the first three bytes without touching any image data. The conv=notrunc flag prevents truncating the rest of the file.
    printf '\xff\xd8\xff' | dd of=corrupted bs=1 count=3 conv=notrunc
    file corrupted
    Learn more

    The JPEG file format specifies that every valid file must begin with the byte sequence FF D8 FF. This is the SOI (Start of Image) marker followed by the first segment marker. Image decoders check for this signature before attempting to parse the rest of the file - without it, they refuse to render the image.

    dd is a low-level copy utility that operates on raw bytes. The flags used here are: bs=1 (block size of 1 byte), count=3 (write exactly 3 bytes), and conv=notrunc (do not truncate the output file - without this flag, dd would overwrite the file entirely with just those 3 bytes, destroying all the image data). printf with \xff-style escape sequences outputs the exact raw bytes needed.

    Alternatively, a Python one-liner or a hex editor like hexedit or 010 Editor can patch specific byte offsets interactively. In real forensics investigations, restoring a corrupted file header is a standard recovery technique used to repair deliberately or accidentally damaged files.

  3. Step 3Open the repaired image
    Open the repaired JPEG in any image viewer. The flag is visible inside the image.
    eog corrupted
    Learn more

    Once the magic bytes are restored, the file is a fully valid JPEG that any compliant decoder can render. eog (Eye of GNOME) is the default image viewer on many Linux desktops. Alternatives include display (ImageMagick), feh, or simply opening the file in a browser.

    This challenge demonstrates that file formats are defined by their internal structure, not their extension or filename. Understanding byte-level file structures is a foundational skill in digital forensics - it applies to recovering accidentally overwritten headers, analyzing malware that disguises its type, and extracting data from partially damaged storage media.

Flag

picoCTF{...}

File magic bytes (file signatures) are the first few bytes that identify a file format - corrupting them makes the file unreadable without changing any of the actual image data.

Want more picoMini by CMU-Africa writeups?

Useful tools for Forensics

More Forensics