Fail2ban 入侵防御软件应用总结
虽然最近fail2ban没有太多版本更新了,但它仍然是Linux系统入侵防御、防暴力破解的好帮手。它的原理是通过读应用程序产生的日志调用iptables产生阻挡策略。作为Linux系统管理员你会碰到恶意扫描、字典攻击、寻找漏洞等非法攻击,Fail2ban能够助你消除系统风险、防范入侵的重要手段之一。
通过Fail2ban你对恶意的IP进行阻断,发送告警通知。默认安装后Fail2ban提供了多种过滤器,如ssh、nginx、apache、postfix、dovecot等,下面配置中我们以ssh、postfix作为例子
Linux中安装Fail2ban(以CentOS7为例)
1 | yum install epel-release |
配置Fail2ban
安装成功后,Fail2ban的配置文件默认是在/etc/fail2ban目录中的,主配置文件为jail.conf,包含了一些默认的设置及预计规则。在以后升级过程中会被覆盖,所以不要编辑该文件。正确的作法是在当前目录 创建一个jail.local的配置文件或者在jail.d目录下创建一个新文件编辑规则
1
cp jail.conf jail.local
默认的配置中大部分配置都是很合理的,只有个别配置需要修改
1
2
3
4
5
6
7
8vim /etc/fail2ban/jail.local
[DEFAULT]
ignoreip = 127.0.0.1/8 192.168.5.0/24
bantime = 600
findtime = 600
maxretry = 3
destemail = info@example.com
sender = robot@example.comignoreip 例外的IP地址列表,Fail2ban会把这里列的IP排队在规则之外,多个IP可以用空隔、逼号分隔
- bantime 主机被禁的秒数
- findtime 如果访问主机在这个时间内达到了maxretry数会触发自动防御机制
- maxretry 最大尝试次数
- destemail 阻挡后发送告警邮件接收地址
- sender 发送地址,可以通过mailx配置发送帐号和密码
配置服务(以ssh和postfix为例)
- Fail2ban已经默认预设了很多过滤器可以 /etc/fail2ban/filter.d中看到,如果不是非常规服务,我们只需在配置文件中启用即可,enable = true 为启用 false为禁用。
启用SSH防护
1
2
3
4enabled = true
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s启用postfix防护
1
2
3
4
5[postfix-sasl]
port = smtp,465,submission,imap3,imaps,pop3,pop3s
enabled = true
logpath = %(postfix_log)s
backend = %(postfix_backend)s
启动Fail2ban验证规则
1 | systemctl enable fail2ban && systemctl start fail2ban |
从禁止列表中删除IP
1 | fail2ban-client set ssh unbanip 8.8.8.8 |
参考文档
- https://www.digitalocean.com/community/tutorials/how-to-protect-ssh-with-fail2ban-on-centos-7
- https://www.2daygeek.com/how-to-install-setup-configure-fail2ban-on-linux/
最后更新: 2023年08月27日 03:06