Skip to content

Commit d58e78b

Browse files
ksfikasclaude
andcommitted
Ban repeat failed logins on the FTP box with fail2ban
The FTP and SFTP ports are open to the internet with no firewall and are scanned constantly: ~1,450 refused logins in the first day, 88% from one address, as root/admin/ubuntu/postgres and so on. Nothing got in (no such accounts, long random passwords), but nothing stopped it either. Provisioning now installs fail2ban with a jail on the login log ProFTPD already writes for both servers: five refused logins (PASS 530) within ten minutes ban the address from both ports for an hour, doubling per repeat offence up to a week. The workflow checks the filter against a made-up log first (a refusal must count, a success and a bare USER must not), and takes an optional unban_ip input for a provider who locked themselves out. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent c9c3db6 commit d58e78b

3 files changed

Lines changed: 95 additions & 43 deletions

File tree

‎.github/workflows/attempts-summary.yml‎

Lines changed: 0 additions & 42 deletions
This file was deleted.

‎.github/workflows/provision-ftp-box.yml‎

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@ name: Provision FTP box
1818
# the relay with zero accounts configured yet: it just idles.
1919
on:
2020
workflow_dispatch:
21+
inputs:
22+
unban_ip:
23+
description: "Optional: an address fail2ban has banned that should be let back in (a provider who locked themselves out)"
24+
required: false
25+
default: ""
2126

2227
jobs:
2328
provision:
2429
runs-on: [self-hosted, loader-ftp]
2530
env:
2631
SYNC_KEY: ${{ secrets.LOADER_FTP_SYNC_KEY }}
32+
UNBAN_IP: ${{ github.event.inputs.unban_ip }}
2733
steps:
2834
- name: Pull the ftp-forwarder checkout up to date
2935
run: |
@@ -44,7 +50,7 @@ jobs:
4450
- name: Install ProFTPD (core, SQLite backend, crypto/sftp module) and Python venv support
4551
run: |
4652
sudo apt update
47-
sudo apt install -y proftpd-core proftpd-mod-sqlite proftpd-mod-crypto sqlite3 python3-venv
53+
sudo apt install -y proftpd-core proftpd-mod-sqlite proftpd-mod-crypto sqlite3 python3-venv fail2ban iptables
4854
4955
- name: Enable the SQL and SFTP modules (idempotent -- no-op if already uncommented)
5056
run: |
@@ -185,6 +191,55 @@ jobs:
185191
- name: Confirm both listeners are actually up
186192
run: sudo ss -tlnp | grep -E ':21 |:2222 '
187193

