使用连接控制插件保护MySQL连接安全
2023-05-18 15:56 abce 阅读(766) 评论(0) 编辑 收藏 举报connection_control插件是在MySQL 8.0中引入,并支持向后移植到MySQL 5.7和MySQL 5.6。
在一定次数的连续登录失败尝试后,连接控制插件允许管理员增加服务器对连接的响应延迟。没有得到服务器的响应之前,未经授权的用户或客户端不知道密码是否正确。因此,如果攻击者通过生成多个连接请求来攻击服务器,那么这些连接必须处于活动状态,直到服务器响应为止。引入延迟使攻击者更难攻击,因为现在资源被用于确保连接请求处于活动状态。这种技术可以减缓针对MySQL用户帐户的暴力攻击。
连接控制插件库包含两个插件:
1.connection_control:检查进来的连接尝试,根据需要增加延迟响应。
2.connection_control_failed_login_attempts:information_schema中增加了一个表,记录失败连接的具体信息。
安装连接控制插件
运行时安装
1 2 3 4 | >install plugin connection_control soname 'connection_control.so' ; Query OK, 0 rows affected (0.01 sec) >install plugin connection_control_failed_login_attempts soname 'connection_control.so' ; Query OK, 0 rows affected (0.01 sec) |
也可以在配置文件中加入连接控制插件
1 2 3 4 | [mysqld] plugin- load - add =connection_control.so #加载connection_control.so库 connection -control=FORCE_PLUS_PERMANENT #使用连接控制插件,如果初始化失败,MySQL启动会失败 connection -control-failed-login-attempts=FORCE_PLUS_PERMANENT #使用 connection -control-failed-login-attempts插件,如果初始化失败,MySQL启动会失败 |
检查安装结果
1 2 3 4 5 6 7 8 | > select plugin_name, plugin_status from information_schema.plugins where plugin_name like '%connection%' ; + ------------------------------------------+---------------+ | plugin_name | plugin_status | + ------------------------------------------+---------------+ | CONNECTION_CONTROL | ACTIVE | | CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS | ACTIVE | + ------------------------------------------+---------------+ 2 rows in set (0.00 sec) |
配置连接控制的阈值
现在,使用这些服务器参数为失败的连接尝试配置服务器响应延迟。我们将尝试连续失败连接的阈值设置为3,并添加至少1秒的连接延迟。
1 2 3 | > set global connection_control_failed_connections_threshold = 3;#在增加延迟响应之前,允许连接失败尝试的次数;0表示禁用该属性 set global connection_control_min_connection_delay = 1000; #延迟响应的最小毫秒阈值 set global connection_control_max_connection_delay = 90000;#延迟响应的最大毫秒阈值 |
持久化配置:
1 2 3 | SET PERSIST connection_control_failed_connections_threshold = 3; SET PERSIST connection_control_min_connection_delay = 1000; SET PERSIST connection_control_max_connection_delay = 90000; |
也可以在配置文件中配置:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [mysqld] connection_control_failed_connections_threshold=3 connection_control_min_connection_delay=1000 connection_control_max_connection_delay=90000 >show variables like '%connection_control%' ; + -------------------------------------------------+-------+ | Variable_name | Value | + -------------------------------------------------+-------+ | connection_control_failed_connections_threshold | 3 | | connection_control_max_connection_delay | 90000 | | connection_control_min_connection_delay | 1000 | + -------------------------------------------------+-------+ 3 rows in set (0.00 sec) |
测试过程
第一个终端:
1 2 3 4 5 6 7 8 9 10 11 12 | > select * from information_schema.connection_control_failed_login_attempts; Empty set (0.00 sec) >show global status like 'connection_control_%' ; + ------------------------------------+-------+ | Variable_name | Value | + ------------------------------------+-------+ | Connection_control_delay_generated | 0 | + ------------------------------------+-------+ 1 row in set (0.01 sec) > |
目前是没有延迟响应产生。
在第二个终端,尝试使用错误的密码进行登录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 | # for i in `seq 60`;do time mysql mysql? -uroot -p "try_an_incorrect_password" 2>&1 >/dev/ null | grep real ;done real 0m0.005s user 0m0.005s sys 0m0.000s real 0m0.004s user 0m0.001s sys 0m0.004s real 0m0.003s user 0m0.001s sys 0m0.003s real 0m1.004s user 0m0.004s sys 0m0.000s real 0m2.007s user 0m0.005s sys 0m0.003s real 0m3.006s user 0m0.006s sys 0m0.000s real 0m4.007s user 0m0.008s sys 0m0.000s real 0m5.005s user 0m0.005s sys 0m0.000s real 0m6.005s user 0m0.006s sys 0m0.000s real 0m7.007s user 0m0.004s sys 0m0.003s real 0m8.007s user 0m0.006s sys 0m0.002s real 0m9.005s user 0m0.005s sys 0m0.000s real 0m10.005s user 0m0.005s sys 0m0.000s real 0m11.006s user 0m0.006s sys 0m0.001s real 0m12.006s user 0m0.004s sys 0m0.003s real 0m13.006s user 0m0.006s sys 0m0.000s real 0m14.005s user 0m0.000s sys 0m0.006s real 0m15.006s user 0m0.001s sys 0m0.006s real 0m16.006s user 0m0.003s sys 0m0.003s real 0m17.006s user 0m0.006s sys 0m0.000s real 0m18.006s user 0m0.006s sys 0m0.000s real 0m19.005s user 0m0.001s sys 0m0.004s real 0m20.006s user 0m0.007s sys 0m0.000s real 0m21.005s user 0m0.005s sys 0m0.000s real 0m22.005s user 0m0.001s sys 0m0.005s real 0m23.005s user 0m0.001s sys 0m0.005s real 0m24.009s user 0m0.008s sys 0m0.002s real 0m25.006s user 0m0.004s sys 0m0.003s real 0m26.005s user 0m0.005s sys 0m0.000s real 0m27.005s user 0m0.004s sys 0m0.002s real 0m28.004s user 0m0.005s sys 0m0.000s real 0m29.005s user 0m0.005s sys 0m0.000s real 0m30.005s user 0m0.005s sys 0m0.000s real 0m31.006s user 0m0.006s sys 0m0.000s real 0m32.004s user 0m0.005s sys 0m0.000s real 0m33.006s user 0m0.004s sys 0m0.003s real 0m34.005s user 0m0.005s sys 0m0.000s real 0m35.006s user 0m0.006s sys 0m0.000s real 0m36.006s user 0m0.003s sys 0m0.004s real 0m37.005s user 0m0.005s sys 0m0.000s real 0m38.005s user 0m0.001s sys 0m0.004s real 0m39.005s user 0m0.000s sys 0m0.006s real 0m40.006s user 0m0.006s sys 0m0.000s real 0m41.006s user 0m0.006s sys 0m0.000s real 0m42.008s user 0m0.008s sys 0m0.001s real 0m43.009s user 0m0.002s sys 0m0.008s real 0m44.006s user 0m0.006s sys 0m0.000s real 0m45.005s user 0m0.005s sys 0m0.000s real 0m46.007s user 0m0.003s sys 0m0.005s real 0m47.007s user 0m0.007s sys 0m0.001s real 0m48.006s user 0m0.006s sys 0m0.000s real 0m49.006s user 0m0.005s sys 0m0.002s real 0m50.005s user 0m0.005s sys 0m0.000s real 0m51.006s user 0m0.003s sys 0m0.003s real 0m52.006s user 0m0.006s sys 0m0.000s real 0m53.006s user 0m0.006s sys 0m0.000s real 0m54.008s user 0m0.005s sys 0m0.004s real 0m55.006s user 0m0.006s sys 0m0.000s real 0m56.009s user 0m0.006s sys 0m0.004s real 0m57.006s user 0m0.005s sys 0m0.002s |
查看mysql中的进程,可以看到,连接的状态是"Waiting in connection_control plugin"
1 2 3 4 5 6 7 8 | >show processlist; + ----+-----------------+-----------+------+---------+------+--------------------------------------+------------------+---------+-----------+---------------+ | Id | User | Host | db | Command | Time | State | Info | Time_ms | Rows_sent | Rows_examined | + ----+-----------------+-----------+------+---------+------+--------------------------------------+------------------+---------+-----------+---------------+ | 5 | event_scheduler | localhost | NULL | Daemon | 323 | Waiting on empty queue | NULL | 323577 | 0 | 0 | | 8 | root | localhost | NULL | Query | 0 | init | show processlist | 0 | 0 | 0 | | 27 | root | localhost | NULL | Connect | 1 | Waiting in connection_control plugin | NULL | 1935 | 0 | 0 | + ----+-----------------+-----------+------+---------+------+--------------------------------------+------------------+---------+-----------+---------------+ |
三个连接之后的连接,开始经过一段时间的响应延迟。直至尝试结束。每个尝试的延迟都会增加一秒。
第二个终端的脚本运行结束后,回到第一个终端:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | >show global status like 'connection_control_%' ; + ------------------------------------+-------+ | Variable_name | Value | + ------------------------------------+-------+ | Connection_control_delay_generated | 57 | + ------------------------------------+-------+ 1 row in set (0.00 sec) > select failed_attempts from information_schema.connection_control_failed_login_attempts; + -----------------+ | failed_attempts | + -----------------+ | 60 | + -----------------+ 1 row in set (0.00 sec) |
多次错误尝试失败后,如果一个正确的密码尝试连接进来,因为之前已经有N个尝试失败,正确的连接也需要等待N秒的延迟。
1 2 3 4 5 6 7 8 9 | # date ; mysql -uroot -p 'xxx' -e "select now();" ; date Thu 18 May 2023 02:25:09 PM CST mysql: [Warning] Using a password on the command line interface can be insecure. + ---------------------+ | now() | + ---------------------+ | 2023-05-18 14:26:08 | + ---------------------+ Thu 18 May 2023 02:26:08 PM CST |
接下来正确的连接就不用延迟响应了:
1 2 3 4 5 6 7 8 9 | # date ; mysql -uroot -p 'xxx' -e "select now();" ; date Thu 18 May 2023 02:27:07 PM CST mysql: [Warning] Using a password on the command line interface can be insecure. + ---------------------+ | now() | + ---------------------+ | 2023-05-18 14:27:07 | + ---------------------+ Thu 18 May 2023 02:27:07 PM CST |
找出是哪个用户在尝试暴力破解登录
1 2 3 4 5 6 7 | > select * from information_schema.connection_control_failed_login_attempts; + --------------------+-----------------+ | USERHOST | FAILED_ATTEMPTS | + --------------------+-----------------+ | 'root' @ 'localhost' | 60 | + --------------------+-----------------+ 1 row in set (0.01 sec) |
重新设置阈值
如果想重新设置这些计数器,重新给connection_control_failed_connections_threshold设置一个值即可:
1 2 3 4 5 6 7 8 9 10 11 12 13 | > SET GLOBAL connection_control_failed_connections_threshold = 3; Query OK, 0 rows affected (0.00 sec) > select * from information_schema.connection_control_failed_login_attempts; Empty set (0.00 sec) >show global status like 'connection_control_%' ; + ------------------------------------+-------+ | Variable_name | Value | + ------------------------------------+-------+ | Connection_control_delay_generated | 0 | + ------------------------------------+-------+ 1 row in set (0.00 sec) |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2022-05-18 SQL Server AlwaysOn的监听
2022-05-18 SQLServer AlwaysON修改可用性组的监听端口
2022-05-18 SQLServer修改Availability Group Endpoint的属主
2022-05-18 SQLServer将数据库置为只读