CentOS 安装配置 Fail2ban 防爆破

2021年12月15日21:48:56 发表评论 1,008 views

0x01 Fail2ban是什么?

Fail2ban 是一款使用软件,可以监视你的系统日志,然后匹配日志的错误信息(正则匹配)

执行相应的屏蔽动作。

0x02 Fail2ban安装

  1. #安装fail2ban
  2. yum -y install fail2ban
  3. #配置
  4. cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  5. vim /etc/fail2ban/jail.local
  6. #修改需要的策略,此处以sshd为例
  1. [sshd]
  2. # To use more aggressive sshd modes set filter parameter "mode"
  3. in jail.local:
  4. # normal (default), ddos, extra or aggressive (combines all).
  5. # See "tests/files/logs/sshd" or "filter.d/sshd.conf"
  6. for usage example and details.
  7. #mode = normal
  8. port = 2202#sshd使用端口
  9. logpath = %(sshd_log)s
  10. backend = %(sshd_backend)s
  11. maxretry = 5
  12. enabled = true
  1. #查看fai2ban状态
  2. fail2ban-client status
  3. #查看配置策略sshd状态
  4. fail2ban-client status sshd
  5. #手工解除fail2ban中被拉黑ip
  6. fail2ban-client set sshd unbanip xx.xx.xx.xx

0x03 配置Fail2ban邮件通知

  1. #查看未被#注释的行
  2. egrep -v "^#|^$"/etc/fail2ban/action.d/mail-whois.conf
  3. #修改/etc/mail.rc文件,添加如下邮件账号密码信息
  4. # For Linux and BSD, this should be set.
  5. set bsdcompat
  6. set from=xxx@xxx.com
  7. set smtp=smtp.xxx.com
  8. set smtp-auth-user=xxx@xxx.com
  9. set smtp-auth-password=xxxxx
  10. set smtp-auth=login
  11. #测试邮件发送
  12. echo "邮件内容".|mail -v -s "邮件标题" xxx@xxx.com
  13. #如提示命令未找到需安装sendmail,默认centos自带安装
  14. yum -y install sendmail mailx jwhois
  15. #查看sendmail状态,如错误
  16. systemctl status sendmail

