如何配置fail2ban来保护Apache HTTP服务器
生产环境中的 Apache Web 服务器可能会受到各种不同方式的攻击。攻击者可能会尝试通过使用暴力攻击或执行恶意脚本来访问未经授权或禁止的目录。一些恶意机器人可能会扫描您的网站是否存在任何安全漏洞,或收集电子邮件地址或网络表单以发送垃圾邮件。
Apache HTTP 服务器具有全面的日志记录功能,可以捕获表明此类攻击的各种异常事件。然而,系统地解析详细的 Apache 日志并快速对潜在攻击做出反应(例如,禁止/取消禁止违规 IP 地址)仍然很重要,因为这些攻击是在野外进行的。这时,fail2ban 就派上用场了,让系统管理员的工作变得更轻松。
fail2ban 是一款开源入侵防御工具,可根据系统日志检测各种攻击并自动启动防御操作,例如使用 iptables 禁止 IP 地址、通过 /etc/hosts.deny 阻止连接或通过电子邮件通知事件。 fail2ban 附带一组预定义的jails,它们使用特定于应用程序的日志过滤器来检测常见攻击。您还可以编写自定义监狱来阻止对任意应用程序的任何特定攻击。
在本教程中,我将演示如何配置 fail2ban 来保护您的 Apache HTTP 服务器。我假设您已经安装了 Apache HTTP 服务器和 fail2ban。请参考另一个fail2ban安装教程。
什么是 Fail2ban 监狱?
让我详细介绍一下 fail2ban 监狱。 Jail 定义了特定于应用程序的策略,在该策略下,fail2ban 会触发一个操作来保护给定的应用程序。 fail2ban 附带了 /etc/fail2ban/jail.conf 中预定义的多个监狱,适用于 Apache、Dovecot、Lighttpd、MySQL、Postfix、SSH 等流行应用程序。每个监狱都依赖于特定于应用程序的日志过滤器(可在 /etc/fail2ban/fileter.d 中找到)来检测常见攻击。让我们看一下一个监狱示例:SSH监狱。
[ssh]
enabled = true
port = ssh
filter = sshd
logpath = /var/log/auth.log
maxretry = 6
banaction = iptables-multiport
这个 SSH 监狱配置是用几个参数定义的:
[ssh]:带方括号的监狱名称。enabled:监狱是否被激活。port:要保护的端口号(众所周知的名称的数字)。filter:用于检测攻击的日志解析规则。logpath:要检查的日志文件。maxretry:禁止前的最大失败次数。banaction:禁止操作。
监狱配置中定义的任何参数都将覆盖相应的 fail2ban 范围内的默认参数。相反,任何缺少的参数都将被分配在 [DEFAULT] 部分中定义的默认值。
预定义的日志过滤器位于 /etc/fail2ban/filter.d 中,可用操作位于 /etc/fail2ban/action.d 中。
如果您想覆盖 fail2ban 默认值或定义任何自定义监狱,您可以通过创建 /etc/fail2ban/jail.local 文件来实现。在本教程中,我将使用 /etc/fail2ban/jail.local。
启用预定义的 Apache Jails
fail2ban 的默认安装为 Apache HTTP 服务器提供了几个预定义的监狱和过滤器。我将启用那些内置的 Apache Jail。由于 Debian 和 Red Hat 配置之间存在细微差异,我将分别提供它们的 fail2ban 监狱配置。
在 Debian 或 Ubuntu 上启用 Apache Jails
要在基于 Debian 的系统上启用预定义的 Apache Jail,请创建 /etc/fail2ban/jail.local,如下所示。
$ sudo vi /etc/fail2ban/jail.local
# detect password authentication failures
[apache]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache*/*error.log
maxretry = 6
# detect potential search for exploits and php vulnerabilities
[apache-noscript]
enabled = true
port = http,https
filter = apache-noscript
logpath = /var/log/apache*/*error.log
maxretry = 6
# detect Apache overflow attempts
[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/apache*/*error.log
maxretry = 2
# detect failures to find a home directory on a server
[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 中定义)。此操作使用 iptables 和 multiport 模块禁止 IP 地址。
启用监狱后,您必须重新启动 fail2ban 才能加载监狱。
$ sudo service fail2ban restart
在 CentOS/RHEL 或 Fedora 上启用 Apache Jails
要在基于 Red Hat 的系统上启用预定义的 Apache Jail,请创建 /etc/fail2ban/jail.local,如下所示。
$ sudo vi /etc/fail2ban/jail.local
# detect password authentication failures
[apache]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/httpd/*error_log
maxretry = 6
# detect spammer robots crawling email addresses
[apache-badbots]
enabled = true
port = http,https
filter = apache-badbots
logpath = /var/log/httpd/*access_log
bantime = 172800
maxretry = 1
# detect potential search for exploits and php vulnerabilities
[apache-noscript]
enabled = true
port = http,https
filter = apache-noscript
logpath = /var/log/httpd/*error_log
maxretry = 6
# detect Apache overflow attempts
[apache-overflows]
enabled = true
port = http,https
filter = apache-overflows
logpath = /var/log/httpd/*error_log
maxretry = 2
# detect failures to find a home directory on a server
[apache-nohome]
enabled = true
port = http,https
filter = apache-nohome
logpath = /var/log/httpd/*error_log
maxretry = 2
# detect failures to execute non-existing scripts that
# are associated with several popular web services
# e.g. 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)。此操作使用 iptables 和 multiport 模块禁止 IP 地址。
启用监狱后,您必须重新启动fail2ban才能加载fail2ban中的监狱。
在 Fedora 或 CentOS/RHEL 7 上:
$ sudo systemctl restart fail2ban
在 CentOS/RHEL 6 上:
$ sudo service fail2ban restart
检查和管理 fail2ban 禁止状态
激活监狱后,您可以使用 fail2ban-client 命令行工具监控当前的禁止状态。
要查看活动的监狱列表:
$ sudo fail2ban-client status
查看特定监狱的状态(包括禁止的 IP 列表):
$ sudo fail2ban-client status [name-of-jail]

您还可以手动禁止或取消禁止 IP 地址。
要禁止具有特定监狱的 IP 地址:
$ sudo fail2ban-client set [name-of-jail] banip [ip-address]
要取消禁止特定监狱阻止的 IP 地址:
$ sudo fail2ban-client set [name-of-jail] unbanip [ip-address]
概括
本教程介绍了 fail2ban 监狱的工作原理以及如何使用内置 Apache 监狱来保护 Apache HTTP 服务器。根据您的环境和需要保护的 Web 服务类型,您可能需要调整现有的监狱,或编写自定义监狱和日志过滤器。查看fail2ban 的官方 Github 页面,了解更多最新的监狱和过滤器示例。
您是否在任何生产环境中使用 fail2ban ?分享您的经验。