|
| 1 | +--- |
| 2 | +date: "2022-08-01T00:00:00+00:00" |
| 3 | +title: "使用: 设置 Fail2ban" |
| 4 | +slug: "fail2ban-setup" |
| 5 | +weight: 16 |
| 6 | +toc: false |
| 7 | +draft: false |
| 8 | +menu: |
| 9 | + sidebar: |
| 10 | + parent: "usage" |
| 11 | + name: "设置 Fail2ban" |
| 12 | + weight: 16 |
| 13 | + identifier: "fail2ban-setup" |
| 14 | +--- |
| 15 | + |
| 16 | +# 使用 Fail2ban 阻止攻击者的暴力登录 |
| 17 | + |
| 18 | +**Fail2ban 检查客户端登录日志,将多次登录失败的客户端识别为攻击者并在一段时间内阻止其访问服务。如果你的实例是公开的,这一点尤其重要。请管理员仔细设置 fail2ban,错误的配置将导致防火墙阻止你访问自己的服务器。** |
| 19 | + |
| 20 | +Gitea 会在日志文件 `log/gitea.log` 中记录登录失败的 CLI、SSH 或 HTTP 客户端 IP 地址,而你需要将 Gitea 的日志输出模式从默认的 `console` 更改为 `file`。这表示将日志输出到文件,使得 fail2ban 可以定期扫描日志内容。 |
| 21 | + |
| 22 | +当用户的身份验证失败时,日志中会记录此类信息: |
| 23 | + |
| 24 | +```log |
| 25 | +2018/04/26 18:15:54 [I] Failed authentication attempt for user from xxx.xxx.xxx.xxx |
| 26 | +``` |
| 27 | + |
| 28 | +```log |
| 29 | +2020/10/15 16:08:44 [E] invalid credentials from xxx.xxx.xxx.xxx |
| 30 | +``` |
| 31 | + |
| 32 | +## 设置 Fail2ban |
| 33 | + |
| 34 | +添加日志过滤器规则到配置文件 `/etc/fail2ban/filter.d/gitea.conf`: |
| 35 | + |
| 36 | +```ini |
| 37 | +[Definition] |
| 38 | +failregex = .*(Failed authentication attempt|invalid credentials|Attempted access of unknown user).* from <HOST> |
| 39 | +ignoreregex = |
| 40 | +``` |
| 41 | + |
| 42 | +添加监狱规则到配置文件 `/etc/fail2ban/jail.d/gitea.conf`: |
| 43 | + |
| 44 | +```ini |
| 45 | +[gitea] |
| 46 | +enabled = true |
| 47 | +filter = gitea |
| 48 | +logpath = /var/lib/gitea/log/gitea.log |
| 49 | +maxretry = 10 |
| 50 | +findtime = 3600 |
| 51 | +bantime = 900 |
| 52 | +action = iptables-allports |
| 53 | +``` |
| 54 | + |
| 55 | +如果你的 Gitea 实例运行在 Docker 容器中,并且直接将容器端口暴露到外部网络, |
| 56 | +你还需要添加 `chain="FORWARD"` 到监狱规则配置文件 `/etc/fail2ban/jail.d/gitea-docker.conf` |
| 57 | +以适应 Docker 的网络转发规则。但如果你在容器的宿主机上使用 Nginx 反向代理连接到 Gitea 则无需这样配置。 |
| 58 | + |
| 59 | +```ini |
| 60 | +[gitea-docker] |
| 61 | +enabled = true |
| 62 | +filter = gitea |
| 63 | +logpath = /var/lib/gitea/log/gitea.log |
| 64 | +maxretry = 10 |
| 65 | +findtime = 3600 |
| 66 | +bantime = 900 |
| 67 | +action = iptables-allports[chain="FORWARD"] |
| 68 | +``` |
| 69 | + |
| 70 | +最后,运行 `systemctl restart fail2ban` 即可应用更改。现在,你可以使用 `systemctl status fail2ban` 检查 fail2ban 运行状态。 |
| 71 | + |
| 72 | +上述规则规定客户端在 1 小时内,如果登录失败的次数达到 10 次,则通过 iptables 锁定该客户端 IP 地址 15 分钟。 |
| 73 | + |
| 74 | +## 设置反向代理 |
| 75 | + |
| 76 | +如果你使用 Nginx 反向代理到 Gitea 实例,你还需要设置 Nginx 的 HTTP 头部值 `X-Real-IP` 将真实的客户端 IP 地址传递给 Gitea。否则 Gitea 程序会将客户端地址错误解析为反向代理服务器的地址,例如回环地址 `127.0.0.1`。 |
| 77 | + |
| 78 | +``` |
| 79 | +proxy_set_header X-Real-IP $remote_addr; |
| 80 | +``` |
| 81 | + |
| 82 | +额外注意,在 Gitea 的配置文件 `app.ini` 中存在下列默认值: |
| 83 | + |
| 84 | +``` |
| 85 | +REVERSE_PROXY_LIMIT = 1 |
| 86 | +REVERSE_PROXY_TRUSTED_PROXIES = 127.0.0.0/8,::1/128 |
| 87 | +``` |
| 88 | + |
| 89 | +`REVERSE_PROXY_LIMIT` 限制反向代理服务器的层数,设置为 `0` 表示不使用这些标头。 |
| 90 | +`REVERSE_PROXY_TRUSTED_PROXIES` 表示受信任的反向代理服务器网络地址, |
| 91 | +经过该网络地址转发来的流量会经过解析 `X-Real-IP` 头部得到真实客户端地址。 |
| 92 | +(参考 [configuration cheat sheet](https://docs.gitea.io/en-us/config-cheat-sheet/#security-security)) |
0 commit comments