Table of Contents
一、发现问题
二、解决方法
三、找备份及时间点
四、具体的恢复操作
Home Database Mysql Tutorial MySQL数据库遭到攻击篡改—使用备份和binlog进行数据恢复

MySQL数据库遭到攻击篡改—使用备份和binlog进行数据恢复

Jun 07, 2016 pm 04:42 PM
mysql use backup attack database tamper

数据篡改即是对计算机网络数据进行修改、增加或删除,造成数据破坏。数据库数据被攻击了首先得查看是被删除了还是被篡改了?是否有备份数据,是否能够进行恢复并加固。本文来自数据库技术专家张正,主要描述了MySQL遭到攻击篡改数据,利用从库的备份和主库的B

数据篡改即是对计算机网络数据进行修改、增加或删除,造成数据破坏。数据库数据被攻击了首先得查看是被删除了还是被篡改了?是否有备份数据,是否能够进行恢复并加固。本文来自数据库技术专家张正,主要描述了MySQL遭到攻击篡改数据,利用从库的备份和主库的Binlog进行不完全恢复。

以下是作者原文:

一、发现问题

今天是2014-09-26,开发大清早就说昨晚数据库遭到了攻击。数据库中某文章表的文章内容字段遭到篡改,全部改成了同一篇文章。

通过查看日制 发现 数据是在 2014-09-25 21:53:57 遭到篡改。

所有的内容全部被改成了如下:

subject: 桂林阳朔自助游

content:

          一直都是自助游,从不喜欢?团。去之前都是在网上做足了功课,真的是很感谢那些写游记写攻略的朋友。所以,现在也想把自己的体会和经验写出来,和大家分享,希望对后来的朋友有帮助。

         一个月前,朋友约我去阳朔一游,阳朔也是我一直想去的地方,特别是传说中的西街。上网搜集资料,制定出我们的行程计划(呵呵,可能是职业习惯吧,制定计划和行程安排是我们的强项,计划性和灵活性是我们的特点),目的很明确,是度假休闲,不必游走于各个景点,其实我想朋友们也在很多地方都旅游多了,也知道有些景点是怎么出来的,各地都一样。

          制定好主旋律后,我们的大致行程安排如下:

          十九号桂林集中,二十号出发去阳朔,先去阳朔安顿下来(有些人是从桂林带着行李在杨堤路口下车,直接先去漓江漂流,然后再去阳朔,好像节约时间,不过,我们因为没有安排那么满,也不想带着行李游玩,所以选择先去阳朔安顿,和客栈老板好好聊聊情况后,再决定具体细节)

         ?次度假的主要内容是:漓江漂流;遇龙河漂流(全漂),十里画廊;西街打望,发呆,西街酒吧,印象刘三姐,其他的根据情况和心情临时决定。

我把文章贴出来,先谴责一下,很可能是某旅游社的人为了打广告 雇人干的。

二、解决方法

这个库我们是每天凌晨备份,保留30天的备份。主库的Binlog保留时间为7天。

因此很容易想到的方法是将从库2014-09-25凌晨的备份拿出来恢复,然后通过主库的Binlog通过时间段来筛选出凌晨至2014-09-25 21:53:56的所有更改,之后的数据,经业务确认,可以舍弃掉。或者后面再通过其他方法慢慢将这部分数据找出来。但是当务之急,是立马恢复数据库。

三、找备份及时间点

在备份的从库上检查备份:

crontab -l
#0 3 * * * /data/opdir/mysqlbak/backup_mysqldump.sh 6084 >> /data/opdir/mysqlbak/6084/mysql-bakup.log 2>&1

发现备份任务让注释了

查看备份文件:

