MySQL安全问题(防范必知)

    对于任何一种数据库来说,安全问题都是非常重要的。如果数据库出现安全漏洞,轻则数据被窃取,重则数据被破坏,这些后果对于一些重要的数据库都是非常严重的。下面来从操作系统和数据库两个层对MySQL的安全问题进行讨论。

 

操作系统相关的安全问题

    常见的操作系统安全问题主要出现在MySQL的安装和启动过程中.

    1.严格控制操作系统账号和权限

    在数据库服务器上要严格控制操作系统的账号和权限,比如:
  • 锁定mysql用户
  • 其他任何用户都采取独立的账号登录,管理员通过mysql专有用户管理MySQL,或者通过root su到mysql用户下进行管理。
  • mysql用户目录下,除了数据文件目录,其他文件和目录属主都改为root

    2.尽量避免以root权限运行MySQL

    MySQL安装完毕后,一般会将数据目录属主设置为mysql用户,而将MySQL软件目录的属主设置为root,这样做的目的是当使用mysql启动数据库时,可以防止任何具有FILE权限的用户能够用root创建文件。而如果使用root用户启动数据库,则任何具有FILE权限的用户都可以读写root用户的文件,这样会给系统造成严重的安全隐患。

    3.防止DNS欺骗

    创建用户时,host可以指定域名或者IP地址。但是,如果指定域名,就可能带来如下安全隐患:  如果域名对应的IP地址被恶意修改,则数据库就会被恶意的IP地址进行访问,导致安全隐患。
 
 

数据库相关的安全问题

    常见的数据库问题大多数是由于账号的管理不当造成的。应该加强对账号管理的安全意识。

    1.删除匿名账号

    在某些版本的中,安装完毕MySQL后,会自动安装一个空账号,此账号具有对test数据库的全部权限,普通用户只需要执行mysql命令即可登录MySQL数据库,这个时候默认使用了空用户,可以在test数据库里面做各种操作,比如可以创建一个大表,占用大量磁盘空间,这样给系统造成了安全隐患。

    2.给root账号设置口令

    MySQL安装完毕后,root默认口令为空,需要马上修改口令
set password=password('newpassword');

    3.设置安全密码

    密码的安全体现在以下两个方面:
  • 设置安全的密码,建议使用6位以上字母、数字、下划线和一些特殊字符组合的而成的字符串;
  • 使用上的安全,使用密码期间尽量保证使用过程安全,不会被别人窃取。
    第一点就不用说了,越长越复杂越没有规律的密码越安全。对于第二点,可以总结一下,在日常工作中,使用密码一般是采用以下几种方式。
    (1)直接将密码写在命令行中。
mysql -uroot -p123
    (2)交互式方式输入密码。
mysql -uroot -p
    (3)将用户名和密码写在配置文件里面,连接的时候自动读取,比如应用连接数据库或者执行一些批处理脚本。对于这种方式,MySQL供了一种方法,在my.cnf里面写入连接信息。
[client]
user=username
password=password
然后对配置文件进行严格的权限限制,例如:
chomod +600 my.cnf
    以上是3种常见的密码使用方式。很显然,第1种最不安全,因为它将密码写成为明文;第2种比较安全,但是只能使用在交互的界面下;第3种比较方便,但是需要将配置文件设置严格的存取权限,而且任何只要可以登录操作系统的用户都可能自动登录,存在一定的安全隐患。
    第3种方法通常使用不多,下面举一个例子
    (1)输入mysql无法登录。
