为 frp 后的 SSH 配置 fail2ban
家里的 NAS 一直被 SSH 爆破,即使我已经把密码登录关了,还是在不停地刷 SSH 登录失败的系统日志。
于是尝试配置 fail2ban 解决这个问题。
fail2ban 配置
参考了这篇博客(使用 fail2ban 保护 frp 服务)并做了一些修改,从 systemd 而不是从文件读取日志,去掉了 get a new work connection 的匹配(因为 work connection 是从 frpc 发起的连接)。
文件 /etc/fail2ban/jail.d/frps.conf:
[frps]
enabled = true
findtime = 10m
maxretry = 100
bantime = 1d
backend = systemd
filter = frps
journalmatch = _SYSTEMD_UNIT=frps.service
port = all
action = iptables-allports[name=frp,protocol=tcp]
文件 /etc/fail2ban/filter.d/frps.conf:
[Definition]
failregex = ^.*ssh] get a user connection \[<HOST>:[0-9]*\]
ignoreregex =
注意 failregex 开头的 ssh] 部分,用于匹配以 ssh 结尾的 frp 服务。
frps 配置
这台机器上的 frps 是由 systemd 启动的,日志默认从 stdout 输出,并被 systemd 读取和存储。
我们只需要设置 log_level = debug 以启用 get a user connection 这类日志就好。
文件 frps.ini:
[common]
log_level = debug
...
效果
$ sudo fail2ban-client status frps
Status for the jail: frps
|- Filter
| |- Currently failed: 1
| |- Total failed: 1344
| `- Journal matches: _SYSTEMD_UNIT=frps.service
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: <此处是攻击者 IP>