EZ LearnEZ Learn
Courses
EZ Board
Dashboard
All articles
python-for-cyber-securitypythondigital-forensicsfile-carving

File Carving: Recovering Data Without a File System

Tearing a page from an address book leaves its ink untouched. Deleting a file usually works the same way, which is exactly what lets a carving tool pull a file back out of raw, unlabelled bytes.

Ashish Revar17 October 20267 min read1 views
1 video lesson 6:09 total
Sign in to track your progress

Video lesson

Every article so far has assumed the file system still knows where a file begins and ends. This article drops that assumption entirely, and recovers files from nothing but a stream of raw bytes.

Analogy: The shredded letter

When a file is deleted, the operating system rarely erases its content; it only marks the space as free to reuse, the way tearing a letter from an address book leaves the page's content untouched until something else is written over it. File carving goes further than this: rather than trust a damaged or missing file system to say where a file begins and ends, it searches raw disk bytes directly for the signature every file format begins with, and cuts the file out wherever that signature appears.

File signatures

Every common format begins with a fixed sequence of bytes, often called its magic bytes, the same signature python-magic reads in the indexing article, now searched for directly across an entire disk image.

FormatSignature (hex)First bytes, as text
JPEGFF D8 FF(binary, not readable as text)
PNG89 50 4E 47.PNG
PDF25 50 44 46%PDF
ZIP (and DOCX, XLSX)50 4B 03 04PK..
GIF47 49 46 38GIF8

The algorithm

Python concept: Signature-based file carving

Input: a raw disk image and a table of known file signatures. Output: every recoverable file found, written out individually.

  1. Read the disk image as one continuous stream of bytes.
  2. Slide through the stream looking for a known starting signature.
  3. When a signature is found, note the starting position.
  4. Read forward until the format's known end marker, or a fixed maximum size.
  5. Write the bytes between start and end out as a recovered file.
  6. Continue searching from just after this file for the next signature.

Recovering JPEGs from raw bytes

simple_carver.py

# simple_carver.py: recover JPEG images from a raw image file
def carve_jpegs(disk_image_path, output_dir="carved"):
    import os
    os.makedirs(output_dir, exist_ok=True)
    start_marker = b"\xff\xd8\xff"
    end_marker = b"\xff\xd9"

    with open(disk_image_path, "rb") as f:
        data = f.read()

    found = 0
    pos = 0
    while True:
        start = data.find(start_marker, pos)
        if start == -1:
            break
        end = data.find(end_marker, start)
        if end == -1:
            break
        end += len(end_marker)
        with open(f"{output_dir}/carved_{found}.jpg", "wb") as out:
            out.write(data[start:end])
        found += 1
        pos = end
    return found

if __name__ == "__main__":
    print(f"Recovered {carve_jpegs('disk_image.dd')} images")

Run against a small test image built for this article, 1,065 bytes of unlabelled data with three JPEG-shaped byte sequences buried inside it, separated by stretches of ordinary-looking filler:

Sample output

Recovered 3 images
fillerJPEG #1205 bytesfillerJPEG #2345 bytesfillerJPEG #3150 bnone of this is a file system; carve_jpegs() never looked at one
Figure 3.4. A carver ignores every byte that is not part of a recognised file. It finds a start signature, reads to the matching end marker, and writes exactly those bytes out as a file, three separate times in this 1,065-byte test image.

data.find(start_marker, pos) searches forward from wherever the last file ended, so the loop never rescans bytes it has already dealt with. pos = end after writing each file is what makes this a single pass over the whole image rather than one pass per file found.

What carving can and cannot promise

Why it carries weight: Why this matters

Carving has no idea whether a file system ever labelled these bytes as a file at all, which is exactly what makes it work even when the file system's own records are damaged, encrypted, or deliberately wiped. It also means a carved file can be incomplete or corrupted if the original was fragmented across the disk, a limitation every forensic report using this technique should state plainly.

Caution: A found signature is not a guarantee

The same three bytes that start a real JPEG can appear by coincidence inside completely unrelated data, and a carved file that reaches its end marker is not proof that every byte in between truly belonged to one original photograph. A carved result is always a candidate to be verified, for instance by checking whether the recovered bytes actually open as a valid image, never a confirmed finding on its own.

Check your understanding

Question 1: Why does deleting a file usually leave its content recoverable?

Deleting a file typically only marks the space it occupied as free to be reused; the file system stops pointing to it, but the underlying bytes are not erased until something else happens to be written over that same space. Until that overwrite happens, the original content is still there to be found.

Question 2: What does a file carver search for, and why does it not need a working file system to do its job?

It searches raw bytes directly for a format's known starting signature, such as FF D8 FF for a JPEG. Because it works on the bytes themselves rather than on the file system's records of where files begin and end, it can recover data even when those records are damaged, encrypted or deliberately wiped.

Question 3: Why can a carved file be incomplete or corrupted even when carving succeeds?

If the original file was fragmented, stored in non-contiguous pieces scattered across the disk, a carver that simply reads forward from a start signature to an end marker will capture whatever bytes happen to lie between them on the disk, not necessarily the correct, contiguous original content. The result can look plausible while actually being wrong or incomplete.

Key takeaway: Key takeaway

File carving searches raw bytes for a format's own signature and ignores the file system entirely, which is exactly why it still works when that file system is damaged or gone. It is a powerful last resort, and its results are always candidates to verify, never confirmed findings on their own.

Assessments

Unit Assessment

Digital Forensics with Python

20 questions30 minPass: 60%

Start exam →

Comprehensive Assessment

Python for Cyber Security: Complete Assessment

100 questions120 minPass: 60%

Start final exam →

Related to this topic

Continue learning

Reference material

Cheatsheet

Python for Cyber Security — Cheatsheet

v1.0

Open resource

MCQ Bank

Python for Cyber Security — MCQ Bank

v1.0

Open resource

Question Bank

Python for Cyber Security — Question Bank

v1.0

Open resource

eBook

Python for Cyber Security — eBook

v1.0

Open resource

Discussion

Loading comments…
More articlesTest your knowledge
EZ MentorAI · Free

File Carving: Recovering Data Without a File System

Hi! I'm EZ Mentor

Your AI tutor for
File Carving: Recovering Data Without a File System

Sign in to ask, it's free.

EZ Mentor is AI-powered · Answers may not be perfect · 10 free questions / day

EZ LearnEZ Learn

Structured learning across Reverse Engineering, Cloud Security, and Research Methodology, plus games and practice tools in EZ Arena. Free, and open to anyone.

EZ Learn

  • Courses
  • EZ Articles
  • EZ Watch
  • EZ Talks
  • EZ Bytes
  • Cyber Hygiene

EZ Library

  • All Resources
  • eBooks & PDFs
  • Question Banks
  • MCQ Banks
  • Cheatsheets

EZ Exams

  • All Exams
  • REMA
  • Cloud Security
  • Research Methodology
  • Python for Cyber Security
  • Cyber Law & IPR
  • Community
  • EZ Board

EZ Arena

  • EZ Games
  • Live Quiz
  • ML Challenges
  • दिशा - Hygiene
  • सजग - Awareness
  • अभ्यास - Practice

Platform

  • About
  • Contact
  • Terms of Use
  • Disclaimer
  • Verify Certificate
  • Events

© 2026 EZ Learn by EpochZero. Educational content for learning purposes.

Platform Lead: Ashish Revar