07-操作系统相关的安全问题
本节介绍一些常见的操作系统安全问题,这些问题主要出现在MySQL的安装和启动过程中,希望读者能够从安装开始就重视安全问题。
1.严格控制操作系统账号和权限
在数据库服务器上要严格控制操作系统的账号和权限,比如:
锁定mysql用户;
其他任何用户都采取独立的账号登录,管理员通过mysql专有用户管理MySQL,或者通过 root su到mysql用户下进行管理;
mysql用户目录下,除了数据文件目录,其他文件和目录属主都改为root。
2.尽量避免以root权限运行MySQL
MySQL安装完毕后,一般会将数据目录属主设置为mysql用户,而将MySQL软件目录的属主设置为 root,这样做的目的是当使用 mysql 用户启动数据库时,可以防止任何具有 FILE权限的用户能够用root创建文件。而如果使用root用户启动数据库,则任何具有FILE权限的用户都可以读写root用户的文件,这样会给系统造成严重的安全隐患。来看下面的一个例子。
(1)MySQL以用户zzx启动:
[root@localhost mysql]# ./bin/mysqld_safe --user=zzx &
[1] 705
[root@localhost mysql]# Starting mysqld daemon with databases from /home/zzx/ mysql/data
(2)将mysql数据库下的表user写到根目录/下的user.txt:
[root@localhost mysql]# mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 5.1.11-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select * from user into outfile '/user.txt';
ERROR 1 (HY000): Can't create/write to file '/user.txt' (Errcode: 13)
由于是用非root权限启动MySQL,则文件无法写入。
(3)将数据库关闭,然后以root启动。
[root@localhost mysql]# mysqladmin -uroot shutdown
STOPPING server from pid file /home/zzx/mysql/data/localhost.localdomain.pid
070723 12:32:49 mysqld ended
[1]+ Done ./bin/mysqld_safe --user=zzx
[root@localhost mysql]# ./bin/mysqld_safe --user=root &
[1] 783
[root@localhost mysql]# Starting mysqld daemon with databases from /home/zzx/ mysql/data
(4)重新执行将mysql数据库下的表user写到根目录/下的user.txt:
mysql> select * from user into outfile '/user.txt';
Query OK, 9 rows affected (0.01 sec)
mysql> system more /user.txt
localhostroot Y Y Y Y Y Y Y Y
YY Y Y YY Y Y Y Y Y Y
YY Y Y Y Y Y Y0 0 0 0
localhost.localdomain root Y Y Y Y Y Y
YY Y Y Y YY Y Y Y Y Y
YY Y Y Y Y Y Y Y Y0 0
00
此时文件成功写入根目录下。由上例可以看出,以root用户启动MySQL,任何只要有FILE权限的用户都可以在任意目录写出文件,这样对操作系统的安全将造成严重隐患。
3.防止DNS欺骗
创建用户时,host可以指定域名或者IP地址。但是,如果指定域名,就可能带来如下安全隐患:如果域名对应的IP地址被恶意修改,则数据库就会被恶意的IP地址进行访问,导致安全隐患。
在下例中,尝试改变域名对应的IP地址,以此来观察一下对连接的影响。
(1)创建测试用户z1,域名指定为test_hostname。
mysql> grant select on test1.* to z1@test_hostname;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for z1@test_hostname;
+---------------------------------------------------+
| Grants for z1@test_hostname |
+---------------------------------------------------+
| GRANT USAGE ON . TO 'z1'@'test_hostname' |
| GRANT SELECT ON test1.* TO 'z1'@'test_hostname' |
+---------------------------------------------------+
(2)编辑hosts文件,增加此域名和IP地址的对应关系:
[root@localhost mysql]# vi /etc/hosts
Do not remove the following line, or various programs
that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
192.168.7.55 localhost.localdomain
192.168.7.187 zzx
192.168.52.24 test_hostname
(3)客户端尝试连接成功:
C:\mysql\bin>ipconfig
Windows IP Configuration
Ethernet adapter无线网络连接:
Media State . . . . . . . . . . . : Media disconnected
Ethernet adapter本地连接:
Connection-specific DNS Suffix . : netease.internal
IP Address. . . . . . . . . . . . : 192.168.52.24
Subnet Mask . . . . . . . . . . . : 255.255.254.0
Default Gateway . . . . . . . . . : 192.168.52.254
C:\mysql\bin>mysql -h192.168.7.55 -P3311 -uz1 -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6 to server version: 5.1.11-beta-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> exit
(4)修改域名IP地址的对应关系,将192.168.52.24改为192.168.52.23:
[root@localhost mysql]# vi /etc/hosts
Do not remove the following line, or various programs
that require network functionality will fail.
127.0.0.1 localhost.localdomain localhost
192.168.7.55 localhost.localdomain
192.168.7.187 zzx
28.2.1.23 test_hostname
(5)客户端再次尝试连接失败:
C:\mysql\bin>mysql -h192.168.7.55 -P3311 -uz1 -p
Enter password:
ERROR 1045 (28000): Access denied for user 'z1'@'192.168.52.24' (using NO)