نحوه استفاده از MySQL بدون رمز عبور (و خطرات امنیتی)

نحوه استفاده از MySQL بدون رمز عبور (و خطرات امنیتی)

آنها می گویند بهترین رمز عبور رمزی است که لازم نیست به خاطر بسپارید. در مورد MySQL این به لطف افزونه امکان پذیر است auth_socket و نسخه آن برای MariaDB - یونیکس_سوکت.

هر دوی این افزونه ها اصلاً جدید نیستند؛ در همین وبلاگ در مورد آنها زیاد گفته شده است، مثلاً در مقاله ای درباره نحوه تغییر رمز عبور در MySQL 5.7 با استفاده از افزونه auth_socket. با این حال، در حین بررسی موارد جدید در MariaDB 10.4، متوجه شدم که unix_socket اکنون به صورت پیش فرض نصب شده است و یکی از روش های احراز هویت است ("یکی از"، زیرا در MariaDB 10.4 بیش از یک افزونه برای یک کاربر برای احراز هویت در دسترس است، که در سند توضیح داده شده است "احراز هویت" از MariaDB 10.04).

همانطور که گفتم، این خبر جدیدی نیست و وقتی MySQL را با استفاده از تیم پشتیبانی نصب می‌کنید Debian برای بسته‌های .deb، یک کاربر ریشه برای احراز هویت سوکت ایجاد می‌شود. این موضوع هم برای MySQL و هم برای MariaDB صدق می‌کند.

root@app:~# apt-cache show mysql-server-5.7 | grep -i maintainers
Original-Maintainer: Debian MySQL Maintainers <pkg-mysql-maint@lists.alioth.debian.org>
Original-Maintainer: Debian MySQL Maintainers <<a href="mailto:pkg-mysql-maint@lists.alioth.debian.org">pkg-mysql-maint@lists.alioth.debian.org</a>>

با کیف Debian برای MySQL، کاربر ریشه به صورت زیر احراز هویت می‌شود:

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)

در مورد بسته .deb برای 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)

بسته‌های .deb از مخزن رسمی Percona نیز احراز هویت کاربر ریشه را در سوکت auth-socket و برای سرور Percona پیکربندی می‌کنند. بیایید یک مثال با سرور Percona برای MySQL 8.0.16-7 и 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)

خب، جادوی ماجرا چیست؟ این افزونه بررسی می‌کند که کاربر Linux با استفاده از گزینه سوکت SO_PEERCRED، کاربر MySQL را تطبیق می‌دهد تا اطلاعات مربوط به کاربری که برنامه کلاینت را اجرا می‌کند را جمع‌آوری کند. بنابراین، این افزونه فقط می‌تواند در سیستم‌هایی که از گزینه SO_PEERCRED پشتیبانی می‌کنند، مانند Linuxگزینه سوکت SO_PEERCRED به شما امکان می‌دهد UID فرآیند مرتبط با سوکت را تعیین کنید. سپس فرآیند، نام کاربری مرتبط با آن UID را دریافت می‌کند.

در اینجا یک مثال با کاربر "vagrant" آورده شده است:

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

از آنجایی که هیچ کاربر "ولگرد" در MySQL وجود ندارد، دسترسی به ما ممنوع است. بیایید چنین کاربری ایجاد کنیم و دوباره امتحان کنیم:

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)

اتفاق افتاد!

خب، در مورد غیر- چی؟Debian توزیعی که این به طور پیش‌فرض ارائه نشده است؟ بیایید Percona Server را برای MySQL 8 که روی آن نصب شده است، امتحان کنیم. 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

آزاردهنده. چه چیزی کم بود؟ افزونه بارگیری نشده است:

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

بیایید یک افزونه به فرآیند اضافه کنیم:

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)

حالا همه چیزهایی که نیاز داریم داریم. بیایید دوباره تلاش کنیم:

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)

اکنون می توانید با استفاده از نام کاربری "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)

و دوباره کار کرد!

سوال: آیا می توان با همان ورود به سیستم percona، اما به عنوان یک کاربر متفاوت، وارد سیستم شد؟

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

نه ، این کار نخواهد کرد

نتیجه

MySQL از چندین جنبه کاملاً انعطاف پذیر است که یکی از آنها روش احراز هویت است. همانطور که از این پست می بینید، بر اساس کاربران سیستم عامل بدون رمز عبور می توان دسترسی داشت. این می تواند در سناریوهای خاصی مفید باشد، و یکی از آنها هنگام مهاجرت از RDS/Aurora به MySQL معمولی با استفاده از احراز هویت پایگاه داده IAMتا همچنان دسترسی داشته باشید، اما بدون رمز عبور.

منبع: www.habr.com

خرید هاست قابل اعتماد برای سایت های دارای حفاظت DDoS، سرورهای VPS VDS 🔥 خرید هاستینگ معتبر با محافظت در برابر حملات DDoS، سرورهای VPS و VDS | ProHoster