防暴力破解 Fail2Ban之python

fai2ban的介绍

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安装与配置操作实例

1:安装epel更新源:http://fedoraproject.org/wiki/EPEL/zh-cn

# yum install shorewall gamin-python shorewall-shell shorewall-perl shorewall-common python-inotify python-ctypes fail2ban 

# yum install gamin-python python-inotify python-ctypes
# wget http://dl.fedoraproject.org/pub/epel/6/i386/fail2ban-0.8.11-2.el6.noarch.rpm
# rpm -ivh fail2ban-0.8.11-2.el6.noarch.rpm 

# yum install gamin-python python-inotify python-ctypes
# wget http://ftp.sjtu.edu.cn/fedora/epel//5/i386/fail2ban-0.8.4-29.el5.noarch.rpm
# rpm -ivh fail2ban-0.8.4-29.el5.noarch.rpm 

2:源码包安装

# wget https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0
# tar -xzvf fail2ban-0.9.0.tar.gz 

12345678910111213141516171819202122232425262728293031 /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          #启动脚本文件 

3. vi /etc/fail2ban/fail2ban.conf 

[Definition]
loglevel =3
logtarget = SYSLOG  #我们需要做的就是把这行改成/var/log/fail2ban.log,方便用来记录日志信息
socket =/var/run/fail2ban/fail2ban.sock
4. vi /etc/fail2ban/jail.conf 

[DEFAULT]              #全局设置
ignoreip = 127.0.0.1      #忽略的IP列表,不受设置限制
bantime  = 600            #屏蔽时间,单位:秒
findtime  = 600           #这个时间段内超过规定次数会被ban掉
maxretry = 3              #最大尝试次数
backend = auto            #日志修改检测机制(gamin、polling和auto这三种) 

[sshd]                     #单个服务检查设置,如设置bantime、findtime、maxretry和全局冲突,服务优先级大于全局设置。
enabled  = true               #是否激活此项(true/false)
filter   = sshd               #过滤规则filter的名字,对应filter.d目录下的sshd.conf
action   = iptables[name=SSH, port=ssh, protocol=tcp]#动作的相关参数,对应action.d/iptables.conf文件
logpath  = /var/log/secure     #检测的日志文件path
bantime  = 3600
findtime  = 300
maxretry = 3                   #最大尝试次数   

service fail2ban start 启动服务 

4.解除fail2ban绑定的IP

查询限制列表

# iptables -L --line-numbers

Chain fail2ban-SSH (1references)

num  target    prot opt source            destination

1    DROP      all  -- 118.152.158.61.ha.cnc anywhere

2    RETURN    all  -- anywhere           anywhere

解除限制

# iptables -D fail2ban-SSH 我们主要编辑jail.conf这个配置文件,其他的不要去管它

# vi /etc/fail2ban.conf

SSH防攻击规则

ssh-iptables]enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
           sendmail-whois[name=SSH, dest=root, sender=[email protected], sendername="Fail2Ban"]
logpath  = /var/log/secure
maxretry = 5
[ssh-ddos]
enabled = true
filter  = sshd-ddos
action  = iptables[name=ssh-ddos, port=ssh,sftp protocol=tcp,udp]
logpath  = /var/log/messages
maxretry = 2
[osx-ssh-ipfw]
enabled  = true
filter   = sshd
action   = osx-ipfw
logpath  = /var/log/secure.log
maxretry = 5
[ssh-apf]
enabled = true
filter  = sshd
action  = apf[name=SSH]
logpath = /var/log/secure
maxretry = 5
[osx-ssh-afctl]
enabled  = true
filter   = sshd
action   = osx-afctl[bantime=600]
logpath  = /var/log/secure.log
maxretry = 5
[selinux-ssh]
enabled = true
filter  = selinux-ssh
action  = iptables[name=SELINUX-SSH, port=ssh, protocol=tcp]
logpath  = /var/log/audit/audit.log
maxretry = 5 

1 <BR> 

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 proftp防攻击规则 

[proftpd-iptables]
enabled  = true
filter   = proftpd
action   = iptables[name=ProFTPD, port=ftp, protocol=tcp]
           sendmail-whois[name=ProFTPD, dest=[email protected]]
logpath  = /var/log/proftpd/proftpd.log
maxretry = 6 

邮件防攻击规则 

[sasl-iptables]
enabled  = true
filter   = postfix-sasl
backend  = polling
action   = iptables[name=sasl, port=smtp, protocol=tcp]
           sendmail-whois[name=sasl, dest=[email protected]]
