A spare-machine Gitea forge, hardened, backed up and with CI, as a template. Personal values are placeholders listed in the README.
204 lines
6.7 KiB
Markdown
204 lines
6.7 KiB
Markdown
---
|
|
type: Guide
|
|
purpose: Ban brute force on admin SSH and Gitea logins, and harden Gitea.
|
|
answers:
|
|
- "How are brute-force logins banned and Gitea hardened?"
|
|
cites:
|
|
- compose.yml
|
|
---
|
|
|
|
# 09 — fail2ban and Gitea Hardening
|
|
|
|
fail2ban for admin SSH and Gitea logins, bans landing in nftables, and Gitea
|
|
app.ini hardening.
|
|
|
|
## Prerequisites
|
|
|
|
- nftables firewall active (from [08-firewall.md](08-firewall.md))
|
|
- fail2ban installed (from [01-arch-install.md](01-arch-install.md))
|
|
|
|
## 1. Point fail2ban at nftables
|
|
|
|
Arch's fail2ban defaults to `banaction = iptables-multiport`. Left alone, bans
|
|
are written into iptables while the real firewall lives in nftables — they
|
|
appear to work but land in a stack you are not inspecting.
|
|
|
|
Create `/etc/fail2ban/jail.d/00-banaction.conf`:
|
|
|
|
```ini
|
|
[DEFAULT]
|
|
banaction = nftables-multiport
|
|
banaction_allports = nftables-allports
|
|
```
|
|
|
|
This applies to every jail, including the `sshd` one from
|
|
[03-ssh-hardening.md](03-ssh-hardening.md).
|
|
|
|
## 2. fail2ban for Gitea
|
|
|
|
Gitea in Docker defaults to `[log] MODE = console` — everything goes to
|
|
`docker logs` and `/data/gitea/log/` stays empty. A jail pointed at
|
|
`gitea.log` then fails to start and silently protects nothing.
|
|
|
|
`compose.yml` sets `GITEA__log__MODE=console,file` to fix this. Confirm the file
|
|
exists and is growing before writing the jail:
|
|
|
|
```bash
|
|
docker exec gitea ls -la /data/gitea/log/gitea.log
|
|
```
|
|
|
|
If it is missing, the env var is not reaching the container — fix that first.
|
|
There is no point configuring a tail on a file that will never appear.
|
|
|
|
Create `/etc/fail2ban/filter.d/gitea.conf`:
|
|
|
|
```ini
|
|
[Definition]
|
|
failregex = .* Failed authentication attempt for .* from <HOST>
|
|
ignoreregex =
|
|
```
|
|
|
|
Create `/etc/fail2ban/jail.d/gitea.conf`:
|
|
|
|
```ini
|
|
[gitea]
|
|
enabled = true
|
|
port = http,https
|
|
filter = gitea
|
|
logpath = /home/admin/mithlond/gitea-data/gitea/log/gitea.log
|
|
maxretry = 10
|
|
bantime = 1h
|
|
findtime = 10m
|
|
```
|
|
|
|
Replace `/home/admin/mithlond` with your actual project path.
|
|
|
|
Restart fail2ban:
|
|
|
|
```bash
|
|
sudo systemctl restart fail2ban
|
|
sudo fail2ban-client status gitea
|
|
```
|
|
|
|
## 3. Gitea hardening
|
|
|
|
Set these in `compose.yml`, not by editing `app.ini`. Every `GITEA__*` env var is
|
|
re-applied to `app.ini` on each container start, so a hand edit to a key that is
|
|
also an env var is silently reverted at the next `docker compose up` — and a
|
|
hand edit to a key that is *not* one lives only on that server, in a file no
|
|
version control sees.
|
|
|
|
`compose.yml` already carries:
|
|
|
|
```yaml
|
|
- GITEA__service__DISABLE_REGISTRATION=true
|
|
- GITEA__service__DEFAULT_KEEP_EMAIL_PRIVATE=true
|
|
- GITEA__server__LFS_START_SERVER=false
|
|
- GITEA__openid__ENABLE_OPENID_SIGNIN=false
|
|
- GITEA__security__MIN_PASSWORD_LENGTH=12
|
|
- GITEA__security__PASSWORD_COMPLEXITY=lower,upper,digit,spec
|
|
```
|
|
|
|
`REQUIRE_SIGNIN_VIEW` stays at its default `false` on purpose — public repos
|
|
must be readable without an account. `INSTALL_LOCK` is set to `true` by the
|
|
wizard itself; confirm it rather than setting it.
|
|
|
|
Apply and confirm:
|
|
|
|
```bash
|
|
docker compose up -d # not `restart` — restart does not re-read env
|
|
docker exec gitea grep -E 'LFS_START_SERVER|MIN_PASSWORD_LENGTH|INSTALL_LOCK' \
|
|
/data/gitea/conf/app.ini
|
|
```
|
|
|
|
Do not print the whole `[server]` or `[security]` section — they contain
|
|
generated secrets (`*_JWT_SECRET`, `SECRET_KEY`). To rotate one, delete its line
|
|
and restart; Gitea regenerates it on boot.
|
|
|
|
## 4. Verify fail2ban bans work
|
|
|
|
Three things need proving here, and only the first is obvious: that the jail
|
|
counts failures, that the ban lands in nftables on the *right port*, and that a
|
|
banned host is actually turned away.
|
|
|
|
Ban a source that is not your own machine. The f2b chain hooks `input` at
|
|
priority -1, ahead of `inet filter`, and carries no `ct state established`
|
|
bypass — so banning yourself severs the session you are holding open, escape
|
|
hatch and all. A throwaway container has its own address on the docker0 bridge
|
|
and costs nothing to ban:
|
|
|
|
```bash
|
|
docker run --rm alpine sh -c 'apk add -q openssh-client; \
|
|
for i in $(seq 4); do ssh -p 2234 -o BatchMode=yes -o StrictHostKeyChecking=no baduser@192.168.1.50; done; \
|
|
sleep 45; \
|
|
for i in $(seq 4); do ssh -p 2234 -o BatchMode=yes -o StrictHostKeyChecking=no baduser@192.168.1.50; done'
|
|
```
|
|
|
|
**Four at a time, not six in a row.** OpenSSH 9.8+ enables `PerSourcePenalties`,
|
|
and after about four invalid-user attempts sshd starts dropping connections on
|
|
its own:
|
|
|
|
```
|
|
srclimit_penalise: 172.17.0.2/32: activating ipv4 penalty of 19.673 seconds
|
|
drop connection #0 from [172.17.0.2]:56240 ... penalty: attempted authentication by invalid user
|
|
```
|
|
|
|
A dropped connection never logs `Invalid user`, and that line is what the `sshd`
|
|
filter counts. sshd's own rate limiter starves fail2ban of its evidence: the
|
|
count stalls below `maxretry = 5` and no ban fires. Two rounds of four, spaced
|
|
past the penalty, stay within `findtime = 10m` and reach 8.
|
|
|
|
Then read the rule the ban generated:
|
|
|
|
```bash
|
|
sudo fail2ban-client status sshd
|
|
sudo nft list table inet f2b-table
|
|
```
|
|
|
|
`f2b-table` does not exist until the first ban — `actionstart` runs on demand —
|
|
so its absence beforehand proves nothing. Once it appears, check the port:
|
|
|
|
```
|
|
tcp dport 2234 ip saddr @addr-set-sshd reject with icmp port-unreachable
|
|
```
|
|
|
|
That number comes from `port` in `/etc/fail2ban/jail.d/sshd.conf`, not from
|
|
sshd's own config, and nothing validates it. A typo bans the IP from a port
|
|
nothing listens on: `fail2ban-client status` reports the ban, the ruleset agrees
|
|
with it, and the banned host walks back in on 2234 untouched. The server's copy
|
|
of that file said `234` for a full session before this check caught it.
|
|
|
|
Which is why the last check is not optional — go back to the banned source:
|
|
|
|
```bash
|
|
docker run --rm alpine sh -c 'apk add -q openssh-client; \
|
|
ssh -p 2234 -o BatchMode=yes -o StrictHostKeyChecking=no -o ConnectTimeout=8 admin@192.168.1.50'
|
|
```
|
|
|
|
Expect `Connection refused` — `blocktype = reject` answers with ICMP port
|
|
unreachable. `Permission denied (publickey)` means the ban is cosmetic. A
|
|
*timeout* means something other than fail2ban is dropping the packets.
|
|
|
|
Unban when done:
|
|
|
|
```bash
|
|
sudo fail2ban-client set sshd unbanip 172.17.0.2
|
|
```
|
|
|
|
## Checklist
|
|
|
|
- [ ] fail2ban bans land in nftables, not iptables (`00-banaction.conf`)
|
|
- [ ] fail2ban watching admin SSH on 2234
|
|
- [ ] `gitea.log` exists on disk and grows
|
|
- [ ] fail2ban watching Gitea login log
|
|
- [ ] Gitea hardening set via `compose.yml` env, confirmed in `app.ini`
|
|
- [ ] ban registers *and* `f2b-table` rejects on 2234, not some other port
|
|
- [ ] banned source actually refused on 2234, re-tested after the ban
|
|
|
|
Only once every box above and in [08](08-firewall.md) is ticked, create the port-forwards in
|
|
[06-docker-deploy.md](06-docker-deploy.md) §1.
|
|
|
|
## Next step
|
|
|
|
Continue with [10-backup-manual.md](10-backup-manual.md).
|