原 MySQL安全插件之Connection Control Plugins介绍
Tags: 原创MySQL插件安全登陆失败Connection Control
简介
MySQL 的 Connection-Control Plugins 是一组插件,用于增强数据库的安全性,主要通过控制客户端连接行为来防止恶意访问或减轻潜在的 DDoS 攻击。
包括CONNECTION_CONTROL 和 CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS两个插件,这2个插件使管理员能设置当连续连接失败超过指定次数后,服务端根据配置值增加对连接的相应延迟(延迟验证)。
使用该插件能缓解MySQL用户的暴力破解;
1. 插件分类
Connection-Control Plugins 包括以下两个插件:
- connection_control:负责管理客户端连接的限制功能。
- connection_control_failed_login_attempts:跟踪客户端的失败登录尝试。
2. 主要功能
- 限制客户端连接频率:防止单个客户端频繁建立连接,从而减少资源耗尽的风险。
- 监控登录失败尝试:记录失败登录次数,超过阈值后对客户端采取限制措施(如延迟响应)。
- 增强安全性:减轻暴力破解攻击的影响,通过限制频繁的错误登录行为。
3. 配置参数
1 2 3 4 5 6 7 8 9 10 | mysql> show variables like "connection_control%"; +-------------------------------------------------+------------+ | Variable_name | Value | +-------------------------------------------------+------------+ | connection_control_failed_connections_threshold | 3 | | connection_control_max_connection_delay | 2147483647 | | connection_control_min_connection_delay | 1000 | +-------------------------------------------------+------------+ 3 rows in set (0.01 sec) |
Connection-Control Plugins 提供了一些重要的系统变量,供管理员进行配置:
1. connection_control 系统变量
connection_control_min_connection_delay:定义客户端连接的最小延迟。- 默认值:1000
- 用于防止短时间内大量连接的建立。服务端增加响应延迟的最少时间,单位为毫秒。默认值1000,取值范围 1000–2147483647。
connection_control_max_connection_delay:定义客户端连接的最大延迟。- 默认值:2147483647
- 如果客户端连接频率超出限制,延迟会逐步增加直到达到最大值。服务端增加响应延迟的最大时间,单位为毫秒。取值范围 1000–2147483647。
2. connection_control_failed_login_attempts 系统变量
connection_control_failed_connections_threshold:定义允许的最大登录失败次数。
- 默认值:3
- 超过该阈值时,客户端将受到延迟惩罚。连接失败阈值,当连续连接失败超过该阈值时,服务端会增加连接响应延迟。为0时禁用连接失败计数,服务端永不会增加延迟。该值为非零值N时,连续连接失败的N+1次尝试将会增加响应延迟。默认为3,取值范围0–2147483647。
3.状态变量 Connection_control_delay_generated
服务端增加响应延迟的次数,这是一个全局累计值,当修改connection_control_failed_connections_threshold值后该状态值会重置为零;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | mysql> show status like 'connection_control_delay_generated'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | Connection_control_delay_generated | 13 | +------------------------------------+-------+ 1 row in set (0.00 sec) mysql> set global connection_control_failed_connections_threshold=4; Query OK, 0 rows affected (0.00 sec) mysql> show status like 'connection_control_delay_generated'; +------------------------------------+-------+ | Variable_name | Value | +------------------------------------+-------+ | Connection_control_delay_generated | 0 | +------------------------------------+-------+ 1 row in set (0.00 sec) |
更详细的信息请查询information_schema.connection_control_failed_login_attempts表,表中记录了各用户失败连接的次数,正常连接后记录会被清理;当修改connection_control_failed_connections_threshold值后该表会清空
1 2 3 4 5 6 7 8 9 10 | mysql> select * from information_schema.connection_control_failed_login_attempts; +----------------------+-----------------+ | USERHOST | FAILED_ATTEMPTS | +----------------------+-----------------+ | 'test1'@'localhost' | 1 | | 'test'@'%' | 2 | | 'root'@'localhost' | 2 | | 'teste1'@'localhost' | 6 | +----------------------+-----------------+ 4 rows in set (0.00 sec) |
4. 启用插件
该插件默认未启用需要进行安装
首先确认MySQL插件目录,plugin library文件需位于该路径下。在linux系统下plugin library文件名为connection_control.so
1 2 3 4 5 6 7 8 9 10 11 | mysql> show global variables like 'plugin_dir'; +---------------+--------------------------+ | Variable_name | Value | +---------------+--------------------------+ | plugin_dir | /usr/lib64/mysql/plugin/ | +---------------+--------------------------+ 1 row in set (0.01 sec) mysql> system ls -ltr /usr/lib64/mysql/plugin/ |grep connection_control.so -rwxr-xr-x 1 root root 55104 Sep 18 19:37 connection_control.so |
要启用 Connection-Control Plugins,可以在 MySQL 配置文件或运行时使用以下命令:


