كيفية استخدام MySQL بدون كلمة مرور (ومخاطر الأمان)


يقولون أن أفضل كلمة مرور لا تحتاج إلى تذكرها. في حالة MySQL ، هذا حقيقي بفضل المكون الإضافي auth_socket وإصداره لـ MariaDB - unix_socket .


كلا هذين الملحقين ليسا بجديد على الإطلاق ، فقد تمت مناقشتهما كثيرًا في نفس المدونة ، على سبيل المثال ، في مقال حول كيفية تغيير كلمات المرور في MySQL 5.7 باستخدام البرنامج المساعد auth_socket . ومع ذلك ، عند تحليل ما هو جديد في MariaDB 10.4 ، وجدت أن unix_socket مثبت الآن بشكل افتراضي وهو أحد أساليب المصادقة ("واحد من" ، لأنه في MariaDB 10.4 يتوفر أكثر من مكون إضافي للمصادقة لمستخدم واحد ، وهو موضح في المستند "المصادقة" من MariaDB 10.04 ).


كما قلت ، هذه ليست أخبارًا ، وعندما تقوم بتثبيت MySQL باستخدام حزم .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>> 

مع حزم دبيان لـ 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 الرسمي أيضًا بتكوين مصادقة المستخدم الجذر ضمن مأخذ مصادقة خادم Percona Server أيضًا. فيما يلي مثال على Percona Server for 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 يطابق مستخدم MySQL باستخدام خيار مأخذ توصيل SO_PEERCRED - لجمع معلومات حول المستخدم الذي يقوم بتشغيل برنامج العميل. وبالتالي ، لا يمكن استخدام المكون الإضافي إلا على الأنظمة التي تدعم خيار SO_PEERCRED ، مثل Linux. يسمح لك خيار مأخذ التوصيل SO_PEERCRED بالتعرف على معرف المستخدم للعملية المرتبطة بالمقبس. وبعد ذلك ، حصل بالفعل على اسم المستخدم المرتبط بهذا المعرف.


هنا مثال مع المستخدم "المتشرد":


 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) 

اتضح!


حسنًا ، ماذا عن توزيع غير دبيان حيث لا يتم توفير ذلك افتراضيًا؟ دعنا نجرب Percona Server for 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 للاستمرار في الوصول ، ولكن بدون كلمات مرور.

Source: https://habr.com/ru/post/ar478930/


All Articles