Ransomware-Encrypted MySQL Database Recovery: InnoDB .ibd Files, Dumps and Binary Logs
MySQL and MariaDB run behind a huge number of web shops, ERPs, hospital and school systems and custom business applications. When ransomware hits the server, the data directory is encrypted with everything else: MySQL will not start, and every .ibd, ibdata1 and log file has a new extension. Dump files and backups on the same server or a reachable share are often encrypted too.
Whether the data comes back without the key depends on how the files were encrypted and on how InnoDB stores them. This guide explains both, and also covers the other kind of MySQL ransom attack, where nothing is encrypted at all.
How MySQL stores a database on disk
Almost every modern MySQL database uses the InnoDB engine with one file per table (the default since MySQL 5.6): each table and its indexes live in a file named after the table, such as orders.ibd, inside a folder named after the database. Every .ibd file is made of 16 kB pages, and each page has a header with its own page number, the table's space id and a checksum.
Beside those are the system tablespace ibdata1 (undo data, and in MySQL 5.7 and older also the data dictionary), the redo log (ib_logfile0 and ib_logfile1, or the #innodb_redo folder in MySQL 8.0.30 and later) and, if enabled, the binary logs (binlog.000001 and so on). The table structure is kept in .frm files in MySQL 5.7 and MariaDB, and inside each .ibd file in MySQL 8.0.
Older tables may use MyISAM instead: a .MYD file with the rows one after another and a .MYI file with the indexes.
What ransomware usually destroys
Most ransomware families encrypt only the first part of each file, or stripes through it, because encrypting every byte of a large server takes too long. For a big .ibd file this is good news: the first megabytes are encrypted, but the remaining pages, often more than 99% of a large table, are untouched.
Small files suffer most. Tables smaller than the encrypted region are encrypted from end to end, and so are the .frm files, the start of ibdata1 and, in MySQL 8.0, the first pages of each .ibd where the table structure is stored. The typical result: large tables mostly intact, small tables gone, and MySQL unable to start because the dictionary and system files cannot be read.
If every byte of every file was encrypted with a strong cipher, nothing can be recovered from those files without the key. Measuring a few sample files first shows which case you are in.
How the tables are rebuilt
Because every 16 kB InnoDB page identifies itself, the intact pages of an .ibd file can be read directly, without starting MySQL and without ibdata1. The leaf pages of each table's primary index hold the complete rows; rows that were in an encrypted page are lost, the others are decoded.
To decode rows, the column layout of each table must be known. When the .frm files or the dictionary pages are encrypted, the layout comes from somewhere else: an older mysqldump or schema file, the application's installation scripts, a copy of the database on a test or reporting server, or analysis of the rows themselves.
The recovered rows are loaded into a clean MySQL or MariaDB server of the same version, checked against what the business expects (row counts, the latest invoice or order numbers, date ranges) and handed back as a working database or as SQL files.
mysqldump files and binary logs
A plain mysqldump .sql file that was partly encrypted is often the easiest case: outside the encrypted region it is readable SQL, and the INSERT statements of each table can be cut out and loaded. If the dump was compressed (.sql.gz or .zip), the part after the encrypted region of the compressed stream is much harder to read, so uncompressed dumps survive better.
Binary logs record every change made to the database. On their own they rarely rebuild a whole database, but on top of an older clean backup they can replay the changes made up to the attack. Large binlog files are often only partly encrypted and keep most of their events.
Any old copy helps: last month's dump, a replica, or a developer's copy gives the exact table structure, and comparing an old file with its encrypted version shows exactly how the ransomware encrypted.
The other MySQL ransom attack: deleted, not encrypted
MySQL servers that are open to the internet with a weak password are attacked by automated scripts that log in, copy or simply delete the databases, and leave behind a new database or table, often named PLEASE_READ_ME or WARNING, with a demand for Bitcoin. Nothing is encrypted, and in many such attacks the data was never copied, so paying brings nothing back.
Here the data was dropped, not encrypted. If the binary logs were on, they may contain the data up to the moment of deletion. If not, the deleted pages often still sit on the disk until they are overwritten, and can be recovered from an image of the drive. Stop the server and the application immediately to avoid overwriting them, and close MySQL's port (3306) to the internet before restoring anything.
What to do first
Do not reinstall MySQL over the encrypted data directory, do not run mysqld with innodb_force_recovery, and do not try repair tools on the encrypted files: they cannot fix encryption and can overwrite pages that are still intact. Copy the whole data directory, the binary logs and every dump file to separate storage and work only on copies.
Collect the ransom note, two or three encrypted files (one large .ibd, one small table file and a dump if you have one), the MySQL or MariaDB version, and any older copy of the database or its schema. Send these for a free assessment: we measure how each file was encrypted and tell you which tables can come back before any work is paid for.
Questions and answers
Can a ransomware-encrypted MySQL database be recovered without paying?
Often, if the ransomware encrypted only part of each file. InnoDB stores each table in an .ibd file of 16 kB pages, and the intact pages can be read directly and the rows rebuilt. Files encrypted from end to end with a strong cipher cannot be recovered without the key.
ibdata1 and the .frm files are encrypted. Are the tables lost?
No. ibdata1 and the .frm files describe the tables, but the rows themselves are in the .ibd files. The table structure is rebuilt from an older dump, the application's scripts, another copy of the database or the rows themselves, and the intact .ibd pages are then decoded.
Should I start MySQL with innodb_force_recovery?
Not on encrypted files. innodb_force_recovery helps with some kinds of corruption, but it cannot read encrypted pages and starting the server can write to the files. Copy the data directory first and work only on copies.
Our MySQL server shows a PLEASE_READ_ME table asking for Bitcoin. Will paying return the data?
Usually not. These automated attacks often delete the databases without keeping a copy. The data may still be recoverable from the binary logs or from deleted pages on the disk, so stop the server, close port 3306 to the internet and get the disk imaged before anything else is written.
Ransomware data recovery service →
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