Corrupted file picoCTF Writeup


🖼️ Recovering a Corrupted JFIF Image in CTF Challenges: A Beginner-Friendly Writeup

✨ Introduction

Recovering corrupted image files is a common challenge in digital forensics and beginner CTF (Capture The Flag) tasks. This guide explains how to identify file types, inspect binary data, and repair broken file headers using essential Linux tools such as file, strings, hexdump, and hexedit. If you’re new to CTFs or cybersecurity, this step-by-step process will help you build confidence in analyzing unknown files and restoring them to reveal hidden flags.

This challenge involves inspecting a mysterious file downloaded from here, understanding its format, repairing broken magic numbers, and ultimately retrieving the flag embedded in the restored image.


🧩 Challenge Overview

In this challenge, you download a mysterious file named “file” using the link provided as here.
Your task is to:

  • Determine what type of file it is
  • Understand why it cannot be opened
  • Inspect and repair corrupted binary data
  • Restore the file so that the hidden flag becomes visible

Although the file looks unreadable at first, a careful analysis reveals that it is actually a broken JFIF (JPEG) file with an incorrect magic number.


🛠️ Tools Used

  • file – Detects file types using magic numbers
  • cat – Prints raw file contents to the terminal
  • strings – Extracts printable text from binary files
  • hexdump – Displays binary data in hexadecimal format
  • hexedit – Edits binary files directly in hex
  • Linux terminal – Main environment for analysis

🧭 Step-by-Step Solution


🪪 Step 1: Identify the File Format

$ file file
file: data

The file command only reports data, meaning it cannot identify the format. This suggests either:

  • The file type is unusual, or
  • The file header (magic number) is corrupted

🔍 Explanation:

The file command checks magic numbers, which are signatures stored in the first bytes of a file.
If the header is broken, file cannot classify it — a common situation in CTF puzzles.


📄 Step 2: Print the Content to Inspect Raw Data

$ cat file
\x��JFIF��C

The output shows unreadable binary data but also includes the string JFIF.


🔍 Explanation:

cat is useful to confirm whether the file contains printable ASCII data.
Seeing JFIF is a clue that the file might be a JPEG-based format.


🔠 Step 3: Extract Printable Strings from the File

$ strings file
JFIF

Now it’s clear — the file contains the JFIF identifier.


🔍 Explanation:

The strings command pulls human-readable text out of binary files, often revealing metadata, file signatures, or hints.


🖼️ Step 4: Try Renaming the File as a JFIF Image

$ cp file test.jfif

Opening the file still fails. Why?
Because simply renaming it does not repair the corrupted binary header.


🔍 Explanation:

Most image viewers require correct segment structure and SOI (Start of Image) magic numbers.
The file extension alone doesn’t make a file valid.


🔬 Step 5: Inspect the Binary Header with Hexdump

$ hexdump -C -n 8 test.jfif
00000000  5c 78 ff e0 00 10 4a 46   |\x....JF|

The first two bytes are 5c 78, which is wrong.

The correct JPEG/JFIF header should begin with:

FF D8 FF E0

🔍 Explanation:

According to the JPEG specification, the magic number FF D8 represents the Start Of Image (SOI).
If these first bytes are incorrect, the image cannot be decoded.


✏️ Step 6: Repair the Magic Number Using hexedit

Open the file in a hex editor:

$ hexedit test.jfif

Navigate to the first bytes and replace:

Wrong bytesCorrect bytes
5c 78 ff e0ff d8 ff e0

Save and exit with Ctrl + X.


🔍 Explanation:

hexedit lets you modify the raw binary structure of a file.
By restoring the correct SOI header, the image becomes readable again.


🏁 Capture the Flag

When you open the repaired image, the hidden flag appears:

🎉 picoCTF{r3st0r1ng_th3_by73s_b67c1558}


📊 Summary

StepCommandPurposeKey Result
1file fileIdentify file formatUnknown data
2cat fileView raw contentsShows binary + “JFIF”
3strings fileExtract printable textConfirms JFIF
4cp file test.jfifRename to imageStill unreadable
5hexdump -C -n 8Inspect headerWrong magic number
6hexeditFix headerRestored image → flag

🧪 Short explanations for commands/techniques used

file

Reads magic numbers to detect file type.

cat

Displays file content in plain text, helpful for quick inspection.

strings

Extracts readable ASCII/Unicode characters from binaries.

hexdump -C

Shows binary data in human-readable hexadecimal + ASCII format.

hexedit

Allows direct editing of binary file content.

JPEG SOI Magic Number

JPEG files must start with FF D8, which marks the beginning of an image.


🌱 Beginner Tips

  • If a file “looks broken,” always check its magic numbers first.
  • Never rely only on file extensions — CTF challenges often change them.
  • Use strings early; it often reveals hints or file type clues.
  • hexdump is essential for identifying corrupted headers.
  • Don’t fear hex editors — editing a few bytes can fully restore a file.

🎓 What You Learn (Takeaways)

  • How to analyze unknown binary files
  • How magic numbers define file types
  • How to inspect and modify binary data safely
  • How to repair corrupted file headers
  • How forensic techniques apply to CTF challenges
  • A practical workflow for file recovery tasks

📚 Further Reading

Here are related articles from alsavaudomila.com that complement this challenge:

  1. Enhance! picoCTF Writeup
  2. Flag in Flame picoCTF Writeup
  3. DISKO 1 picoCTF Writeup

Leave a Reply

Your email address will not be published. Required fields are marked *