如何在沒有密碼的情況下使用 MySQL(以及安全性風險)

如何在沒有密碼的情況下使用 MySQL(以及安全性風險)

他們說最好的密碼是您不必記住的密碼。 對於 MySQL,這可以透過外掛程式實現 auth_socket 及其 MariaDB 版本 - unix_socket.

這兩個插件根本不是什麼新鮮事,它們在本部落格中已經討論了很多,例如在關於 如何使用 auth_socket 外掛程式來變更 MySQL 5.7 中的密碼。 然而,在研究MariaDB 10.4 中的新增功能時,我發現unix_socket 現在是預設安裝的,並且是身份驗證方法之一(“其中之一”,因為在MariaDB 10.4 中,一個用戶可以使用多個插件進行身份驗證,這文檔中有解釋 MariaDB 10.04 中的“身份驗證”).

正如我所說,這不是新聞,當使用 Debian 團隊支援的 .deb 軟體包安裝 MySQL 時,會建立一個 root 使用者用於套接字驗證。 對於 MySQL 和 MariaDB 來說都是如此。

root@app:~# apt-cache show mysql-server-5.7 | grep -i maintainers
Original-Maintainer: Debian MySQL Maintainers <[email protected]>
Original-Maintainer: Debian MySQL Maintainers <<a href="mailto:[email protected]">[email protected]</a>>

對於 MySQL 的 Debian 軟體包,root 使用者的身份驗證如下:

root@app:~# whoami
root=
root@app:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 4
Server version: 5.7.27-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2019, 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 user, host, plugin, authentication_string from mysql.user where user = 'root';
+------+-----------+-------------+-----------------------+
| user | host      | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket |                       |
+------+-----------+-------------+-----------------------+
1 row in set (0.01 sec)

MariaDB 的 .deb 套件也是如此:

10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04

MariaDB [(none)]> show grants;
+------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                      |
+------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED VIA unix_socket WITH GRANT OPTION |
| GRANT PROXY ON ''@'%' TO 'root'@'localhost' WITH GRANT OPTION                                  |
+------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

來自官方 Percona 儲存庫的 .deb 套件也在 auth-socket 和 Percona Server 下設定 root 使用者身份驗證。 讓我們舉個例子 Percona 伺服器 MySQL 8.0.16-7 和 Ubuntu 16.04:

root@app:~# whoami
root
root@app:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 9
Server version: 8.0.16-7 Percona Server (GPL), Release '7', Revision '613e312'

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, 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 user, host, plugin, authentication_string from mysql.user where user ='root';
+------+-----------+-------------+-----------------------+
| user | host      | plugin | authentication_string |
+------+-----------+-------------+-----------------------+
| root | localhost | auth_socket |                       |
+------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)

那麼有什麼魔力呢? 該插件使用 SO_PEERCRED 套接字選項檢查 Linux 用戶是否與 MySQL 用戶匹配,以收集有關運行客戶端程式的用戶的資訊。 因此,該外掛程式只能在支援 SO_PEERCRED 選項的系統上使用,例如 Linux。 SO_PEERCRED 套接字選項可讓您找出與套接字相關的程序的 uid。 然後他已經收到與該 uid 關聯的使用者名稱。

這是用戶“vagrant”的範例:

vagrant@mysql1:~$ whoami
vagrant
vagrant@mysql1:~$ mysql
ERROR 1698 (28000): Access denied for user 'vagrant'@'localhost'

由於MySQL中沒有「vagrant」用戶,因此我們被拒絕存取。 讓我們創建一個這樣的用戶並重試:

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket;
Query OK, 0 rows affected (0.00 sec)

vagrant@mysql1:~$ mysql
Welcome to the MariaDB monitor.  Commands end with ; or g.
Your MariaDB connection id is 45
Server version: 10.0.38-MariaDB-0ubuntu0.16.04.1 Ubuntu 16.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> show grants;
+---------------------------------------------------------------------------------+
| Grants for vagrant@localhost                                                    |
+---------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'vagrant'@'localhost' IDENTIFIED VIA unix_socket |
+---------------------------------------------------------------------------------+
1 row in set (0.00 sec)

發生了!

那麼,預設不提供此功能的非 Debian 發行版又如何呢? 讓我們嘗試在 CentOS 8 上安裝 Percona Server for MySQL 7:

mysql> show variables like '%version%comment';
+-----------------+---------------------------------------------------+
| Variable_name   | Value                                   |
+-----------------+---------------------------------------------------+
| version_comment | Percona Server (GPL), Release 7, Revision 613e312 |
+-----------------+---------------------------------------------------+
1 row in set (0.01 sec)

mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
ERROR 1524 (HY000): Plugin 'auth_socket' is not loaded

真糟糕。 缺什麼? 插件未載入:

mysql> pager grep socket
PAGER set to 'grep socket'
mysql> show plugins;
47 rows in set (0.00 sec)

讓我們為該流程添加一個插件:

mysql> nopager
PAGER set to stdout
mysql> INSTALL PLUGIN auth_socket SONAME 'auth_socket.so';
Query OK, 0 rows affected (0.00 sec)

mysql> pager grep socket; show plugins;
PAGER set to 'grep socket'
| auth_socket                     | ACTIVE | AUTHENTICATION | auth_socket.so | GPL     |
48 rows in set (0.00 sec)

現在我們已經擁有了我們所需要的一切。 讓我們再試一次:

mysql> CREATE USER 'percona'@'localhost' IDENTIFIED WITH auth_socket;
Query OK, 0 rows affected (0.01 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'percona'@'localhost';
Query OK, 0 rows affected (0.01 sec)

現在您可以使用使用者名稱“percona”登入。

[percona@ip-192-168-1-111 ~]$ whoami
percona
[percona@ip-192-168-1-111 ~]$ mysql -upercona
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 19
Server version: 8.0.16-7 Percona Server (GPL), Release 7, Revision 613e312

Copyright (c) 2009-2019 Percona LLC and/or its affiliates
Copyright (c) 2000, 2019, 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 user, host, plugin, authentication_string from mysql.user where user ='percona';
+---------+-----------+-------------+-----------------------+
| user    | host   | plugin   | authentication_string |
+---------+-----------+-------------+-----------------------+
| percona | localhost | auth_socket |                       |
+---------+-----------+-------------+-----------------------+
1 row in set (0.00 sec)

它又起作用了!

問題:是否可以使用相同的 percona 登入名稱但以不同的使用者身分登入系統?

[percona@ip-192-168-1-111 ~]$ logout
[root@ip-192-168-1-111 ~]# mysql -upercona
ERROR 1698 (28000): Access denied for user 'percona'@'localhost'

不,這是行不通的。

產量

MySQL 在幾個方面都相當靈活,其中之一就是身分驗證方法。 正如您從這篇文章中看到的,根據作業系統用戶,無需密碼即可獲得存取權限。 這在某些情況下很有用,其中之一是使用以下命令從 RDS/Aurora 遷移到常規 MySQL 時: IAM 資料庫身份驗證仍然可以獲得存取權限,但無需密碼。

來源: www.habr.com

添加評論