如何设置 Fail2ban 保护您的服务器免受暴力破解

Fail2ban 是一款入侵防御软件,可以保护您的服务器免受暴力攻击。它通过监视日志文件(例如 /var/log/auth.log/var/log/apache/access.log 等)并根据配置的规则采取行动来实现。

1. 安装 Fail2ban (如果尚未安装):

通常,Fail2ban 可以通过您的 Linux 发行版的包管理器安装。以下是一些常见发行版的示例:

  • Debian/Ubuntu:
    sudo apt update
    sudo apt install fail2ban
    
  • CentOS/RHEL/Fedora:
    sudo yum install epel-release  # 如果尚未安装 EPEL
    sudo yum install fail2ban
    
  • Arch Linux:
    sudo pacman -S fail2ban
    

2. 理解配置文件:

Fail2ban 的主要配置文件是:

  • /etc/fail2ban/jail.conf: 这是主配置文件。不要直接编辑此文件,因为它可能会在软件包更新时被覆盖。
  • /etc/fail2ban/jail.local: 这是您应该放置自定义配置的地方。此文件中的设置将覆盖 jail.conf 中的默认值。
  • /etc/fail2ban/jail.d/: 您也可以在此目录中创建单独的 .conf 文件来组织您的配置(例如,sshd.confapache.conf)。

3. 创建和编辑 jail.local (您提供的命令):

命令 sudo vim /etc/fail2ban/jail.local 是使用 vim 文本编辑器以 root 权限打开(或创建)jail.local 文件。

  • sudo: 以超级用户(root)权限运行命令。这对于修改系统配置文件是必需的。
  • vim: 一个流行的基于终端的文本编辑器。您可以使用任何您喜欢的文本编辑器(例如,nanogedit 等)。
  • /etc/fail2ban/jail.local: 要编辑的配置文件的路径。

如果是第一次创建这个文件,它将是空的。如果它已经存在,它可能包含一些默认设置或之前所做的更改。

4. 配置 jail.local (示例):

以下是一个 jail.local 文件的示例,其中包含一些常见的设置和解释:

[DEFAULT]
# "ignoreip" 可以是 IP 地址、CIDR 前缀或 DNS 主机名。
# Fail2ban 不会禁止这些 IP。可以列出多个地址,用空格分隔。
ignoreip = 127.0.0.1/8 ::1 192.168.1.0/24  # 忽略本地网络和回环地址

# "bantime" 是禁止主机的持续时间(秒)。
bantime  = 10m  # 默认禁止时间为 10 分钟

# "findtime" 是 Fail2ban 检查日志中失败尝试的时间窗口(秒)。
findtime  = 10m  # 10 分钟内

# "maxretry" 是在主机被禁止之前允许的失败尝试次数。
maxretry = 5  # 5 次失败尝试后禁止

# 用于发送电子邮件通知的操作。
# 默认情况下,Fail2ban 使用 sendmail。
mta = sendmail

# [sshd] 部分是针对 SSH 服务的特定配置。
[sshd]
enabled = true  # 启用 SSH 保护
port    = ssh    # SSH 端口(可以是数字或名称)
logpath = /var/log/auth.log  # SSH 日志文件的路径(Debian/Ubuntu)
#logpath = /var/log/secure   # (CentOS/RHEL/Fedora) - 取决于您的系统

# 您可以为其他服务添加类似的部分,例如 [apache-auth]、[nginx-http-auth] 等。

重要配置选项解释:

  • [DEFAULT]: 此部分中的设置将应用于所有 jail,除非在特定 jail 部分中被覆盖。
  • ignoreip: 不应被禁止的 IP 地址列表。始终包含您的本地网络和任何受信任的 IP。
  • bantime: 禁止 IP 的持续时间。可以使用后缀(smhd)表示秒、分钟、小时或天。
  • findtime: Fail2ban 检查失败尝试的时间窗口。
  • maxretry: 在禁止之前允许的失败尝试次数。
  • enabled: 设置为 true 以启用 jail。
  • port: 服务的端口号或名称。
  • logpath: 服务的日志文件路径。
  • backend: (可选) 指定用于监视日志文件更改的后端。默认情况下,Fail2ban 会自动检测最佳后端(通常是 pyinotifygamin,如果没有安装,则回退到 polling)。
  • action: (可选) 指定在达到 maxretry 时要采取的操作。默认操作是禁止 IP。您可以配置 Fail2ban 发送电子邮件通知、更新防火墙规则等。

