Press "Enter" to skip to content

Fail2Ban安装与配置

fail2ban是一款实用软件,可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作.

1、支持大量服务.如sshd,apache,qmail,proftpd,sasl等等;
2、支持多种动作.如iptables,tcp-wrapper,shorewall(iptables第三方工具),mail notifications(邮件通知)等等;
3、在logpath选项中支持通配符;
4、需要Gamin支持(注:Gamin是用于监视文件和目录是否更改的服务工具);
5、需要安装python,iptables,tcp-wrapper,shorewall,Gamin.如果想要发邮件,那必需安装postfix或sendmail.

主机环境;
[root@test ~]# lsb_release -a
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.4.1708 (Core)
Release: 7.4.1708
Codename: Core
[root@test ~]# uname -a
Linux test 3.10.0-693.2.2.el7.x86_64 #1 SMP Tue Sep 12 22:26:13 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

具体安装步骤:
[root@test yum.repos.d]# yum install -y fail2ban
具体配置文件:
/etc/fail2ban/fail2ban.conf 日志设定文档
/etc/fail2ban/jail.conf 阻挡设定文档
/etc/fail2ban/filter.d 具体阻挡内容设定目录

这里只需修改jail.conf:

总体配置
#此ip或者ip段为例外,不受以下条件影响
ignoreip = 127.0.0.1
# 封锁时间(如一天为:86400)
bantime = 600
# 在多长时间以内达到条件则开始执行封锁,如600秒达到3次则执行。 # 单位:秒
findtime = 600
# 在以上条件的出错次数,如600秒达到3次则执行。
# 单位:次
maxretry = 3
enabled = false //默认全局是不开启的,需要开启的话只要在相应模块添加‘enabled = true’即可

SSH配置(ssh还可以使用denyhosts,安装配置参考https://www.tracymc.cn/archives/273):
[sshd]

# To use more aggressive sshd filter (inclusive sshd-ddos failregex):
#filter = sshd-aggressive
enabled = true //添加这句话开启sshd模块
port = ssh
logpath = %(sshd_log)s
backend = %(sshd_backend)s

启动:
[root@test ~]# systemctl restart fail2ban
[root@test ~]# fail2ban-client status
Status
|- Number of jail: 1
`- Jail list: sshd //说明sshd配置成功

测试是否生效:
查看日志(/var/log/fail2ban.log):
2018-05-17 23:36:43,657 fail2ban.actions [23249]: NOTICE [sshd] Ban 1.2.3.4
以上日志说明1.2.3.4这个IP已经被ban禁止ssh登录本机,说明fail2ban安装成功.

fail2ban还可以配置ftp、mysql、MongoDB等相关服务,具体可自行搜索安装配置.

Be First to Comment

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注