fail2ban能够监视你的系统日志,而后匹配日志的错误信息(正则式匹配)执行相应的屏蔽动做(通常状况下是调用防火墙iptables屏蔽),如:当有人在试探你的SSH、SMTP、FTP密码,只要达到你预设的次数,fail2ban就会调用防火墙屏蔽这个IP,并且能够发送e-mail通知系统管理员,是一款很实用、很强大的软件!php
fail2ban由python语言开发,基于logwatch、gamin、iptables、tcp-wrapper、shorewall等。若是想要发送邮件通知道,那还须要安装postfix或sendmail。python
添加密码策略,增长密码复杂度nginx
修改ssh登陆端口web
禁止root登陆bash
配置fail2ban,拒绝失败链接app
下载地址:http://www.fail2ban.org/wiki/index.php/Downloadsssh
安装要求:tcp
Required:
- [Python2 >= 2.6 or Python >= 3.2](http://www.python.org) or [PyPy](http://pypy.org)模块化
安装:post
tar xvfj fail2ban-0.9.4.tar.bz2 cd fail2ban-0.9.4 python setup.py install cp files/debian-initd /etc/init.d/fail2ban update-rc.d fail2ban defaults service fail2ban start 添加开机启动 chkconfig --add fail2ban chkconfig --list fail2ban 启动 /etc/init.d/fail2ban start fail2ban-client status ssh-iptables
安装完成后,服务配置目录为:/etc/fail2ban
/etc/fail2ban/action.d #动做文件夹,内含默认文件。iptables以及mail等动做配置
/etc/fail2ban/fail2ban.conf #定义了fai2ban日志级别、日志位置及sock文件位置
/etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启用ban动做的服务及动做阀值
/etc/rc.d/init.d/fail2ban #启动脚本文件
ssh远程登陆5分钟内3次密码验证失败,禁止用户IP访问1小时,
一分钟后解除限制。
[DEFAULT] #全局设置
ignoreip = 127.0.0.1 #忽略的IP列表,不受设置限制(白名单)
bantime = 3600 #屏蔽时间,单位:秒
findtime = 300 #这个时间段内超过规定次数会被ban掉
maxretry = 3 #最大尝试次数
backend = auto #日志修改检测机制(gamin、polling和auto这三种)
[ssh-iptables] #针对各服务的检查配置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置
enabled = true #是否激活此项(true/false)
filter = sshd #过滤规则filter的名字,对应filter.d目录下的sshd.conf
action = iptables[name=SSH, port=ssh, protocol=tcp] #动做的相关参数
sendmail-whois[name=SSH, dest=root, sender=fail2ban@example.com] #触发报警的收件人
logpath = /var/log/secure #检测的系统的登录日志文件
maxretry = 5 #最大尝试次数
service fail2ban restart
链接三次失败,第四次
[root@redbull ~]# ssh root@192.168.128 ssh: connect to host 192.168.128 port 22: Connection refused [root@redbull fail2ban]# fail2ban-client status ssh-iptables Status for the jail: ssh-iptables |- Filter | |- Currently failed: 0 | |- Total failed: 20 | `- File list: /var/log/secure `- Actions |- Currently banned: 2 |- Total banned: 3 `- Banned IP list: 192.168.5.128 192.168.5.131
监控pop、http等服务:
[pop3] enabled = true filter = courierlogin action = iptables[name=pop3, port=110, protocol=tcp] logpath = /var/log/maillog bantime = 1800 findtime = 300 maxretry = 30 [webmail] enabled = true filter = webmail action = iptables[name=httpd, port=http, protocol=tcp] logpath = /var/log/maillog bantime = 900 findtime = 300 maxretry = 5
fail2ban读取nginx日志禁止非法ip访问
在fail2ban配置文件(/etc/fail2ban/fail2ban.conf)中添加如下记录: [yunvn-get-dos] enabled = true port = http,https filter = nginx-bansniffer action = iptables[name=IT300, port=http, protocol=tcp] #sendmail-whois[name=IT300, dest=xxxxx@qq.com, sender=xxxxxx@163.com] logpath = /home/wwwlogs/access.log maxretry = 300 findtime = 60 bantime = 3600
修改/etc/fail2ban/action.d/iptables.conf actionban = iptables -I INPUT -p all -s -j DROP actionunban = iptables -D INPUT -p all -s -j DROP