0x04 配置Fail2ban telegram 机器人通知

  1. #创建Telegram 机器人为关注`Botfather`,按提示操作即可,
  2. #可查找机器人Token,添加`userinfobot`查找自己聊天ID
  3. #在/etc/fail2ban/action.d/中新建telegram.conf文件并写入
  4. [Definition]
  5. actionstart = /etc/fail2ban/scripts/send_telegram_notif.sh -a start
  6. actionstop = /etc/fail2ban/scripts/send_telegram_notif.sh -a stop
  7. actioncheck =
  8. actionban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -b <ip>
  9. actionunban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -u <ip>
  10. [Init]
  11. init = 123
  1. #在/etc/fail2ban/中新建scripts目录,并新增send_telegram_notif.sh脚本文件写入
  2. #!/bin/bash
  3. # Version 1.0
  4. # Send Fail2ban notifications using a Telegram Bot
  5. # Add to the /etc/fail2ban/jail.conf:
  6. # [sshd]
  7. # ***
  8. # action = iptables[name=SSH, port=22, protocol=tcp]
  9. # telegram
  10. # Create a new file in /etc/fail2ban/action.d with the following information:
  11. # [Definition]
  12. # actionstart = /etc/fail2ban/scripts/send_telegram_notif.sh -a start
  13. # actionstop = /etc/fail2ban/scripts/send_telegram_notif.sh -a stop
  14. # actioncheck =
  15. # actionban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -b <ip>
  16. # actionunban = /etc/fail2ban/scripts/send_telegram_notif.sh -n <name> -u <ip>
  17. #
  18. # [Init]
  19. # init = 123
  20. # Telegram BOT Token
  21. telegramBotToken='xxxxx'
  22. #此处替换为自己Telegram 机器人Token
  23. # Telegram Chat ID
  24. telegramChatID='xxxxx'#此处替换为自己的Chat ID
  25. function talkToBot() {
  26. message=$1
  27. curl -s -X POST https://api.telegram.org/bot${telegramBotToken}/
  28. sendMessage -d text="${message}"-d chat_id=${telegramChatID} > /dev/null 2>&1
  29. }
  30. if[ $# -eq 0 ]; then
  31. echo "Usage $0 -a ( start || stop ) || -b $IP || -u $IP"
  32. exit 1;
  33. fi
  34. while getopts "a:n:b:u:" opt; do
  35. case"$opt"in
  36. a)
  37. action=$OPTARG
  38. ;;
  39. n)
  40. jail_name=$OPTARG
  41. ;;
  42. b)
  43. ban=y
  44. ip_add_ban=$OPTARG
  45. ;;
  46. u)
  47. unban=y
  48. ip_add_unban=$OPTARG
  49. ;;
  50. ?)
  51. echo "Invalid option. -$OPTARG"
  52. exit 1
  53. ;;
  54. esac
  55. done
  56. if[[ ! -z ${action} ]]; then
  57. case"${action}"in
  58. start)
  59. talkToBot "Fail2ban has been started on `hostname`."
  60. ;;
  61. stop)
  62. talkToBot "Fail2ban has been stopped on `hostname`."
  63. ;;
  64. *)
  65. echo "Incorrect option"
  66. exit 1;
  67. ;;
  68. esac
  69. elif[[ ${ban} == "y"]]; then
  70. talkToBot "[${jail_name}] The IP: ${ip_add_ban} has been banned on `hostname`."
  71. exit 0;
  72. elif[[ ${unban} == "y"]]; then
  73. talkToBot "[${jail_name}] The IP: ${ip_add_unban} has been unbanned on `hostname`."
  74. exit 0;
  75. else
  76. info
  77. fi
  1. #给send_telegram_notif.sh脚本添加可执行权限
  2. chmod +x send_telegram_notif.sh
  3. #修改jail.local配置文件,将启用的jail的action下添加一个telegram,如下
  4. action = iptables[name=SSH,port=2202,protocol=tcp]
  5. telegram
  6. #重启fail2ban验证
  7. systemctl restart fail2ban

Telegram 机器人告警通知效果如图

CentOS 安装配置 Fail2ban 防爆破

0x05 fail2ban 配置文件字段说明

  1. 如需开启服务,在配置文件中指定服务中添加enabled=true开启
  2. 1.ignoreip:永远不会被禁止的IP地址白名单。他们拥有永久的“摆脱Jails”卡。
  3. 该本地主机的IP地址 (127.0.0.1)是在列表中默认情况下,其IPv6相当于沿(::1)。
  4. 如果您知道永远不应禁止其他IP地址,请将它们添加到此列表中,并在每个IP地址之间留一个空格。
  5. 2.bantime:禁止IP地址的持续时间(“ m”代表分钟)。
  6. 如果键入的值不带“ m”或“ h”(代表小时),则将其视为秒。值-1将永久禁止IP地址。
  7. 要非常小心,不要将自己永久锁定在自己的面前。
  8. 3.findtime:尝试失败的连接次数过多会导致IP地址被禁止的时间。
  9. 4.maxretry:“尝试失败次数过多”的值。
  10. 如果来自同一IP地址的maxretry连接在该findtime时间段内尝试失败的连接,则在持续时间内将其禁止bantime。
  11. 唯一的例外是ignoreip列表中的IP地址。
  12. Fail2Ban将IP地址放入Jails一段时间。fail2ban支持许多不同的Jails,每个Jails代表适用于单个连接类型的设置。
  13. 这使您可以对各种连接类型进行不同的设置。或者,您可以Fail2Ban仅监视一组选定的连接类型。
站长小智

发表评论

您必须登录才能发表评论!