Loading... ## 安装 使用包管理器安装 ```bash # deb apt install fail2ban -y # redhat yum install fail2ban -y ``` 路径作用 ``` /etc/fail2ban # 配置目录 /etc/fail2ban/action.d # 动作配置 /etc/fail2ban/filter.d # 过滤配置 /etc/fail2ban/jail.d # 防护配置文件夹 /etc/fail2ban/jail.local # 防护主配置文件(优先。当有jail.local文件时,不会读取jail.conf) /etc/fail2ban/jail.conf # 防护主配置文件 /etc/fail2ban/fail2ban.conf # 主配置文件 ``` ## 配置 创建并编辑 /etc/fail2ban/jail.local 配置 如果是 Debian / Ubuntu 系统,先写 ```ini [INCLUDES] before = paths-debian.conf ``` 然后定义默认规则 ```ini [DEFAULT] # 白名单IP ignoreip = 127.0.0.1/8 ::1 1.1.1.1 119.1.1.1 10.0.0.0/8 # 屏蔽时间 bantime = 7d # 查找时间 findtime = 5m # 尝试次数 maxretry = 5 # 日志修改检测机制 backend = auto ``` 保护 SSH 服务 ```bash [sshd] enabled = true # 端口 port = 22,23322 # 拦截操作 blocktype = DROP # 过滤(这里使用自带的过滤器) filter = sshd # 封禁动作 # # iptables-allports 全端口封禁 action = iptables-allports[name="sshd", blocktype="%(blocktype)s"] # iptables-multiport 多端口封禁,需要配置port参数,参考iptables multiport参数 #action = iptables-multiport[name="sshd", blocktype="%(blocktype)s", port="%(port)s"] ``` 保护 Bitwarden(Vaultwarden) ```ini [bitwarden] enabled = true # 端口 port = 80,443 # 拦截操作 blocktype = DROP # 过滤(自定义过滤器) filter = vaultwarden # 日志路径 logpath = /home/bitwarden/data/vaultwarden.log # 封禁动作 # # iptables-allports 全端口封禁 action = iptables-allports[name="bitwarden", blocktype="%(blocktype)s"] ``` 保存上面编辑的 `/etc/fail2ban/jail.local` 配置 创建一个 vaultwarden 的过滤器:`vim /etc/fail2ban/filter.d/vaultwarden.conf` ``` [INCLUDES] before = common.conf [Definition] failregex = ^.*Username or password is incorrect\. Try again\. IP: <ADDR>\. Username:.*$ ignoreregex = ``` ## 启动 启动服务并设置开机启动 ```bash service fail2ban restart systemctl enable fail2ban service fail2ban status ``` ## 常用操作 - **重载配置**:`fail2ban-client reload` - **启动监狱**:`fail2ban-client start 监狱名称` - **停止监狱(暂时停止,重启f2b后恢复)**:`fail2ban-client stop 监狱名称` - **在某个监狱封禁IP**:`fail2ban-client set 监狱名称 banip IP地址` 例如:`fail2ban-client set sshd banip 123.123.123.123`  - **在某个监狱取消封禁IP**:`fail2ban-client set 监狱名称 unbanip IP地址` 例如:`fail2ban-client set sshd banip 123.123.123.123`  - **查看监狱目前封禁的IP**:`fail2ban-client status 监狱名称` 例如:`fail2ban-client status sshd`  最后修改:2024 年 03 月 11 日 © 允许规范转载 赞 如果觉得我的文章对你有用,请随意赞赏