Sut i Ddefnyddio MySQL Heb Gyfrinair (a Risgiau Diogelwch)

Sut i Ddefnyddio MySQL Heb Gyfrinair (a Risgiau Diogelwch)

Maen nhw'n dweud mai'r cyfrinair gorau yw'r un nad oes rhaid i chi ei gofio. Yn achos MySQL, mae hyn yn bosibl diolch i'r ategyn awd_soced a'i fersiwn ar gyfer MariaDB - unix_soced.

Nid yw'r ddau ategyn hyn yn newydd o gwbl; mae llawer wedi'i ddweud amdanynt yn yr un blog hwn, er enghraifft yn yr erthygl am sut i newid cyfrineiriau yn MySQL 5.7 gan ddefnyddio auth_socket plugin. Fodd bynnag, wrth edrych i mewn i'r hyn sy'n newydd yn MariaDB 10.4, darganfyddais fod unix_socket bellach wedi'i osod yn ddiofyn a'i fod yn un o'r dulliau dilysu (“un o”, oherwydd yn MariaDB 10.4 mae mwy nag un ategyn ar gael i un defnyddiwr i'w ddilysu, sy'n cael ei esbonio yn y ddogfen "Dilysu" gan MariaDB 10.04).

Fel y dywedais, nid yw hyn yn newyddion, ac wrth osod MySQL gan ddefnyddio'r pecynnau .deb a gefnogir gan dîm Debian, crëir defnyddiwr gwraidd ar gyfer dilysu soced. Mae hyn yn wir am MySQL a 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>>

Gyda phecynnau Debian ar gyfer MySQL, mae'r defnyddiwr gwraidd wedi'i ddilysu fel a ganlyn:

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)

Mae'r un peth yn wir gyda'r pecyn .deb ar gyfer 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)

Mae'r pecynnau .deb o'r ystorfa Percona swyddogol hefyd yn ffurfweddu dilysiad defnyddiwr gwraidd o dan auth-socket ac ar gyfer Percona Server. Gadewch i ni roi enghraifft gyda Gweinydd Percona ar gyfer 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)

Felly beth yw'r hud? Mae'r ategyn yn gwirio bod y defnyddiwr Linux yn cyfateb i'r defnyddiwr MySQL gan ddefnyddio'r opsiwn soced SO_PEERCRED i gasglu gwybodaeth am y defnyddiwr sy'n rhedeg y rhaglen cleient. Felly, dim ond ar systemau sy'n cefnogi'r opsiwn SO_PEERCRED y gellir defnyddio'r ategyn, fel Linux. Mae'r opsiwn soced SO_PEERCRED yn eich galluogi i ddarganfod uid y broses sy'n gysylltiedig â'r soced. Ac yna mae eisoes yn derbyn yr enw defnyddiwr sy'n gysylltiedig â'r uid hwn.

Dyma enghraifft gyda'r defnyddiwr "crwydrol":

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

Gan nad oes defnyddiwr "crwydrol" yn MySQL, ni chaniateir mynediad i ni. Gadewch i ni greu defnyddiwr o'r fath a cheisio eto:

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)

Digwyddodd!

Wel, beth am ddosbarthiad nad yw'n Debian lle na ddarperir hwn yn ddiofyn? Gadewch i ni roi cynnig ar Percona Server ar gyfer MySQL 8 wedi'i osod ar 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

Bummer. Beth oedd ar goll? Nid yw'r ategyn wedi'i lwytho:

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

Gadewch i ni ychwanegu ategyn i'r broses:

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)

Nawr mae gennym bopeth sydd ei angen arnom. Gadewch i ni geisio eto:

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)

Gallwch nawr fewngofnodi gan ddefnyddio'r enw defnyddiwr "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)

Ac fe weithiodd eto!

Cwestiwn: a fydd hi'n bosibl mewngofnodi i'r system o dan yr un mewngofnodi percona, ond fel defnyddiwr gwahanol?

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

Na, ni fydd yn gweithio.

Allbwn

Mae MySQL yn eithaf hyblyg mewn sawl agwedd, ac un ohonynt yw'r dull dilysu. Fel y gwelwch o'r swydd hon, gellir cael mynediad heb gyfrineiriau, yn seiliedig ar ddefnyddwyr OS. Gall hyn fod yn ddefnyddiol mewn rhai senarios, ac un ohonynt yw wrth fudo o RDS/Aurora i ddefnyddio MySQL yn rheolaidd Dilysu cronfa ddata IAMi gael mynediad o hyd, ond heb gyfrineiriau.

Ffynhonnell: hab.com

Ychwanegu sylw