Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 36 ยท Disk and File System Forensics

File Signatures and File Carving

Learn how file headers and footers (magic numbers) identify file types, how carving recovers files from unallocated space without file-system metadata, and why fragmentation and extension mismatches matter.

When a file system is damaged, formatted or wiped, the directory entries that said "this file lives here" may be gone. The file contents are often still on disk, though. File carving recovers files by scanning raw bytes for the distinctive patterns at the start (header) and sometimes the end (footer) of known file types, then cutting out what lies between. Carving needs no file system at all, which makes it a tool of last resort and a way to find data hiding in unallocated space.

Magic numbers

Most file formats begin with a fixed sequence of bytes called a file signature or magic number:

Type Header (hex) Footer (hex) Notes
JPEG FF D8 FF FF D9 Next byte after the header differs by variant (E0 JFIF, E1 Exif)
PNG 89 50 4E 47 0D 0A 1A 0A 49 45 4E 44 AE 42 60 82 Ends with the IEND chunk
GIF 47 49 46 38 39 61 or ...37 61 00 3B "GIF89a" / "GIF87a"
PDF 25 50 44 46 2D 25 25 45 4F 46 "%PDF-" and "%%EOF"
ZIP (also DOCX, XLSX, JAR, APK) 50 4B 03 04 (end-of-central-directory record) Office files are ZIPs
RAR 52 61 72 21 1A 07 "Rar!"
EXE / DLL (Windows) 4D 5A "MZ"
ELF (Linux) 7F 45 4C 46 "\x7fELF"
SQLite 53 51 4C 69 74 65 20 66 6F 72 6D 61 74 20 33 00 "SQLite format 3"
MP3 with ID3 49 44 33 "ID3"

Never trust a file extension alone. A user can rename plan.jpg to plan.txt, but the first bytes still say JPEG. Comparing a file's extension to its signature is a classic triage check for extension mismatches.

Python Detect extension mismatches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the Office exception: .xlsx is correctly a ZIP container, so a naive check needs a table of acceptable pairs.

How carving works

Header/footer carving scans the image from start to end:

  1. Look for a header signature.
  2. From there, look for the matching footer, or stop after a maximum size for that type.
  3. Save the bytes in between as a candidate file.
  4. Continue after the carved region (or from the byte after the header, to find nested files).
flowchart LR
    A["Scan raw bytes"] --> B{"Header found?"}
    B -- "no" --> A
    B -- "yes" --> C["Look for footer<br/>or size limit"]
    C --> D["Save candidate file"]
    D --> A

Here is a working carver for JPEG-like data inside a made-up "unallocated space" buffer. It uses only a start marker, an end marker and a size cap.

Python Carve files out of raw bytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The output includes each file's offset in the image, which you record in the report so the find can be reproduced.

Limits of carving

  • Fragmentation: if the file's clusters are not contiguous, bytes from another file get stuck in the middle. Smarter carvers use the file's internal structure to validate the result.
  • No names or timestamps: a carved file has no metadata from the file system, only what is inside the file itself (such as Exif data).
  • False positives: a signature can appear by chance in other data. Always validate by opening the file in a safe viewer or checking its structure.
  • Formats without footers (like many Office or video files) rely on length fields or maximum sizes.
  • Compressed or encrypted data hides signatures; look for entropy instead (see below).
  • SSD TRIM may have erased unallocated space already.

Entropy: what if there is no signature?

Entropy measures randomness in bytes: plain text has low entropy (about 4 to 5 bits per byte), compressed or encrypted data is close to 8. A large block of high-entropy bytes with no header might be an encrypted container such as a VeraCrypt volume. High entropy is a clue, never proof.

Python Estimate entropy of a data block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real carving tools you may meet (not run here): foremost, scalpel, photorec and the carving modules of Autopsy. They read a configuration file of headers, footers and size limits just like the stand-in above.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the most reliable way to determine a file's type?

    Extensions can be renamed freely; the first bytes of the content identify the format.
  2. 2. What makes carving possible without a working file system?

    Carving ignores file-system structures and relies purely on content signatures.
  3. 3. Why do fragmented files cause carving problems?

    A carver copies a contiguous range, so fragments from other files can end up inside the result.
  4. 4. A large region with entropy near 8 bits per byte and no signature suggests what?

    High entropy is typical for compressed or encrypted content, though it is only a clue.

Summary

  • File signatures (magic numbers) identify types independently of extensions; mismatches deserve a look.
  • Carving finds files in raw data by header, footer and size limit, without any file-system metadata.
  • Record the offset and hash of every carved file.
  • Fragmentation, false positives and missing metadata limit carving; validate every result.
  • Entropy highlights compressed or encrypted blocks that lack signatures.

Next: Deleted files and slack space