配置fail2ban对Apache服务器进行安全防护的方法

2019-10-14 10:37:34于丽

启用预定义的apache监狱

fail2ban的默认安装为Apache服务提供了一些预定义监狱和过滤器。我要启用这些内建的Apache监狱。由于Debian和RedHat配置的稍微不同,我会分别提供它们的配置文件。
在Debian 或者 Ubuntu启用Apache监狱

要在基于Debian的系统上启用预定义的apache监狱,如下创建/etc/fail2ban/jail.local。

  $ sudo vi /etc/fail2ban/jail.local 

    # 检测密码认证失败
    [apache]
    enabled  = true
    port     = http,https
    filter   = apache-auth
    logpath  = /var/log/apache*/*error.log
    maxretry = 6
    # 检测漏洞和 PHP 脆弱性扫描
    [apache-noscript]
    enabled  = true
    port     = http,https
    filter   = apache-noscript
    logpath  = /var/log/apache*/*error.log
    maxretry = 6
    # 检测 Apache 溢出攻击
    [apache-overflows]
    enabled  = true
    port     = http,https
    filter   = apache-overflows
    logpath  = /var/log/apache*/*error.log
    maxretry = 2
    # 检测在服务器寻找主目录的尝试
    [apache-nohome]
    enabled  = true
    port     = http,https
    filter   = apache-nohome
    logpath  = /var/log/apache*/*error.log
    maxretry = 2

由于上面的监狱没有指定措施,这些监狱都将会触发默认的措施。要查看默认的措施,在/etc/fail2ban/jail.conf中的[DEFAULT]下找到“banaction”。

    banaction = iptables-multiport

本例中,默认的操作是iptables-multiport(定义在/etc/fail2ban/action.d/iptables-multiport.conf)。这个措施使用iptable的多端口模块禁止一个IP地址。

在启用监狱后,你必须重启fail2ban来加载监狱。

  $ sudo service fail2ban restart 

在CentOS/RHEL 或者 Fedora中启用Apache监狱

要在基于红帽的系统中启用预定义的监狱,如下创建/etc/fail2ban/jail.local。

  $ sudo vi /etc/fail2ban/jail.local 

    # 检测密码认证失败
    [apache]
    enabled  = true
    port     = http,https
    filter   = apache-auth