前戲介紹
最近要給一些互聯網企業的技術人員進行安全攻防方面的培訓,想著今天要把我們自己的
攻防演練的靶場搭建起來,提前備課,習慣養成-再熟悉的課程也會在培訓之前進行備課(感覺在誇自己認真負責了, 自己都不好意思了),但是遇到了坑,原來靶場是使用的php5.0,mysql應該是5.5的一直正常,但是這次搭好了之後,靶場頁面無法打開,apache日誌始終報一個錯誤:連接mysql數據時提示錯誤: “ERROR 1698 (28000): Access denied for user ‘root’@’localhost'”,網上眾說紛紜,很多方法都不靈,有很多隻告訴了方法,而且還不好使,都是一知半解 ,沒有分析問題的原因的,所以,經過自虐了好長一段時間,終於算是找到根上了,特將此坑分享給大家,希望大家少踩坑。
畢竟小編現在不是專職開發,如果對您有用,請幫忙點贊關注,如果你是開發的大神,估計這個坑你已經踩過了,請放過小編。
環境介紹:
操作系統:Linux kali 4.15.0-kali2-amd64 #1 SMP Debian 4.15.11-1kali1 (2018-03-21) x86_64 GNU/Linux
PHP版本:PHP 7.2.4-1 (cli) (built: Apr 5 2018 08:50:27) ( NTS )
數據庫:mysql Ver 15.1 Distrib 10.1.29-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
科普 MariaDB 和Mysql 啥關係,版本有啥區別:
MariaDB 和 mysql啥關係: MySQL之父Widenius先生離開了Sun之後,覺得依靠Sun/Oracle來發展MySQL,實在很不靠譜,於是決定另開分支,這個分支的名字叫做MariaDB。
MariaDB跟MySQL在絕大多數方面是兼容的,對於開發者來說,幾乎感覺不到任何不同。目前MariaDB是發展最快的MySQL分支版本,新版本發佈速度已經超過了Oracle官方的MySQL版本。
MariaDB 是一個採用Aria存儲引擎的MySQL分支版本,是由原來 MySQL 的作者Michael Widenius創辦的公司所開發的免費開源的數據庫服務器。
版本區別:這個項目的更多的代碼都改編於 MySQL 6.0,例如 “pool of threads”功能提供解決多數據連接問題。MariaDB 5.1.41 RC可以到這裡下載,32位和64位已編譯Linux版本,還包括源代碼包。MariaDB基於GPL 2.0發佈。
所以對於大部分的MySQL用戶來說,從現在主流的MySQL轉到MariaDB應該是沒有什麼難度的。
以上內容是提各位看官百度而來,更加詳細的內容,請自行百度,啥?為啥不直接給連接,讓「自行百度」,因為頭條不允許外部鏈接,回答完畢 ‘(*>﹏
問題現象就是:
web頁面報錯:「Could not connect:」
web服務器日誌/var/log/apache2/error.log 中顯示如下錯誤:
[Tue Jun 05 06:12:55.420068 2018] [php7:warn] [pid 9648] [client 172.16.155.1:56318] PHP Warning: mysqli_connect(): (HY000/1698): Access denied for user 'root'@'localhost' in /var/www/html/sys/config.php on line 7
在系統上,使用root帳號登錄mysql是沒有問題的,可以正常登錄,具體操作如下:
root@kali:/var/www/html# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 66
Server version: 10.1.29-MariaDB-6 Debian buildd-unstable
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
如果輸入mysql -h 127.0.0.1 -uroot -p,就加入了一個-h的參數指定服務器的地址,就會禁止登錄(有看官會問,為啥要這樣做,其實就是為了模擬web服務器連接數據庫的過程,所以你應該懂得)
root@kali:/var/www/html# mysql -h 127.0.0.1 -uroot -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
數據庫的用戶名稱是root,密碼為空
原因分析:
為了找到這個問題:查看了數據庫用戶的信息,確定了兩個關鍵的字段;
MariaDB [(none)]> select user,plugin from mysql.user;
+------+-------------+
| user | plugin |
+------+-------------+
| root | unix_socket |
| wt | |
+------+-------------+
2 rows in set (0.00 sec)
這個裡面非常關鍵的信息就是在數據庫用戶的表裡有一個列屬性是plugin, mysql從5.5.7開始引入plugins 以進行用戶連接時的密碼驗證,plugin創建外部/代理用戶。而此處的MariaDB至少相當於Mysql6.0 以上, mysql官網上原文如下:
Plugins for authenticating attempts by clients to connect to MySQL Server. Plugins are available for several authentication protocols.
進一步查看相關內容得到:
Plugin主要提供了三中方法:unix_socket、mysql_native_password、mysql_old_password(在MySQL 5.7.5版本已經被廢除掉了)
解決辦法:
其中mysql_native_password是指使用mysql數據庫中的user表裡的用戶密碼進行驗證,unix_socket 是socket 鏈接。 所以需要將root用戶的plugin 值由unix_socket 改為mysql_native_password,然後就flush privileges ,重啟mysql服務,果然ok了。
具體過程如下:
root@kali:/var/www/html# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 69
Server version: 10.1.29-MariaDB-6 Debian buildd-unstable
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> update user set plugin='mysql_native_password' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> select user,plugin from user;
+------+-----------------------+
| user | plugin |
+------+-----------------------+
| root | mysql_native_password |
| wt | |
+------+-----------------------+
2 rows in set (0.00 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> exit
Bye
root@kali:/var/www/html# service mysql restart
root@kali:/var/www/html#
再次打開網頁,Happy,Happy,我又看到了熟悉的界面
閱讀更多 Web安全陪跑團 的文章