Skip to main content

2026-07-17 · 2 min read

How a scanner bot polluted my visitor stats

SecurityNext.jsPrivacy

Translated from the Chinese original: read it in Chinese

The footer of my portfolio has a small visitor-origin tool I built myself. It uses no third-party service such as Clustrmaps: it reads the cf-ipcountry header that Cloudflare Tunnel already attaches when it forwards a request, and aggregates only "country code plus count", storing no IP and nothing personally identifiable. After it had been live for a while, the screen showed "visitors from 2 countries 🇹🇼 🇹🇷".

Taiwan is fine, but where did Turkey come from?

Check the log first, do not guess first

Rather than guess, I went to logs/server.log and counted the status code distribution. Over 10 days there were nearly 19,887 requests, of which 39% were 404s. Looking at the paths, almost all were automated vulnerability scanners at work:

481  /cgi-bin/luci/;stok=/locale        <- known home-router vulnerability
192  /SDK/webLanguage                    <- IoT device vulnerability scan
115  /wp-admin/install.php?step=1        <- WordPress scan
 62  /.env
 32  /.git/config
 ...

This is background noise that any server exposed to the internet receives, not a targeted attack on this site. These paths do not exist in a Next.js project, all correctly returned 404, and nothing was actually leaked.

But what does that have to do with Turkey?

The old visitor counting logic in scripts/server.js judged every request that hit the home page: the method is GET, the path is not /api/ or /_next/, there is no file extension, the status is 200, and the User-Agent does not obviously say "bot". If all hold, it counts as one visitor.

The problem is the last condition: many automated scanners, to dodge a simple UA blacklist like this, just disguise themselves with an ordinary browser's User-Agent string. Such a scanner is not stopped by the old logic at all: as long as it also happens to hit the home page once (many scanning tools first hit / for a rough fingerprint before starting on known vulnerable paths), it gets counted as a "visitor".

The fix: from "the server guesses" to "the browser reports"

Instead of continuing to strengthen the UA blacklist (essentially whack-a-mole, with new disguises always appearing), I changed visitor counting so it counts only after the browser really runs JS:

useEffect(() => {
  fetch("/api/visitor-stats", { method: "POST" })
    .then((r) => r.json())
    .then(setStats);
}, []);

The POST /api/visitor-stats request itself is the report of "this visit". The vast majority of automated scanning tools (curl, python-requests, all kinds of vulnerability scanners) do not run JavaScript at all, so by architecture they never reach this API: not "harder to bypass", but "this kind of traffic never shows up in the first place". The UA blacklist stays as a second line of defense (extended with keywords such as curl/wget/python-requests/axios/scrapy/headless), but it is no longer the only one.

Result

After clearing and counting afresh, the stats on the live site are clean. If an odd-looking country appears again, it is more likely that Cloudflare's geolocation of some IP happened to be inaccurate (VPN, CGNAT and the like) than a hole in the counting logic itself; the two cases can be told apart, which makes future investigation easier.

Type to search projects and posts too