logpath  = /var/log/mail.log
[dovecot]
enabled = true
filter  = dovecot
action  = iptables-multiport[name=dovecot, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
logpath = /var/log/mail.log
[dovecot-auth]
enabled = true
filter  = dovecot
action  = iptables-multiport[name=dovecot-auth, port="pop3,pop3s,imap,imaps,submission,smtps,sieve", protocol=tcp]
logpath = /var/log/secure
[perdition]
enabled = true
filter  = perdition
action  = iptables-multiport[name=perdition,port="110,143,993,995"]
logpath = /var/log/maillog 

[uwimap-auth]
enabled = true
filter  = uwimap-auth
action  = iptables-multiport[name=uwimap-auth,port="110,143,993,995"]
logpath = /var/log/maillog 

apache防攻击规则 

[apache-tcpwrapper]
enabled  = true
filter  = apache-auth
action   = hostsdeny
logpath  = /var/log/httpd/error_log
maxretry = 6
[apache-badbots]
enabled  = true
filter   = apache-badbots
action   = iptables-multiport[name=BadBots, port="http,https"]
           sendmail-buffered[name=BadBots, lines=5, dest=[email protected]]
logpath  = /var/log/httpd/access_log
bantime  = 172800
maxretry = 1
[apache-shorewall]
enabled  = true
filter   = apache-noscript
action   = shorewall
           sendmail[name=Postfix, dest=[email protected]]
logpath  = /var/log/httpd/error_log 

nginx防攻击规则 

[nginx-http-auth]
enabled = true
filter  = nginx-http-auth
action  = iptables-multiport[name=nginx-http-auth,port="80,443"]
logpath = /var/log/nginx/error.log

lighttpd防规击规则 

[suhosin]
enabled  = true
filter   = suhosin
action   = iptables-multiport[name=suhosin, port="http,https"]
# adapt the following two items as needed
logpath  = /var/log/lighttpd/error.log
maxretry = 2
[lighttpd-auth]
enabled  = true
filter   = lighttpd-auth
action   = iptables-multiport[name=lighttpd-auth, port="http,https"]
# adapt the following two items as needed
logpath  = /var/log/lighttpd/error.log
maxretry = 2 

vsftpd防攻击规则 

[vsftpd-notification]
enabled  = true
filter   = vsftpd
action   = sendmail-whois[name=VSFTPD, dest=[email protected]]
logpath  = /var/log/vsftpd.log
maxretry = 5
bantime  = 1800
[vsftpd-iptables]
enabled  = true
filter   = vsftpd
action   = iptables[name=VSFTPD, port=ftp, protocol=tcp]
           sendmail-whois[name=VSFTPD, dest=[email protected]]
logpath  = /var/log/vsftpd.log
maxretry = 5
bantime  = 1800 

pure-ftpd防攻击规则 

[pure-ftpd]
enabled  = true
filter   = pure-ftpd
action   = iptables[name=pure-ftpd, port=ftp, protocol=tcp]
logpath  = /var/log/pureftpd.log
maxretry = 2
bantime  = 86400 

mysql防攻击规则 

[mysqld-iptables]
enabled  = true
filter   = mysqld-auth
action   = iptables[name=mysql, port=3306, protocol=tcp]
           sendmail-whois[name=MySQL, dest=root, sender=[email protected]]
logpath  = /var/log/mysqld.log
maxretry = 5 

apache phpmyadmin 防攻击规则 

[apache-phpmyadmin]
enabled  = true
filter   = apache-phpmyadmin
action  = iptables[name=phpmyadmin, port=http,https protocol=tcp]
logpath  = /var/log/httpd/error_log
maxretry = 3
# /etc/fail2ban/filter.d/apache-phpmyadmin.conf
将以下内容粘贴到apache-phpmyadmin.conf里保存即可以创建一个apache-phpmyadmin.conf文件.
# Fail2Ban configuration file
#
# Bans bots scanning for non-existing phpMyAdmin installations on your webhost.
#
# Author: Gina Haeussge
# 

[Definition] 

docroot = /var/www
badadmin = PMA|phpmyadmin|myadmin|mysql|mysqladmin|sqladmin|mypma|admin|xampp|mysqldb|mydb|db|pmadb|phpmyadmin1|phpmyadmin2 

# Option:  failregex
# Notes.:  Regexp to match often probed and not available phpmyadmin paths.
# Values:  TEXT
#
failregex = [[]client []] File does not exist: %(docroot)s/(?:%(badadmin)s) 

# Option:  ignoreregex
# Notes.:  regex to ignore. If this regex matches, the line is ignored.
# Values:  TEXT
#
ignoreregex =
# service fail2ban restart 

写在最后,在安装完fail2ban后请立即重启一下fail2ban,看是不是能正常启动,因为在后边我们配置完规则后如果发生无法启动的问题我们可以进行排查.如果安装完后以默认规则能够正常启动,而配置完规则后却不能够正常启动,请先检查一下你 /var/log/ 目录下有没有规则里的 logpath= 后边的文件,或者这个文件的路径与规则里的是不是一致. 如果不一致请在 logpath 项那里修改你的路径, 如果你的缓存目录里没有这个文件,那么请你将该配置项的 enabled 项目的值设置为 false. 然后再进行重启fail2ban,这样一般不会有什么错误了 
时间: 2024-10-27 05:14:52

防暴力破解 Fail2Ban之python的相关文章

Centos6.4 安装fail2ban防暴力破解

Centos6.4 安装fail2ban防暴力破解 一. 安装 curl -O https://codeload.github.com/fail2ban/fail2ban/tar.gz/0.9.0 mv 0.9.0 0.9.0.tar.gz tar zxvf 0.9.0.tar.gz cd fail2ban-0.9.0/ python setup.py build python setup.py install 二.配置(防ssh暴力) vi /etc/fail2ban/jail.conf [s

SSH防止暴力破解--fail2ban

一.ssh密钥对无交互登录 实战1:通过密钥进行sshd服务认证 服务端:linl_S    IP:10.0.0.15 客户端:lin_C    IP:10.0.0.16 1)在客户端生成密钥对 命令:ssh-keygen 1 [[email protected]_C ~]# ssh-keygen 2 Generating public/private rsa key pair. 3 Enter file in which to save the key (/root/.ssh/id_rsa):

