Description
A JPEG file has corrupted magic bytes. Fix the header to view the flag image.
Setup
Download the corrupted file from the challenge page.
file corruptedSolution
- Step 1Diagnose the corruptionRun 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 corruptedxxd corrupted | headLearn more
Magic bytes (also called file signatures) are specific byte sequences at the start of a file that identify its format. The
filecommand 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.jpgstill produces "PNG image data" in the output - the content determines the type, not the name.xxdproduces 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.
- Step 2Restore the JPEG magic bytesA 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=notruncfile corruptedLearn 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.ddis 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), andconv=notrunc(do not truncate the output file - without this flag,ddwould overwrite the file entirely with just those 3 bytes, destroying all the image data).printfwith\xff-style escape sequences outputs the exact raw bytes needed.Alternatively, a Python one-liner or a hex editor like
hexeditor010 Editorcan 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. - Step 3Open the repaired imageOpen the repaired JPEG in any image viewer. The flag is visible inside the image.
eog corruptedLearn 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 includedisplay(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.