首页 >  帮助中心 > Mysql被黑客入侵后的安全应对措施

Mysql被黑客入侵后的安全应对措施

来源:5G云 点击量(34) 发布时间:2018-05-31

情况概述

今天登陆在云服务器上搭建的Mysql数据库,发现数据库被黑了,黑客提示十分明显。

Mysql中只剩下两个数据库,一个是information_schema,另一个是黑客创建的PLEASE_READ,其中有一张info表,内容如下:

Info: Your DB is Backed up at our servers, to restore send 0.2 BTC to the Bitcoin Address then send an email with your server ip

Bitcoin_Address: 1F33LEJjdphD6YpaonNCHejwLcgkgDGQW9

Email: mysqldata@mail2tor.com

显然,我这是遇到比特币敲诈了。我的数据在别人的服务器里安然的躺着,需要向黑客支付0.2比特币才有可能恢复。按照当前的汇率,0.2比特币大约为1400人民币,这是我第一次遇到网络敲诈,金额还不小。

所幸数据库里并没有值钱的数据,就当是送给黑客了,不过数据库安全问题引起了我的注意。


安全措施

由于缺乏必要的安全措施和备份机制,数据库中原有的数据均已丢失。为了恢复到Mysql初始的状态,重新安装了Mysql数据库,并且重新创建原先存在的数据库,同时,为了防止再次被黑客入侵,对Mysql进行了一些安全配置。

禁用或限制远程访问。若允许远程访问,需要确保特定主机才拥有访问权。

对用户进行合理授权,应用程序中最好不要直接使用root用户。

限制打开网络socket,此时仍可以建立与Mysql服务器的本地连接。

?

1
2
[mysqld]
skip-networking

强迫Mysql仅监听本机。

?

1
2
[mysqld]
bind-address=127.0.0.1

更改root用户的登录名称和密码。

移除测试数据库和匿名账户及废弃的账户。

禁用LOCAL INFILE。

?

1
2
[mysqld]
set-variable=local-infile=0

删除历史命令记录。

?

1
2
cat /dev/null > ~/.bash_history
cat /dev/null > ~/.mysql_history

及时安装Mysql安全补丁。

使用chroot限制Mysql运行环境。

自动定期备份数据库。

用防火墙实现mysql绑定多个ip

my.cnf中有选项bind-address=127.0.0.1,是说mysql server监听的是本地发来的请求,如果是任意主机都可以请求,则写为0.0.0.0,但是这样又不太安全。监听某ip,指定此ip地址即可,但是要保证mysql的user中有允许此ip访问,否则不能对数据库操作。那么是否可以在配置里只规定几个ip呢?

简单直接回答:不可能

请参考:https://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_bind-address

The MySQL server listens on a single network socket for TCP/IP connections. This socket is bound to a single address, but it is possible for an address to map onto multiple network interfaces. The default address is 0.0.0.0. To specify an address explicitly, use the –bind-address=addr option at server startup, where addr is an IPv4 address or a host name. If addr is a host name, the server resolves the name to an IPv4 address and binds to that address. The server treats different types of addresses as follows:

If the address is 0.0.0.0, the server accepts TCP/IP connections on all server host IPv4 interfaces.

If the address is a “regular” IPv4 address (such as 127.0.0.1), the server accepts TCP/IP connections only for that particular IPv4 address.

但是有此需求,就会到访问控制,那么使用防火墙iptables可实现此效果

mysql-server为192.168.1.3,只允许192.168.1.4, 192.168.1.5, 192.168.1.6来访问3306端口

在my.cnf中

?

1
bind-address = 0.0.0.0

在访问3306端口的主机中,只允许192.168.1.4-6,其他ip一律DROP掉

?

1
2
3
4
/sbin/iptables -A INPUT -p tcp -s 192.168.1.4 --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s 192.168.1.5 --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp -s 192.168.1.6 --dport 3306 -j ACCEPT
/sbin/iptables -A INPUT -p tcp --dport 3306 -j DROP

?

1
2
3
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.4 -j DROP
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.5 -j DROP
/sbin/iptables -A INPUT -p tcp --dport 3306 ! -s 192.168.1.6 -j DROP

保存防火墙规则

?

1
service iptables save

查看INPUT链包含3306的规则

?

1
echo -e "target prot opt source destination\n$(iptables -L INPUT -n | grep 3306)"

这样就实现了mysql只允许指定ip访问。

yum 和 rpm安装mysql彻底删除

1、yum方式安装的MySQL

$yum remove mysql mysql-server mysql-libs compat-mysql51

$rm -rf /var/lib/mysq

$rm /etc/my.cnf

查看是否还有mysql软件:

$rpm -qa|grep mysql

如果存在的话,继续删除即可,删除方式:yum remove + 【名字】。

2、rpm方式安装的mysql

a)查看系统中是否以rpm包安装的mysql:

[root@localhost opt]# rpm -qa | grep -i mysql

MySQL-server-5.6.17-1.el6.i686

MySQL-client-5.6.17-1.el6.i686

b)卸载mysql

[root@localhost local]# rpm -e MySQL-server-5.6.17-1.el6.i686

[root@localhost local]# rpm -e MySQL-client-5.6.17-1.el6.i686

c)删除mysql服务

[root@localhost local]# chkconfig --list | grep -i mysql

[root@localhost local]# chkconfig --del mysql

d)删除分散mysql文件夹

[root@localhost local]# whereis mysql 或者find / -name mysql

mysql: /usr/lib/mysql /usr/share/mysql

清空相关mysql的所有目录以及文件

rm -rf /usr/lib/mysql

rm -rf /usr/share/mysql

rm -rf /usr/my.cnf

通过以上几步,mysql应该已经完全卸载干净了。


  • 服务热线
    • 全方位的购买咨询
    • 精准的配置推荐
    • 1对1贴心服务
    • 7*24小时服务热线
  • 付款方式
    • 支持支付宝、微信、转账等多种支付方式