SSH防暴力破解的解决方法

SSH防暴力破解的解决方法: 1.禁止root用户ssh登陆: 1.1.修改PermitRootLogin项: [[email protected] ~]# vi /etc/ssh/sshd_config [[email protected] ~]# grep Root /etc/ssh/sshd_config PermitRootLogin no ### 将默认的 #PermitRootLogin yes 修改成这样的 ### # the setting of "PermitRootLogin

防暴力破解一些安全机制

今天一个朋友突然QQ上找我说,网站被攻击了,整个网站内容被替换成违法信息,听到这个消息后着实吓了一跳.于是赶紧去找原因,最后才发现由于对方网站管理密码过于简单,被暴力破解了..在此我把对于防暴力破解预防一些心得分享给朋友们.首先给用户的建议是尽量使用复杂字符组合,例如数字和英文大小写组合等..给开发人员建议第一:建立验证码机制,验证码虽然也能被破掉,但在一定程度上也增加了暴力破解的难度:第二:建立一套完善的防暴力破解安全机制,如连续3次输入错误,锁定账户,24小时内无法再次登录:第三:如果用户绑

SSH防暴力破解shell脚本

写的一个SSH的防暴力破解脚本. 原理是检测SSH连接日志,过滤登录失败的IP,超过登录次数就将其添加进hosts.deny文件中,限制其登录. 脚本如下: #! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /root/black.txt DEFINE="10" for i in `cat  /root/bla

YS登录接口防暴力破解设计缺陷

YS使用的防暴力破解机制存在缺陷,该缺陷可被用于暴力破解其它用户密码[高] 问题描述:          YS在用户登录页面设置了验证码机制,当用户输入密码错误次数达到3次时,再次登录需要验证码以防止攻击者进行暴力破解,但在需要验证码的情形下,当用户成功认证登录之后,该验证码机制将失效,此时攻击者可以无数次向服务器重放用于登录认证的http请求. 测试步骤: 1.  安装浏览器拦截代理工具burp,并启动拦截功能. 2.  在用户名和密码栏填入有效的帐户名和无效的密码,登录失败3次直至出现验证码

Linux 系统安全 -- 防暴力破解

fail2ban防止暴力破解 1. 下载stable稳定版解压缩1 2. 编译安装1 3. 拷贝启动脚本1 4. 修改配置文件1 5. 启动fail2ban2 6. 测试,ssh连接,连续输入3次密码2 7. 此时在查看服务端的防火墙和failban状态2 8. 解锁被failban禁止登录的ip3 9. 自动部署的脚本3  防止暴力破解的一般方法: 1) 密码足够复杂  2)修改端口号  3) 禁用root登  4)第三方防爆破软件 fail2ban实现锁IP 说明:监视系统日志,然后通过匹配

Linux VPS 安全配置:禁用22端口、root用户以及配置Denyhosts防暴力破解

最近租用了一台Vultr东京机房的VPS,每天都会生成许多异常登录失败的日志,疑似受到扫描软件的暴力破解,遂Google了一下服务器安全防护方面的知识. 废话不多说,下面将操作过程记录下来: 注意:以下操作基于CentOS 7,不同系统下的具体操作可能会有区别 一.修改SSH服务默认端口 SSH服务的默认端口是22,扫描软件首先会通过此端口尝试登录,因此把它改成一个不易猜到的端口(推荐使用10000~65535之间的,复杂度最高也不易和其他进程发生冲突) 1 vim /etc/ssh/sshd_

linux防暴力破解远程密码神器-fail2ban

安装:fail2ban [[email protected] ~]# tar  -zxvf fail2ban-0.8.14.tar.gz [[email protected] fail2ban-0.8.14]# cdfail2ban-0.8.14 查看安装指南README.md [[email protected] ~]# cdfail2ban-0.8.14 [[email protected]]#python setup.py install 生成服务启动脚本: [[email protect