MySQL 5.6.35 开始提供Connnection Control 插件;

如果客户端在连续失败登陆一定次数后,那么此插件可以给客户端后续登陆行为的响应增加一个延迟。该插件可以防止恶意暴力破解MySQL账户。该插件包含以下2个组件:

插件的安装与卸载

安装可以通过配置文件静态安装,也可以在MySQL中动态安装。

  1. [mysqld]
  2. plugin-load-add = connection_control.so

动态安装

卸载

  1. -- 插件卸载
  2. UNINSTALL PLUGIN CONNECTION_CONTROL;
  3. UNINSTALL PLUGIN CONNECTION_CONTROL_FAILED_LOGIN_ATTEMPTS;

更多关于插接件安装/卸载的信息

插件参数

  • connection_control_failed_connections_threshold:失败登陆次数达到此值后触发延迟。
    • 值域:[0, INT_MAX32(2147483647)],0表示关闭此功能。
    • 默认值:3
  • connection_control_max_connection_delay:登陆发生延迟时,延迟的最大时间;此值必须大于等于connection_control_min_connection_delay
    • 值域:[1, INT_MAX32(2147483647)]
    • 默认值:INT_MAX32
    • 单位:毫秒
  • connection_control_min_connection_delay:登陆发生延迟时,延迟的最小时间,此值必须小于等于connection_control_max_connection_delay
    • 值域:[1000, INT_MAX32(2147483647)]
    • 默认值:1000
    • 单位:毫秒
  • Connection Control 插件通过订阅MYSQL_AUDIT_CONNECTION_CLASSMASK 来处理 MYSQL_AUDIT_CONNECTION_CONNECT(完成认证后触发)和MYSQL_AUDIT_CONNECTION_CHANGE_USER(完成COM_CHANGE_USER RPC后触发)子事件;通过这两种子事件的处理来检查给客户端发送回包时是否需要延迟。
  • Connection Control 插件通过 LF hash来存储不同账户的失败登陆信息。LF hash中的key为user@host **,这里的userhost**将遵循以下条件:
    • 如果在MySQL的security context有proxy user信息,那么这个信息将用于userhost
    • 否则,查看security context是否有priv_user 和 priv_host信息,如果有则用于userhost
    • 否则,将security context中已经连接的user 和 host信息用于userhost
  • LF hash的更新:对于每次失败的登陆通过user@host **的key值对其value加1;对于每次成功的登陆,如果需要延迟,处理完延迟后将user@host **从LF hash中删除。
  • 为什么在达到connection_control_failed_connections_threshold失败登陆次数后的第一次成功登陆需要延迟?
    • 这其实还是出于对攻击者开销的考虑;如果成功登陆后马上返回,不需要延迟,那么攻击者就可以使用更少的连接数,进一步攻击者所消耗的资源就会更少;为了增加攻击者的开销,在连续失败登陆后的第一次成功登陆,还是会产生延迟。
  • 具体延迟的时间如何计算?
    • 一旦连续的失败登陆次数超过设定阈值,那么就会产生延迟,并且延迟随着失败次数增加而增加,上限为connection_control_max_connection_delay;具体的计算方式如下:
    • MIN ( MAX((failed_attempts - threshold), MIN_DELAY), MAX_DELAY)

主要处理流程如下:

下面我们主要看一下最终Connection Control插件是怎么处理连接事件的。

  1. /**
  2. @brief Handle a connection event and if requried,
  3. wait for random amount of time before returning.
  4. We only care about CONNECT and CHANGE_USER sub events.
  5. @param [in] coordinator Connection_event_coordinator
  6. @param [in] connection_event Connection event to be handled
  7. @param [in] error_handler Error handler object
  8. @returns status of connection event handling
  9. @retval false Successfully handled an event.
  10. @retval true Something went wrong.
  11. error_buffer may contain details.
  12. */
  13. bool Connection_delay_action::notify_event(
  14. MYSQL_THD thd, Connection_event_coordinator_services *coordinator,
  15. const mysql_event_connection *connection_event,
  16. Error_handler *error_handler) {
  17. ...
  18. // 只关注CONNECT与CHANGE_USER事件
  19. if (subclass != MYSQL_AUDIT_CONNECTION_CONNECT &&
  20. DBUG_RETURN(error);
  21. RD_lock rd_lock(m_lock);
  22. int64 threshold = this->get_threshold();
  23. // 拿到当前阈值检查阈值是否有效,DISABLE_THRESHOLD=0
  24. if (threshold <= DISABLE_THRESHOLD) DBUG_RETURN(error);
  25. int64 current_count = 0;
  26. bool user_present = false;
  27. Sql_string userhost;
  28. make_hash_key(thd, userhost);
  29. DBUG_PRINT("info", ("Connection control : Connection event lookup for: %s",
  30. userhost.c_str()));
  31. // 获取到当前失败登陆的次数
  32. user_present = m_userhost_hash.match_entry(userhost, (void *)&current_count)
  33. : true;
  34. // 如果失败次数超过阈值,无论这次连接成功与否,都需要延迟
  35. // 同时更新统计信息
  36. if (current_count >= threshold || current_count < 0) {
  37. ulonglong wait_time = get_wait_time((current_count + 1) - threshold);
  38. &self, STAT_CONNECTION_DELAY_TRIGGERED, ACTION_INC))) {
  39. error_handler->handle_error(
  40. ER_CONN_CONTROL_STAT_CONN_DELAY_TRIGGERED_UPDATE_FAILED);
  41. }
  42. // 在产生延迟时,需要释放读写锁,以减少锁的粒度
  43. // 防止阻塞对于IS table的数据访问
  44. rd_lock.unlock();
  45. conditional_wait(thd, wait_time);
  46. rd_lock.lock();
  47. }
  48. if (connection_event->status) {
  49. // 如果此次登陆失败,那么更新LF Hash
  50. if (m_userhost_hash.create_or_update_entry(userhost)) {
  51. error_handler->handle_error(
  52. ER_CONN_CONTROL_FAILED_TO_UPDATE_CONN_DELAY_HASH, userhost.c_str());
  53. error = true;
  54. }
  55. } else {
  56. // 如果此次登陆成功并且LF Hash中有数据,那么就删除LF Hash中的数据
  57. if (user_present) {
  58. (void)m_userhost_hash.remove_entry(userhost);
  59. }
  60. }
  61. DBUG_RETURN(error);

1,通过分析Connection Control处理流程与具体实现,我们可以知道插件是如何来处理连接事件的。