My hosting panel now bans scanners and emails me every morning
A couple of weeks ago I gave HostPanel a security page. Every minute, a collector on the server summarizes the web and SSH logs, and the panel shows me who's probing my sites and what they're asking for. Like the rest of the panel, it's only reachable on my private network.
That page had two problems. It could show me attacks, but it couldn't do anything about them. And I only learned anything if I remembered to open it.
Last week I fixed both.
What I was trying to do
- Ban the obvious scanners automatically, without banning anyone real, and without any way to lock myself out.
- Get a short email every morning: visitors, attacks, bans, backups, and anything that needs a look.
How much scanning are we talking about?
On a normal day last week, my four small static sites got about 7,500 requests. About a third of those were attack probes, from 30 scanner addresses: requests for .env files, .git/config, WordPress logins on sites that have never run WordPress, and so on.
None of it works. The sites are static files, so there's nothing to exploit. But it's noise in the logs, and an address that spends all day hunting for secret files is not going to become a reader.
The ban rules (no AI involved)
I wanted the ban decision to be boring and predictable, so it's a short list of fixed rules in autoban.py, run by the same collector every minute.
An address gets banned if, over today and yesterday, it:
- asked for 3 or more secret files (
.env,.git, keys, backups), or - sent 10 or more attack probes, making up at least 30% of everything it asked for.
The 30% part matters. A real visitor who trips one odd rule while reading 40 pages isn't a scanner. An address whose traffic is mostly probes is.
Bans escalate: 1 day, then 7 days, then 30 days. Strikes are forgotten 90 days after the last ban.
Some addresses are never banned:
- private and Tailscale ranges, so I can't lock myself out,
- Cloudflare's ranges,
- anything on my allowlist,
- real Googlebot, Bingbot and Applebot, verified by reverse DNS. A scanner that just claims to be Googlebot in its user agent gets banned like anyone else.
There's a circuit breaker. If more than 200 bans happen within an hour, something is wrong with the rules, not the internet. New bans pause and the security page turns red.
Undo is one click. Pressing Unblock on an automatic ban also adds the address to the allowlist, so the rules can't ban it again the next minute.
Where the ban actually happens
This was the part I had to get right. My sites sit behind Caddy in Docker, and ufw deny doesn't work for that. Docker installs its own firewall rules for published ports, and they run before ufw's. A "blocked" address would still reach the sites.
So bans go into their own nftables table, hooked at prerouting, before Docker forwards anything:
table inet hostpanel_block {
set v4 { type ipv4_addr; flags interval; }
set v6 { type ipv6_addr; flags interval; }
chain pre {
type filter hook prerouting priority -300; policy accept;
iifname { "lo", "tailscale0" } accept # never filter my own way in
ip saddr @v4 counter drop
ip6 saddr @v6 counter drop
}
}
Two smaller choices:
- IPv6 bans cover the whole
/64. Banning one IPv6 address does nothing, because the scanner just picks another from its block. - The panel itself still has no root access. It writes a request, and a small root service on the host applies it. The panel can ask for a ban. It can't touch the firewall.
It starts by only watching
New installs start in observe mode. The rules run, but instead of banning anyone they record who would have been banned. That list shows up in the panel and in the daily email.
On its first run it flagged 32 addresses. Before I let it actually ban anyone, I want a few days of that list showing only things I'd ban by hand. A rule that's wrong in observe mode costs nothing. A rule that's wrong in enforce mode bans a real person.
The daily email
The recap passes through three machines, and each one does one thing:
- The VPS, at 4:10 AM: a timer runs
make-digest.py, which writes yesterday's numbers to a small JSON file. - My home server, at 4:30 AM: the backup job that already pulls the VPS backups grabs the digest too, using the same restricted key.
- My PC, later in the morning:
Send-DailyRecap.ps1reads the digest, asks my local LLM in LM Studio for a few "Highlights" bullets, builds the email and sends it.
The email has visitors per site, requests, probes, scanner addresses, server errors, ban activity, whether last night's backup exists on the server and on my home server, and any check that failed.
The LLM writes words, never numbers
I like a few plain-English bullets at the top. I don't trust a local model with arithmetic. So the split is strict:
- Every number in the tables comes from the script, straight out of the digest.
- The model only writes the highlight bullets, from the same data.
- Any bullet that quotes a number not in the data gets dropped.
- If the model is down or answers badly, the email still goes out with the numbers only.
The digest contains no attacker-written text
This is the part I'm happiest with. Everything in a web access log is written by strangers: the paths, the user agents, the query strings. If I fed raw log lines to an LLM, any scanner could put instructions in its user agent and have them read by my model.
So the digest holds only counts, fixed names (my sites, known crawlers) and short codes. No paths, no user agents, no IP addresses. Nothing that reaches the model was written by anyone but me.
And if the digest doesn't show up?
That's the alert. If the digest is missing or a day old, the email still goes out and says so: maybe the home server is off, or the VPS timer failed. Silence would be the worst failure, so there isn't one.
What broke
The model thought itself out of an answer. On the first runs, the Highlights section came back empty. The log showed why: the reply stopped because it hit its length limit, after nearly 10,000 characters of reasoning and zero characters of answer. Qwen3-family models think before answering by default, and a short summary doesn't need any of that.
The fix was three small things: send /no_think (Qwen3-family models skip their reasoning when they see it, and other models ignore it), give it plenty of token headroom in case a model reasons anyway, and strip any <think> block before reading the bullets. Since then it answers in one to two minutes.
The email still went out when the model failed. That wasn't a bug, but it's worth saying: the "numbers only" fallback did exactly what it was built for, the first day it was tested.
If I did it again
- Start every automatic ban in observe mode. It costs a few days, and it's the only honest way to test rules against real traffic.
- Put Docker hosts' blocks in
prerouting. If you rely on ufw in front of Docker-published ports, check that it's actually blocking anything. Mine wouldn't have been. - Never let an LLM see text a stranger wrote, unless you're ready for that stranger to talk to your model. Summarize to counts first.
- Let the model write the words, and let code own the numbers. Same lesson as the daily briefing: keep the model to the part that needs a model.