Comment utiliser MySQL sans mot de passe (et risques de sécurité)

Comment utiliser MySQL sans mot de passe (et risques de sécurité)

On dit que le meilleur mot de passe est celui dont vous ne devez pas vous souvenir. Dans le cas de MySQL, cela est possible grâce au plugin auth_socket et sa version pour MariaDB - unix_socket.

Ces deux plugins ne sont pas nouveaux du tout ; on en a beaucoup parlé dans ce même blog, par exemple dans l'article sur comment changer les mots de passe dans MySQL 5.7 à l'aide du plugin auth_socket. Cependant, en examinant les nouveautés de MariaDB 10.4, j'ai découvert qu'unix_socket est désormais installé par défaut et est l'une des méthodes d'authentification (« l'une des », car dans MariaDB 10.4, plus d'un plugin est disponible pour un utilisateur pour l'authentification, ce qui est expliqué dans le document "Authentification" de MariaDB 10.04).

Comme je l'ai dit, ce n'est pas une nouveauté, et lors de l'installation de MySQL à l'aide des packages .deb pris en charge par l'équipe Debian, un utilisateur root est créé pour l'authentification par socket. Cela est vrai pour MySQL et 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>>

Avec les packages Debian pour MySQL, l'utilisateur root est authentifié comme suit :

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)

Il en va de même avec le package .deb pour 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)

Les packages .deb du référentiel officiel Percona configurent également l'authentification de l'utilisateur root sous auth-socket et pour Percona Server. Donnons un exemple avec Serveur Percona pour MySQL 8.0.16-7 et 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)

Alors, quelle est la magie ? Le plugin vérifie que l'utilisateur Linux correspond à l'utilisateur MySQL à l'aide de l'option de socket SO_PEERCRED pour collecter des informations sur l'utilisateur exécutant le programme client. Ainsi, le plugin ne peut être utilisé que sur les systèmes prenant en charge l'option SO_PEERCRED, comme Linux. L'option socket SO_PEERCRED permet de connaître l'uid du processus associé à la socket. Et puis il reçoit déjà le nom d'utilisateur associé à cet uid.

Voici un exemple avec l'utilisateur "vagrant" :

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

Puisqu'il n'y a pas d'utilisateur « vagabond » dans MySQL, l'accès nous est refusé. Créons un tel utilisateur et réessayons :

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)

Il s'est avéré!

Eh bien, qu'en est-il d'une distribution non-Debian où cela n'est pas fourni par défaut ? Essayons Percona Server pour MySQL 8 installé sur 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

Déception. Ce qui manquait? Plugin non chargé :

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

Ajoutons un plugin au processus :

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)

Maintenant, nous avons tout ce dont nous avons besoin. Essayons encore:

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)

Vous pouvez maintenant vous connecter en utilisant le nom d'utilisateur « 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)

Et ça a encore fonctionné !

Question : sera-t-il possible de se connecter au système sous le même identifiant percona, mais en tant qu'utilisateur différent ?

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

Non, ça ne marchera pas.

conclusion

MySQL est assez flexible sur plusieurs aspects, dont la méthode d'authentification. Comme vous pouvez le voir dans cet article, l'accès peut être obtenu sans mot de passe, en fonction des utilisateurs du système d'exploitation. Cela peut être utile dans certains scénarios, notamment lors de la migration de RDS/Aurora vers MySQL standard à l'aide de Authentification de la base de données IAMpour toujours y accéder, mais sans mots de passe.

Source: habr.com

Ajouter un commentaire