Testdisk Documentation: Release 7.1
Testdisk Documentation: Release 7.1
Testdisk Documentation: Release 7.1
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 macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
7.4 Repairing FAT32, exFAT and NTFS boot sector using TestDisk . . . . . . . . . . . . . . . . . . . . 20
7.5 TestDisk: Repairing FAT boot sector . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
i
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
7.9 Repairing BitLocker volume . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
ii
16.1 ddrescue on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
16.2 ddrescue on macOS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
16.3 DDRescue: disk to file image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
16.4 DDRescue: disk to disk copy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
16.5 ddrutility: restricting ddrescue to NTFS allocated data block . . . . . . . . . . . . . . . . . . . . . . 50
17 Scripted run 51
17.1 Automating recovery using TestDisk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
17.2 Automating recovery using PhotoRec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
17.3 Windows UAC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
iii
iv
CHAPTER
ONE
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.
This documentation can be found online at https://github.com/cgsecurity/testdisk_documentation. Anyone can con-
tribute to TestDisk & PhotoRec documentation. We especially welcome the contributions of beginners. In fact, begin-
ners have a distinct advantage over the experts, because they can more easily spot the places where documentation is
lacking. If it’s only to fix a spelling or grammar error, your contribution is also welcome!
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)
• macOS / Mac OS X (PowerPC or Intel) / OS X
• Marvell 88F628x Linux
TestDisk & PhotoRec can also be compiled for other platforms, 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
• Humax
• PC/Intel Partition Table (master boot record)
• Sun Solaris slice
• Xbox fixed partitioning scheme
It also handles non-partitioned media.
TestDisk can
• recover deleted partition
• rebuild partition table
• rewrite the Master boot record (MBR)
TestDisk does a quick check of the disk’s structure and compares it with the partition table for entry errors. Next, it
searches for lost partitions of these file systems:
• Be File System (BeOS)
• BSD disklabel (FreeBSD/OpenBSD/NetBSD)
• Cramfs, Compressed File System
• DOS/Windows FAT12, FAT16, and FAT32
• Windows exFAT
• HFS, HFS+ and HFSX, Hierarchical File System
• IBM Journaled File System 2 (JFS2)
• Linux ext2, ext3 and ext4
• Linux RAID
– RAID 1: mirroring
– RAID 4: striped array with parity device
– RAID 5: striped array with distributed parity information
– RAID 6: striped array with distributed dual redundancy information
• Linux Swap (versions 1 and 2)
• LVM and LVM2, Linux Logical Volume Manager
• Novell Storage Services (NSS)
• Windows New Technology File System (NTFS)
• ReiserFS 3.5, 3.6 and 4
• Sun Solaris i386 disklabel
• Unix File System UFS and UFS2 (Sun/BSD/. . . )
• XFS, SGI’s Journaled File System
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 wasn’t fragmented and the clusters haven’t 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 doesn’t 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). It’s 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 doesn’t
recover the original filenames but it can recover delete files even from corrupted filesystem.
4 Chapter 1. Presentation
CHAPTER
TWO
INSTALLATION
2.1.1 CentOS
As root,
2.1.2 Fedora
As root,
Copr is an automatic build system. It provide the latest development version. As root,
As root,
Using the development version (WIP=Work In Progress) is usually recommended as fixes are not backported. The
WIP archive may be modified several times per week but keep the same name. If this version doesn’t start, you can
always use the stable version and warn the developer of the problem with the beta version.
5
TestDisk Documentation, Release 7.1
List your files, a directory named testdisk-7.2-WIP should has been created.
6 Chapter 2. Installation
CHAPTER
THREE
3.1.1 Linux
7
TestDisk Documentation, Release 7.1
3.1.2 macOS
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 https://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, it’s possible to generate binaries for Windows. Two cross-compiler toolchains are available under Fedora
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/
– https://copr.fedorainfracloud.org/coprs/yselkowitz/cygwin/
• 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
Once you have been able to build a “normal” version, you can try to build a static version.
make static
A static build is a compiled version of a program which has been statically linked against libraries. A static binary
does not depend on library availability of the computer it’s running on, usually you can copy this binary on another
computer and it will work. It is still architecture specific (i.e. CPU) and may be kernel (OS version) dependent, so
static binaries may be used for portable applications. For the build to be successful, you may have to install static
version of libraries.
3.3. Compilation 9
TestDisk Documentation, Release 7.1
FOUR
If you need to repair a computer that isn’t booting correctly, you can move its harddisk to a working computer or start
your computer from an USB key or a DVD. It’s 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
it’s 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 doesn’t support status=progress option and you’ll need to remove it (and you won’t see writing
progress).
Warning: The dd command is very powerful and can destroy any existing data on the specified device. Make
absolutely sure of the device name to write to and do not mistype the device name when using dd!
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 you’re really, really sure you don’t 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 as destination.
Hereafter, we’ll 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 filesys-
tem location being appended to the command. Now complete the command with of=/dev/rdisk2 bs=1m,
but don’t 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 you’re really, really sure you don’t 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
FIVE
13
TestDisk Documentation, Release 7.1
SIX
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.1
sudo ./testdisk_static
15
TestDisk Documentation, Release 7.1
cd testdisk-7.1
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 isn’t 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 (i.e. testdisk-7.1/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.
Terminal doesn’t show the password as you type. If you enter the wrong password or a blank password, the command
isn’t executed and Terminal asks you to try again.
Fidentify checks all the files from a directory with the same signatures than PhotoRec. It’s useful 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.1
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.1
./fidentify_static /home/user/
SEVEN
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, it’s recommended to backup what it’s 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 automatically the filesystem on /dev/sda, run as root
fsck -y /dev/sda1
fsck starts a filesystem specific command, in example 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, i.e. man fsck.ext4. If
some files or directories are missing, remember to check the lost+found directory at the root of this filesystem.
ntfsfix can be used to repair NTFS filesystem followed by Windows chkdsk . Note that it resets the NTFS journal
file, so it should be used only if Windows failed to repair the filesystem.
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, it’s 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, it’s 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, it’s not possible to mount and access the files normally. Fortunately copies of 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
>[ Quit ]
Return to Advanced menu
If superblock 0 is listed, it means the main superblock is correct. If it’s 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 normally. 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
Repair-bde can reconstruct critical parts of the drive and salvage recoverable data as long as a valid recovery
password or recovery key is used to decrypt the data. See https://technet.microsoft.com/en-us/library/ff829851(v=ws.
11).aspx
EIGHT
When a file is deleted, the data remains on the disk. Unless new data has overwritten your lost file, TestDisk can
usually recover it. It’s possible for
• FAT12/16/32
• exFAT
• NTFS
• ext2
For other filesystems or if sought-after lost files 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 doesn’t 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 hasn’t 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 superseded by ext3 and ext4, so it’s not found often now. With ext3 and ext4,
it’s possible to find the names of the deleted files but the location of the deleted data isn’t available anymore, so even
if ext3/ext4 is similar to ext2, it’s not possible to recover lost files using TestDisk.
25
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 can’t 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.
macOS 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 can’t 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.
macOS 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
It’s not visible in interface but it’s possible to filter the results, press ‘f’ to add a filter. Several filters can be added. To
cancel all the filters, press ‘r’ (reset).
NINE
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 can’t 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: macOS - If available, use raw device /dev/rdisk* instead of /dev/disk* for faster data transfer.
Warning: Windows - Do not select C:, D: or another drive letter. It’s useless to search partitions inside a
partition.
29
TestDisk Documentation, Release 7.1
• 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 doesn’t overlap
another one. . . ) you want to access, it’s 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 (e.g. 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.
TEN
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 doesn’t boot, you can reinstall the system files with sys c:.
fixmbr \Device\HardDisk0
33
TestDisk Documentation, Release 7.1
10.4 Linux/FreeBSD
lilo
grub-install device
grub2-install device
ELEVEN
PhotoRec doesn’t 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 macOS: 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.
35
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, it’s started after the normal
scan process.
• 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/formatted. When working on the whole disk (i.e. original partitions
are lost) or a reformatted partition, if PhotoRec has found very few files, you may want to try the minimal value
that PhotoRec let you select (it’s 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.
• macOS: 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
it’s 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: macOS / 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
TWELVE
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
macOS)
In this case, the file type is listed as unknown, so PhotoRec can’t recover this kind of file, at least for the moment. We
will check if it’s 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, e.g. “data”. Special characters can be escaped like “b”, “n”, “r”, “t”, “0” or “".
• hexadecimal data, e.g. 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 PhotoFil-
tre Image at offset 0.
39
TestDisk Documentation, Release 7.1
or
pfi 0 0x50686f746f46696c74726520496d616765
Warning: Be careful, hexdump displays non-printable chars as dots. The following signature is wrong:
pfi 0 "PhotoFiltre Image."
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 [email protected].
THIRTEEN
Due to the way videos are recorded, all videos created by some digital camera (i.e. Canon 5D Mark III, Panasonic
DMC-TZ80’s photos in burst mode) are fragmented on the memory card. 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. Note this chapter
does not concern copies or downloaded files, only files written by some digital camera, not by your computer.
When using PhotoRec, in FileOpts, enable
If you do not have the permissions to write to the directory, before using the type command, take ownership of the
directories or run cmd using right click run as administrator.
• Under macOS and Linux, start a terminal/console, use cd directory_name to go where your files are, and
run
If you do not have the permissions to write to the directory, before using the cat command, change the files and
directories ownership using chown -R username:groupname recup_dir.*
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. This
chapter does not concern copies or downloaded files, only files written by some digital camera, not by your computer.
Note: Panasonic DMC-TZ80’s photos in burst mode are saved as a movie. To extract the photos from this movie,
macOS users can import the movie into Photos and save each frame as a single still photo.
43
TestDisk Documentation, Release 7.1
44 Chapter 13. Recovering lost videos from a memory card using PhotoRec
CHAPTER
FOURTEEN
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 preinstalled on macOS and most Linux distributions. 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 (file:perl-Image-ExifTool for Red Hat, CentOS and Fedora) but otherwise it is available for
Windows, Linux and macOS from https://www.sno.phy.queensu.ca/~phil/exiftool/
exiftool -r -ext avi '-FileName<DateTimeOriginal' -d avi/%Y%m%d_%H%M%S%%-c.%%e avi/
exiftool -r -ext doc '-FileName<CreateDate' -d doc/%Y%m/%%f.%%e doc/
exiftool -r -ext jpg '-FileName<DateTimeOriginal' -d sorted_jpg/%Y%m%d/%Y%m%d_%H%M%S%
,→%-c.%%e jpg/
45
TestDisk Documentation, Release 7.1
/usr/share/fslint/fslint/findup -d jpg/
FIFTEEN
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 degra-
dation and failure.
This package is installed by default on most Linux distribution. For Windows and macOS, 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 doesn’t 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.
As the count of reallocated sectors increases, the read/write speed tends to become worse because the drive head is
47
TestDisk Documentation, Release 7.1
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 recommend to replace a harddisk when the first bad sectors appears.
SIXTEEN
A bad sector is a sector on a computer’s disk drive that is either inaccessible or unwritable 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, it’s recommended 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 macOS. If your computer is using another operating system, no problem, create a
Linux Live USB! (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.
49
TestDisk Documentation, Release 7.1
It’s the recommended 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 over-
write 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.
SEVENTEEN
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
• ask_type: the user will be asked for the partition type (new in 6.9)
51
TestDisk Documentation, Release 7.1
• 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
EIGHTEEN
To learn to use TestDisk and PhotoRec, various test cases are available 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 character of the filename is overwritten. TestDisk represents the lost char by a underscore _ (e.g.
_RAG1.DAT instead of FRAG1.DAT) If a long filename (> 8 characters) is present, it will be use instead. A benefit
is that the whole filename can be displayed (e.g. System Volume Information)
All files are 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 it’s not the case here.
59
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 file’s 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. It’s 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:
60 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 doesn’t 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
62 Chapter 18. TestDisk and PhotoRec in various digital forensics test cases
TestDisk Documentation, Release 7.1