Exercise: Forensics
Moderate
To read the directories and files in a file system, we do not need an OS. Understanding the disk layout and internal structures is sufficient to determine, purely based on bytes in an image file, which files or directories are on the disk. These kinds of skills are useful for data recovery, forensics, etc.
In this exercise, we will analyze an xv6 file system using hexdump (hd). First, download the file forensics.img. If you want to do this exercise in the online environment, you can use wget to download the file:
student@code:~/xv6$ wget https://os.cs-edu.be/files/forensics.img
-
Use
hdto print thestruct superblock.
The -nflag limits the number of bytes in the output. Using-n 1024will print exactly 1 disk block. With-s, you can print bytes starting from a specific byte offset. -
Determine the total number of inodes in this image and which disk block these inodes start on.
-
Calculate the size of a
struct dinode. (Hint: a short is 2 bytes.) -
Now use
hdto view the disk block where the inodes start. The output should start like this:<addr> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * <addr> 01 00 00 00 00 00 02 00 00 04 00 00 2e 00 00 00 |................| <addr> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| - On which data block is the root directory stored? Print this data block with
hdfor verification: at this point, you should see the names of the files in your root directory appear in the output ofhd(for example, README, cat, echo, etc.).
The root directory is always stored in inode 1 in xv6. -
The last entry in the root directory (hint:
struct dirent) is another folder. What is the name of this directory? Which inode does this directory refer to? -
Use
hdto only print the inode of this folder. What parameters do you use? The output should start like this:<addr> 01 00 00 00 00 00 02 00 50 00 00 00 d4 02 00 00 |........P.......| <addr> 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................| * <addr> 01 00 00 00 00 00 02 00 30 00 00 00 d5 02 00 00 |........0.......| -
Use
hdto only print the first data block of this folder. What parameters do you use? -
What type of
inodeis the last entry in this folder? On which data block(s) is the content stored? How large is the entry? - Use
ddto extract this file from the file system. Runman ddto see which parameters you need. Hint: setbs=1.
Optional but educational, complete the rest of the lab session first! Automate the above process. Write a script that recursively prints all folders of an xv6 file system starting from the root folder, in a programming language of your choice.