前边文章实现了只允许国内的IP地址来访问VPS,接下来我们使用fail2ban来防止SSH暴力破解。
fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则表达式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽)。比如有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员。
安装fail2ban
1
| yum install fail2ban fail2ban-systemd
|
更改配置文件fail2ban
这里要注意的是所有.conf文件里面的配置信息都会被.local配置所覆盖。所以你会看到很多文档都创建了一个local文件。我这人比较爱使用原有配置文件。我是直接更改了 jail.conf
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
cd /etc/fail2ban/jail.d/ mv 00-firewalld.conf 00-firewalld.conf.disabled
vi /etc/fail2ban/jail.conf
banaction = iptables-multiport banaction_allports = iptables-allports
banaction = iptables-ipset-proto6 banaction_allports = iptables-ipset-proto6-allports
[sshd] enabled = true
|
我还更改了一处动作配置文件。默认是当ssh或者其他服务被多次爆破后只封端口,我更改配置文件的目的是为了把这个ip直接封掉不论端口。
1 2 3 4 5 6 7
| vi iptables-ipset-proto6.conf
actionstart = ipset create <ipmset> hash:ip timeout <bantime><familyopt> <iptables> -I <chain> -p <protocol> -m multiport --dports <port> -m set --match-set <ipmset> src -j <blocktype>
actionstart = ipset create <ipmset> hash:ip timeout <bantime><familyopt> <iptables> -I <chain> -m set --match-set <ipmset> src -j <blocktype>
|
开启服务
1 2
| systemctl enable fail2ban systemctl start fail2ban
|
后记
fail2ban目录大约如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| /etc/fail2ban/ ├── action.d │ ├── iptables-common.conf │ ├── iptables.conf │ ├── iptables-ipset-proto4.conf │ ├── iptables-ipset-proto6-allports.conf │ ├── nftables.conf │ └── 等等不再列举 ├── fail2ban.conf ├── jail.conf ├── jail.local ├── filter.d │ ├── apache-auth.conf │ ├── apache-noscript.conf │ ├── couriersmtp.conf │ ├── postfix.conf │ ├── proftpd.conf │ ├── qmail.conf │ ├── sasl.conf │ ├── sshd.conf │ └── 等等不在列举 └── paths-common.conf
|
参考:https://www.linuxjournal.com/content/advanced-firewall-configurations-ipset