<p>[root@localhost 6084]# ll</p><p>total 128</p><p>drwxr-xr-x 2 root root 4096 Aug 25 03:13 20140825</p><p>drwxr-xr-x 2 root root 4096 Aug 26 03:13 20140826</p><p>drwxr-xr-x 2 root root 4096 Aug 27 03:13 20140827</p><p>drwxr-xr-x 2 root root 4096 Aug 28 03:13 20140828</p><p>drwxr-xr-x 2 root root 4096 Aug 29 03:13 20140829</p><p>drwxr-xr-x 2 root root 4096 Aug 30 03:13 20140830</p><p>drwxr-xr-x 2 root root 4096 Aug 31 03:13 20140831</p><p>drwxr-xr-x 2 root root 4096 Sep  1 03:13 20140901</p><p>drwxr-xr-x 2 root root 4096 Sep  2 03:13 20140902</p><p>drwxr-xr-x 2 root root 4096 Sep  3 03:13 20140903</p><p>drwxr-xr-x 2 root root 4096 Sep  4 03:13 20140904</p><p>drwxr-xr-x 2 root root 4096 Sep  5 03:13 20140905</p><p>drwxr-xr-x 2 root root 4096 Sep  6 03:13 20140906</p><p>drwxr-xr-x 2 root root 4096 Sep  7 03:13 20140907</p><p>drwxr-xr-x 2 root root 4096 Sep  8 03:13 20140908</p><p>drwxr-xr-x 2 root root 4096 Sep  9 03:13 20140909</p><p>drwxr-xr-x 2 root root 4096 Sep 10 03:13 20140910</p><p>drwxr-xr-x 2 root root 4096 Sep 11 03:13 20140911</p><p>drwxr-xr-x 2 root root 4096 Sep 12 03:13 20140912</p><p>drwxr-xr-x 2 root root 4096 Sep 13 03:13 20140913</p><p>drwxr-xr-x 2 root root 4096 Sep 14 03:13 20140914</p><p>drwxr-xr-x 2 root root 4096 Sep 15 03:13 20140915</p><p>drwxr-xr-x 2 root root 4096 Sep 16 03:13 20140916</p><p>drwxr-xr-x 2 root root 4096 Sep 17 03:13 20140917</p><p>drwxr-xr-x 2 root root 4096 Sep 18 03:14 20140918</p><p>drwxr-xr-x 2 root root 4096 Sep 19 03:14 20140919</p><p>drwxr-xr-x 2 root root 4096 Sep 20 03:13 20140920</p><p>drwxr-xr-x 2 root root 4096 Sep 21 03:13 20140921</p><p>drwxr-xr-x 2 root root 4096 Sep 22 03:14 20140922</p><p><strong>drwxr-xr-x 2 root root 4096 Sep 23 18:33 20140923</strong></p><p>-rw-r--r-- 1 root root 5475 Sep 23 18:33 mysql-bakup.log</p>
Copy after login

备份只到20140923日,下午18:33分。

备份日志最后一段截取:

?tail -n 5 mysql-bakup.log
deleting backup of 30 days ago — 20140824
2014-09-23 18:19:12 begin backup …
20140824 deleted OK
2014-09-23 18:33:43 end backup …

因为这些表是在从库备份的,而且表都是MyiSAM的表。查看备份脚本,是先Stop Slave之后,才开始备份,因此从备份脚本输出的日志中找到备份开始的时间是:

2014-09-23 18:19:12

通过:

Drwxr-xr-x 2 root root 4096 Sep 23 18:33 20140923

可看到结束时间是:2014-09-23 18:33:00

现在考虑到底是以备份开始的时间:2014-09-23 18:19:12 为Start-DateTime还是以2014-09-23 18:33:00 为Start-DateTime。

前面 提到备份脚本是从库进行备份的,是在2014-09-23 18:19:12开始的,在这个时刻备份开始,执行了Stop Slave;因此整个备份的状态反映的是从库2014-09-23 18:19:12 这个时间的状态。而且通过监控可以看到在这个时间点,从库的延迟为0,因此可以认为这个备份就是?主库在这个时间的备份

NOTES:
(有人可能会因为从库上有Binlog,从库也会接受主库的Binlog之类的机制而造成混淆。这里要结合我们具体的备份方式和恢复方式来看,以选出正确的时间点。)

前面提到通过日志查到遭到篡改的时间为:2014-09-25?21:53:57,因此可以将2014-09-25?21:53:56作为Stop-DateTime

因此Binlog命令应该是这样:

<strong>mysqlbinlog --database=[db_name] --start-datetime='2014-09-23 </strong><strong>18:19:12</strong><strong>' --stop-datetime='2014-09-25 21:53:56' <br></strong><strong>[binlog_name] > binlog_name0000x.sql</strong>
Copy after login

四、具体的恢复操作

清楚了这些,具体的操作就简单了:

1.从备份机拷贝备份:

SCP:/data/MySQLbak/20140923/20140923.db_name.gz :/data/opdir/20140926

2.恢复测试机 解压:

gunzip 20140923.db_name.gz

3.恢复测试机导入(测试恢复库中之前没有db_name这个库):

MySQL -uroot -pxxxxxx -S /tmp/MySQL.sock

4.将主库的Binlog拷贝到恢复测试机:

查看主库Binlog

-rw-rw---- 1 MySQL MySQL  87669492 Sep 23 00:00 MySQL-bin.000469<br>-rw-rw---- 1 MySQL MySQL 268436559 Sep 23 04:20 MySQL-bin.000470<br>-rw-rw---- 1 MySQL MySQL 268435558 Sep 23 17:32 MySQL-bin.000471<br>-rw-rw---- 1 MySQL MySQL  37425262 Sep 24 00:00 MySQL-bin.000472<br>-rw-rw---- 1 MySQL MySQL 137389819 Sep 25 00:00 MySQL-bin.000473<br>-rw-rw---- 1 MySQL MySQL 147386521 Sep 26 00:00 MySQL-bin.000474<br>我们需要的Binlog时间段为:2014-09-23 18:28:00 至 2014-09-25 21:53:56<br>因此只需要:<br>-rw-rw---- 1 MySQL MySQL  37425262 Sep 24 00:00 MySQL-bin.000472<br>-rw-rw---- 1 MySQL MySQL 137389819 Sep 25 00:00 MySQL-bin.000473<br>-rw-rw---- 1 MySQL MySQL 147386521 Sep 26 00:00 MySQL-bin.000474
Copy after login

将这3个Binlog ?Copy过去:

SCP MySQL-bin.000472 :/data/opdir/20140926
SCP MySQL-bin.000473 :/data/opdir/20140926
SCP MySQL-bin.000474 :/data/opdir/20140926

5.使用MySQLBinlog 生成SQL脚本:

MySQLBinlog --database=[db_name] --start-datetime='2014-09-23 18:19:12' --stop-datetime='2014-09-25 21:53:56' <br>MySQL-bin.000472 > 472.SQL<br>MySQLBinlog --database=[db_name] --start-datetime='2014-09-23 18:19:12' --stop-datetime='2014-09-25 21:53:56' <br>MySQL-bin.000473 > 473.SQL<br>MySQLBinlog --database=[db_name] --start-datetime='2014-09-23 18:19:12' --stop-datetime='2014-09-25 21:53:56' <br>MySQL-bin.000474 > 474SQL
Copy after login

6.Binlog生成的SQL脚本导入:

待20140923.db_name导入到恢复测试库之后,将MySQLBinlog生成的SQL脚本导入到数据库中:

MySQL -uroot -pxxxxxx -S /tmp/MySQL.sock db_name MySQL -uroot -pxxxxxx -S /tmp/MySQL.sock db_name MySQL -uroot -pxxxxxx -S /tmp/MySQL.sock db_name

7.导入完成后检查数据正确性:

大致看一下数据的情况,然后可以通过时间字段来看一下情况:

MySQL> select max(createtime),max(updatetime) from table_name;<br>+-----------------+-----------------+<br>| max(createtime) | max(updatetime) |<br>+-----------------+-----------------+<br>|      1411648043 |      1411648043 |<br>+-----------------+-----------------+<br>1 row in set (0.00 sec)
Copy after login

时间差不多为 晚上20:27了

这个判断,作为DBA,查看部分数据,只能起到辅助作用,具体的需要 到底是否OK,需要业务开发的人来判断。

经过业务开发确认后,即可将该数据导出后,再导入到线上主库中。

8、将该库导出,并压缩:

MySQLdump -uroot -pxxxxxx -S /tmp/MySQL.sock -q db_name table_name > table_name.SQL

压缩:

gzip table_name.SQL

SCP 到主库 (复制的时候,请将网络因素考虑进去,确认不会占用过多带宽而影响其他线上业务)

9.恢复测试的数据导入到线上主库中:

线上主库操作:

操作之前,最好让开发把应用业务那段先暂停,否则可能会影响导入。比如这个表示MyISAM的,应用那边如果不听有update进来,就会阻塞数据导入。

a、主库将原始被篡改的表改名:(不要上来就drop,先rename,后续确认没问题了再考虑drop,因为很多问题不是一瞬间就能全部反映上来的)

rename table_name to old_table_name;

b、解压:

gunzip table_name.SQL.gz

c、导入新表数据:

MySQL -uroot -pxxxxxx -S /tmp/MySQL.sock db_name

后面就需要开发来进一步验证数据是否 OK 了。 验证没问题后,再启动应用程序。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

See all articles