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

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

    logpath  = /var/log/httpd/*error_log
    maxretry = 6
    # 检测抓取邮件地址的爬虫
    [apache-badbots]
    enabled  = true
    port     = http,https
    filter   = apache-badbots
    logpath  = /var/log/httpd/*access_log
    bantime  = 172800
    maxretry = 1
    # 检测漏洞和 PHP 脆弱性扫描
    [apache-noscript]
    enabled  = true
    port     = http,https
    filter   = apache-noscript
    logpath  = /var/log/httpd/*error_log
    maxretry = 6
    # 检测 Apache 溢出攻击
    [apache-overflows]
    enabled  = true
    port     = http,https
    filter   = apache-overflows
    logpath  = /var/log/httpd/*error_log
    maxretry = 2
    # 检测在服务器寻找主目录的尝试
    [apache-nohome]
    enabled  = true
    port     = http,https
    filter   = apache-nohome
    logpath  = /var/log/httpd/*error_log
    maxretry = 2
    # 检测执行不存在的脚本的企图
    # 这些都是流行的网站服务程序
    # 如:webmail, phpMyAdmin,WordPress
    port     = http,https
    filter   = apache-botsearch
    logpath  = /var/log/httpd/*error_log
    maxretry = 2

注意这些监狱文件默认的操作是iptables-multiport(定义在/etc/fail2ban/jail.conf中[DEFAULT]字段下的“banaction”中)。这个措施使用iptable的多端口模块禁止一个IP地址。

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

在 Fedora 或者 CentOS/RHEL 7中:

  $ sudo systemctl restart fail2ban 

在 CentOS/RHEL 6中:

  $ sudo service fail2ban restart 

检查和管理fail2ban禁止状态

监狱一旦激活后,你可以用fail2ban的客户端命令行工具来监测当前的禁止状态。

查看激活的监狱列表:

  $ sudo fail2ban-client status 

查看特定监狱的状态(包含禁止的IP列表):

  $ sudo fail2ban-client status [监狱名] 

201561195749494.jpg (800×298)