如何从 FAT 映像恢复已删除的文件?

How to recover deleted file from FAT image?

我想知道如何从 FAT 中恢复已删除的文件。我创建了 fat.img 如下。

cd /tmp
dd if=/dev/zero of=fat.img bs=1024 count=100
mkfs.msdos fat.img
mkdir -p /tmp/fs
sudo mount -t msdos fat.img /tmp/fs -o umask=000,loop

现在我正在用一些文本创建文件。

cd/tmp/fs
echo "hello world"> name

使用 hexdump 查看它是如何保存的

cd ..
hexdump -C fat.img 

00000000  eb 3c 90 6d 6b 66 73 2e  66 61 74 00 02 04 01 00  |.<.mkfs.fat.....|
00000010  02 00 02 c8 00 f8 01 00  20 00 40 00 00 00 00 00  |........ .@.....|
00000020  00 00 00 00 80 01 29 3c  69 e6 fb 4e 4f 20 4e 41  |......)<i..NO NA|
00000030  4d 45 20 20 20 20 46 41  54 31 32 20 20 20 0e 1f  |ME    FAT12   ..|
00000040  be 5b 7c ac 22 c0 74 0b  56 b4 0e bb 07 00 cd 10  |.[|.".t.V.......|
00000050  5e eb f0 32 e4 cd 16 cd  19 eb fe 54 68 69 73 20  |^..2.......This |
00000060  69 73 20 6e 6f 74 20 61  20 62 6f 6f 74 61 62 6c  |is not a bootabl|
00000070  65 20 64 69 73 6b 2e 20  20 50 6c 65 61 73 65 20  |e disk.  Please |
00000080  69 6e 73 65 72 74 20 61  20 62 6f 6f 74 61 62 6c  |insert a bootabl|
00000090  65 20 66 6c 6f 70 70 79  20 61 6e 64 0d 0a 70 72  |e floppy and..pr|
000000a0  65 73 73 20 61 6e 79 20  6b 65 79 20 74 6f 20 74  |ess any key to t|
000000b0  72 79 20 61 67 61 69 6e  20 2e 2e 2e 20 0d 0a 00  |ry again ... ...|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f8 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000400  f8 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00000410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00000600  4e 41 4d 45 20 20 20 20  20 20 20 20 00 00 00 00  |NAME        ....|
00000610  00 00 00 00 00 00 21 86  91 4b 03 00 0c 00 00 00  |......!..K......|
00000620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004e00  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a 00 00 00 00  |hello world.....|
00004e10  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00019000

删除文件名后我们可以在hexdump中看到变化

00000600  4e 41 4d 45 20 20 20 20  20 20 20 20 00 00 00 00  |.AME        ....|
00000610  00 00 00 00 00 00 21 86  91 4b 03 00 0c 00 00 00  |......!..K......|
00000620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|

这是我的问题,您有什么建议吗?我怎样才能将 fat.img 更改为 .AME 到 NAME 以恢复我的文件?

how can i change fat.img to .AME to NAME to recovery my file?

简短的回答是dd,下面是一个带有必要注意事项的示例。

添加到 Martin 的答案中,同时操纵字节以恢复软盘映像中的文件是一个相对直接的命题 dd,计算文件分配的位置和内容 table( s) 需要恢复是挑战。通过使用 dd 恢复文件本身,了解需要注意的字节如下例所示。

创建要使用的软盘映像使您不必在实际映像上进行试验。只需复制您希望使用的图像,或在硬盘驱动器上的文件中创建一个新图像。您可以使用 mkfs.msdos 轻松做到这一点(根据需要调整文件系统类型),然后按如下方式将文件挂载到您的文件系统中,例如

$ mkfs.msdos -C /home/david/tmp/tt/floppy_144.img 1440
$ sudo mount /home/david/tmp/tt/floppy_144.img /mnt/fd

现在让我们添加 NAME 文件:

$ echo "hello world" > NAME
$ sudo cp -a NAME /mnt/fd
$ ls -l /mnt/fd
total 1
-rwxr-xr-x  1 root root   12 Dec 17 13:55 NAME
$ cat /mnt/fd/NAME
hello world

在从您的图像中删除文件之前,对内容进行十六进制转储,以便您可以准确地看到需要恢复的内容。 (这是您必须计算的内容,以便了解要用原始图像恢复到哪里以及恢复什么,您将需要查阅相关文件系统的参考资料)

$ hexdump -C floppy_144.img >flpwname.txt

现在从您的图像中删除该文件并再次保存显示更改的 hexdump。

$ sudo rm /mnt/fd/NAME
$ hexdump -C floppy_144.img >flpwoname.txt

现在您可以检查与 diff 的区别。你发现你必须恢复的不仅仅是被删除文件的名字,你将需要恢复文件分配 table 条目,以便恢复的文件可以再次位于文件系统中(两个副本的脂肪),例如

$ diff flpwname.txt flpwoname.txt
16c16
< 00000200  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
---
> 00000200  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
19c19
< 00001400  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
---
> 00001400  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
22c22
< 00002600  4e 41 4d 45 20 20 20 20  20 20 20 20 00 00 fa 9e  |NAME        ....|
---
> 00002600  e5 41 4d 45 20 20 20 20  20 20 20 20 00 00 fa 9e  |.AME        ....|

注意上面的文件分配 table(s) 在 0x2040x1404 的条目在文件被删除时被清零。使用 dd 可以轻松将字节恢复为原始字节,但 请注意您的选项 。具体来说,你的 block size (bs)、output block size (obs)、countseek 都必须在 bytes 中(指定通过将 c) 附加到数字,您必须设置 notrunc 转换选项以防止在您进行更改后截断图像。最后,必须在 decimal 而不是 hexadecimal.

中指定所有尺寸

此外,如果您使用 bash,您可以使用进程重定向来指定要替换的字节(例如 if=<(printf "\xf0\xff") 写入十六进制字节 f0ff),否则,您将不得不准备包含替换字符串的输入文件。恢复 FAT 和文件名第一个字符的 dd 命令相当简单(有关选项说明,请参阅 man 1 dd)。

下面我们恢复FAT的第一个副本,然后是第二个,最后恢复文件名的第一个字符。 seek(偏移量)值只是由 hexdump 提供的转换为十进制的值。 (您应该在进行更改之前卸载您的文件系统。您可以在安装软盘映像时进行更改,但在您重新安装之前它们不会反映出来)

$ sudo umount /mnt/fd

$ dd if=<(printf "\xf0\xff") of=floppy_144.img \
bs=1c obs=1c count=2c seek=516c conv=notrunc

$ dd if=<(printf "\xf0\xff") of=floppy_144.img \
bs=1c obs=1c count=2c seek=5124c conv=notrunc

$ dd if=<(printf "N") of=floppy_144.img \
bs=1c obs=1c count=1c seek=9728c conv=notrunc

现在您可以创建修复后的软盘映像的 hexdump 并将其与原始文件进行比较。如果一切顺利,那就没有区别了。

$ hexdump -C floppy_144.img >flprepair.txt
$ diff flpwname.txt flprepair.txt

最后,只需重新挂载您的文件系统并确认文件已恢复。

$ sudo mount /home/david/tmp/tt/floppy_144.img /mnt/fd
$ ls -l /mnt/fd
total 1
-rwxr-xr-x 1 root root 12 Dec 17 13:55 NAME
$ cat /mnt/fd/NAME
hello world

就是这样。我希望这就是您要找的。有许多工具可以为您自动执行此过程,但是 dd 和铅笔和纸就可以完成。

完整的十六进制转储如下:

Original/Restored

$ cat flpwname.txt
00000000  eb 3c 90 6d 6b 66 73 2e  66 61 74 00 02 01 01 00  |.<.mkfs.fat.....|
00000010  02 e0 00 40 0b f0 09 00  12 00 02 00 00 00 00 00  |...@............|
00000020  00 00 00 00 00 01 29 2c  72 18 ba 4e 4f 20 4e 41  |......),r..NO NA|
00000030  4d 45 20 20 20 20 46 41  54 31 32 20 20 20 0e 1f  |ME    FAT12   ..|
00000040  be 5b 7c ac 22 c0 74 0b  56 b4 0e bb 07 00 cd 10  |.[|.".t.V.......|
00000050  5e eb f0 32 e4 cd 16 cd  19 eb fe 54 68 69 73 20  |^..2.......This |
00000060  69 73 20 6e 6f 74 20 61  20 62 6f 6f 74 61 62 6c  |is not a bootabl|
00000070  65 20 64 69 73 6b 2e 20  20 50 6c 65 61 73 65 20  |e disk.  Please |
00000080  69 6e 73 65 72 74 20 61  20 62 6f 6f 74 61 62 6c  |insert a bootabl|
00000090  65 20 66 6c 6f 70 70 79  20 61 6e 64 0d 0a 70 72  |e floppy and..pr|
000000a0  65 73 73 20 61 6e 79 20  6b 65 79 20 74 6f 20 74  |ess any key to t|
000000b0  72 79 20 61 67 61 69 6e  20 2e 2e 2e 20 0d 0a 00  |ry again ... ...|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001400  f0 ff ff 00 f0 ff 00 00  00 00 00 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002600  4e 41 4d 45 20 20 20 20  20 20 20 20 00 00 fa 9e  |NAME        ....|
00002610  91 4b 91 4b 00 00 f5 9e  91 4b 03 00 0c 00 00 00  |.K.K.....K......|
00002620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004400  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a 00 00 00 00  |hello world.....|
00004410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00168000

NAME删除后

$ cat flpwoname.txt
00000000  eb 3c 90 6d 6b 66 73 2e  66 61 74 00 02 01 01 00  |.<.mkfs.fat.....|
00000010  02 e0 00 40 0b f0 09 00  12 00 02 00 00 00 00 00  |...@............|
00000020  00 00 00 00 00 01 29 2c  72 18 ba 4e 4f 20 4e 41  |......),r..NO NA|
00000030  4d 45 20 20 20 20 46 41  54 31 32 20 20 20 0e 1f  |ME    FAT12   ..|
00000040  be 5b 7c ac 22 c0 74 0b  56 b4 0e bb 07 00 cd 10  |.[|.".t.V.......|
00000050  5e eb f0 32 e4 cd 16 cd  19 eb fe 54 68 69 73 20  |^..2.......This |
00000060  69 73 20 6e 6f 74 20 61  20 62 6f 6f 74 61 62 6c  |is not a bootabl|
00000070  65 20 64 69 73 6b 2e 20  20 50 6c 65 61 73 65 20  |e disk.  Please |
00000080  69 6e 73 65 72 74 20 61  20 62 6f 6f 74 61 62 6c  |insert a bootabl|
00000090  65 20 66 6c 6f 70 70 79  20 61 6e 64 0d 0a 70 72  |e floppy and..pr|
000000a0  65 73 73 20 61 6e 79 20  6b 65 79 20 74 6f 20 74  |ess any key to t|
000000b0  72 79 20 61 67 61 69 6e  20 2e 2e 2e 20 0d 0a 00  |ry again ... ...|
000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa  |..............U.|
00000200  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00001400  f0 ff ff 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00001410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00002600  e5 41 4d 45 20 20 20 20  20 20 20 20 00 00 fa 9e  |.AME        ....|
00002610  91 4b 91 4b 00 00 f5 9e  91 4b 03 00 0c 00 00 00  |.K.K.....K......|
00002620  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00004400  68 65 6c 6c 6f 20 77 6f  72 6c 64 0a 00 00 00 00  |hello world.....|
00004410  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00168000