Fail2ban就是一款软件,一般主要是通过监控分析日志来更新防火墙规则从而拒绝一些IP地址达到保护服务器的目的。比如尝试后台密码,寻×××器漏洞等。

    安装需求:Python2.4版本以上,防火墙软件一种iptables或者shorewall。

    安装方法:ubuntu14.04

        sudo apt-get install fail2ban

    理解几个关键词语:

        filter:用来定义一系列正则表达式去匹配log中的内容,实际上可理解为一个过滤器;

        action:用来定义一些列命令用以在不同的情况下执行,就是一个执行动作

        jail:这个就是用来把一个filter和一个或者诺干个action结合起来,每当log中有filter能匹配就去执行action,这可以说是fail2ban这个软件的基本原理。

        client:对应fail2ban-client这个脚本命令

        server:对应fail2ban-server这个脚本命令

    Fail2ban分为两个部分:服务端和客户端,服务端是一个多线程的,监听命令的socket,本身对配置文件是没有任何联系的,而客户端是Fail2ban的前端,它连接到服务端的socket负责读取配置文件并且发送配置文件中的命令给服务端。

    fail2ban-server       

        -b                   start in background
        -f                   start in foreground
        -s <FILE>            socket path
        -x                   force execution of the server
        -h, --help           display this help message
        -V, --version        print the version

    fail2ban-client

        -c <DIR>                configuration directory
        -s <FILE>               socket path
        -d                      dump configuration. For debugging 备份配置文件
        -i                      interactive mode
        -v                      increase verbosity
        -q                      decrease verbosity
        -x                      force execution of the server
        -h, --help              display this help message
        -V, --version           print the version

    fail2ban-client start    

    fail2ban-client reload    重新加载配置文件

    fail2ban-client status [jailname]    查看jail状态

    配置文件目录:

        /etc/fail2ban/
├── action.d
│   ├── dummy.conf
│   ├── hostsdeny.conf
│   ├── iptables.conf
│   ├── mail-whois.conf
│   ├── mail.conf
│   └── shorewall.conf
├── fail2ban.conf
├── fail2ban.local
├── filter.d
│   ├── apache-auth.conf
│   ├── apache-noscript.conf
│   ├── couriersmtp.conf
│   ├── postfix.conf
│   ├── proftpd.conf
│   ├── qmail.conf
│   ├── sasl.conf
│   ├── sshd.conf
│   └── vsftpd.conf
├── jail.conf
└── jail.local

    一般配置:

    jails:(举例)

        [ssh-iptables]
        #enabled  = false    
        enabled  = true    代表开启这个jail
        filter   = sshd    代表结合的filter过滤器,在/etc/fail2ban/filter.d目录下
        action   = iptables[name=SSH, port=ssh, protocol=tcp]    代表结合的执行动作,在/etc/fail2ban/action.d下
        #          mail-whois[name=SSH, dest=yourmail@mail.com]
        #logpath  = /var/log/sshd.log    代表监控的日志路径
        logpath  = /var/log/auth.log
        maxretry = 5    代表最大的尝试次数,一旦filter匹配上的超过这个次数将会执行action

        findtime    表示在这个时间段内发生的匹配次数,如果没有达到action的执行条件则会重新归零,单位是s

        bantime    代表IP地址被ban住的时间,负数代表永久,单位是s

    Filters:(举例)

        failregex = Authentication failure for .* from <HOST>
                    Failed [-/\w]+ for .* from <HOST>
                    ROOT LOGIN REFUSED .* FROM <HOST>
                    [iI](?:llegal|nvalid) user .* from <HOST>    这就是匹配日志的正则表达式

    actions:(举例)

        [INCLUDES]
        before = iptables-blocktype.conf

        [Definition]

        actionstart = iptables -N fail2ban-<name>
              iptables -A fail2ban-<name> -j RETURN
              iptables -I <chain> -m state --state NEW -p <protocol> --dport <port> -j fail2ban-<name>

             actionstop = iptables -D <chain> -m state --state NEW -p <protocol> --dport <port> -j fail2ban-<name>
             iptables -F fail2ban-<name>
             iptables -X fail2ban-<name>

        actioncheck = iptables -n -L <chain> | grep -q 'fail2ban-<name>[ \t]'

        actionban = iptables -I fail2ban-<name> 1 -s <ip> -j <blocktype>

        actionunban = iptables -D fail2ban-<name> -s <ip> -j <blocktype>

        [Init]

        name = default

        port = ssh

        protocol = tcp

        chain = INPUT


        fail2ban-regex "line" "failregex"    进行语法检查的格式

        (举例)

        fail2ban-regex /var/log/auth.log "Failed [-/\w]+ for .* from <HOST>"

        解除ban住的IP可用命令:fail2ban-client set YOURJAILNAMEHERE unbanip IPADDRESSHERE(本人测试过没有问题,可能版本或者系统不同会有差异)