Ako používať MySQL bez hesla (a bezpečnostných rizík)

Ako používať MySQL bez hesla (a bezpečnostných rizík)

Hovorí sa, že najlepšie heslo je to, ktoré si nemusíte pamätať. V prípade MySQL je to možné vďaka pluginu auth_socket a jeho verzia pre MariaDB - zásuvka unix.

Oba tieto pluginy nie sú vôbec nové, na tom istom blogu sa o nich popísalo veľa, napríklad v článku o ako zmeniť heslá v MySQL 5.7 pomocou doplnku auth_socket. Keď som sa však pozrel na to, čo je nové v MariaDB 10.4, zistil som, že unix_socket je teraz predvolene nainštalovaný a je jednou z metód autentifikácie („jedna z“, pretože v MariaDB 10.4 je k dispozícii viac ako jeden doplnok pre jedného používateľa na overenie, ktorý je vysvetlené v dokumente "Autentifikácia" z MariaDB 10.04).

Ako som povedal, toto nie je novinka a pri inštalácii MySQL pomocou balíkov .deb podporovaných tímom Debianu sa vytvorí užívateľ root na autentifikáciu soketu. To platí pre MySQL aj 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>>

S balíčkami Debianu pre MySQL je používateľ root overený nasledovne:

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)

To isté platí pre balík .deb pre MariaDB:

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)

Balíky .deb z oficiálneho úložiska Percona tiež konfigurujú autentifikáciu užívateľa root pod auth-socket a pre Percona Server. Uveďme príklad s Percona Server pre MySQL 8.0.16-7 a 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)

V čom je teda kúzlo? Doplnok skontroluje, či sa používateľ systému Linux zhoduje s používateľom MySQL pomocou možnosti soketu SO_PEERCRED na zhromažďovanie informácií o používateľovi, ktorý spúšťa klientsky program. Doplnok je teda možné použiť iba na systémoch, ktoré podporujú možnosť SO_PEERCRED, ako je napríklad Linux. Voľba soketu SO_PEERCRED vám umožňuje zistiť uid procesu spojeného so soketom. A potom už dostane používateľské meno spojené s týmto uid.

Tu je príklad s používateľom „vagrant“:

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

Keďže v MySQL nie je žiadny „nepotulný“ používateľ, je nám odmietnutý prístup. Vytvorme takého používateľa a skúste to znova:

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)

Stalo!

A čo distribúcia mimo Debianu, kde to nie je štandardne poskytované? Skúsme Percona Server pre MySQL 8 nainštalovaný na CentOS 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

Blbosť. čo chýbalo? Plugin nie je načítaný:

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

Pridajme do procesu plugin:

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)

Teraz máme všetko, čo potrebujeme. Skúsme to opäť:

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)

Teraz sa môžete prihlásiť pomocou používateľského mena „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)

A opäť to fungovalo!

Otázka: bude možné sa prihlásiť do systému pod rovnakým prihlasovacím menom percona, ale ako iný používateľ?

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

Nie, nebude to fungovať.

Výkon

MySQL je pomerne flexibilný v niekoľkých aspektoch, jedným z nich je metóda autentifikácie. Ako môžete vidieť z tohto príspevku, prístup je možné získať bez hesiel na základe používateľov OS. To môže byť užitočné v určitých scenároch a jedným z nich je migrácia z RDS/Aurora na bežné MySQL pomocou Autentifikácia databázy IAMstále získať prístup, ale bez hesiel.

Zdroj: hab.com

Pridať komentár