##思路:MySQL的用户与密码以表记录形式存储,通过修改表记录实现破解密码
#了解MySQL用户及密码存储情况
[root@server51 ~]# mysql -hlocalhost -uroot -p'123123123'
mysql> SHOW TABLES FROM mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
|... |
| user | #MySQL用户信息记录在mysql.user表中
+---------------------------+
31 rows in set (0.00 sec)
#查看mysql.user表结构
mysql> DESC mysql.user;
#查看mysql.user表中符合条件的表记录
mysql> SELECT
-> user,host,authentication_string
-> FROM mysql.user WHERE user='root';
+------+-----------+-------------------------------------------+
| user | host | authentication_string |
+------+-----------+-------------------------------------------+
| root | localhost | *28FA516E8DA40768CBADCFC72E7A6CD4ADA15D1E |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@server51 ~]#
##破解密码(skip_grant_tables)
#修改/etc/my.cnf并重启服务
[root@server51 ~]# ls /etc/my.cnf #MySQL主配置文件
/etc/my.cnf
[root@server51 ~]# vim /etc/my.cnf #编辑配置文件
[root@server51 ~]# sed -rn '4,5p' /etc/my.cnf #新增内容,注意不是删除其他内容!!
[mysqld]
skip_grant_tables #跳过权限认证表格校验
[root@server51 ~]# systemctl restart mysqld #服务正常重启表示配置文件内容正确
[root@server51 ~]#
#登录MySQL服务(无密码)
[root@server51 ~]# mysql
#使用UPDATE语句修改表记录(修改mysql.user表中root用户的authentication_string字段值)
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('123qqq...A')
-> WHERE
-> user='root' AND host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
#刷新授权确保修改生效
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT user,host,authentication_string FROM mysql.user
-> WHERE
-> user='root' AND host='localhost';
+------+-----------+-------------------------------------------+
| user | host | authentication_string |
+------+-----------+-------------------------------------------+
| root | localhost | *F19C699342FA5C91EBCF8E0182FB71470EB2AF30 |
+------+-----------+-------------------------------------------+
1 row in set (0.00 sec)
mysql> exit
Bye
[root@server51 ~]#
#修改配置文件并重启服务
[root@server51 ~]# vim /etc/my.cnf
[root@server51 ~]# sed -rn '4,5p' /etc/my.cnf
[mysqld]
#skip_grant_tables #注释或删除掉skip_grant_tables语句
[root@server51 ~]# systemctl restart mysqld
#确认新密码登录
[root@server51 ~]# mysql #空密码无法登录
[root@server51 ~]# mysql -hlocalhost -uroot -p'123qqq...A' #使用破解密码可登录