Como usar MySQL sen contrasinal (e riscos de seguridade)

Como usar MySQL sen contrasinal (e riscos de seguridade)

Din que o mellor contrasinal é aquel que non tes que lembrar. No caso de MySQL isto é posible grazas ao complemento auth_socket e a súa versión para MariaDB - unix_socket.

Estes dous complementos non son nada novos, xa se comentaron moito neste blog, por exemplo no artigo sobre como cambiar contrasinais en MySQL 5.7 usando o complemento auth_socket. Non obstante, mentres investigaba as novidades de MariaDB 10.4, descubrín que unix_socket agora está instalado de forma predeterminada e é un dos métodos de autenticación ("un dos", porque en MariaDB 10.4 hai máis dun complemento dispoñible para un usuario para a súa autenticación, o cal explícase no documento "Autenticación" de MariaDB 10.04).

Como dixen, isto non é novidade, e ao instalar MySQL usando os paquetes .deb soportados polo equipo de Debian, créase un usuario root para a autenticación do socket. Isto é certo tanto para MySQL como para 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>>

Cos paquetes de Debian para MySQL, o usuario root autenticarase do seguinte xeito:

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)

O mesmo ocorre co paquete .deb para 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)

Os paquetes .deb do repositorio oficial de Percona tamén configuran a autenticación do usuario root baixo auth-socket e para o servidor Percona. Poñamos un exemplo con Servidor Percona para MySQL 8.0.16-7 e 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)

Entón, cal é a maxia? O complemento comproba que o usuario de Linux coincide co usuario de MySQL mediante a opción de socket SO_PEERCRED para recoller información sobre o usuario que executa o programa cliente. Así, o complemento só se pode usar en sistemas que admiten a opción SO_PEERCRED, como Linux. A opción de socket SO_PEERCRED permítelle coñecer o uid do proceso asociado ao socket. E entón xa recibe o nome de usuario asociado a este uid.

Aquí tes un exemplo co usuario "vagrant":

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

Dado que non hai ningún usuario "vagabundo" en MySQL, negamos o acceso. Imos crear un usuario deste tipo e ténteo de novo:

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)

Pasou!

Ben, que pasa cunha distribución non Debian onde isto non se proporciona por defecto? Probemos Percona Server para MySQL 8 instalado en 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

Caramba. Que faltaba? Plugin non cargado:

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

Engademos un complemento ao proceso:

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)

Agora temos todo o que necesitamos. Imos tentalo de novo:

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)

Agora podes iniciar sesión co nome de usuario "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)

E volveu funcionar!

Pregunta: será posible iniciar sesión no sistema co mesmo inicio de sesión percona, pero como usuario diferente?

[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, non vai funcionar.

Saída

MySQL é bastante flexible en varios aspectos, un dos cales é o método de autenticación. Como podes ver nesta publicación, pódese acceder sen contrasinais, en función dos usuarios do sistema operativo. Isto pode ser útil en certos escenarios, e un deles é cando se migra de RDS/Aurora a MySQL normal usando Autenticación de base de datos IAMpara acceder aínda, pero sen contrasinais.

Fonte: www.habr.com

Engadir un comentario