type
status
date
slug
summary
tags
category
icon
password
URL
起因是看到使用iptables限制区域访问 - 配置调优 - LINUX DO这篇帖子,我就想起我当时天天被爆破ssh时的解决方案
安装
Fail2Ban的安装和配置过程非常直接,相关信息可以在其GitHub仓库以及官方Wiki中找到:
配置
wiki里是这么说的:
If not configured manually, Fail2ban will load configuration files from the directory /etc/fail2ban. You can find many files called *.conf there.Before you start the fail2ban service for the first time, you should do some configuration appropriate to your system. The least would be to enable jails for the services that you want to protect with fail2ban.[Q] Should I make my configuration directly injail.confandfail2ban.conf?[A] No. You should avoid to change.conffiles, created by fail2ban installation. Instead, you should create new files with a.localextension.
官方推荐在配置 fail2ban 的时候应该避免直接更改由 fail2ban 安装创建的
.conf 文件(例如 fail2ban.conf 和 jail.conf),应该创建扩展名为.local 的新文件(例如 jail.local)来进行自定义配置。.local 文件将覆盖.conf 文件相同部分的参数。我拿sshd爆破来举个例子:
- 首先创建
.local文件:vim /etc/fail2ban/jail.local
- 然后添加规则(可根据需要修改):
以上配置就是针对于
22 端口的ssh服务,在 5分钟 内,连续超过 3 次登录失败,就使用TCP Wrapper封禁尝试登录的ip 1小时 , 1小时 解封后继续登录失败,那就增量封禁到 7天->14天 ... ,最长封禁时间为 8周:wq保存退出,然后重启服务生效内存节省选项(可选)
在网站或服务器被DDoS时,由于
findtime和bantime很长,可能导致 fail2ban 在访问量较大的网站上产生大量内存消耗,解决方式就是配置中添加类似这样:fail2ban.local:jail.local:验证配置是否生效
tail -f /var/log/ban_sshd.log或者fail2ban-client status sshd就能看到目前正在爆破你和ban掉爆破ssh的ip日志,和目前已经封禁的ip了
可以看到我阿里云香港的vps,就这刚刚一会已经被ssh爆破了2435次,ban掉97个ip了
通过正则来ban特定日志记录的ip
如果需要,还可以根据失败日志中的特定模式来封禁IP。例如,对于如下
Speed-Test类型的日志:可以在
.local文件中添加相应的failregex规则来匹配这种模式。在
.local文件里添加一行这样所有触发特定失败日志的ip都会根据规则来封禁。
基于此,可以用来保护WordPress,WP登录地址为
/wp-login.php所以可以新增配置文件: