拒绝暴力破解——Fail2ban
约 390 个字
24 行代码
预计阅读时间 2 分钟
无论在哪都有一群不怀好意的机器人和脚本(真的会有人手动爆破吗)在试图攻破每一个暴露在公网或者同一内网下的各个IP的脆弱端口。虽然类似SSH这种较为安全,但是弱口令下被对方猜中密码很可能只是时间问题,谁都不敢赌自己短短的密码不在对方的爆破字典里。
所以,Fail2ban应运而生,它的人生信条就是——不爽就ban。
Fail2ban由一个client和一个service组成,我们查询当前Fail2ban工作状态和日志信息的时候都是通过client向service完成的。
安装 Fail2ban
Fail2ban可在各大发行版的包管理器里下载,这里以Ubuntu/Debian系统举例:
启动Fail2ban
配置 Fail2ban
我主要是为了防护SSH爆破攻击,新建/etc/fail2ban/jail.local
。
添加以下配置:
# jail.local
[DEFAULT]
# Set the default ban time for all jails to 1 hours.
bantime = 60m
# A host is banned if it has generated "maxretry" during the last "findtime"
# seconds.
findtime = 5m
# "maxretry" is the number of failures before a host get banned.
maxretry = 5
[sshd]
# This section enables and customizes the SSH jail.
enabled = true
maxretry = 6
# The port and logpath are usually detected automatically from defaults.
# You only need to add them here if they are non-standard.
# port = 22
# logpath = /var/log/auth.log
[recidive]
enabled = true
# If someone is baned by some jails more than 5 times, then it will be banned for 1 month.
findtime = 1d
maxretry = 5
bantime = 4w
重启服务。
监测日志与放行IP
日常可以使用lastb
查看SSH登录失败的日志信息。
也可以直接查看Fail2ban的日志信息。
获取Fail2ban下已经运行的jail,并查看相应状态。
# 查询所有启动的jail
sudo fail2ban-client status
# 查询某一个jail,比如sshd的状态,这里可以输出被Ban的IP
sudo fail2ban-client status jail
# 直接输出对应jail的被Ban IP
sudo fail2ban-client get jail banip
手动添加/解除 IP封锁。