Test Disk
Test Disk
Test Disk
Release 7.1
Christophe GRENIER
1 Presentation 1
1.1 TestDisk - Partition recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 TestDisk - Filesystem repair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 TestDisk - File recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 PhotoRec - File recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 QPhotoRec - File recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Installation 5
2.1 Linux: Installation of distribution package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Official binaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
7 Repairing filesystem 19
7.1 Repairing filesystems from Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.2 Repairing filesystems from Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.3 Repairing filesystems from Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
i
7.4 Repairing FAT32, exFAT and NTFS boot sector using TestDisk . . . . . . . . . . . . . . . . . . . . 20
7.5 TestDisk: Repairing FAT boot sector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
7.6 TestDisk: Repairing NTFS boot sector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.7 TestDisk: repairing ext2/3/4 filesystem superblock . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
7.8 Repairing HFS/HFS+ volume header using TestDisk . . . . . . . . . . . . . . . . . . . . . . . . . . 22
ii
16.1 ddrescue on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
16.2 ddrescue on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
16.3 DDRescue: disk to file image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
16.4 DDRescue: disk to disk copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
16.5 ddrutility: restricting ddrescue to NTFS allocated data block . . . . . . . . . . . . . . . . . . . . . . 48
17 Scripted run 49
17.1 Automating recovery using TestDisk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
17.2 Automating recovery using PhotoRec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
17.3 Windows UAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
iii
iv
CHAPTER 1
Presentation
TestDisk & PhotoRec are free and open-source data recovery utilities. TestDisk has been created in 1998 and Pho-
toRec in April 2002 by Christophe GRENIER, they can be downloaded from https://www.cgsecurity.org/. They are
distributed under the GNU General Public License v2 or later, you can
run the program as you wish, for any purpose,
study how the program works, and change it so it does your computing as you wish (You have access to the
source code.),
redistribute copies so you can help your neighbor,
distribute copies of your modified versions to others under the same license. By doing this you can give the
whole community a chance to benefit from your changes.
Archives with ready-to-use binaries are available for
DOS (32-bit x86)
Microsoft Windows (32-bit x86 or 64-bit x64)
Linux (32-bit x86 or 64-bit x64)
Mac OS X (PowerPC or Intel)
Marvell 88F628x Linux
TestDisk & PhotoRec can also be compiled for other plateforms, notably
FreeBSD/OpenBSD/NetBSD, Unix-like computer operating system descended from Berkeley Software Distri-
bution (BSD), a Research Unix derivative developed at the University of California, Berkeley.
Haiku, a free and open-source operating system compatible with the now discontinued BeOS.
SunOS/Solaris, a Unix-branded operating system developed by Sun Microsystems for their workstation and
server computer systems,
1
TestDisk Documentation, Release 7.1
2 Chapter 1. Presentation
TestDisk Documentation, Release 7.1
When a file is deleted, the list of disk clusters occupied by the file is erased, marking those sectors available for use by
other files created or modified thereafter. If the file wasnt fragmented and the clusters havent been reused, TestDisk
can recover the deleted file for various filesystem:
FAT
NTFS
exFAT
ext2
PhotoRec is a file carver data recovery software tool. It doesnt recover the original filenames but it can recover delete
files even from corrupted filesystem. PhotoRec recognizes and recovers numerous file formats including ZIP, Office,
PDF, HTML, JPEG and various graphics file formats. The whole list of file formats recovered by PhotoRec contains
more than 480 file extensions (about 300 file families). Its possible to create custom signature to recover file format
unknown to PhotoRec.
QPhotoRec is a file carver data recovery software tool with a graphical user interface. Like PhotoRec, it doesnt
recover the original filenames but it can recover delete files even from corrupted filesystem.
4 Chapter 1. Presentation
CHAPTER 2
Installation
2.1.1 CentOS
As root,
2.1.2 Fedora
As root,
Copr is an automatic build system. It provide the latest developpement version. As root,
As root,
Using the developpement version (WIP=Work In Progress) is usually recommanded as fixes are not backported. The
WIP archive may be modified several times per week but keep the same name. If this version doesnt start, you can
5
TestDisk Documentation, Release 7.1
always use the stable version and warn the developper of the problem with the beta version.
List your files, a directory named testdisk-7.1-WIP should has been created.
6 Chapter 2. Installation
CHAPTER 3
3.1.1 Linux
7
TestDisk Documentation, Release 7.1
3.1.2 Mac OS X
Install Xcode
3.1.3 Windows
cygwin
Cygwin https://cygwin.com/ is a large collection of GNU and Open Source tools which provide functionality similar
to a Linux distribution on Windows, it includes the gcc compiler. A DLL (cygwin1.dll) provides substantial POSIX
API functionality, such functions may be required by some libraries that TestDisk or PhotoRec can use.
MinGW-w64
MinGW-w64 http://mingw-w64.org/ is a free and open source software development environment for creating Mi-
crosoft Windows applications. It provides GCC for Windows 64 & 32 bits.
Using Linux, its possible to generate binaries for Windows. Two cross-compiler toolchains are available under Fedora
and CentOS 7 to create binaries for Windows 32 and 64 bits. All packages needed are available at
Windows cygwin target: https://copr.fedorainfracloud.org/coprs/grenier/cygwin-testdisk/
Windows mingw target: https://copr.fedorainfracloud.org/coprs/grenier/mingw-testdisk/
testdisk, photorec and fidentify official binaries are generated using cygwin, qphotorec using mingw.
3.3 Compilation
Once you have downloaded the source archive from https://www.cgsecurity.org/wiki/TestDisk_Download, run
If you have already cloned the project, to update your local copy, run git pull from the testdisk directory.
cd testdisk
mkdir config
autoreconf --install -W all -I config
./configure
make
3.3. Compilation 9
TestDisk Documentation, Release 7.1
If you need to repair a computer that isnt booting correctly, you can move its harddisk to a working computer or start
your computer from an USB key or a DVD. Its this later solution that will be presented here.
You need an USB flash drive also known as USB stick, thumb drive, pen drive, or jump drive that you can erase. Note
its also possible to use a blank DVD.
Download Fedora Image Live from https://getfedora.org/fr/workstation/download/
4.1 Windows
lsblk
umount /run/media/user/mountpoint
sudo dd if=/path/to/image.iso of=/dev/sdX bs=8M status=progress oflag=direct
Wait until the command completes. If you receive dd: invalid status flag: progress error,
your dd version doesnt support status=progress option and youll need to remove it (and you wont see writing
progress).
11
TestDisk Documentation, Release 7.1
This method is for people running Linux with GNOME, Nautilus and the GNOME Disk Utility installed. A standard
installation of Fedora, or a standard GNOME installation of many other distributions, should be able to use this
method. On Fedora, ensure the packages nautilus and gnome-disk-utility are installed. Similar graphical direct-write
tools may be available for other desktops.
Download a Fedora image, choose a USB stick that does not contain any data you need, and connect it
Run Nautilus (Files) - for instance, open the Overview by pressing the Start/Super key, and type Files, then hit
enter
Find the downloaded image, right-click on it, go to Open With, and click Disk Image Writer
Double-check youre really, really sure you dont need any of the data on the USB stick!
Select your USB stick as the Destination, and click Start Restoring...
Wait for the operation to complete, then reboot your computer, and do whatever you need to do to boot from a
USB stick - often this will involve pressing or holding down F12, F2 or Del.
4.4 OS X
Open a terminal
Run diskutil list. This will list all disks connected to the system, as /dev/rdisk1, /dev/rdisk2
and so on. Identify - very carefully! - which one corresponds to the USB stick you wish to use. Hereafter, well
assume it was /dev/rdisk2 - modify the commands as appropriate for your stick.
Run diskutil unmountDisk /dev/rdisk2
Type dd if=, then drag and drop the Fedora image file to the terminal window - this should
result in its filesystem location being appended to the command. Now complete the com-
mand with of=/dev/rdisk2 bs=1m, but dont hit Enter yet. You should wind up with
something like sudo dd if=/Volumes/Images/Fedora-Live-Desktop-x86_64-20-1.iso
of=/dev/rdisk2 bs=1m
Double-check you have the correct disk number and youre really, really sure you dont need any of the data on
the USB stick!
Hit Enter
Plug the USB key on the damaged computer and boot this computer, and do whatever you need to do to boot from a
USB stick - often this will involve pressing or holding down F12, F2 or Del. If you are using a Mac computer, hold
down the left Alt/Option key to access the boot menu - you should see a Fedora logo. Click this to boot.
Original source of this page: https://fedoraproject.org/wiki/How_to_create_and_use_Live_USB
13
TestDisk Documentation, Release 7.1
Note: Windows users, if you see cygwin1.dll not found,c\\cygwin is missing, extract all the files
from the archive before running TestDisk or PhotoRec.
cd testdisk-7.0
sudo ./testdisk_static
15
TestDisk Documentation, Release 7.1
cd testdisk-7.0
sudo ./photorec_static
Note: If your Raid device (ie. Intel raid) is missing, run sudo dmraid -ay to activate it.
QPhotoRec is a Qt5 application, it isnt shipped with the official Linux binaries from www.cgsecurity.org. But it is
available on most Linux distribution or can be compiled from source. To run it in a Terminal,
sudo qphotorec
xhost +local:
sudo qphotorec
If you are not root, TestDisk (ie testdisk-7.0/testdisk) or PhotoRec will restart itself using sudo after
confirmation from your part.
If your administrator account has no password (a blank password), you must give that user a password before using
the sudo command:
Choose Apple menu > System Preferences and click Accounts.
Click Change Password.
fidentify checks all the files from a directory with the same signatures than photorec. Its usefull to check if PhotoRec
is able to recover some file extensions/some file formats. Run cmd, Windows Command Prompt. cd is the command
to change directory.
cd testdisk-7.0
fidentify_win.exe d:\directory
Start a terminal, go in testdisk directory and use fidentify to check if the files present in a directory are recognized.
This identification is identical in PhotoRec.
cd testdisk-7.0
./fidentify_static /home/user/
Repairing filesystem
Repairing a filesystem may be a risky business as sometimes the problem is fixed by removing all invalid files. So if
you have access to some of your files but not all, its recommanded to backup what its possible to access before trying
to repair the filesystem.
Windows can read and write files from FAT, exFAT and NTFS filesystem. The chkdsk command is used to check and
repair filesystems. Run cmd (Right-click Run As Administrator)
chkdsk /f d:
Linux can read and write from a large variety of filesystems. The fsck generic command is used to run a filesystem
check. To check and repair automaticaly the filesystem on /dev/sda, run as root
fsck -y /dev/sda1
fsck starts a filesystem specific command, in exemple for ext4, it run fsck.ext4. If you need a fine grained repair, you
should read the man page of the command related to the filesystem you want to repair, ie. man fsck.ext4.
You may have to repeat the fsck command several times until no remaining error is reported.
If you get Invalid b-tree node size, you can try
19
TestDisk Documentation, Release 7.1
7.4 Repairing FAT32, exFAT and NTFS boot sector using TestDisk
The boot sector is a sector containing information required to access any files from a FAT, exFAT or NTFS filesystem.
FAT32 and NTFS filesystems have a main boot sector and a backup. If the main boot sector is damaged, the filesystem
is listed as raw or unreadable. TestDisk is able to use the backup boot sector to repair the main boot sector:
start testdisk
select the device containing the partition (avoid drive letter like D:)
confirm the partition table type
go in the Advanced menu
select the partition
choose Boot
If the boot sector is damaged, Boot sector: Bad will be shown. If the backup is ok, Backup boot sector: Ok will also
be listed.
choose BackupBS
confirm
Quit
restart the computer
The first sector of a FAT filesystem is named boot sector. It contains the main filesystem properties and some small
code necessary only to start the computer from this partition. If the boot sector is damaged, its impossible to access
your data. Windows chkdsk or Linux fsck can not repair a filesystem without a valid boot sector, they return error
message like Chkdsk is not available for RAW drives. Fortunately TestDisk can find all the parameters that need to be
recorded in the boot sector and rewrite this sector, so further repair operations or normal access can be conducted.
start testdisk
select the device containing the partition (avoid drive letter like D:)
confirm the partition table type
go in the Advanced menu
select the FAT partition
choose Boot
select RebuildBS
choose List
If testdisk is able to list your files, choose
quit the file listing
choose Write
confirm
Quit
restart the computer
The first sector of a NTFS filesystem is named boot sector. It contains the main filesystem properties and some small
code necessary only to start the computer from this partition. If the boot sector is damaged, its impossible to access
your data. Windows chkdsk or Linux fsck can not repair a filesystem without a valid boot sector, they return error
message like Chkdsk is not available for RAW drives. Fortunately TestDisk can find all the parameters that need to be
recorded in the boot sector and rewrite this sector, so further repair operations or normal access can be conducted.
start testdisk
select the device containing the partition (avoid drive letter like D:)
confirm the partition table type
go in the Advanced menu
select the NTFS partition
choose Boot
select RebuildBS
choose List
If testdisk is able to list your files, choose
quit the file listing
choose Write
confirm
Quit
1024 bytes after the beginning of the ext2/3/4 filesystem sits the superblock. It contains the main filesystem properties.
With a damaged main superblock, its not possible to mount and access the files normally. Fortunately copies are the
main superblock are spread over the filesystem. To be precise, they are not exact copy of the main superblock, each
copy contains its own location to prevent confusion between copies and the original. TestDisk can search for alternate
superblocks.
start testdisk
select the device containing the partition
confirm the partition table type
go in the Advanced menu
select the Linux partition
choose SuperBlock
TestDisk 7.1-WIP, Data Recovery Utility, August 2016
Christophe GRENIER <grenier@cgsecurity.org>
http://www.cgsecurity.org
>[ Quit ]
Return to Advanced menu
If superblock 0 is listed, it means the main superblock is correct. If its damaged, this line will be missing, use next
superblock and block size information to run fsck.
The volume header is locate 1024 bytes after the beginning of the HFS/HFS+ filesystem. If it is damaged, it is not
possible to access files normaly. TestDisk is able to use the backup volume header to repair the main volume header:
start testdisk
select the device containing the partition
confirm the partition table type
go in the Advanced menu
select the partition
choose SuperBlock
If the main superblock is damaged, Volume header: Bad will be shown. If the backup is ok, Backup volume header:
HFS+ Ok (or HFS Ok) will also be listed. In this case,
choose BackupBS
confirm
Quit
restart the computer
When a file is deleted, the data remains on the disk. Unless new data has overwritten your lost file, TestDisk can
usually recover it. Its possible for
FAT12/16/32
exFAT
NTFS
ext2
For other filesystems or if seeked lost file are still missing, give PhotoRec a try. PhotoRec is a signature based file
recovery utility and may be able to recover your data where other methods failed.
Do not further use the media (HDD, USB key, ...) on which the data stored have been deleted until data recovery
process is completed.
It is highly recommended that TestDisk or PhotoRec recovers files on another destination media, at minimum
on another filesystem.
For maximum security, TestDisk doesnt try to unerase files but lets you copy the deleted files onto another partition
or disk. Remember, you must avoid writing anything on the filesystem that was holding the data. If you do, deleted
files may be overwritten by new ones.
FAT is mainly used on memory cards from digital cameras and on USB keys. When a file is deleted, the filename
is marked as deleted and the data area as unallocated/free, but TestDisk can read the deleted directory entry and find
where the file began. If the data area hasnt been overwritten by a new file, the file is recoverable.
exFAT can be found on large memory card, large USB keys and hard disk.
ext2 is a Linux filesystem. It has been superceeded by ext3 and ext4, so its not found often now. with ext3 and ext4,
its possible to find the names of the deleted files but the location of the deleted data isnt available anymore, so even
if ext3/ext4 is similar to ext2, its not possible to recover lost files using TestDisk.
23
TestDisk Documentation, Release 7.1
Choose Create unless you have a reason to append data to the log or if you execute TestDisk from read only
media and cant create it elsewhere.
Press Enter to proceed.
All hard drives should be detected and listed with the correct size by TestDisk.
Use up/down arrow keys to select your hard drive with the lost partition/s.
Press Enter to Proceed.
Macosx If available, use raw device /dev/rdisk* instead of /dev/disk* for faster data transfer.
Select Advanced
Select the partition that was holding the lost files and choose Undelete
Navigate to the folder where your files were. Deleted files and directories are displayed in red.
To undelete a file, select the file to recover and press c to copy the file.
To recover a deleted directory, select the directory and press c to undelete the directory and its content.
Choose Create unless you have a reason to append data to the log or if you execute TestDisk from read only
media and cant create it elsewhere.
Press Enter to proceed.
All hard drives should be detected and listed with the correct size by TestDisk.
Use up/down arrow keys to select your hard drive with the lost partition/s.
Press Enter to Proceed.
Macosx If available, use raw device /dev/rdisk* instead of /dev/disk* for faster data transfer.
Select Advanced
Select the partition that was holding the lost files and choose Undelete
TestDisk scans MFT entries for deleted files. A list of NTFS deleted files found by TestDisk is displayed
To recover a single file, highlight the file and press c (lowercase) to copy it.
To recover a several files, move the first file you want to recover, press : to select it, repeat the process for the
others files, press C (uppercase) to copy them
Its not visible in interface but its possible to filter the results, press f to add a filter. Several filters can be added. To
cancel all the filters, press r (reset).
When a partition is deleted or if the partition table is corrupted, the filesystems remain on the disk but their location is
unknown and no data can be accessed. TestDisk can search partitions and rewrite the partition table with the partitions
selected by the user.
Choose Create unless you have a reason to append data to the log or if you execute TestDisk from read only
media and cant create it elsewhere.
Press Enter to proceed.
Note: Windows users, if you have difficulties to find the testdisk.log file, consult https://support.microsoft.com/en-us/
kb/865219 on how to show file name extensions in Windows Explorer.
All hard drives should be detected and listed with the correct size by TestDisk.
Use up/down arrow keys to select your hard drive with the lost partition/s.
Press Enter to Proceed.
Note: Macosx - If available, use raw device /dev/rdisk* instead of /dev/disk* for faster data transfer.
27
TestDisk Documentation, Release 7.1
Warning: Windows - Do not select C:, D: or another drive letter. Its useless to search partitions inside a partition.
Select Analyse
Confirm with the Enter key
TestDisk will list the current partition table.
If a partition is damaged or a partition entry corrupted, the problem will be listed and the partition listed twice. By
example, if you see Invalid NTFS or exFAT boot on a partition (partition size is ok, the partition doesnt overlap
another one...) you want to access, its better to fix this problem ( TestDisk: Repairing NTFS boot sector) before
searching other partitions.
Confirm at Quick Search to proceed
TestDisk displays the first results in real time. If necessary, you can choose Stop to abort the quick search. TestDisk
lists all partitions it has found. To list the files of a FAT, exFAT, NTFS, ext2/3/4 filesystem, highlight this partition and
press P. Press Q to return to the partition list.
If a partition is still missing, choose [Deeper Search]. It can take a few hours, so you need to be certain that your
computer will not sleep (Power management feature...)
Partitions listed as D(eleted) will not be recovered if you let them listed as deleted. Use the arrow keys to switch
the partitions you want to recover (check the partition size, list the file contents...) from D(eleted) to *(bootable),
P(rimary) or L(ogical). Only one partition can be listed as *(bootable). It is not a problem if a partition is marked as
bootable on a disk you will not start from (ie. an external disk) but there MUST be a bootable partition on a disk you
want to start your computer from.
Once all the partitions you want to keep and all the partitions you want to recover are properly marked as non deleted,
continue on next screen. Review the partitions list. If all partitions are listed and only in this case, confirm at Write
with Enter, y and OK. Now, the partitions are registered in the partition table.
If a FAT32 or an NTFS partition was found using its backup boot sector, TestDisk will let you rewrite the main boot
sector with the content of the backup boot sector: to copy the backup of the boot sector over the boot sector, select
Backup BS, validate with Enter, use y to confirm.
Restart your computer.
Check that
all partitions are listed in the partition table
a partition with your computer os is listed as *(bootable)
you can list the files from the bootable partition
If your OS doesnt boot, you can reinstall the system files with sys c:.
fixmbr \Device\HardDisk0
10.4 Linux/FreeBSD
31
TestDisk Documentation, Release 7.1
Lilo: /etc/lilo.conf
Grub: /boot/grub/grub.conf
Grub2: /etc/grub2-efi.cfg
Reinstall the multiboot in the Master Boot Record.
lilo
grub-install device
grub2-install device
PhotoRec doesnt recover the original filenames or the file structure but it can recover lost files even from corrupted
filesystem. PhotoRec is a signature based file recovery utility (a file carver) and may be able to recover your data
where other methods failed.
Remember, you must avoid writing anything on the filesystem that was holding the data. If you do, deleted files may
be overwritten by new ones.
Available media are listed. Use up/down arrow keys to select the disk that holds the lost files.
Use up/down arrow keys to select your hard drive with the lost partition/s.
Press Enter to Proceed.
Hint for Mac OS X: If available, use raw device /dev/rdisk* instead of /dev/disk* for faster data transfer.
Choose
Search after selecting the partition that holds the lost files to start the recovery,
Options to modify the options,
File Opt to modify the list of file types recovered by PhotoRec.
33
TestDisk Documentation, Release 7.1
Paranoid By default, recovered files are verified and invalid files rejected. Enable bruteforce if you want
to recover more fragmented JPEG files, note it is a very CPU intensive operation.
The expert mode option allows the user to force the file system block size and the offset. Each filesystem
has his own block size (a multiple of the sector size) and offset (0 for NTFS, exFAT, ext2/3/4), these value are
fixed when the filesystem has been created/formated. When working on the whole disk (ie. original partitions
are lost) or a reformated partition, if PhotoRec has found very few files, you may want to try the minimal value
that PhotoRec let you select (its the sector size) for the block size (0 will be used for the offset).
Enable Keep corrupted files to keep files even if they are invalid in the hope that data may still be
salvaged from an invalid file using other tools.
Enable Low memory if your system does not have enough memory and crashes during recovery. It may be
needed for large file systems that are heavily fragmented. Do not use this option unless absolutely necessary.
In FileOpts, enable or disable the recovery of certain file types, for example,
The whole list of file formats recovered by PhotoRec contains more than 300 file families representing more than 480
file extensions.
Once a partition has been selected and validated with Search, PhotoRec needs to know how the data blocks are
allocated. Unless it is an ext2/ext3/ext4 filesystem, choose Other.
Choose the directory where the recovered files should be written. Use the arrow keys (up, down, left, right) to navigate,
you can also use the enter key to enter into a directory.
Dos/Windows/Os2: To get the drive list (C:, D:, E:, etc.), use the arrow keys to select .., press the Enter key
- repeat until you can select the drive of your choice. Validate with Y es when you get the expected destination.
Linux: File system from external disk may be available in a /media, /mnt or /run/media sub-directory.
Mount your destination drive if necessary.
Mac OS X: Partitions from external disk are usually mounted in /Volumes.
Warning: Do not store the recovered files on the source filesystem. Otherwise lost data may be overwritten and
definitively lost.
When the recovery is complete, a summary is displayed. Note that if you interrupt the recovery, the next time PhotoRec
is restarted you will be asked to resume the recovery.
Thumbnails found inside pictures are saved as t*.jpg
If you have chosen to keep corrupted files/file fragments, their filenames will beginning by the letter b (roken).
Windows: You may have disabled your live antivirus protection during the recovery to speed up the process, but
its recommended to scan the recovered files for viruses before opening them - PhotoRec may have undeleted
an infected document or a trojan.
Hint: When looking for a specific file. Sort your recovered files by extension and/or date/time. PhotoRec uses
time information (metadata) when available in the file header to set the file modification time
Note: Windows - You may need to take ownership of the recup_dir.* folders: https://technet.microsoft.com/en-us/
library/Cc753659.aspx
Note: Mac OS X / Linux - To change the owner of the files, run sudo chown -R username recup_dir.*
By default, files are saved in directories named recup_dir.1, recup_dir.2... A new directory is created each new 500
files (The thumb files are not included in this count, nor the report.xml file). A filename begins by a letter followed by
PhotoRec recognizes numerous file formats. More than 480 file extensions (about 300 file families) are referenced. In
example, PhotoRec is able to identify the JPEG file format and it can recover lost files using this format whatever the
original file extension (jpg, jpeg, JPG...).
To check if a file format is already recognized, you can
consult the file formats.
submit a sample file to the PhotoRec online checker.
use fidentify on a file sample (See Running fidentify under Windows or Running fidentify under Linux or Mac
OS X)
In this case, the file type is listed as unknown, so PhotoRec cant recover this kind of file, at least for the moment. We
will check if its possible to add a custom signature for it.
If instead of unknown an extension is listed, PhotoRec knows this file format, it may recover the file with another
extension than the extension you are used to.
The file must contain one signature definition per line. A signature is composed of
extension name
offset of the signature
signature or magic value
The magic value can be composed of
a string, ie data. Special characters can be escaped like b, n, r, t, 0 or \.
hexadecimal data, ie 0x12, 0x1234, 0x123456... Note that 0x123456, 0x12 0x34 0x56 and 0x12, 0x34, 0x56 are
equivalents.
space or comma delimiters are ignored
By using an hexadecimal editor, you can see that the pfi file from our example begins by a distinctive string PhotoFiltre
Image at offset 0.
37
TestDisk Documentation, Release 7.1
or
pfi 0 0x50686f746f46696c74726520496d616765
You are now ready to use PhotoRec with your custom signature to recover your files. If a signature file is present,
PhotoRec will use it by default.
To control all aspects of the recovery (file content check, file size control, footer detection...), the best way to add a
signature, if you are developer, is to modify PhotoRec itself.
Commercial support is also available from the author grenier@cgsecurity.org.
Due to the way videos are recorded, all videos created by some digital camera (ie. Canon 5D Mark III) are fragmented
on disk. Data recovery software, photorec included, expect non fragmented files. If all videos (.mov / .mp4) recovered
by PhotoRec are unreadable, you are probably in this case.
When using PhotoRec, in FileOpts, enable
Under Mac OS X and Linux, start a terminal/console, use cd directory_name to go where your files are,
and run
Play the resulting test.mov file. If it works, you need to do the same with each couple of files.
This solution works only for videos written in two fragments. Videos from GoPro HD2, Hero3-Black Edition, HERO4
Silver are stored in more than 2 fragments, so special software solutions are needed to recover such videos.
41
TestDisk Documentation, Release 7.1
42 Chapter 13. Recovering lost videos from a memory card using PhotoRec
CHAPTER 14
Usually PhotoRec and QPhotorec recover a lot of files but without the original filenames, it may be hard to locate the
files you are interested in.
https://github.com/lconte/Copy-PhotoRecFilesbyExtension.ps1
Python comes pre-installed on Mac OS X and most Linux distribution. It can also be installed under Windows. The
Python program sort-PhotorecRecoveredFiles
sorts all files by file extensions into own folders.
limits the number of files/folder by creating subfolders if a certain numbers is exceeded. The file/folder number
can be customized.
For all jpgs: it put them into their own folders per year (EXIF-Data). Within a year, folders for every event
are created, e.g. all photos taken at one weekend or vacation are sorted into one folder.
exiftool can use meta-data from several popular file formats to rename files. All Linux distributions comes with
a package for exiftool (ie. perl-Image-ExifTool for RedHat, CentOS and Fedora) but otherwise it is available for
Windows, Linux and Mac OS X from http://www.sno.phy.queensu.ca/~phil/exiftool/
43
TestDisk Documentation, Release 7.1
The smartmontools package contains two utility programs (smartctl and smartd) to control and monitor storage systems
using the Self-Monitoring, Analysis and Reporting Technology System (SMART) built into most modern ATA/SATA,
SCSI/SAS and NVMe disks. In many cases, these utilities will provide advanced warning of disk degradation and
failure.
This package is installed by default on most Linux distribution. For Windows and Mac OS X, there are respectively a
setup.exe and an dmg available from https://sourceforge.net/projects/smartmontools/files/smartmontools/
Even if the SMART health status is PASSED, it doesnt mean the disk is ok. You should also check the Reallo-
cated_Sector_Ct attribute.
When the hard drive finds a read/write/verification error, it marks that sector as reallocated and transfers data to
a special reserved area (spare area). This process is also known as remapping, and reallocated sectors are called
remaps. The raw value normally represents a count of the bad sectors that have been found and remapped. Thus,
the higher the attribute value, the more sectors the drive has had to reallocate. This allows a drive with bad sectors to
continue operation; however, a drive which has had any reallocations at all is significantly more likely to fail in the
near future. While primarily used as a metric of the life expectancy of the drive, this number also affects performance.
45
TestDisk Documentation, Release 7.1
As the count of reallocated sectors increases, the read/write speed tends to become worse because the drive head is
forced to seek to the reserved area whenever a remap is accessed. If sequential access speed is critical, the remapped
sectors can be manually marked as bad blocks in the file system in order to prevent their use.
I recommand to replace a harddisk when the first bad sectors appears.
A bad sector is a sector on a computers disk drive that is either inaccessible or unwriteable due to permanent damage,
such as physical damage to the disk surface. Flash memory may also have bad sectors (even if technically there is
no sector in flash memory) due to permanent damage like failed flash memory transistors.
Instead of working directly on the damaged disk, its recommand to create a copy and to work on the clone. Two
possibilities: create a disk image (a file) or overwrite a new/empty disk.
ddrescue can be found for Linux or Mac OS X. If your computer is using another operating system, no problem, create
a Linux LiveUSB! (See Creating a live USB)
To install ddrescue:
Press Command+Space and type Terminal and press enter/return key.
Run in Terminal app:
Done! You can now use ddrescue. Use diskutil list to get information on all available disks and their
partitioning.
47
TestDisk Documentation, Release 7.1
Its the recommanded method for forensic purpose. You need enough space to store the file: if you want to create a
clone of a 1TB disk, you need at least 1TB free on a filesystem. Avoid FAT filesystem for the destination as they are
limited to 4GB file.
In the following example, an image named sdb.dd will be created from the second disk /dev/sdb.
The log file sdb.log can be used to restart the recovery. It can take a few hours to several days to clone a disk with
a lot of bad sectors.
The destination disk must be at least as big as the original one. Be careful, two disks of the same announced capacity
from different vendors or sometimes from different models of the same vendor can differ slightly in size (a few 100
MB).
Ie. WD10EZRZ and WD10EZEX are two models sold by Western Digital as 1TB model, in fact the first one is
1,000,000 MB, the second one 1,000,204 MB.
Before beginning, disconnect all disks, usb device, cd/dvd reader/writer not needed: there is less chance to overwrite
the wrong disk.
When a disk contains a lot of bad sectors, it may be safer to use ddrutility to limit the copy to allocated data block
from an NTFS partition.
In this example, the first NTFS partition begins at sector 32 and the sector size is 512 bytes.
Scripted run
TestDisk and PhotoRec can run automatically using their own built-in commands. A script file (such as .cmd or .bat
batch files under MS-DOS/Windows, or some shell under Linux) may also be helpful.
Syntax:
partition_i386
partition_gpt
partition_humax
partition_mac
partition_none
partition_sun
partition_xbox
49
TestDisk Documentation, Release 7.1
ask_type: the user will be asked for the partition type (new in 6.9)
If no partition type is specified or asked, TestDisk will detect it automatically.
advanced
analyze
delete
geometry
mbr_code
options
list
type
addpart: add a partition entry (not written to disk)
boot: for FAT12/FAT16, FAT32, exFAT and NTFS partition, go to the specific menu
copy: backup the partition to the file image.dd (new in 6.9)
list: list the content of the partition (new in 6.10)
list,recursive: list the content of the whole partition (new in 6.10)
list,recursive,fullpathname: list the content of the whole partition with the whole pathname (new in 6.11)
list,filecopy: list and copy all the files (new in 7.1)
superblock: search ext2/ext3 superblocks or go to HFS+ menu depending of the partition
undelete: go in the undelete menu (FAT12/16/32, NTFS, exFAT, ext2)
number: the partition number to select
Add partition
PC Intel
c,XX starting cylinder
h,XX starting head
s,XX starting sector
C,XX ending cylinder
H,XX ending head
S,XX ending sector
T,XX type
EFI GPT, Mac, XBoX
s,XX starting sector
s,XX ending sector
T,XX type
Humax, Sun
c,XX starting cylinder
C,XX ending cylinder
T,XX type
dump
list (new in 6.9)
list,recursive: list the contents of the whole partition (new in 6.10)
list,recursive,fullpathname: list the contents of the whole partition with the whole path name (new in 6.11)
rebuildbs
repairfat
initroot
dump
list (new in 6.9)
list,recursive: list the contents of the whole partition (new in 6.10)
list,recursive,fullpathname: list the contents of the whole partition with the whole path name (new in 6.11)
rebuildbs
repairfat
originalfat
backupfat
list
list,recursive: list the contents of the whole partition (new in 6.10)
dump
noconfirm,write
write
dump
originalexFAT
backupexFAT
rebuildbs
dump
list
list,recursive: list the contents of the whole partition (new in 6.10)
list,recursive,fullpathname: list the contents of the whole partition with the complete path name (new in 6.11)
originalntfs
backupntfs
repairmft
noconfirm,backupntfs
noconfirm,repairmft
allundelete (new in 7.1): list and recover all deleted files. WARNING: stores them in current local directory.
list
list,recursive: list the contents of the whole partition (new in 6.10)
list,recursive,fullpathname: list the contents of the whole partition with the complete path name (new in 6.11)
dump
noconfirm,write
write
dump
originalhfsp
backuphfsp
C,number of cylinders
H,number of heads
S,number of sectors
N,sector size
17.1.8 Options
dump
nodump
align
noalign
expert
noexpert
General syntax:
/debug: switch on debug mode
/log: switch on logging (a log file named photorec.log will be created/appended to in the current working
directory
/logname file.log: log will be written to file.log instead of photorec.log
/d recup_dir: specify directory to store the recovered files into. This should be on a device different from the
one you are recovering from. PhotoRec will add a numeric extension to the path specified, starting with .1 -
and increase this number as long as a directory with this name already exists.
/cmd: introduces the command section for scripted run
<device>: the device (or image file) to recover from (Hint: use single-quote if the image file contains spaces)
<command>: the command list (see below)
Recover from the second IDE drives i386 partition the user selects
photorec /debug /log /cmd /dev/hdb partition_i386,select,search
Recover from the first IDE drives i386 partition #5, which is using ext2/ext3/ext4
photorec /debug /log /cmd /dev/hda partition_i386,options,mode_ext2,5,search
Recover from a given disk image file named disk.dmp which only has a single ext4 partition (or a part of it) Restore
all file types known to PhotoRec to /mnt/recover/disk.
photorec /debug /log /d /mnt/recover/disk /cmd disk.dmp options,mode_ext2, \
fileopt,everything,enable,search
The same without debug and log - but recover only *.gif and *.jpg
photorec /d /mnt/recover/disk /cmd disk.dmp options,mode_ext2,fileopt,everything,
,disable, \
jpg,enable,gif,enable,search
Recover all files from freespace from each partition as detected by testdisk
PARENT=`pwd`
DEVICE=/dev/sda
testdisk -l $DEVICE | tee testdisk.log | \
egrep "[[:digit:]][[:space:]][P,E,L,D,*][[:space:]].+([[:space:]]+[[:digit:]]+){3}"
,| \
Below you find a list of available command options, grouped into categories. It is best to use them in the order they
are mentioned here. These options must be separated by a comma. Partition type selection and options from the main
menu can be used directly.
partition_i386
partition_gpt
partition_humax
partition_mac
partition_none
partition_sun
partition_xbox
ask_type: the user will be asked for the partition type
If no partition type is specified, it is auto-detected.
everything,enable: use the values by default (may be different than the saved values, new in 6.9)
everything,disable: empty the list of file formats to locate (new in 6.9)
jpg,enable: will search for jpg
jpg,disable: will not search for jpg
You can use the same syntax for all file formats.
To use anything from the options menu, you must specify the keyword options first.
expert
keep_corrupted_file_no (new in 6.10)
keep_corrupted_file
paranoid_no / paranoid / paranoid_bf (new in 6.10)
lowmem
mode_ext2
If you run TestDisk and PhotoRec, Windows User Account Control will ask Do you want the following program
from an unknown publisher to make changed to this computer ? (or something similar). As administrator rights
are unneeded for disk images, you may want to avoid this UAC prompt with the __COMPAT_LAYER environment
variable. Example:
set __COMPAT_LAYER=RunAsInvoker
photorec_win.exe /cmd image.dd search
To learn to use TestDisk and PhotoRec, various test cases are avaible to practice in safe conditions.
Download the small FAT filesystem image archive and extract all the files. This test image is a 6MB FAT16 file system
with six deleted files and two deleted directories. The files range from single cluster files to multiple fragments.
To undelete all files manually,
run testdisk 6-fat-undel.dd
Choose Proceed.
A non partitioned media is detected automatically, press Enter to confirm.
Choose Undelete.
All files and directories are deleted, they are listed in red.
Press a to select all files.
The selected files and directories are now listed in green and prefixed by * or < for the current highlighted file.
Press C (uppercase) to copy all selected files and directories.
Choose a destination to copy all the files: use the arrow keys (up, down, left, right) to navigate, you can also use
the enter key to enter into a directory.
Press C when the destination is correct.
All files are copied.
Press q to quit
Choose [Quit] until you have exited all menus
The usual filenames for a FAT filesystem are composed of 8 chars for the name and 3 for the extension. When a file
is deleted, the first caracter of the filename is overwritten. TestDisk represents the lost char by a underscore _ (ie.
_RAG1.DAT instead of FRAG1.DAT) If a long filename (> 8 caracters) is present, it will be use instead. A benefit is
that the whole filename can be displayed (ie. System Volume Information)
All files ar recovered successfully except the 3 fragmented files. The size of these 3 files is correct but the content is
wrong. When a file is deleted, the linked list formed by the cluster numbers used by the file are marked as free in the
FAT tables. TestDisk assumes there is no fragmentation but its not the case here.
57
TestDisk Documentation, Release 7.1
Download the small NTFS filesystem image archive and extract all the files. This test image is a 6MB NTFS file
system with eight deleted files, two deleted directories, and a deleted alternate data stream. The files range from
resident files, single cluster files, and multiple fragments. No data structures were modified in this process to thwart
recovery. They were created in Windows XP, deleted in XP, and imaged in Linux.
To undelete all files manually,
run testdisk 7-ntfs-undel.dd
Choose Proceed.
A non partitioned media is detected automatically, press Enter to confirm.
Choose Undelete.
TestDisk lists all lost files successfully. The alternate data stream is listed as ./mult1.dat:ADS, alternate streams are not
listed in Windows Explorer, and their size is not included in the files size. Malware has used alternate data streams
to hide code. As a result, malware scanners and other special tools now check for alternate data streams. Forensics
analyst should also search for them as they may be used to hide documents.
Press C (uppercase) to copy all selected files and directories.
Choose a destination to copy all the files: use the arrow keys (up, down, left, right) to navigate, you can also use
the enter key to enter into a directory.
Press C when the destination is correct.
All files are copied.
Press q to quit
Choose [Quit] until you have exited all menus
DFRWS 2006 Forensics Challenge is a data carving challenge. Its possible to use PhotoRec to recover most files:
run photorec dfrws-2006-challenge.raw
Choose Proceed
Go In Options menu
Set Paranoid : Yes (Brute force enabled)
Set Keep corrupted files : Yes
Use Quit to return to the main menu
Chose Search
Confirm the filesystem type [ Other ]
Use C key to confirm the destination of the recovered files (current directory)
Wait for the recovery to finish
Quit
All these steps can also be automated in a single command:
58 Chapter 18. TestDisk and PhotoRec in various digital forensics test cases
TestDisk Documentation, Release 7.1
The file to analyze contained 32 files (not including the embedded files, such as pictures in Word documents or the
files inside of ZIP files). The 32 files were used to create 22 different scenarios. Each scenario was designed to test a
specific situation that might occur in a real file system.
Category 1 focused on HTML files with ASCII text:
1a) One HTML non-fragmented X
1b) One HTML fragmented with a JPEG in between
1c) One HTML fragmented with Unicode text in between
1d) Two HTML files that are intertwined
PhotoRec doesnt recover fragmented HTML correctly.
Category 2 focused on Microsoft Office documents:
2a) One Word file, non-fragmented X
2b) One Word file, fragmented with 3 fragments and random data in between
2c) One Excel file fragmented with random data in between
2d) One Word file fragmented with a JPEG in between X
2e) One Word file fragmented with text in between
Category 3 focused on JPEG files:
3a) One JPEG non-fragmented X
3b) One JPEG non-fragmented, larger than a typical default max file size X
3c) One JPEG non-fragmented, but sector before it has 0xffd8 in the first two bytes X
3d) One JPEG fragmented with text in between X
3e) One JPEG fragmented with a Word document in between X
3f) One JPEG fragmented with random data in between X
3g) One JPEG fragmented with a JPEG in between X
3h) Two JPEGs that are intertwined
3i) One JPEG non-fragmented that is REALLY big X
3j) One JPEG fragmented with singe sector in between that starts with 0xffd9 X
PhotoRec has good results in the JPEG category.
Category 4 focused on ZIP files:
4a) One ZIP file, non-fragmented X
4b) One ZIP file fragmented with text in between X
4c) One ZIP file fragmented with random data in between
60 Chapter 18. TestDisk and PhotoRec in various digital forensics test cases
TestDisk Documentation, Release 7.1