Introduction
During a pentest I encountered an MSSQL backup (.bak) and I wanted to extract the password hash for the master database. This turned out to be harder than expected so I'd like to share my findings in this blog.
Steps
- Install MSSQL server
- Run sqlcmd
- Get database names from backup file
RESTORE FILELISTONLY FROM DISK = 'C:\Foo\Full\Path\To\Backup.bak' GO
RESTORE DATABASE master_copy FROM DISK = 'C:\Foo\Full\Path\To\Backup.bak' WITH MOVE 'master' TO 'C:\Foo\master.mdf', MOVE 'master_log' TO 'C:\Foo\master_log.ldf' GO
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
Import-Module .\Get-MDFHashes.ps1
Get-MDFHashes | Out-GridView
Background
The bak file for MSSQL is an archive that contains the mdf and the ldf file for some database. You can use MSSQL with SSMS to simply import a normal database backup by right-clicking on Databases -> Restore backup. However, the master database is a special database that doesn't like to be restored this way.
After some messing around I asked on DBA Stackexchange about my issue. It turned out that you can just change the name of the database - using sqlcmd - and it will work fine.
Next, the awesome script from XPN can extract the password hashes for you. Note that you need to shut down your MSSQL server because the server is using the files. Also, you can use SSMS to simply browse the copy of the master database.