194+
- name: Ban addresses that keep failing to log in (fail2ban)
195+
run: |
196+
# The FTP/SFTP ports are open to the whole internet with no
197+
# firewall in front, and are scanned constantly (1,000+ refused
198+
# root/admin logins from a single address in a day). fail2ban
199+
# watches the login log ProFTPD writes for both servers
200+
# (`ExtendedLog ... loader_auth`, see above): "PASS 530" is a
201+
# refused login. Five within ten minutes bans that address from
202+
# both ports for an hour, doubling for each repeat up to a week --
203+
# short enough that a provider who mistyped a password waits out
204+
# the hour, long enough to make scanning this box pointless.
205+
test -f /var/log/proftpd/loader-auth.log || { echo "::error::loader-auth.log doesn't exist -- ProFTPD isn't logging logins"; exit 1; }
206+
207+
sudo tee /etc/fail2ban/filter.d/loader-ftp.conf > /dev/null <<'EOF'
208+
[Definition]
209+
failregex = ^\d+ <HOST> \S+ PASS 530\s*$
210+
ignoreregex =
211+
datepattern = ^{EPOCH}
212+
EOF
213+
214+
sudo tee /etc/fail2ban/jail.d/loader-ftp.local > /dev/null <<'EOF'
215+
[loader-ftp]
216+
enabled = true
217+
filter = loader-ftp
218+
logpath = /var/log/proftpd/loader-auth.log
219+
port = 21,2222
220+
maxretry = 5
221+
findtime = 10m
222+
bantime = 1h
223+
bantime.increment = true
224+
bantime.factor = 2
225+
bantime.maxtime = 1w
226+
ignoreip = 127.0.0.1/8 ::1
227+
EOF
228+
229+
# Prove the filter matches what ProFTPD really writes, on a made-up log:
230+
# a refused login must count, a successful one and a bare USER must not.
231+
now=$(date +%s)
232+
printf '%s 203.0.113.9 - PASS 530\n%s 203.0.113.9 - USER 331\n%s 203.0.113.9 alice PASS 230\n' "$now" "$now" "$now" > /tmp/f2b-selftest.log
233+
sudo fail2ban-regex /tmp/f2b-selftest.log loader-ftp | tee /tmp/f2b-selftest.out
234+
grep -q "Failregex: 1 total" /tmp/f2b-selftest.out || { echo "::error::the fail2ban filter did not match exactly one refused login"; exit 1; }
235+
rm -f /tmp/f2b-selftest.log /tmp/f2b-selftest.out
236+
237+
sudo systemctl enable fail2ban
238+
sudo systemctl restart fail2ban
239+
sleep 3
240+
sudo systemctl is-active --quiet fail2ban && echo "fail2ban running" || (sudo journalctl -u fail2ban -n 30 --no-pager; exit 1)
241+
sudo fail2ban-client status loader-ftp
242+
188243
- name: Install and (re)start the file-relay service
189244
run: |
190245
# User=localadmin in the checked-in unit file is a
@@ -201,3 +256,15 @@ jobs:
201256
sudo systemctl restart loader-ftp-forwarder
202257
sleep 3
203258
sudo systemctl is-active --quiet loader-ftp-forwarder && echo "file-relay service running" || (sudo journalctl -u loader-ftp-forwarder -n 30 --no-pager; exit 1)
259+
260+
- name: Let an address back in, if one was asked for
261+
run: |
262+
if [ -n "$UNBAN_IP" ]; then
263+
# The input is free text -- only ever pass something that looks like an address.
264+
if ! printf '%s' "$UNBAN_IP" | grep -Eq '^[0-9a-fA-F:.]{2,45}$'; then
265+
echo "::error::'$UNBAN_IP' does not look like an IP address"
266+
exit 1
267+
fi
268+
sudo fail2ban-client set loader-ftp unbanip "$UNBAN_IP"
269+
fi
270+
sudo fail2ban-client status loader-ftp

‎ftp-forwarder/README.md‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,33 @@ here (the same proven, already-debugged setup loader's own
8080
`mod_sql`-backed virtual users, `mod_sftp` for SFTP, one account
8181
system, both protocols).
8282

83+
## Protection against password guessing
84+
85+
The FTP (21) and SFTP (2222) ports are open to the internet with no
86+
firewall in front (the okeanos network here is "unprotected mode"), and
87+
they are scanned constantly: the first day alone saw ~1,450 refused logins
88+
-- 88% from one address -- cycling through `root`, `admin`, `ubuntu`,
89+
`postgres` and the rest of the usual dictionary. None of those are real
90+
accounts and generated passwords are long and random, so nothing got in;
91+
but there was nothing stopping it either.
92+
93+
`provision-ftp-box.yml` therefore installs **fail2ban** with a jail
94+
(`loader-ftp`) on the login log ProFTPD writes for both servers
95+
(`/var/log/proftpd/loader-auth.log`; a refused login is a `PASS 530`
96+
line): five refusals within ten minutes ban that address from both ports
97+
for an hour, doubling for each repeat offence up to a week. The workflow
98+
proves the filter matches a refused login (and not a successful one) on a
99+
made-up log before enabling it, and prints the jail's status each run.
100+
101+
A legitimate provider who mistypes a password too often is banned like
102+
anyone else and waits out the hour -- or an admin can let them back in
103+
right away: run `provision-ftp-box.yml` with the address in its
104+
`unban_ip` input (it validates the value before use).
105+
106+
Not covered, deliberately left for later: enforcing each account's
107+
per-account allowed-IP list on the box (stored in loader, not yet
108+
applied), and whether plain FTP (unencrypted) should stay open at all.
109+
83110
## One-time setup on the box itself
84111

85112
`.github/workflows/provision-ftp-box.yml` (idempotent, safe to re-run)

0 commit comments

Comments
 (0)