5. 为其他服务配置 Jail:

要保护其他服务(如 Apache、Nginx、FTP 等),您需要:

  1. jail.local 中为每个服务添加一个部分(例如 [apache-auth])。
  2. 设置 enabled = true
  3. 指定正确的 portlogpath
  4. (可选)根据需要调整 maxretryfindtimebantime

您可以在 /etc/fail2ban/jail.conf 文件中找到可用 jail 的示例配置(但不要直接编辑该文件)。

6. 重启 Fail2ban:

进行更改后,您需要重新启动 Fail2ban 以使配置生效:

sudo systemctl restart fail2ban

或者,如果系统使用 SysVinit:

sudo service fail2ban restart

7. 检查 Fail2ban 状态:

您可以使用以下命令检查 Fail2ban 的状态和已激活的 jail:

sudo fail2ban-client status

要查看特定 jail 的详细信息(例如,sshd):

sudo fail2ban-client status sshd

重要安全提示:

  • 始终将您的 IP 地址添加到 ignoreip 列表中,以避免意外锁定自己。
  • 定期检查 Fail2ban 日志,以确保它按预期工作并识别恶意活动。
  • 根据您的需要调整 bantimefindtimemaxretry。过于宽松的设置可能无法有效阻止攻击,而过于严格的设置可能会阻止合法用户。
  • 考虑使用更强的身份验证方法,例如 SSH 密钥对,以减少对 Fail2ban 的依赖。
  • 不要仅仅依赖 Fail2ban。它只是您安全策略中的一层。还应使用其他安全措施,例如强密码、防火墙、定期安全审计等。
  • 日志路径: 请确保日志路径的正确.

给你的永久服务器搭建的吗

对的,iptable防火墙没按照流程设置,差点让我服务器变砖头

hhhhh,大不了重做系统就是了

@论坛助手,尝试总结话题

This topic provides a comprehensive guide to setting up and configuring Fail2ban, an intrusion prevention software, on a Linux server. It covers the following key aspects:

  • Installation: Instructions for installing Fail2ban on Debian/Ubuntu, CentOS/RHEL/Fedora, and Arch Linux.
  • Configuration Files: Explanation of the main configuration files (jail.conf, jail.local, and the jail.d/ directory) and the importance of using jail.local for customizations.
  • jail.local Configuration: A detailed example of a jail.local file with explanations of key configuration options like ignoreip, bantime, findtime, maxretry, enabled, port, and logpath.
  • Configuring Jails for Other Services: Guidance on how to configure Fail2ban to protect other services like Apache, Nginx, and FTP.
  • Restarting Fail2ban: Instructions on how to restart Fail2ban after making configuration changes.
  • Checking Fail2ban Status: Commands for checking the status of Fail2ban and individual jails.
  • Security Tips: Important security recommendations, including adding trusted IPs to ignoreip, regularly checking logs, adjusting bantime, findtime, and maxretry values, using strong authentication methods, and employing other security measures.

The topic also includes a note about verifying the correctness of log paths.

请使用中文总结话题

本话题详细介绍了如何在Linux服务器上安装和配置 Fail2ban,一款入侵防御软件。内容涵盖:

  • 安装: 针对 Debian/Ubuntu、CentOS/RHEL/Fedora 和 Arch Linux 的安装指导。
  • 配置文件: 解释了主要的配置文件 (jail.confjail.localjail.d/ 目录),以及使用 jail.local 进行自定义的重要性。
  • jail.local 配置: 详细的 jail.local 文件示例,解释了关键配置选项,如 ignoreipbantimefindtimemaxretryenabledportlogpath
  • 为其他服务配置 Jail: 指导如何配置 Fail2ban 来保护其他服务,如 Apache、Nginx 和 FTP。
  • 重启 Fail2ban: 介绍了在进行配置更改后如何重启 Fail2ban。
  • 检查 Fail2ban 状态: 提供了检查 Fail2ban 和单个 jail 状态的命令。
  • 安全提示: 重要的安全建议,包括将受信任的 IP 添加到 ignoreip,定期检查日志,调整 bantimefindtimemaxretry 值,使用强身份验证方法,以及采取其他安全措施。

该话题还提醒需要验证日志路径的正确性。