CTF Arsenal
DFIR/Image & File Forensics

File Signature Identification & Magic Bytes

Concept

Every file format starts with a recognizable byte signature ('magic bytes') regardless of its extension. Inspecting raw bytes confirms true file type, reveals extension mismatches, and is the first step toward spotting appended/embedded data.

When to Use

  • A file's extension seems suspicious or was deliberately changed.
  • 'file' reports an unexpected type.

Workflow

  1. 1
    Dump the first bytes in hex

    Compare against known magic-byte tables.

  2. 2
    Check for a mismatched or missing signature

    Renamed/corrupted file indicator.

  3. 3
    Scan the whole file for additional embedded signatures

    binwalk for embedded files appended after the main one.

  4. 4
    Carve out any embedded file found

    dd or binwalk -e.

Commands

xxd FILE_PATH | head
xxd evidence.bin | head

Hex-dumps the start of the file for manual magic-byte inspection.

When to use: Whenever 'file' is inconclusive or you want to see raw structure yourself.

Expected output:

00000000: 4a46 4946 0000 0000  ....  (example byte pattern)

Look for: Known signatures: FFD8FF (JPEG), 89504E47 (PNG), 504B0304 (ZIP/docx/jar/apk), 7F454C46 (ELF).

Next step: If the signature doesn't match the extension, rename/treat the file as its real type.

binwalk FILE_PATH
binwalk evidence.jpg

Scans the entire file for known file-format signatures at any offset, revealing embedded/appended files.

When to use: Any file suspected of hiding another file inside it (classic steganography/carving challenge).

Expected output:

0    0x0     JPEG image data
52341  0xCC75  Zip archive data

Look for: A second signature appearing after the main file's expected end.

Next step: Carve out the embedded data with binwalk -e or dd using the reported offset.

binwalk -e FILE_PATH
binwalk -e evidence.jpg

Extracts all identified embedded files into a new directory automatically.

  • -e — Extract discovered signatures into _FILE_PATH.extracted/.

When to use: After binwalk's scan confirms an embedded file is present.

Expected output:

Creates _evidence.jpg.extracted/ containing the carved file(s).

Look for: Successfully extracted files that 'file' then identifies as something meaningful (archive, executable, text).

Next step: Run 'file' and continue analysis on each extracted artifact.

dd if=FILE_PATH of=OUTPUT_PATH bs=1 skip=OFFSET
dd if=evidence.jpg of=hidden.zip bs=1 skip=52341

Manually carves data starting at a specific byte offset into a new file — useful when binwalk -e doesn't cleanly extract.

  • bs=1 — Block size of 1 byte, for precise offset control (slow on large files).
  • skip=OFFSET — Number of bytes to skip from the start before copying.

When to use: When you know the exact offset (from binwalk/xxd) and need a clean manual carve.

Expected output:

A new file containing only the bytes from the given offset onward.

Look for: Confirm the output file's own signature with 'file' after carving.

Next step: Treat the carved file as new evidence and restart the analysis workflow on it.

Tools

Binwalk

Firmware/file-format signature scanner and extractor.

When to use: Any suspected embedded/appended file scenario.

sudo apt install binwalk
binwalk FILE_PATH

Example workflow: Scan -> note offsets -> binwalk -e to auto-extract, or dd for a precise manual carve.

Foremost / Scalpel

Signature-based file carving across a larger blob (disk image, raw dump) rather than a single file.

When to use: When carving many files of multiple types out of a disk image rather than one container file.

sudo apt install foremost scalpel
foremost -i FILE_PATH -o OUTPUT_DIR

Example workflow: Run against a disk image; review output/audit.txt for a summary of everything carved by type.

Findings & Interpretation

Interesting

  • A second valid file signature appended after the expected EOF
  • Extension not matching the actual magic bytes

False Positives

  • Binwalk sometimes flags coincidental byte sequences as false-positive signatures inside compressed image data — verify by actually extracting and checking with 'file'

How to Validate

  • Always run 'file' on anything carved to confirm it's a genuine, valid file and not a false-positive fragment

Next Step

  • Carve, then recursively re-run the full file-forensics workflow on any extracted artifact.

Troubleshooting

binwalk -e produces files that won't open / are corrupt.

Likely cause: Wrong offset boundary, or the embedded data is itself compressed/encrypted.

Verify: Compare the extracted file's size and 'file' output against expectations.

Fix: Try a manual dd carve using the exact offset from binwalk's scan rather than its automatic extraction.

Recovery: If the file format supports it, try repairing with a format-specific tool (e.g. zip -FF for corrupt zips).

Avoid repeating: Always verify extracted output with 'file' before assuming extraction succeeded.