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
| Word | What it means | Example |
|---|---|---|
| File carving | recovering files from raw bytes by their content | |
| Header | the bytes a type of file starts with | %PDF- |
| Footer | the bytes a type of file ends with | %%EOF |
| Cluster | the block a FAT file system hands out | |
| Fragmented | stored in pieces that are not next to each other | |
| CRC | a 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 questionsThe 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
cd ~/forensics && ls -l suspect.imgShown 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
blkls -o 2048 suspect.img > fat-free.binShown here, not run. Press Start lab to type it into your own lab’s terminal.
How big is the free space?
ls -l fat-free.binShown here, not run. Press Start lab to type it into your own lab’s terminal.
Find the headers in it
grep -a -b -o -e PNG -e %PDF- fat-free.binShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsA 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
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the carver, part 2
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.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Carve the free space
python3 carve.py fat-free.binShown here, not run. Press Start lab to type it into your own lab’s terminal.
Ask what the carved files are
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
grep -a -o "(.*)" carved/own-2.pdfShown here, not run. Press Start lab to type it into your own lab’s terminal.
Carve the same bytes with foremost
rm -rf carved/fm && foremost -q -t png,pdf -i fat-free.bin -o carved/fm && grep -A3 "^Num" carved/fm/audit.txtShown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsWhat 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
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
grep -a -c "@client" fat-free.binShown 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
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.
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