パスワードなしで MySQL を使用する方法 (およびセキュリティリスク)

パスワードなしで MySQL を使用する方法 (およびセキュリティリスク)

最良のパスワードは、覚える必要のないパスワードであると言われています。 MySQL の場合、プラグインのおかげでこれが可能になります。 認証ソケット MariaDB のバージョンとそのバージョン - unix_socket.

これらのプラグインは両方ともまったく新しいものではありません。これらについては、この同じブログで多くのことが述べられています。たとえば、 auth_socket プラグインを使用して MySQL 5.7 でパスワードを変更する方法。 しかし、MariaDB 10.4 の新機能を調べているときに、unix_socket がデフォルトでインストールされ、認証方法の 10.4 つ (「のうちの XNUMX つ」。MariaDB XNUMX では XNUMX 人のユーザーが認証に複数のプラグインを使用できるため) であることがわかりました。文書で説明されています 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 サーバーの root ユーザー認証も構成します。 例を挙げてみましょう MySQL 8.0.16-7 用 Percona サーバー および 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 ユーザーと一致することを確認します。 したがって、プラグインは、Linux などの SO_PEERCRED オプションをサポートするシステムでのみ使用できます。 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 にインストールされた MySQL 7 用 Percona Server を試してみましょう。

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 はいくつかの点で非常に柔軟であり、そのうちの XNUMX つは認証方法です。 この投稿からわかるように、OS ユーザーに応じてパスワードなしでアクセスできます。 これは特定のシナリオで役立つ可能性があり、その XNUMX つは、RDS/Aurora から通常の MySQL に移行する場合です。 IAMデータベース認証引き続きアクセスできますが、パスワードは必要ありません。

出所: habr.com

コメントを追加します