Fail2Ban 使用教程

使用 Fail2Ban 增强服务器安全, 外加配置 Webhook-钉钉 通知的快速教程( ̄m ̄)

0%

简单了解

Fail2Ban是一款开源的安全工具,用于保护系统免受恶意攻击和暴力破解。通过监视系统日志,识别并限制来自恶意IP地址的连接尝试,从而保护服务器免受暴力破解和DDoS攻击等攻击。而且支持多个服务和协议,包括SSH、FTP、SMTP、HTTP等。

Fail2Ban还提供了一些额外的功能,如发送警报邮件、记录日志等,以帮助管理员更好地监控和保护服务器。

安装

Ubuntu/Debian

sudo apt install -y fail2ban

CentOS/Fedora

sudo yum install -y epel-release fail2ban

检查运行状态

systemctl status fail2ban

配置

前往软件目录/etc/fail2ban

cd /etc/fail2ban

参数说明

  • enabled - 表示是否开启监控服务
  • port - 监控服务的端口号
  • filter - 启用的日志过滤器, 引用位于 /etc/fail2ban/filter.d 目录中的文件
  • logpath - 监控服务的日志文件路径, 下文使用var/log/auth.log做演示
    • Ubuntu/Debian: /var/log/auth.log
    • CentOS/Fedora: /var/log/secure
  • maxretry - 允许的最大攻击次数
  • bantime - 封禁时间,单位为秒
  • findtime - 发现攻击次数的时间窗口,单位为秒
  • action - 触发 filter 激活的动作

创建配置文件jail.local

vim jail.local

填入一下内容

[DEFAULT]
# 服务后端选择,自动即可
backend = auto
# 忽略的IP地址段
ignoreip = 127.0.0.1/8
# 允许的最大攻击次数
maxretry = 3
# 600秒内3次登录失败进行封禁
findtime = 600
# 封禁时间设置为一周
bantime = 604800

# [sshd]
# enabled = false

# 这里重写了默认的`sshd`规则, 要改名的话需要把原先的`sshd`禁用掉(如上), 不然会有冲突
[sshd]
enabled = true
# 过滤器选择,这里选择sshd
filter = sshd
# 这里选择iptables封禁,并且给封禁的规则命名为SSH,端口为ssh,协议为tcp
action = iptables[name=SSH, port=ssh, protocol=tcp]
logpath = /var/log/auth.log

检查配置是否正确

fail2ban-client -t

更新配置

systemctl restart fail2ban
# or
fail2ban-client reload

查看正在运行的规则

fail2ban-client status

测试

日志文件位于/var/log/fail2ban.log, 输出进行查看

tail -f /var/log/fail2ban.log

然后使用另一台服务器进行登录

ssh root@12.13.14.15

错误3次后日志会出现类似下面的记录, 表示IP已被封禁

要解封IP可用使用以下命令, 规则名

fail2ban-client set sshd unbanip 12.13.14.15

查看服务状态

fail2ban-client status sshd

常用命令

  • 解禁对应规则封禁IP

    fail2ban-client set <jail> unbanip x.x.x.x
  • 查看所有活动规则及其状态:

    fail2ban-client status
  • 查看指定规则的状态:

    fail2ban-client status <jail>
  • 解禁指定的IP地址:

    fail2ban-client set <jail> unbanip <ip_address>
  • 重新加载fail2ban配置:

    fail2ban-client reload
  • 停止fail2ban服务:

    fail2ban-client stop
  • 启动fail2ban服务:

    fail2ban-client start

添加Webhook通知(钉钉)

添加配置文件action.d/webhook.conf

vim action.d/webhook.conf

使用以下配置

[Definition]
# 程序启动时执行
actionstart = curl -X POST <webhook_url> -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content":"<_start_text>"}}'
actionstop =
actioncheck =
# IP被封执行
actionban = curl -X POST <webhook_url> -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content":"<_ban_text>"}}'
actionunban =

_start_text = Fail2Ban <name> jail has started from [<hostname>].
_ban_text = Fail2Ban <name> banned *<ip>* from [<hostname>] after <failures> attempts.

webhook_url = https://oapi.dingtalk.com/robot/send?access_token=<access_token>

[Init]
# 钉钉机器人的access_token
access_token = e804dd5e0f4173ac7dc8a07b436ca9be0f4173ac878d5d1bce195a06063b2807
hostname = <fq-hostname>

修改jail.local配置, 添加action, 空行添加

[DEFAULT]
backend = auto
ignoreip = 127.0.0.1/8
maxretry = 3
findtime = 600
bantime = 604800

[sshd]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
webhook

logpath = /var/log/auth.log

也可用通过传参的方式改变其配置值

action   = webhook[hostname='You hostname', access_token='You token']

检查配置

fail2ban-client -t

重启服务

systemctl restart fail2ban

钉钉看到通知时就配置成功了

快速配置

命令简版, 删除多余注释

cd /etc/fail2ban/ && vim action.d/webhook.conf
[Definition]
actionstart = curl -X POST <webhook_url> -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content":"<_start_text>"}}'
actionstop =
actioncheck =
actionban = curl -X POST <webhook_url> -H 'Content-Type: application/json' -d '{"msgtype": "text","text": {"content":"<_ban_text>"}}'
actionunban =
_start_text = Fail2Ban <name> jail has started from [<hostname>].
_ban_text = Fail2Ban <name> banned *<ip>* from [<hostname>] after <failures> attempts.

webhook_url = https://oapi.dingtalk.com/robot/send?access_token=<access_token>

[Init]
access_token =
hostname = <fq-hostname>
vim jail.local
[DEFAULT]
backend = auto
ignoreip = 127.0.0.1/8
maxretry = 3
findtime = 600
bantime = 604800

[sshd]
enabled = true
filter = sshd
action = iptables[name=SSH, port=ssh, protocol=tcp]
webhook[hostname='You hostname', access_token='You token']
logpath = /var/log/auth.log
#logpath = /var/log/secure
fail2ban-client -t
systemctl restart fail2ban

疑难杂症

防火墙规则

Fail2Ban默认使用iptables作为防火墙,如果使用的其他防火墙(例如ufw),则需要修改防火墙规则以使Fail2Ban正常工作。如使用ufw则可以如下修改:

sudo ufw allow ssh
sudo ufw limit ssh

日志路径

不同发行版的日志路径可能会有所不同,在创建配置文件时请仔细检查日志路径是否正确。

检查配置语法

fail2ban-client -t

项目地址: https://github.com/fail2ban/fail2ban
文档链接: https://fail2ban.readthedocs.io/en/latest/

------------ 已触及底线了 感谢您的阅读 ------------
  • 本文作者: OWQ
  • 本文链接: https://www.owq.world/fail2ban/
  • 版权声明: 本站所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处( ̄︶ ̄)↗