[root@iZ28dr6w0qvZ ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    (2)修改配置文件,加入连接信息
[root@iZ28dr6w0qvZ ~]# vim /etc/my.cnf 
...
[client]
#password       = your_password
user=cqh
password=123
    (3)重启数据库后,输入mysql
[root@iZ28dr6w0qvZ ~]# service mysqld restart
Shutting down MySQL... SUCCESS! 
Starting MySQL.. SUCCESS! 
[root@iZ28dr6w0qvZ ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> select current_user();
+----------------+
| current_user() |
+----------------+
| cqh@localhost  |
+----------------+
1 row in set (0.02 sec)

    4.只授予账号必须的权限

    只需要赋予普通用户必须的权限,比如:
grant select,insert,update,delete on tablename to 'username'@'hostname';
    在很多情况下,DBA由于图方便,而经常赋予用户all privileges权限,这个all privileges到底具体包含哪些权限呢?来看下面的例子:
mysql> select * from db where user='cqh'\G
*************************** 1. row ***************************
                 Host: localhost
                   Db: test
                 User: cqh
          Select_priv: Y
          Insert_priv: Y
          Update_priv: Y
          Delete_priv: Y
          Create_priv: Y
            Drop_priv: Y
           Grant_priv: N
      References_priv: Y
           Index_priv: Y
           Alter_priv: Y
Create_tmp_table_priv: Y
     Lock_tables_priv: Y
     Create_view_priv: Y
       Show_view_priv: Y
  Create_routine_priv: Y
   Alter_routine_priv: Y
         Execute_priv: Y
           Event_priv: Y
         Trigger_priv: Y
1 row in set (0.00 sec)
    all privileges里面的权限,远远超过了我们一般应用所需要的权限。而且,有些权限如果误操作,将会产生非常严重的后果,比如drop_priv等。因此,用户权限的时候越具体,则对数据库越安全。

    5.除root外,任何用户不应有mysql库user表的存取权限

    由于MySQL中可以通过更改mysql数据库的user表进行权限的增加、删除、变更等操作,因此,除了root以外,任何用户都不应该拥有对user表的存取权限(SELECT、UPDATE、INSERT、DELETE等),造成系统的安全隐患。下例对普通用户cqh授予user表的存取权限,看看会对系统产生了怎么样的安全隐患。
    (1)创建普通用户chenqionghe,拥有对mysql数据库中的user表的各种权限。
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 103
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant select,update,insert,delete on mysql.user to chenqionghe@localhost;
Query OK, 0 rows affected (0.00 sec)
    (2)用chenqionghe来更新root权限。
[root@iZ28dr6w0qvZ ~]# mysql -uchenqionghe
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 106
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use mysql;
Database changed
mysql> 
mysql> update user set password=password('abcd') where user='root' and host='localhost';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0
    (3)当数据库重启或者root刷新权限表后,root登录时密码已经被更改。
[root@iZ28dr6w0qvZ ~]# mysql -uroot -pabcd
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    6.不要把FILE、PROCESS或SUPER权限授予管理员以外的账号

    FILE权限主要以下作用:
将数据库的信息通过SELECT ...INTO OUTFILE...写到服务器上有写权限的目录下,作为文本格式存放。具有权限的目录也就是启动MySQL时的用户权限目录。
可以将有读权限的文本文件通过LOAD DATA INFILE...命令写入数据表,如果这些表中存放了很重要的信息,将对系统造成很大的安全隐患。
    在例中详细描述了FILE权限可能造成的隐患。
    (1)连接数据库并创建测试表t。
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> create table t (name varchar(500));
Query OK, 0 rows affected (0.02 sec)
    (2)将/etc/password文件加载到表t中
mysql> load data infile '/etc/passwd' into table t;
Query OK, 23 rows affected (0.01 sec)
Records: 23  Deleted: 0  Skipped: 0  Warnings: 0
    (3)查看t的内容
mysql> select * from t;
+----------------------------------------------------------------------+
| name                                                                 |
+----------------------------------------------------------------------+
| root:x:0:0:root:/root:/bin/bash                                      |
| bin:x:1:1:bin:/bin:/sbin/nologin                                     |
| daemon:x:2:2:daemon:/sbin:/sbin/nologin                              |
| adm:x:3:4:adm:/var/adm:/sbin/nologin                                 |
| lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin                             |
| sync:x:5:0:sync:/sbin:/bin/sync                                      |
| shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown                         |
| halt:x:7:0:halt:/sbin:/sbin/halt                                     |
| mail:x:8:12:mail:/var/spool/mail:/sbin/nologin                       |
| uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin                      |
| operator:x:11:0:operator:/root:/sbin/nologin                         |
| games:x:12:100:games:/usr/games:/sbin/nologin                        |
| gopher:x:13:30:gopher:/var/gopher:/sbin/nologin                      |
| ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin                          |
| nobody:x:99:99:Nobody:/:/sbin/nologin                                |
| vcsa:x:69:69:virtual console memory owner:/dev:/sbin/nologin         |
| ntp:x:38:38::/etc/ntp:/sbin/nologin                                  |
| saslauth:x:499:76:"Saslauthd user":/var/empty/saslauth:/sbin/nologin |
| postfix:x:89:89::/var/spool/postfix:/sbin/nologin                    |
| sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin   |
| nscd:x:28:28:NSCD Daemon:/:/sbin/nologin                             |
| www:x:500:500::/alidata/www:/sbin/nologin                            |
| mysql:x:501:501::/home/mysql:/sbin/nologin   
这样,重要的用户信息/etc/passwd内容将被写入表t中,造成安全隐患。
    PROCESS权限能被用来执行“show processlist”命令,查看当前所有用户执行的查询的明文文本,包括设定或改变密码的查询。在默认情况下,每个用户都可以执行“show processlist”命令,但是只能查询本用户的进程。因此,对PROCESS权限管理不当,有可能会使得普通用户能够看到管理员执行的命令。
    下例中对普通用户赋予了PROCESS权限,来看看会造成什么安全隐患。
    (1)将PROCESS权限授予给普通用户:
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 26
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show processlist;
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host      | db   | Command | Time | State | Info             |
+----+------+-----------+------+---------+------+-------+------------------+
|  2 | root | localhost | NULL | Sleep   |   53 |       | NULL             |
| 26 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
2 rows in set (0.00 sec)
mysql> grant process on *.* to 'cqh'@'localhost';
Query OK, 0 rows affected (0.00 sec)
    (2)锁定表user,可以让进程阻塞,以方便用户看到进程内容:
mysql> lock table user read;
Query OK, 0 rows affected (0.00 sec)
    (3)打开另外一个session,用root执行修改密码操作,此时因为user表被锁定,此进程被阻塞挂起
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 27
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> set password=password('123');
    (4)打开第3个session,用cqh登录,执行show processlist语句:
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host      | db    | Command | Time | State                        | Info                         |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep   |   20 |                              | NULL                         |
| 27 | root | localhost | NULL  | Query   |   15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh  | localhost | NULL  | Query   |    0 | NULL                         | show processlist             |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
    可以发现,cqh显示的进程中清楚地看到了root的修改密码操作,并看到了明文的密码,这将对系统造成严重的安全隐患。
    SUPER权限能够执行kill命令,终止其他用户进程。下面例子中,普通用户拥有了SUPER权限后,便可以任意kill任何用户的进程。
    (1)cqh登录后想kill掉root修改密码进程(进程号27)
mysql> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host      | db    | Command | Time | State                        | Info                         |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep   |   20 |                              | NULL                         |
| 27 | root | localhost | NULL  | Query   |   15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh  | localhost | NULL  | Query   |    0 | NULL                         | show processlist             |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
mysql> kill 27;
ERROR 1095 (HY000): You are not owner of thread 27
    (2)kill失败后,root将super权限赋予cqh;
mysql> grant super on *.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+--------------------------------------------------+
| Grants for cqh@localhost                         |
+--------------------------------------------------+
| GRANT PROCESS, SUPER ON *.* TO 'cqh'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)
    (3)重新kill root的进程成功:
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 40
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show processlist;
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| Id | User | Host      | db    | Command | Time | State                        | Info                         |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
| 26 | root | localhost | mysql | Sleep   |   20 |                              | NULL                         |
| 27 | root | localhost | NULL  | Query   |   15 | Waiting for table level lock | set password=password('123') |
| 31 | cqh  | localhost | NULL  | Query   |    0 | NULL                         | show processlist             |
+----+------+-----------+-------+---------+------+------------------------------+------------------------------+
3 rows in set (0.00 sec)
mysql> kill 27;
Query OK, 0 rows affected (0.00 sec)
    从上面的例子中,可以看到FILE、PROCESS、SUPER三个管理权限可能会带来的安全隐患,因此除了管理员外,不要把这些权限赋予给普通用户。
 

    7.LOAD DATA LOCAL带来的安全问题

    LOAD DATA默认读的是服务器上的文件,但是加上LOCAL参数后,就可以将本地具有访问权限的文件加载到数据库中。这在在带来方便的同时,可带来了以下安全问题。
可以任意加载本地文件到数据库。
在Web环境中,客户从Web服务器连接,用户可以使用LOAD DATA LOCAL语句来读取Web服务器进程在读访问权限的任何文件(假定用户可以运行SQL服务器的任何命令)。在这种环境中,MySQL服务器的客户实际上的是Web服务器,而不是连接Web服务器的用户运行的程序。
    解决的方法是,可以用--local-infile=0选项启动mysqld从服务器禁用所有LOAD DATA LOCAL命令。
    对于mysql命令行客户端,可以通过指定--local-infile[=1]选项启用LOAD DATA LOCAL,或通过--local-infile=0选项禁用。类似地,对于mysqlimport,--local or -L选项启用本地文件装载。在任何情况下,成功进行本地装载需要服务器启用相关选项。
 

    8.DROP TABLE命令并不收回以前的相关访问权限

    DROP表的时候,其他用户对此表的权限并没有被收回,这样导致重新创建同名的表时,以前其他用户对此表的权限会自动自动赋予,进而产生权限外流。因此,在删除表时,要同时取消其他用户在此表上的相应权限。
    下面的例子说明了不收回相关访问授权的隐患。
    (1)用root创建用户cqh,授权test下所有表的select权限:
mysql> grant select on test.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+-----------------------------------------------+
| Grants for cqh@localhost                      |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost'       |
| GRANT SELECT ON `test`.* TO 'cqh'@'localhost' |
+-----------------------------------------------+
2 rows in set (0.00 sec)
    (2)cqh登录,测试权限:
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 287
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu           |
| salary         |
| t              |
| t1             |
| t12            |
| t2             |
+----------------+
6 rows in set (0.00 sec)
    (3)root登录,删除表t12:
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 288
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> drop table t12;
Query OK, 0 rows affected (0.00 sec)
    (4)cqh登录,再次测试权限:
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 290
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu           |
| salary         |
| t              |
| t1             |
| t2             |
+----------------+
5 rows in set (0.00 sec)
    (5)此时t12表已经看不到了。
mysql> show grants for cqh@localhost;
+-----------------------------------------------+
| Grants for cqh@localhost                      |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost'       |
| GRANT SELECT ON `test`.* TO 'cqh'@'localhost' |
+-----------------------------------------------+
2 rows in set (0.00 sec)
    权限仍然显示对test下所有表的有SELECT权限(安全漏洞)
    (6)root再次登录,创建表t12:
[root@iZ28dr6w0qvZ ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 292
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> create table t12(id int);
Query OK, 0 rows affected (0.03 sec)
    (7)cqh登录,对t1权限依旧存在:
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 293
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu           |
| salary         |
| t              |
| t1             |
| t12            |
| t2             |
+----------------+
6 rows in set (0.00 sec)
    因此,对表做删除后,其他用户对此表的权限不会自动收回,一定要记住手工收回。
    

    9.使用SSL

    SSL(Secure Socket Layer,安全套接字层)是一种安全传输的协议,最初Netscape公司所开发,用以保障在Internet上数据传输之安全,利用 数据加密(Encryption)技术,可确保数据在网络上传输过程中不会被截取及窃听。
    SSL协议提供的服务主要有:
    (1)认证用户和服务器,确保数据发送到正确的客户机和服务器;
    (2)加密数据以防止数据中途被窃取;
    (3)维护数据的完整性,确保数据在传输过程中不被改变。
    在MySQL中,要想使用SSL进行安全传输,需要在命令行中或选项文件中设置“--ssl”选项。
    对于服务器,“ssl”选项规定该服务器允许SSL连接。对于客户端端程序,它允许客户使用SSL连接。对于客户端程序,它允许客户端用SSL连接服务器。单单该选项不足以使用SSL连接。还必须指定--ssl-ca、--ssl-cert和--ssl-key选项。如果不想启用SSL,可以将选项指定为--skip-ssl或--ssl=0。
    请注意,如果编译的服务器或客户端不支持SSL,则使用普通的示加密的连接。
    确保使用SSL连接的安全方式是,使用含REQUIRE SSL子句的GRANT语句在服务器上创建一账户,然后使用该账户来连接服务器,服务器和客户端均应启用SSL支持。下面例子创建了一个含REQUIRE SSL子句的账号:
mysql> grant select on *.* to cqh identified by '123' REQUIRE ssl;
Query OK, 0 rows affected (0.00 sec)
  • --ssl-ca=file_name    含可信的SSL CA的清单的文件的路径
  • --ssl-cert=file_name    SSL证书文件名,用于建立安全连接
  • --ssl-key=file_name    SSL密钥文件名,用于建立 安全连接
 

    10.如果可能,给所有用户加上访问IP限制

    对数据库来说,我们希望客户端过来的连接都是安全的,因此,就很有必要在创建用户的时候指定可以进行连接的服务器IP或者HOSTNAME,只有符合授权的IP或者HOSTNAME才可以进行数据库的访问。
 

    11.REVOKE命令的漏洞

    当用户多次赋予权限后,由于各种原因,需要将此用户的权限全部取消,此时,REVOKE命令可能并不会按照我们的意愿执行,来看看下面的例子。
    (1)连续赋予用户两次权限,其中,第2次是对所有数据库的所有权限。
mysql> grant select,insert on test.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for cqh@localhost;
+-------------------------------------------------------+
| Grants for cqh@localhost                              |
+-------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'cqh'@'localhost'      |
| GRANT SELECT, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
    (2)此时,需要取消用户的所有权限。
mysql> revoke all privileges on *.* from cqh@localhost;
Query OK, 0 rows affected (0.00 sec)
    (3)我们很可能以为,此时用户已经没有任何权限了,而不会再去查看他的权限表。而实际上,此时的用户依然拥有test上的SELECT和INSERT权限。
mysql> show grants for cqh@localhost;
+-------------------------------------------------------+
| Grants for cqh@localhost                              |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost'               |
| GRANT SELECT, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
    (4)此时,再次用cqh登录,测试一下是否能对test数据库做操作。
[root@iZ28dr6w0qvZ ~]# mysql -ucqh
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 395
Server version: 5.5.37-log MySQL Community Server (GPL)
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| menu           |
| salary         |
| t              |
| t1             |
| t12            |
| t2             |
+----------------+
6 rows in set (0.00 sec)
mysql> insert into t1 values (1);
Query OK, 1 row affected (0.01 sec)
    这个是MySQL权限机制造成的隐患,在一个数据库上多次赋予权限,权限会自动合并;但是在多个数据库上多次赋予权限,每个数据库上都会认为是单独的一组权限,必须在此数据库上用REVOKE命令来单进行权限收回,而 REVOKE ALL PRIVILEGES ON *.* 并不会替用户自动完成这个情况。
 
 
到这里,就把衣钵都传给你们了,别忘了点个赞哦!~
posted @ 2015-10-13 10:16  雪山飞猪  阅读(15743)  评论(2编辑  收藏  举报