天天看點

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 預設密碼為空,需要馬上修改密碼

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 無法登入

[[email protected] ~]# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
           

(2)修改配置檔案,加入連接配接資訊

[[email protected] ~]# vim /etc/my.cnf
...
[client]
#password       = your_password
user=cqh
password=123
           

(3)重新開機資料庫後,輸入mysql

[[email protected] ~]# service mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.. SUCCESS!
[[email protected] ~]# 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() |
+----------------+
| [email protected]  |
+----------------+
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表的各種權限

[[email protected] ~]# 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 [email protected];
Query OK, 0 rows affected (0.00 sec)
           

(2)用chenqionghe 來更新root 權限

[[email protected] ~]# 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 登入時密碼已經被更改

[[email protected] ~]# 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

[[email protected] ~]# 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權限授予給普通使用者

[[email protected] ~]# 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表被鎖定,此程序被阻塞挂起

[[email protected] ~]# 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語句

[[email protected] ~]# 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 [email protected];
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+--------------------------------------------------+
| Grants for [email protected]                         |
+--------------------------------------------------+
| GRANT PROCESS, SUPER ON *.* TO 'cqh'@'localhost' |
+--------------------------------------------------+
1 row in set (0.00 sec)
           

(3)重新kill root的程序成功

[[email protected] ~]# 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 [email protected];
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+-----------------------------------------------+
| Grants for [email protected]               |
+-----------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost'     |
| GRANT SELECT ON `test`.* TO 'cqh'@'localhost' |
+-----------------------------------------------+
2 rows in set (0.00 sec)
           

(2)cqh登入,測試權限

[[email protected] ~]# 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

[[email protected] ~]# 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登入,再次測試權限

[[email protected] ~]# 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 [email protected];
+-----------------------------------------------+
| Grants for [email protected]                      |
+-----------------------------------------------+
| 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

[[email protected] ~]# 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權限依舊存在

[[email protected] ~]# 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 [email protected];
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to [email protected];
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for [email protected];
+-------------------------------------------------------+
| Grants for [email protected]                              |
+-------------------------------------------------------+
| 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 [email protected];
Query OK, 0 rows affected (0.00 sec)
           

(3)我們很可能以為,此時使用者已經沒有任何權限了,而不會再去檢視他的權限表。而實際上,此時的使用者依然擁有test上的SELECT和INSERT權限

mysql> show grants for [email protected];
+-------------------------------------------------------+
| Grants for [email protected]                              |
+-------------------------------------------------------+
| GRANT USAGE ON *.* TO 'cqh'@'localhost'               |
| GRANT SELECT, INSERT ON `test`.* TO 'cqh'@'localhost' |
+-------------------------------------------------------+
2 rows in set (0.00 sec)
           

(4)此時,再次用cqh登入,測試一下是否能對test資料庫做操作

[[email protected] ~]# 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 *.*  并不會替使用者自動完成這個情況。

來源:雪上飛豬

www.cnblogs.com/chenqionghe/p/4873665.html