Kako uporabljati MySQL brez gesla (in varnostna tveganja)

Kako uporabljati MySQL brez gesla (in varnostna tveganja)

Pravijo, da je najboljše geslo tisto, ki si ga ni treba zapomniti. V primeru MySQL je to mogoče zaradi vtičnika auth_socket in njegova različica za MariaDB - unix_socket.

Oba vtičnika sploh nista nova; o njiju je bilo že veliko povedanega v tem istem blogu, na primer v članku o kako spremeniti gesla v MySQL 5.7 z uporabo vtičnika auth_socket. Toda med raziskovanjem, kaj je novega v MariaDB 10.4, sem odkril, da je unix_socket zdaj privzeto nameščen in je eden od načinov preverjanja pristnosti (»eden od«, ker je v MariaDB 10.4 več kot en vtičnik na voljo enemu uporabniku za preverjanje pristnosti, kar je razloženo v dokumentu "Avtentikacija" iz MariaDB 10.04).

Kot sem rekel, to ni novica in pri namestitvi MySQL z uporabo paketov .deb, ki jih podpira ekipa Debian, se ustvari korenski uporabnik za preverjanje pristnosti vtičnice. To velja za MySQL in 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>>

Pri paketih Debian za MySQL je korenski uporabnik overjen na naslednji način:

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)

Enako velja za paket .deb za 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)

Paketi .deb iz uradnega repozitorija Percona prav tako konfigurirajo preverjanje pristnosti korenskega uporabnika pod auth-socket in za strežnik Percona. Navedimo primer z Strežnik Percona za MySQL 8.0.16-7 in 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 čem je torej čar? Vtičnik preveri, ali se uporabnik Linuxa ujema z uporabnikom MySQL z uporabo možnosti vtičnice SO_PEERCRED za zbiranje informacij o uporabniku, ki izvaja odjemalski program. Tako je vtičnik mogoče uporabljati le v sistemih, ki podpirajo možnost SO_PEERCRED, kot je Linux. Možnost vtičnice SO_PEERCRED vam omogoča, da ugotovite uid procesa, povezanega z vtičnico. In potem že prejme uporabniško ime, povezano s tem uid.

Tukaj je primer z uporabnikom "vagrant":

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

Ker v MySQL ni "potepuškega" uporabnika, nam je dostop onemogočen. Ustvarimo takega uporabnika in poskusimo 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)

Zgodilo se!

No, kaj pa distribucija, ki ni Debian, kjer to ni privzeto na voljo? Poskusimo Percona Server za MySQL 8, nameščen 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

Neumnost. Kaj je manjkalo? Vtičnik ni naložen:

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

V postopek dodamo vtičnik:

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)

Zdaj imamo vse, kar potrebujemo. Poskusimo znova:

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)

Zdaj se lahko prijavite z uporabniškim imenom “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)

In spet je delovalo!

Vprašanje: ali se bo možno prijaviti v sistem pod isto percona prijavo, vendar kot drug uporabnik?

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

Ne, ne bo šlo.

Izhod

MySQL je precej prilagodljiv v več vidikih, eden od njih je metoda avtentikacije. Kot lahko vidite iz te objave, je dostop mogoče pridobiti brez gesel glede na uporabnike OS. To je lahko uporabno v določenih scenarijih, eden od njih pa je pri selitvi iz RDS/Aurora na običajni MySQL z uporabo Preverjanje pristnosti baze podatkov IAMda še vedno pridobite dostop, vendar brez gesel.

Vir: www.habr.com

Dodaj komentar