优质vps,便宜VPS,美国VPS推荐,国外VPS评测,VPS新手教程,美国VPS代购,免费VPS

VpsAdd教程:Debian下安装fail2ban进行主动防御vps

fail2ban是什么

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

fail2ban如何安装

先更新,这个不用解释:
apt-get update
apt-get upgrade --show-upgraded

一行命令这就算安装完了:
apt-get install fail2ban

配置文件在目录/etc/fail2ban/中:
/etc/fail2ban/fail2ban.conf #fail2ban的配置文件
/etc/fail2ban/jail.conf #阻挡设定文件
/etc/fail2ban/filter.d/ #具体过滤规则文件目录
/etc/fail2ban/action.d/ #具体过滤规则检测到后采取相对应措施的目录

很多参数其实默认里面都有了,我们要做的就是设置enable属性为true即可启动

fail2ban其它

监控nginx日志

假设nginx默认日志路径为/var/log/nginx/access_log,需要防止黑客暴力破解HTTP FORM登陆,此登陆检测链接为

http://test.com/login_check.do
根据分析正常登陆一般不超过三四次,并且登陆时间一般也不超过一分钟;因此,规定检测周期为1分钟,最大尝试登陆次数为10次;在规定时间内超过指定次数则被认为是黑客在尝试暴力破解。

具体设置方法如下:

1) 首先在jail.conf文件下追加以下内容:

[nginx] ;规则名字
enabled = true ;是否户用
port = http,https ;监控端口
filter = nginx ;需要过滤匹配规则
logpath = /var/log/nginx/access_log; 日志路径
findtime =60 ;检测周期 单位秒 以下一样
bantime =300 ;iptable封禁IP时间
maxretry =10 ;最大尝试次数
action = iptables[name=nginx, port=http, protocal=tcp] ;发现暴力破解采取iptalbes封禁IP的措施
sendmail[name=nginx, dest=my-email@xx.com
/* 2)然后创建 /etc/fail2ban/filter.d/nginx.conf文件,并添加以下内容:

[Definition]
failregex =.*-.*-.*POST.*/login_check.do.* HTTP\/1.*http://test.com.*$ ;需要匹配日志发现攻击行为的正则,为fail2ban内置变量匹配IP,不可修改
ignoreregex = ;需要忽略的正则
完成上述步骤就可以运行命令/etc/init.d/fail2ban restart重启了。查看iptables有fail2ban-nginx的规则和收到sendmail发送fail2ban已经启动的邮件就说明OK了。

不过运维的同学可能知道,sendmail发送邮件延迟很多,并不好用,使用mutt代替sendmail是个不错的选择。安装mutt的过程就不在时阐述了,这里介绍我修改使用mutt发送action的配置文件。

1)首先创建一个/etc/fail2ban/action.d/mutt.conf文件,然后添加以下内容:

# Fail2Ban configuration file
#
# Author: Cyril Jaquier
#
#
[Definition]
# Option: actionstart
# Notes.: command executed once at the start of Fail2Ban.
# Values: CMD
#
actionstart = printf %%b "Hi,\n
The jail has been started successfully.\n
Regards,\n
Fail2Ban"|mutt -s "[Fail2Ban] : started on `uname -n`"
# Option: actionstop
# Notes.: command executed once at the end of Fail2Ban
# Values: CMD
#
actionstop = printf %%b "Hi,\n
The jail has been stopped.\n
Regards,\n
Fail2Ban"|mutt -s "[Fail2Ban] : stopped on `uname -n`"
# Option: actioncheck
# Notes.: command executed once before each actionban command
# Values: CMD
#
actioncheck =
# Option: actionban
# Notes.: command executed when banning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionban = printf %%b "Hi,\n
The IP has just been banned by Fail2Ban after
attempts against .\n
Regards,\n
Fail2Ban"|mutt -s "[Fail2Ban] : banned from `uname -n`"
# Option: actionunban
# Notes.: command executed when unbanning an IP. Take care that the
# command is executed with Fail2Ban user rights.
# Tags: See jail.conf(5) man page
# Values: CMD
#
actionunban =
[Init]
# Default name of the chain
#
name = default
# Destination/Addressee of the mutt
#
dest = root
2)然后在jail.conf文件下添加以下内容:

action = mutt[name=nginx, dest=my-email@xx.com
/* 重启过后就可以使用mutt文件发送邮件了。

赞(0) 打赏

上一篇:

下一篇:

相关推荐

0 条评论关于"VpsAdd教程:Debian下安装fail2ban进行主动防御vps"

表情

最新评论

    暂无留言哦~~

支付宝扫一扫打赏

微信扫一扫打赏