服务器在线 - 服务器软件 - 网站地图 服务器在线,专注于服务器技术!

当前位置:主页 > 服务器系统 > CentOS > 正文

fail2ban安装及配置方法

时间:2016-06-13    来源:未知    投稿:admin    点击:

1 安装
rpm -ivh http://mirror.pnl.gov/epel/6/i386/epel-release-6-8.noarch.rpm
yum -y install fail2ban
 
 
其中
 
/etc/fail2ban/ 为配置文件目录;
 
/usr/share/fail2ban/ 为项目文件里面包含了整应用的python代码;
 
/etc/init.d/fail2ban 为启动脚本
 
 
方式二
 
源码安装
 
你可以直接clone最新的代码或者下载最新代码
git clone https://github.com/fail2ban/fail2ban.git
 
 
or
wget "https://github.com/fail2ban/fail2ban/archive/master.zip";unzip master.zip
 
 
然后
cd fail2ban
python setup.py install
 
 
/etc/fail2ban/ 为配置文件目录;
 
/usr/lib/pythonx.x/site-packages/fail2ban-0.9.2.dev-py2.6.egg/fail2ban 项目文件,依赖你的python版本
 
启动脚本:复制源码路径files下相应的操作系统的启动脚本到/etc/init.d/下即可
cp files/redhat-initd /etc/init.d/fail2ban
 
 
安装过程结束
 
 
 
 
 
 
配置
 
rpm包安装方式和源码包安装方式的配置方法不同,官方在0.9或更高的版本将jail.conf文件拆分成,jail.conf文件和jail.d目录 这一点通过对比配置文件的目录结构便可以发现,
 
#rpm安装
ll /etc/fail2ban/
 
total 32
drwxr-xr-x 2 root root  4096 May 22 14:01 action.d
-rw-r--r-- 1 root root  1510 Aug 20  2014 fail2ban.conf
drwxr-xr-x 2 root root  4096 May 22 14:01 filter.d
-rw-r--r-- 1 root root 19313 Aug 20  2014 jail.conf
 
 
其中fail2ban.conf为fail2ban的主配置文件
grep -v ^# fail2ban.conf |grep -v ^$
 
[Definition]
loglevel = 3
logtarget = SYSLOG
socket = /var/run/fail2ban/fail2ban.sock
pidfile = /var/run/fail2ban/fail2ban.pid
 
 
各个参数在配置文件中均有详细说明,jail.conf为定义的需要处理的的动作是否生效,指定需要分析的logpath等等
 
这里我们直接追加下面的内容
[nginx-get-cc]
#是否开启防护
enabled = true
#使用的是哪个filter文件  filter.d/nginx-get-cc.conf
filter = nginx-get-cc
#执行的操作,更多操作查看配置文件。
action = iptables-multiport[name=cc-attrack, port="http", protocol=tcp]
#日志路径 
logpath = /var/log/nginx/*error.log   
#时间范围
findtime = 60
#添加到防火墙后多久失效
bantime = 7200
#最大重试次数
maxretry = 1000
 
 
然后添加filter文件
vim filter.d/nginx-get-cc.conf
 
# Fail2Ban configuration file
#
#
[Definition]
 
# Option: failregex
# Note: This regex will match any GET entry in your logs, so basically all valid and not valid entries are a match.
# You should set up in the jail.conf file, the maxretry and findtime carefully in order to avoid false positives.
 
failregex =  ^<HOST> - .*GET
 
# Option: ignoreregex
# Notes.: regex to ignore. If this regex matches, the line is ignored.
# Values: TEXT
#
ignoreregex =
 
 
然后启动
/etc/init.d/fail2ban start
 
 
然后使用ab或者webbench工具
ab -n 10000 -c 1000 http://192.168.0.208/
 
 
然后观察防火墙状态
iptables-save
 
-A f2b-cc-attrack -s 192.168.0.213/32 -j REJECT --reject-with icmp-port-unreachable
 
 
#源码包安装
ll
 
total 60
drwxr-xr-x 2 root root  4096 May 20 12:44 action.d
-rw-r--r-- 1 root root  2329 May 21 06:19 fail2ban.conf
drwxr-xr-x 2 root root  4096 May 20 12:44 fail2ban.d
drwxr-xr-x 3 root root  4096 May 21 02:37 filter.d
-rw-r--r-- 1 root root 17755 May 20 12:44 jail.conf
drwxr-xr-x 2 root root  4096 May 21 02:36 jail.d
-rw-r--r-- 1 root root  1889 May 20 12:44 paths-common.conf
-rw-r--r-- 1 root root   645 May 20 12:44 paths-debian.conf
-rw-r--r-- 1 root root   689 May 20 12:44 paths-fedora.conf
-rw-r--r-- 1 root root  1174 May 20 12:44 paths-freebsd.conf
-rw-r--r-- 1 root root   290 May 20 12:44 paths-osx.conf
 
 
和yum安装配置唯一的区别在于 jail.conf会自动引用jail.d下以.conf结尾的配置文件,这里我们直接添加一个nginx-get-cc.conf 同时添加内容
vim nginx-get-cc.conf
 
[nginx-get-cc]
\#是否开启防护
enabled = true
\#使用的是哪个filter文件  filter.d/nginx-get-cc.conf
filter = nginx-get-cc
\#执行的操作,更多操作查看配置文件。
action = iptables-multiport[name=cc-attrack, port="http", protocol=tcp]
\#日志路径 
logpath = /var/log/nginx/*error.log   
\#时间范围
findtime = 60
\#添加到防火墙后多久失效
bantime = 7200
\#最大重试次数
maxretry = 1000
 
 
然后添加filter文件,在启动fail2ban然后使用ab工具测试。
 

如果您的问题仍未解决,还可以加入服务器在线技术交流QQ群:8017413寻求帮助。


相关内容
最新热点内容