Skip to content

Ransomware-Encrypted SQL Server Database Recovery: MDF and BAK Files

By Srinivasan Krishnappa · Ransomware & database recovery engineer · 656+ ransomware recoveries
13 min read
Need this solved now? We have already solved 656+ ransomware and data recovery cases. Call +91 74187 05822, WhatsApp us or email info [at] asittech.com. Free assessment; no data, no charge.

When ransomware hits a SQL Server, the instinct is to assume the database is gone unless you pay for a key or restore a backup. Often neither is true. Large database files are frequently only partially encrypted, and where that is the case, a significant amount of data can be recovered directly from the file — without the attacker's key, and without a usable backup. Whether that is possible in a given case depends on how the specific ransomware behaved and which parts of the file it touched.

Why big database files are often only partly encrypted

Encrypting a multi-terabyte file completely is slow, and speed is what ransomware operators optimise for — the faster they finish, the less chance of being interrupted. To get around this, many modern families use intermittent or partial encryption: they encrypt the start of a file and then only periodic chunks, or just the header region, leaving large stretches of the original data untouched. For a small document this still destroys the file. For a 1.4 TB SQL Server MDF, it can leave the majority of the data pages intact and readable.

How SQL Server stores data, and why that helps

A SQL Server database is organised into 8 KB pages. Each page carries a header describing what it holds, and table rows live inside data pages that are indexed by the database's internal system structures. Because the data is spread across millions of these pages, encryption that hits only the start of the file or scattered regions leaves most pages structurally intact.

Recovery works by reading those surviving pages directly — identifying which table each page belongs to from its header, and reassembling rows from the raw page data — rather than relying on the database to open normally, which it will not.

What gets in the way

Two things decide how much comes back: which regions the ransomware encrypted, and how completely. If the encrypted area lands on critical structures — the boot page, the system catalog that maps internal IDs to table names, or the allocation maps — those have to be rebuilt or inferred before the surviving data pages can be put back into named tables.

If the family used full encryption rather than partial, or the file is small enough that 'partial' still covers all of it, there may be little or nothing to recover without the key. This is why the same ransomware can produce very different outcomes on a 2 GB database and a 2 TB one.

This is recovery, not decryption

It is worth being clear about what this is. Recovering data from the unencrypted remainder of a file is not the same as decrypting it — the encrypted pages stay encrypted and unreadable. What is being recovered is the data the ransomware never reached, reconstructed into usable tables. For large production databases that is often most of the data; for heavily or fully encrypted files it can be very little. No honest assessment can promise a figure before examining the actual file.

How to recover an MSSQL database after a ransomware attack, step by step

1. Stop, and do not make it worse. Stop the SQL Server service so nothing else writes to the files, and disconnect the server from the network. Do not delete or rename the encrypted .mdf, .ndf, .ldf or .bak files, do not run DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS, do not try to attach the encrypted files again and again, do not reinstall Windows or SQL Server, and do not run decryptors from unknown sites against the only copy.

2. Copy the encrypted database files. Copy every MDF, NDF and LDF file and all .bak, .trn and .dif backups to a separate disk, together with the ransom note. Work only on the copies from now on. If the server is a virtual machine, a copy of the whole VMDK or VHDX disk is even better.

3. Look for a backup the ransomware did not reach: backups on another disk, a NAS or USB drive, Veeam or other backup software, cloud storage, an Always On or log-shipping secondary, a reporting copy, or an old copy kept by a developer or the software vendor. Test any unencrypted .bak with RESTORE VERIFYONLY and a restore to a different server or database name, never over the original.

4. Find out how the files were encrypted. If no clean backup exists, the question is how much of each file was encrypted, not which ransomware it was. Every 8 KB page the ransomware did not touch can be read and checked on its own, even when SQL Server refuses to attach the file.

5. Recover the tables from the MDF or BAK. The system tables are reconstructed so the table definitions are known again, then the rows of each table are read from its intact pages and written into a new, clean database. An encrypted .bak is recovered the same way, because it contains the same pages.

6. Check the recovered database before going live: attach it to a clean SQL Server instance, run DBCC CHECKDB, compare row counts with what the application expects, and let the people who know the data check recent records. Rebuild the server itself from clean media, because the attacker's way in is usually still open on the old one.

7. Report the incident. In India, CERT-In's directions require ransomware incidents to be reported within six hours of noticing them, and the Digital Personal Data Protection Act applies if personal data is affected. Other countries have their own rules. This is general information, not legal advice.

