Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Carving deleted files out of free space

After this lesson: I can carve files out of unallocated space by their headers and footers.

60 min lesson12 commandsIncident response and forensics lab: investigate a compromised server in your browser

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

A photo with no name

Someone deletes an old photo from a USB stick, then saves a new file in the same folder. The new file takes the old one's place in the folder, so the name is gone.

The photo's bytes may still be on the stick, though. Every PNG starts and ends the same way. Find those ends and you can cut the file back out.

By the end of this lesson

Note

I can carve files out of unallocated space by their headers and footers.

Key words

WordWhat it meansExample
File carvingrecovering files from raw bytes by their content
Headerthe bytes a type of file starts with%PDF-
Footerthe bytes a type of file ends with%%EOF
Clusterthe block a FAT file system hands out
Fragmentedstored in pieces that are not next to each other
CRCa check value that shows whether data is damaged

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

The one rule

Important

Carve only your own lab copy. Recovering deleted files from someone else's device needs their written permission or a lawful order, under the Computer Misuse Act 1990. The stick, the photo and the quote are made up.

The disk from lesson 1

This lesson reads suspect.img, the made-up firm's disk that lesson 1 made in ~/forensics. If the Run below says it is missing, do lesson 1's first Runs on this VM, then come back.

Move in and find the disk

Command
cd ~/forensics && ls -l suspect.img

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Carve only where nothing lives

Allocated files can be read normally. Deleted data hides in the clusters the file system marked free. blkls saves exactly those, in order, into one file.

Then grep shows where each header sits: -b prints the byte offset and -o only the match. A PNG's first byte is not text, so grep reports it one byte early, at PNG.

Save the stick's free clusters

Command
blkls -o 2048 suspect.img > fat-free.bin

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

How big is the free space?

Command
ls -l fat-free.bin

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Find the headers in it

Command
grep -a -b -o -e PNG -e %PDF- fat-free.bin

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A header, a footer and the bytes between

File carving cuts from a header to the matching footer. A PNG ends with an IEND chunk and its four-byte check; a PDF ends with %%EOF.

Each PNG chunk also carries a CRC. If every CRC matches, the carved file is whole. The script checks that; foremost, a carver from Ubuntu's archive, does the same job.

Write the carver, part 1

Command
printf '%s\n' 'import os' 'import struct' 'import sys' 'import zlib' 'data = open(sys.argv[1], "rb").read()' 'os.makedirs("carved", exist_ok=True)' 'PNG = bytes([137, 80, 78, 71, 13, 10, 26, 10])' 'KINDS = [("png", PNG, b"IEND", 8), ("pdf", b"%PDF-", b"%%EOF", 5)]' '' '' 'def crc_ok(blob):' '    at = 8' '    while at < len(blob):' '        size = struct.unpack(">I", blob[at:at + 4])[0]' '        kind = blob[at + 4:at + 8]' '        stored = struct.unpack(">I", blob[at + 8 + size:at + 12 + size])[0]' '        if not zlib.crc32(kind + blob[at + 8:at + 8 + size]) == stored:' '            return False' '        at += 12 + size' '    return True' > carve.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the carver, part 2

Command
printf '%s\n' '' '' 'n = 0' 'for ext, head, foot, extra in KINDS:' '    start = data.find(head)' '    while start > -1:' '        end = data.find(foot, start)' '        if end < 0:' '            break' '        blob = data[start:end + extra]' '        n += 1' '        name = "carved/own-%d.%s" % (n, ext)' '        open(name, "wb").write(blob)' '        note = ""' '        if ext == "png":' '            note = "every chunk CRC matches" if crc_ok(blob) else "a chunk CRC does not match"' '        print(name, "carved:", len(blob), "bytes from offset", start, note)' '        start = data.find(head, end)' >> carve.py

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Carve the free space

Command
python3 carve.py fat-free.bin

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Ask what the carved files are

Command
file carved/own-*

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read the words inside the carved PDF

Command
grep -a -o "(.*)" carved/own-2.pdf

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Carve the same bytes with foremost

Command
rm -rf carved/fm && foremost -q -t png,pdf -i fat-free.bin -o carved/fm && grep -A3 "^Num" carved/fm/audit.txt

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

What carving cannot give back

A carved file has no name, no folder and no times: those lived in the directory entry, which is gone. Plain text has no header, so it is found by searching for its words instead.

A fragmented file breaks header and footer carving. The ext4 download lost its block list, so search the laptop's free blocks for its words.

Search the laptop's free blocks for the download

Command
blkls -o 67584 suspect.img | grep -a "stands in"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Search free space for the client list

Command
grep -a -c "@client" fat-free.bin

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

A common mistake

Warning

Many people think carving gives a deleted file back as it was, name and all. It gives back bytes only, and only when the file was stored in one piece. The name and times have to come from elsewhere, such as a directory entry or a log.

Try it: carve a file in two pieces

Code: python
free = b"....HEAD-one-file-TAIL....HEAD-second-"
more = b"file-TAIL...."
data = free + more
start = data.find(b"HEAD")
while start > -1:
    end = data.find(b"TAIL", start)
    print(data[start:end + 4])
    start = data.find(b"HEAD", end)
# Put b"XXXXXXXX" + between free and more, then press Run.

Make a free account to save your work and track your progress.

Assignment: carve by signature

find_all(data, sig) returns every offset where the bytes sig start in data, in order, overlapping matches included.

carve(data, header, footer) returns a list of byte strings, each from a header to the end of the first footer after it. A header with no footer after it is skipped. The next search starts after the footer.

png_ok(blob) returns True when blob starts with the PNG signature (bytes 137 80 78 71 13 10 26 10) and every chunk after it has a CRC that matches: a chunk is a 4-byte big-endian length, a 4-byte type, the data, then a 4-byte big-endian CRC of the type and the data (zlib.crc32). A chunk that runs past the end makes it False.

Code: python
import struct
import zlib

def find_all(data, sig):
    return []

def carve(data, header, footer):
    return []

def png_ok(blob):
    return False

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.