Fail2ban轻量级安全防御
时间:2024-05-05 14:56 来源:未知 作者:华嵩阳 点击:次
一、简介
Fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的HTTP、SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的软件!
Fail2ban由python语言开发,基于logwatch、gamin、iptables、tcp-wrapper、shorewall等。如果想要发送邮件通知道,那还需要安装postfix或sendmail。
在外网环境下,有很多的恶意扫描和密码猜测等恶意攻击行为,使用Fail2ban配合iptables,实现动态防火墙是一个很好的解决方案。
从CentOS7开始,官方的标准防火墙设置软件从iptables变更为firewalld。
为了使Fail2ban与iptables联动,需禁用自带的firewalld服务,同时安装iptables服务。
二、安装
yum install fail2ban
三、配置Fail2ban
Fail2ban已经内置很多匹配规则,位于filter.d目录下,包含了常见的SSH/FTP/Nginx/Apache等日志匹配,如果都
还无法满足需求,也可以自行新建规则来匹配异常IP。总之,使用Fail2ban+iptables来阻止恶意IP是行之有效的
办法,可极大提高服务器安全。
[root@web ~]# cd /etc/fail2ban/
[root@web fail2ban]# ls
action.d fail2ban.conf fail2ban.d filter.d jail.conf jail.d paths-common.conf paths-debian.conf paths-fedora.conf paths-freebsd.conf paths-opensuse.conf paths-osx.conf
[root@web fail2ban]# cp jail.conf jail.conf-bak
vim /etc/fail2ban/jail.conf
sshd模块的配置修改如下。其中,port应按照实际情况填写
[sshd]
port = ssh
enabled = true
port = ssh
filter = sshd
logpath = /var/log/secure
action = iptables[name=SSH, port=1991, protocol=tcp]
# 客户端主机被禁止的时长(默认单位为秒
bantime = 600
# 过滤的时长(秒)
findtime = 300
# 匹配到的阈值(次数)
maxretry = 2
[root@web fail2ban]# service fail2ban start
启动fail2ban: [确定]
[root@web fail2ban]# service fail2ban status
fail2ban-server (pid 12345) 正在运行...
Status
|- Number of jail: 1
`- Jail list: sshd
四、常用命令
查看当前被禁止登陆的ip
[root@web fail2ban]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0
| |- Total failed: 2
| `- File list: /var/log/secure
`- Actions
|- Currently banned: 1
|- Total banned: 1
`- Banned IP list: 202.61.89.201
设置白名单ip
[root@web fail2ban]# fail2ban-client set sshd addignoreip 202.61.89.201
These IP addresses/networks are ignored:
|- 127.0.0.1/8
`- 202.61.89.201
也可以通过配置文件设置:
ignoreip = 127.0.0.1/8
取消白名单
fail2ban-client set sshd delignoreip 202.61.89.201
iptables封禁策略变更为DROP
[root@cms fail2ban]# vim /etc/fail2ban/action.d/iptables-common.conf
# Values: [ NUM | STRING ] Default:
#
port = ssh
# Option: protocol
# Notes.: internally used by config reader for interpolations.
# Values: [ tcp | udp | icmp | all ] Default: tcp
#
protocol = tcp
# Option: blocktype
# Note: This is what the action does with rules. This can be any jump target
# as per the iptables man page (section 8). Common values are DROP
# REJECT, REJECT --reject-with icmp-port-unreachable
# Values: STRING
#blocktype = REJECT --reject-with icmp-port-unreachable
blocktype = DROP
重启服务生效
|