fail2ban可以独立运行,但如果没有防火墙的话,它只会将非法IP保存到自己的数据库中,而不会进行实际拦截。所以要实现拦截的话,需要系统启用防火墙,例如,FreeBSD的ipfw。
编辑 /etc/rc.conf ,加入以下行:
xxxxxxxxxxfirewall_enable="YES"ipfw的默认规则是拒绝任何数据通行,除非设定排除的条目。
既然使用fail2ban来管理防火墙,就不需要采取如此严格的策略,所以使用以下设置将ipfw的规则改为允许任何数据通行。编辑 /boot/loader.conf ,加入以下行:
xxxxxxxxxxnet.inet.ip.fw.default_to_accept="1"重启系统,运行ipfw命令,查看当前ipfw运行状态:
xxxxxxxxxx$ sudo ipfw list00100 allow ip from any to any via lo000200 deny ip from any to 127.0.0.0/800300 deny ip from 127.0.0.0/8 to any00400 deny ip from any to ::100500 deny ip from ::1 to any00600 allow ipv6-icmp from :: to ff02::/1600700 allow ipv6-icmp from fe80::/10 to fe80::/1000800 allow ipv6-icmp from fe80::/10 to ff02::/1600900 allow ipv6-icmp from any to any icmp6types 101000 allow ipv6-icmp from any to any icmp6types 2,135,13665535 allow ip from any to any此时系统是开放的(最后一句),任何人(IP)都可以进来,安全性完全由系统本身保障,比如强壮的密码或正确的登录限制。
使用以下命令安装fail2ban:
xxxxxxxxxx$ sudo pkg install py311-fail2ban并修改 /etc/rc.conf 文件,加入以下行,以使得开机自动启动fail2ban:
xxxxxxxxxxfail2ban_enable="YES"
fail2ban的配置文件在 /usrl/local/etc/fail2ban/ 目录中。
fail2ban特别警示不要修改 fail2ban.conf 、 jail.conf 等默认安装的配置文件,因为软件更新可能会冲掉用户自定义的选项。
我们在 /usr/local/etc/fail2ban/jail.d/ 目录中创建一个 sshd.conf 文件,内容如下:
xxxxxxxxxx[sshd]enabled=truefilter=sshdaction=ipfwsshd ipfw 过滤器filter位于 /usr/local/etc/fail2ban/filter.d/ 目录中,此目录中有很多预定义的过滤器,可以根据需要选用,本例选用的是sshd.conf配置(在以上配置语句中不需要写文件的扩展名),不需要修改此文件。
动作action位于 /usr/local/etc/fail2ban/action.d/ 目录中,本例选用的是ipfw.conf配置。但需要修改以下行:
xxxxxxxxxxlocalhost = me完成以上设置后,启动fail2ban服务即可。
可以在 /usr/local/etc/fail2ban/jail.d/ 中创建一个含有 [DEFAULT]的段,设置特定的全局设定,例如:
xxxxxxxxxx[DEFAULT]ignoreip=127.0.0.1/8 ::1 fe80:: 192.168.0.0/24 10.8.0.0/24bantime = 10hfindtime = 10hignoreip 设置忽略的IP地址段bantime 设置封锁时长,可以使用m表示分钟、h表示小时、d表示天findtime 设置查找数据库中记录的时长2025年6月8日装了14.3版的FreeBSD,sshd生成的日志格式如下:
x
Jun 9 13:00:51 nc sshd-session[24113]: Invalid user a from 240e::492:164a:7e0e port 25005Jun 9 13:00:51 nc sshd-session[24113]: Connection reset by invalid user a 240e::492:164a:7e0e port 25005 [preauth]以前是 sshd ,现在变成了 sshd-session ,而filter.d中的sshd.conf的定义的 _daemon = sshd ,造成 fail2ban 无法正确识别 auth.log 中对应的 sshd-sesssion 条目。
复制 sshd.conf 为 sshd-session.conf,并修改以下条目:
xxxxxxxxxx[DEFAULT]_daemon = sshd-session并修改 /usr/local/etc/fail2ban/jail.d/ssh.conf 为:
xxxxxxxxxx[sshd]enabled = truefilter = sshd-sessionaction = ipfw_localmaxretry = 3mode = aggressive重启fail2ban服务。
最终,fail2ban需要修改以下文件:
x
[DEFAULT]ignoreip=127.0.0.1/8 ::1 fe80:: 192.168.0.0/24 10.8.0.0/24bantime = 10hfindtime = 10h/usr/local/etc/fail2ban/jail.d/ssh.conf :
xxxxxxxxxx[sshd]enabled = truefilter = sshd-sessionaction = ipfw_localmaxretry = 3mode = aggressive/usr/local/etc/fail2ban/filter.d/sshd-session.conf :
xxxxxxxxxx...[INCLUDES]# Read common prefixes. If any customizations available -- read them from# common.localbefore = common.conf[DEFAULT]_daemon = sshd-session# optional prefix (logged from several ssh versions) like "error: ", "error: PAM: " or "fatal: "__pref = (?:(?:error|fatal): (?:PAM: )?)?.../usr/local/etc/fail2ban/action.d/ipfw_local.conf :
xxxxxxxxxx...actionban = ipfw add <blocktype> ip from <ip> to <localhost> <port>...localhost = me...blocktype = deny