如何部署防攻击服务Fail2Ban?

Fail2Ban

1 理论部分

1.1 环境概述

笔者实战阿里云发现,阿里云提供的centos 6.5版本的镜像是经过阿里云修改的。
默认会禁用以下两个:
1)iptables管理工具
2)SELinux
笔者参阅了一下文献(见本人页最后),发现fail2ban由于版本的变化,原文的设置方法已经发生变化,故而参阅原文的基础上重写关于fail2ban的设置文档。

1.2 fai2ban的介绍

fail2ban可以监视你的系统日志,然后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动作(一般情况下是调用防火墙屏蔽),如:当有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,而且可以发送e-mail通知系统管理员,是一款很实用、很强大的软件!

1.3 管理文件结构介绍

1)/etc/fail2ban/action.d
动作文件夹,内含默认文件。iptables以及mail等动作配置
2)/etc/fail2ban/fail2ban.conf
定义fail2ban日志级别,日志位置及sock文件位置
3)/etc/fail2ban/filter.d
条件文件夹,内含默认文件。过滤日志关键内容设置
4)/etc/fail2ban/jail.conf
主配置文件、模块化。主要设置启用ban动作的服务以及动作阀值。
5)/etc/rc.d/init.d/fail2ban
fail2ban服务的启动脚本

1.4 fai2ban的工作原理

1)读取/etc/fail2ban/jail.conf中开启的服务,如sshd,vsftpd等
2)根据开启的服务,在/etc/fail2ban/filter.d选择对应的日志过滤规则监控日志
3)根据开启的服务和日志过滤规则查找违反/etc/fail2ban/jail.conf所定义的findtime(查找时间段)与maxretry(最大尝试次数)的服务日志。
4)如果发现有违反日志则根据/etc/fail2ban/action.d定义的动作执行(如iptables生成拒绝某个IP访问服务器的规则)。
注:以上原理是本人根据使用经验分析的结果,有不当之处欢迎指正。

2 实验部分

2.1 基yum源安装

yum install -y iptables fail2ban.noarch

2.2 配置iptables

1)iptables设置

vim /etc/sysconfig/iptables

增加如下内容:

# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT DROP [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT

2)重启iptables服务

/etc/init.d/iptables restart

2.3 fail2ban设置

1)检查配置

vim /etc/fail2ban/fail2ban.conf

确定如下配置存在:

[Definition]
loglevel = INFO
logtarget = SYSLOG
syslogsocket = auto
socket = /var/run/fail2ban/fail2ban.sock
pidfile = /var/run/fail2ban/fail2ban.pid
dbfile = /var/lib/fail2ban/fail2ban.sqlite3
dbpurgeage = 86400

2)修改jail配置文件

vim /etc/fail2ban/jail.conf

请参阅注解部分,详细如下:

[INCLUDES]
before = paths-fedora.conf
[DEFAULT]
ignoreip = 127.0.0.1/8
ignorecommand =
bantime  = 600
findtime  = 60 #修改搜索日志的时间段为一分钟
maxretry = 5 #修改上面时间段内最大尝试为5次
backend = auto
usedns = warn
logencoding = auto
enabled = false
filter = %(__name__)s

destemail = will@cmdschool.org
sender = root@localhost
mta = sendmail
protocol = tcp
chain = INPUT
port = 0:65535
banaction = iptables-multiport

action_ = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
action_mw = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
            %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]
action_mwl = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
             %(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
action_xarf = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
             xarf-login-attack[service=%(__name__)s, sender="%(sender)s", logpath=%(logpath)s, port="%(port)s"]
action_cf_mwl = cloudflare[cfuser="%(cfemail)s", cftoken="%(cfapikey)s"]
                %(mta)s-whois-lines[name=%(__name__)s, dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
action_blocklist_de  = blocklist_de[email="%(sender)s", service=%(filter)s, apikey="%(blocklist_de_apikey)s"]
action_badips = badips.py[category="%(name)s", banaction="%(banaction)s"]
action = %(action_)s

[sshd]
enabled = true #开启sshd的防护功能
port    = ssh
logpath = %(sshd_log)s

[sshd-ddos]
port    = ssh
logpath = %(sshd_log)s

[dropbear]
port     = ssh
logpath  = %(dropbear_log)s

[selinux-ssh]
port     = ssh
logpath  = %(auditd_log)s
maxretry = 5

[vsftpd]
port     = ftp,ftp-data,ftps,ftps-data
logpath  = %(vsftpd_log)s

[mysqld-auth]
port     = 3306
logpath  = %(mysql_log)s
maxretry = 5

以上是服务开启范例,请参阅者根据实际情况设置。
注:开启服务在某个服务,如“[sshd]”下一行加入“enabled = true”即可。

2.4 服务重启

重启iptables与fail2ban服务:

/etc/init.d/iptables restart
/etc/init.d/fail2ban restart

注:fail2ban一定后于iptables启动,即重启iptables一定要重启fail2ban,相反重启fail2ban不用重新启iptables。

2.5 fail2ban检查与测试

1)键入如下命令查看防护墙变化:

iptables -L -n

可见如下内容:

Chain f2b-sshd (1 references)
target     prot opt source               destination
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

2)测试:

ssh root@115.5.8.X

尝试找一个客户端去ssh登录你的服务器并故意输入错误的密码5次以上(模拟暴力破解过程)。
在被暴力破解的服务器输入如下命令检查测试结果:

iptables -L -n

可见有IP被拒绝:

Chain f2b-sshd (1 references)
target     prot opt source               destination
REJECT     all  --  14.222.106.0         0.0.0.0/0           reject-with icmp-port-unreachable
RETURN     all  --  0.0.0.0/0            0.0.0.0/0

2.6 fail2ban的解除

输入如下命令解除封锁:

iptables -D f2b-sshd 1

2.7 fail2ban状态监控脚本

因为重启iptables需要重启fai2ban服务,故而我们需要增加此监控脚本防止管理员疏忽。
1)增加脚本文件夹

mkdir ~/scripts

2)创建脚本

vim ~/scripts/f2bAutoRestart.sh

加入如下内容

#!/bin/bash
fl=$(iptables -L -v -n | grep 'Chain f' | wc -l)
#echo $fl
if [ $fl -eq 0 ]; then
  echo 'restart fai2band'
  /etc/init.d/fail2ban restart
fi

3)添加计划任务

crontab -e

添加如下内容:

*/5 * * * * sh ~/scripts/f2bAutoRestart.sh

参考文档:
===============================================
http://blog.sina.com.cn/s/blog_40ce02d70102uxm0.html

没有评论

发表回复

Fail2Ban
如何安装配置Fail2ban?

1 基础知识 1.1 Fail2Ban介绍 – Fail2Ban根据扫描日志(如/var …