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

Free lesson

Reading partitions and files in a disk image

After this lesson: I can find a partition in a disk image and list its files, deleted ones included.

60 min lesson10 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

Sixty-four million bytes and no folders

Open a disk image in a text viewer and you see bytes, not files. Yet somewhere in them is a USB stick's client list, deleted the night it went missing.

A disk has layers: a table of partitions, then a file system in each. Read them in order and the files, deleted ones too, come back.

By the end of this lesson

Note

I can find a partition in a disk image and list its files, deleted ones included.

Key words

WordWhat it meansExample
Sectorthe smallest unit a disk reads, usually 512 bytes
Partition tablea list of where each partition starts and ends
Offsethow far into the image something starts-o 2048
File systemthe structure that turns blocks into named files
Inodea record of one file's size, times and blocks
Unallocatedspace the file system has marked free

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

Examine only your own lab copy. Reading another person's disk needs their written permission or a lawful order, under the Computer Misuse Act 1990. Larkfield Design, its staff and their files 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.

The partition table is 64 bytes

The first sector of an MBR disk ends with a partition table: four entries of 16 bytes from byte 446. Byte 4 of an entry is the type, bytes 8 to 11 the first sector and 12 to 15 the count, written low byte first.

Read the two entries yourself, then let mmls read them for you.

Print the first two partition entries

Command
od -A d -t x1 -j 446 -N 32 suspect.img

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

Worked example: the first entry

BytesHexMeaning
406Type: FAT16
8 to 1100 08 00 000x0800 = sector 2048
12 to 1500 00 01 000x10000 = 65536 sectors
Offset2048 x 5121048576 bytes in

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

Let mmls read the table

Command
mmls suspect.img

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

Try it: decode an entry

Code: python
entry = bytes.fromhex("00 20 21 00 06 34 30 04 00 08 00 00 00 00 01 00")
kind = entry[4]
start = int.from_bytes(entry[8:12], "little")
sectors = int.from_bytes(entry[12:16], "little")
print("type", hex(kind), "starts at sector", start, "and holds", sectors, "sectors")
print("byte offset", start * 512)
# Change the start bytes 00 08 00 00 to 00 08 01 00, then press Run.

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

Each partition holds a file system

The Sleuth Kit's tools read one file system at a time, so you give the offset in sectors with -o. fsstat describes it, fls lists names and istat shows one file's record.

The stick is FAT16 and the laptop folder ext4. In ext4 each file's record is an inode; fls prints its number before the name.

Describe the stick's file system

Command
fsstat -o 2048 suspect.img | head -8

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

List the stick's files

Command
fls -o 2048 suspect.img

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

List every file in the laptop folder

Command
fls -r -o 67584 suspect.img

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

Deleted does not mean gone

FAT deletes a file by marking its directory entry with the byte E5 and freeing its clusters in the table. The bytes stay until something reuses them, so icat can read them back by the entry number.

ext4 is harsher. The kernel empties the inode's list of blocks, so the name survives but the path to the data does not. Those blocks are now unallocated.

Read the deleted CSV's record on the stick

Command
istat -o 2048 suspect.img 9

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

Recover its first four lines

Command
icat -o 2048 suspect.img 9 | head -4

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

Read the deleted download's inode on the laptop

Command
istat -o 67584 suspect.img 25

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

A common mistake

Warning

Many people think deleting a file wipes its data. Usually only the record that points at it changes, and the bytes stay until something overwrites them. So a deleted file can come back, whole on FAT, or by carving when ext4 has lost its blocks.

Read the laptop's authorized_keys by its inode

Command
icat -o 67584 suspect.img 26

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

Assignment: read a partition table

partitions(sector) takes the first 512 bytes of a disk and returns its MBR partitions as a list of dictionaries with "slot" (0 to 3), "type" (an int), "start" and "sectors", skipping any entry whose type is 0. The table starts at byte 446; each entry is 16 bytes, with the type at byte 4, the start at bytes 8 to 11 and the count at 12 to 15, little-endian. If the last two bytes are not 55 AA, return an empty list.

byte_offset(start, sector_size=512) returns where a partition starts in bytes.

fat_name(entry) takes a 32-byte FAT directory entry and returns (name, deleted): the name as BASE.EXT with the spaces removed (just BASE when there is no extension), and deleted True when the first byte is 0xE5, in which case the name starts with _.

Code: python
def partitions(sector):
    return []

def byte_offset(start, sector_size=512):
    return 0

def fat_name(entry):
    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.