Broken Files

Welcome to the Broken Files module.

This module walks you through the internal byte structure of common file formats JPEG, PNG, GIF, WAV, ZIP, MP4, and PDF one format at a time. Each challenge breaks down how a file is laid out under the hood and gives you a corrupted artifact to inspect, repair, and recover.

Work through the challenges in order and save every challenge password you find both the technical difficulty and the story in /challenge/note build from one challenge to the next, and the final challenge requires pieces from every previous password to unlock.

All of these challenges are solvable using hexedit alone except one, which might require you to write a short script.

Note: Challenges require you to view repaired images, listen to audio, or watch video frames—and some GUI viewers are not available inside the Dojo environment. You may want to copy the challenge files to your local machine using scp:

scp -r -i path-to-key [email protected]:/challenge/files/* ./

Alternatively, you can download files directly from the VS Code editor inside pwn.college (Workspace → </> Code):

Hold Ctrl and click on the files you want to download to select multiple files, then right-click and select Download to save them all at once.

Inspect the bytes, read the challenge descriptions, and check the official format specifications to dig deeper.



Challenges

JPEG

What is JPEG?

JPEG (Joint Photographic Experts Group) is a lossy image format commonly used for photographs. It reduces file size by discarding image details that are less noticeable to the human eye.

JPEG Structure

1. Start of Image (Magic Number)

Every JPEG starts with the following two bytes:

FF D8

This is called the SOI (Start of Image) marker. It identifies the beginning of a JPEG file. The byte after it is always FF again (the start of the first segment), so the full signature you will see is FF D8 FF.

2. JPEG Header

After the starting marker, a JPEG contains several segments holding additional information.

A segment generally has this structure:

[MARKER] [LENGTH] [DATA]

For example:

FF D8  FF FE  00 10  4C 61 76 63 ...
│      │      │      └── Comment data
│      │      └───────── Length
│      └──────────────── Comment marker
└─────────────────────── Start of Image

The length is stored in two bytes (big-endian). It includes the length bytes themselves, but not the marker. So 00 10 = 16 bytes: the 2 length bytes plus 14 bytes of data.

FF FE identifies a COM (Comment) segment. Its contents can be plain text. In these files, the comment contains:

Lavc60.31.102

Libavcodec is FFmpeg's codec library (it is used for encoding as well as decoding). FFmpeg stamps its name into the JPEGs it writes, so this comment tells you the file was made with FFmpeg (libavcodec version 60.31.102).

3. Common JPEG Markers

Some markers you may encounter:

FF D8  - Start of Image (SOI)
FF E0  - JFIF label (APP0)
FF E1  - Exif data (APP1)
FF FE  - Comment (COM)
FF DB  - Quantization Table (DQT)
FF C0  - Start of Frame, baseline (SOF0)   <- image height and width
FF C2  - Start of Frame, progressive (SOF2) <- image height and width
FF C4  - Huffman Table (DHT)
FF DA  - Start of Scan (SOS)
FF D9  - End of Image (EOI)

The Start of Frame (SOF) segment holds the image's height and width, so it is the segment to look at when a picture opens wrong or not at all.

4. Image Data

After the JPEG header comes the actual compressed image data.

The FF DA marker (Start of Scan) indicates where the compressed image data begins.

5. End of Image

A JPEG normally ends with:

FF D9

How to Identify a JPEG

Tools

_> file img.jpg
_> identify img.jpg
img.jpg JPEG 3890x5835 3890x5835+0+0 8-bit sRGB 3.82761MiB 0.000u 0:00.000
  • file identifies the format using file signatures.
  • identify decodes the image and reports its dimensions and other properties.

If the JPEG signature is damaged, file reports data, while identify throws an error like: Not a JPEG file: starts with 0xXX 0xXX 'file_name' @ error/jpeg.c/JPEGErrorHandler/339. That error also tells you the first bytes it saw.

Then use xxd to inspect the file manually.

Hex Clues

Look at the ASCII column of xxd near offset 6, right after the first segment's length:

  • JFIF — APP0 (JFIF header)
  • Exif — APP1 (camera metadata)
  • Lavc — COM (an FFmpeg comment, like these files)

These labels are optional. strings file | head shows them too.

Repeated FF xx markers (FF DB, FF C4, FF DA) mean JPEG segments, and the file ends with FF D9.

_> xxd imgExif.jpg | head -n 1
00000000: ffd8 ffe1 0080 4578 6966 0000 4949 2a00  ......Exif..II*.

_> xxd imgJFIF.jpg | head -n 1
00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001  ......JFIF......

hexedit

hexedit views and edits a file in hexadecimal and ASCII. You can modify the file and search through it. Work on a copy: cp file.jpg fixed.jpg, then hexedit fixed.jpg.

The screen has three columns: on the left the offset (byte position in the file), in the middle the hex view, on the right the ASCII view.

Essential Keyboard Shortcuts

  • Arrow keys: Move the cursor
  • Tab: Switch between the hex column and the ASCII column
  • /: Search (hex or ASCII, depending on the column you are in). Type your bytes, press Enter, and the cursor jumps to the match
  • Ctrl+U: Undo
  • Ctrl+X: Save and exit (it asks to confirm)
  • Ctrl+C: Exit without saving

Learn more with man hexedit.

JPEG — Height and Width

1. Find the Fields

Walk the segments to the SOF (FF C0 baseline or FF C2 progressive). You can see them using hexedit: open hexedit img.jpg, press /, and you will see Hexa string to search:. Type ff c2 (or ff c0) and press Enter.

ff c2  00 11  08  [HH HH] [WW WW]  03 ...

HH HH: Height (comes first), big-endian
WW WW: Width (comes second), big-endian

Example: 02 8f 03 e8 = height 0x028F = 655, width 0x03E8 = 1000.

Check the bytes around your match: a real SOF is followed by 00 11 08. The same two bytes can also appear inside a thumbnail or the image data, so skip any match that doesn't fit.

2. What the Damage Looks Like

  • Empty JPEG image (DNL not supported): a dimension is 00 00, the decoder stops, no picture
  • Picture leans or shears diagonally: width wrong by a small amount
  • Picture scrambled into bands: width wrong by a lot
  • Only the top of the picture, the rest missing: height too small
  • Full picture, then a flat grey block at the bottom: height too big

(Exact looks can differ slightly between viewers.)

3. Method A: Thumbnail (Exact, if There Is One)

An EXIF thumbnail is a smaller copy of the same picture, so it has the same aspect ratio. Thumbnails are optional (cameras and phones add them, most generated or stripped images don't), so check first:

_> exiftool -ThumbnailImage file.jpg
_> exiftool -b -ThumbnailImage file.jpg > thumb.jpg
_> identify thumb.jpg

Then:

broken width :  width  = height * thumb_w / thumb_h
broken height:  height = width  * thumb_h / thumb_w

Never use the broken field in the formula. Example: thumbnail 160x100, intact height 500, so width = 500 x 1.6 = 800 = 03 20. A stale thumbnail (image edited afterwards) gives a wrong ratio.

4. Method B: Fix It by Hand, or Brute Force It

No thumbnail? A broken size can still be fixed.

  1. Check what is corrupted. Inspect the SOF fields.
  2. Reason about it. Which field is wrong, the height or the width? Look at both values and ask which one is impossible or doesn't fit the picture. Example: 00 00 04 00 is height = 0 and width = 1024. A height of 0 can't be right, while 1024 is a reasonable width, so the height is the broken field.
  3. Patch it manually. Change the value in hexedit (on a copy), save, open the image, and check whether it looks right. Adjust and repeat until it does.
  4. Or brute force it with a Python script. Try values for the broken field, write each one into the SOF, and check the result.

You don't need the exact original size: a value a few pixels off still shows the whole picture.

If both height and width are broken, several combinations can look almost right (a swapped height and width is a common one), so compare the candidates by eye.

5. Bottom Line

Height and width can be restored as long as the compressed picture data itself is intact, because only the header numbers are wrong. A JPEG with a damaged scan can't be fully recovered.

PNG

What is PNG?

  • PNG (Portable Network Graphics) is a lossless image format: the pixels you save are the pixels you get back.
  • It is common for screenshots, logos, and images with transparency.
  • A PNG is very consistent: the start and the end are always the same bytes, and every piece carries a checksum.

Structure

  • A PNG is an 8-byte signature followed by chunks.
  • The chunks go in this order: IHDR (the header, always first), IDAT (the pixels), and IEND (always last). Optional chunks can sit in between.
  • Every chunk has four parts, and all numbers are big-endian: length (4 bytes), type (4 letters, like IHDR), data, and a CRC (4 bytes).
  • The CRC is a CRC-32 over the type and the data. The length is not included.

What Never Changes

  • The first 16 bytes: 89 50 4E 47 0D 0A 1A 0A (the signature), 00 00 00 0D (the IHDR length), 49 48 44 52 (IHDR).
  • The last 12 bytes: 00 00 00 00 (length 0), 49 45 4E 44 (IEND), AE 42 60 82 (its CRC).
  • So a damaged signature or a missing ending can be rebuilt exactly.

The IHDR Chunk

  • Bytes 16 – 19: width
  • Bytes 20 – 23: height
  • Bytes 24 – 28: bit depth, color type, compression, filter, interlace
  • Bytes 29 – 32: the CRC of bytes 12 – 28
  • Width comes first, then height. A size of zero is not valid.

How to Identify a PNG

  • file img.png and identify img.png tell you the type and size. If the signature is damaged, file says data.
  • xxd img.png | head -3 and xxd img.png | tail -2 show the start and the end.
  • If the start is damaged, look for IHDR at offset 12 and IEND at the end.

What Can Be Broken, and How to Fix It

  • Signature damaged: write the 8 fixed bytes back at offset 0.
  • Bytes 8 – 15 damaged: write back 00 00 00 0D 49 48 44 52.
  • Width or height wrong, CRC left as it was: the stored CRC is the answer key. Try sizes until the CRC of bytes 12 – 28 matches it.
  • IHDR CRC wrong: recalculate it over bytes 12 – 28 and write it back.
  • Ending missing: add the last 12 bytes back.
  • Pixel data (IDAT) damaged: the damaged scan data cannot be rebuilt from headers alone.

Recovering Missing PNG Width & Height Using CRC32

The Idea

  • Every PNG header stores a CRC32: a 4-byte fingerprint of the header chunk.
  • Even when the width and height are zeroed out, the original CRC32 is still intact.
  • You can brute-force width and height pairs until crc32(IHDR + width + height + settings) matches the stored CRC32.

Byte Map (0-Indexed)

  • Bytes 0 – 7: PNG Signature (89 50 4E 47 0D 0A 1A 0A)
  • Bytes 8 – 11: IHDR Length (00 00 00 0D) — Not included in CRC
  • Bytes 12 – 15: Chunk Name (IHDR) — CRC starts at byte 12 (0x0C)
  • Bytes 16 – 19: Width (4 bytes, big-endian, zeroed)
  • Bytes 20 – 23: Height (4 bytes, big-endian, zeroed)
  • Bytes 24 – 28: Bit depth, color type, compression, filter, interlace — CRC ends at byte 28 (0x1C)
  • Bytes 29 – 32: Stored CRC32 checksum — Starts at byte 29 (0x1D)

(In Python, slicing data[12:29] selects bytes 12 through 28.)

How to Fix It

  1. Inspect the first 33 bytes.
  2. Read the target CRC32 at bytes 29 – 32 and keep the 5 setting bytes at 24 – 28.
  3. Loop through possible (width, height) values in Python using struct.pack(">II", w, h) and zlib.crc32().
  4. Once the CRC matches, fix the corrupted dimension fields in the header.

Note: You might want to use Python with the zlib and struct libraries to solve this one.

GIF

What is GIF?

  • GIF (Graphics Interchange Format) is a binary image format built for small animations and indexed color graphics.
  • Every GIF89a file follows a strict byte-level layout: a Header, a Logical Screen Descriptor, an optional Global Color Table (palette), Image Descriptors, and LZW-compressed pixel data.
  • Unlike modern formats that store raw RGB values per pixel, GIF stores a color palette up front and compresses pixel indices using LZW (Lempel-Ziv-Welch) compression.
  • Every file ends with a mandatory single-byte Trailer (0x3b).

GIF89a Byte Layout

  • Header (Bytes 0 – 5): Always 47 49 46 38 39 61 (GIF89a) or 47 49 46 38 37 61 (GIF87a).
  • Logical Screen Descriptor (Bytes 6 – 12):
    • Bytes 6 – 7: Canvas width (little-endian)
    • Bytes 8 – 9: Canvas height (little-endian)
    • Byte 10 (0x0A) — Packed Fields: Bit 7 (the most significant bit, 0x80) is the Global Color Table Flag (1 = palette present, 0 = no palette). Bits 0 – 2 store the palette size exponent N, where the palette has 2^(N + 1) colors (3 * 2^(N + 1) bytes).
    • Byte 11: Background color index
    • Byte 12: Pixel aspect ratio
  • Global Color Table (Starts at Byte 13 / 0x0D): If Bit 7 of Byte 10 is 1, raw RGB triplets follow immediately (e.g., N = 7 means 256 colors × 3 bytes = 768 bytes, spanning 0x0D to 0x30C).
  • Blocks & Image Descriptor:
    • Extension blocks start with 0x21 (such as 21 F9 for Graphic Control or 21 FF for Application Extensions) and end with a 0x00 block terminator.
    • Each frame begins with an Image Descriptor starting with 0x2C (10 bytes total: 2C, 2-byte left, 2-byte top, 2-byte width, 2-byte height, and 1-byte local packed field).
    • Immediately after the 10-byte Image Descriptor (at 2C offset +10) sits the 1-byte LZW Minimum Code Size (which matches the color bit depth, e.g., 0x08 for an 8-bit / 256-color palette, and cannot be 0x00).
  • Trailer (Last Byte): Always 0x3b (;).

What to Look At

  • These files are structurally broken and will not open in standard image viewers.
  • No pixel data was deleted, and no external backups are needed—every broken byte can be determined from the surrounding file structure and the layout above.
  • Check the magic bytes and trailer, the Logical Screen Descriptor flags, and the LZW Minimum Code Size before each image stream.

Note: This challenge is 100% solvable manually using hexedit alone, though writing a quick Python script to parse and patch the binary structure is great practice for larger forensics tasks.

WAV

  • WAV (Waveform Audio File Format) is an uncompressed audio format built on RIFF, storing data in little-endian chunks.
  • A standard PCM WAV starts with a 44-byte header (RIFF, fmt , and data) followed by raw audio samples—no checksums, just raw bytes.

You already know the drill—inspect the bytes, look up the spec, and fix what's broken.

ZIP

What is a ZIP?

  • ZIP is an archive format that bundles files together using lossless compression (usually DEFLATE) with a directory index at the end.
  • Unlike images that read from top to bottom, ZIP tools read backwards—finding the trailer at the end first to locate the index, then jumping to each file.
  • All multi-byte numbers inside a ZIP are little-endian.

Structure

A standard ZIP file has three main sections:

  1. Local File Header (50 4B 03 04) — Sits right before each file's compressed data.
  2. Central Directory (50 4B 01 02) — The master index near the end of the file listing every entry and its sizes.
  3. End of Central Directory (50 4B 05 06) — The 22-byte trailer at the very end that points to where the Central Directory starts.

Key Offsets

Local File Header (starts at 0x00)

  • Bytes 0 – 3: Signature (50 4B 03 04 / PK\x03\x04)
  • Bytes 6 – 7: Bit flag (00 00 = normal, bit 0 set = encrypted)
  • Bytes 18 – 21 (0x12): Compressed size
  • Bytes 22 – 25 (0x16): Uncompressed size
  • Bytes 26 – 29 (0x1A): Filename length (n) and extra field length (m)

Central Directory (50 4B 01 02)

  • +0: Signature (50 4B 01 02 / PK\x01\x02)
  • +8: Bit flag (mirrors the Local File Header flag)
  • +20 (+0x14): Compressed size (backup copy of the local size)

End of Central Directory (50 4B 05 06)

  • +0: Signature (50 4B 05 06 / PK\x05\x06)
  • +16 (+0x10): Byte offset where the Central Directory (50 4B 01 02) starts
  • +20 (+0x14): Comment length (00 00 if empty)

MP4

Structure (Boxes / Atoms)

An MP4 is built out of nested chunks called Boxes (or Atoms). Every box starts with an 8-byte header:

[ 4-byte Size (Big-Endian) ] [ 4-byte Type (ASCII) ] [ Payload / Child Boxes ... ]
  • Size: Total length of the box in bytes (including the 8-byte header). Jumping forward by Size bytes lands you directly on the next sibling box.
  • Type: A 4-character ASCII tag (ftyp = 66 74 79 70).

Core Box Hierarchy

  1. ftyp (File Type): Always the first box at 0x00 (its ASCII tag sits at 0x04).
  2. moov (Movie Container): Holds all video metadata and frame lookup tables.
    • trak (Track): Sits inside moov and defines the video/audio stream.
  3. mdat (Media Data): Holds the raw compressed video frames.

PDF

1. What is a PDF File?

A PDF (Portable Document Format) is not a plain text file—it is a binary-safe container built around a structured storage system of independent objects. Every page, font, layout rule, and compressed text stream is stored as a separate numbered object that links to other objects.

2. File Structure (4 Core Parts)

A standard PDF file is organized into four sequential sections:

  1. Header: Identifies the file magic number and specification version (e.g., %PDF-1.4).
  2. Body: Holds all the indirect objects that make up the document's content, fonts, and page hierarchy.
  3. Cross-Reference Table (xref): An index mapping each object ID to its exact byte offset from the start of the file so readers can jump directly to any object without reading the whole file linearly.
  4. Trailer: Located at the very bottom (trailer, startxref, %%EOF). PDF parsers start reading from the end of the file to find the xref offset and the entry point into the object tree.

3. What is an Object?

Objects are wrapped between obj and endobj markers and prefixed by two numbers: an Object Number and a Generation Number (e.g., 12 0 obj).

Inside, objects use basic PDF data types:

  • Dictionaries (<< /Key Value >>): Key-value maps that define object roles and attributes (such as /Type, /Font, /Filter, or /Length).
  • Arrays ([ ... ]): Ordered lists of values or object references.
  • Streams (stream ... endstream): Dictionaries followed by raw or compressed byte sequences (such as FlateDecode zlib-compressed page drawing instructions).

4. How Objects Connect

Objects reference one another using Indirect References formatted as <ObjectNumber> <GenerationNumber> R (for example, 12 0 R).

Together, these references form a directed tree:

  • The Trailer points to the root document object.
  • That root object points to the page tree node.
  • The page tree points to individual page objects.
  • Each page points to its resources (fonts) and its content stream (the actual rendered text and graphics).

If those references point to invalid or missing object numbers, the parser cannot traverse the tree to render the document.

Hex Editor Note: PDF structure keywords, object IDs, and indirect references are stored as plain ASCII text, not raw binary integers. If you patch numbers in a hex editor, use their ASCII hex codes (for example, '0' is 0x30 and '9' is 0x39, not 0x00 or 0x09), and keep the byte count unchanged so the xref offsets remain valid.


30-Day Scoreboard:

This scoreboard reflects solves for challenges in this module after the module launched in this dojo.

Rank Hacker Badges Score