Should you pay the ransom?

That is your decision, but find out what can be recovered without paying first. Paying does not guarantee a working decryptor, and decryptors supplied by attackers often fail on large database files. In many SQL Server cases most of the data can be restored from the encrypted files themselves, without paying the ransom.

Real case: LockBit-encrypted SQL Server backups, 98.8% of rows recovered

A company's SQL Server 2019 ERP database backups (.bak, 10.5 GB) were encrypted by LockBit and renamed with a random extension. The backup header was destroyed, so SQL Server could not restore them, and the backup from the next day did not help: LockBit had encrypted the same places in both files.

AS SQL Recovery & Repair Studio opened the encrypted backup directly, found every surviving 8 KB page image by its checksum, and rebuilt the tables. The system catalog was partly lost, so a sister company's database with the same structure was used as the reference for table and column definitions (never for data). Result: 354 tables and 4,703,039 of 4,759,135 rows (98.8%), with no table mislabelled. The client also still had a healthy copy of the database, so every recovered row could be checked against it.

The Gentlemen: a 171 GB backup, 91% untouched

The Gentlemen ransomware has a fast mode for large files. On a 171 GB SQL Server backup we measured it encrypting three long stretches, at the start, in the middle and at the end, about 9% of the file in total; the other 91% was plain. Our 9.8 TB case was the same family: seven production databases of 1.4 TB each, all recovered from the encrypted files and repaired to a working state.

Only the first 256 KB of a 188 GB database encrypted

Some ransomware encrypts only the beginning of each file. On a 188 GB SQL Server MDF we measured exactly the first 256 KB encrypted, plus a 204-byte block added at the end. SQL Server refused the database because the boot page and the start of the system catalog were gone, yet more than 99.9% of the pages were untouched. In cases like this the tables are read directly from the surviving pages, and the header can be rebuilt so that SQL Server attaches the database again.

Ransomware families that encrypt SQL Server databases

What matters is how a family encrypts a database file, not its name, and that is measured on the file itself. Our guides to individual families:

A real case: 96 million rows from a 27 GB encrypted database

A 27.3 GB SQL Server 2008 R2 database had been encrypted and renamed with the .M142HIMARS extension. The ransomware had encrypted every other 8 KB page in the first and last 2.5 MB of the file and appended a 1,544-byte block — a small part of the file, but the part holding the boot page, the allocation maps and the start of the system catalog, so SQL Server refused to attach it.

Working from the file directly, 96.0 million rows were recovered from 222 tables, with no row failing its checks. 179 tables were complete, their row counts exactly matching the database's own catalog; partial tables missed only the rows on encrypted pages. A 2018 copy of the same database supplied the structure (never the data) for 10 tables whose definitions had been lost. About 20 small tables could not be linked automatically.

Questions and answers

Can a ransomware-encrypted SQL Server database be recovered without paying?

Often, yes. Large MDF, NDF and BAK files are usually only partly encrypted, because encrypting hundreds of gigabytes takes too long. Every 8 KB page the ransomware did not touch can be read, and the tables are rebuilt into a new database. Files fully encrypted with a strong cipher cannot be recovered without the key.

How do I restore a SQL database without paying the ransom?

First look for a backup the ransomware did not reach and test it on a different server. If none exists, copy the encrypted MDF, NDF, LDF and BAK files and have them measured: the intact pages of a partly encrypted database can be recovered into a new, working database without the attacker's key.

Can an encrypted SQL Server .bak backup be recovered?

Yes, when it is partly encrypted. A .bak file contains the same 8 KB pages as the database, so the surviving pages can be found and the tables rebuilt. In one LockBit case, 98.8% of the rows were recovered from encrypted backups.

Should I run DBCC CHECKDB REPAIR_ALLOW_DATA_LOSS on an encrypted database?

No. Repair cannot fix encrypted pages and can delete data that is still recoverable. Stop SQL Server, copy the files and work only on the copies.

Ransomware data recovery →

Facing this problem? Contact us now.

We have already solved 656+ ransomware and data recovery cases: encrypted and corrupted SQL Server, Oracle and MySQL databases, Tally, Busy and Marg ERP data, Veeam backups, VMware and Hyper-V virtual machines, RAID and NAS. Send us the details and we tell you what we can recover.

  • ✓ Free assessment before you pay anything
  • ✓ No data, no charge
  • ✓ NDA on request

Email: info [at] asittech.com · Mon–Fri, 9:00–18:00 IST

Call 24/7Emergency