http rules: make the LogQL prefilter a superset of classify()

`http-sqli` was unreachable end to end: the prefilter admitted only
`union select`, so boolean (`' OR '1'='1`, `%27%20OR%20…`) and time-based
(`sleep(`, `waitfor delay`) payloads were dropped in Loki before Python saw
them, and the canary passed because it used the one admitted shape.

The prefilter now carries the missing shapes as plain literals (a nested
`(?:...)` group made Loki's filter 10x slower, and re-ordering the alternation
measured 20-60 s, so the old block stays first and the additions append).
Over-match measured on 2026-11-27 06:00-07:00Z over 29 426 lines: 1 202 -> 1 548
lines/h (+29 %), 10-minute query 3.9 s. The extra lines are Forgejo browse URLs
that classify() drops; a calm 10-minute window of 394 admitted lines yields 0
findings.

Durability: tests/fixtures/attack-shapes.json is one table of 105 payload
shapes iterated by both tests - classify() must flag each one and
config.HTTP_LINE_FILTER must match its serialized access line. It fails with 44
dropped shapes on the old filter and passes now, so this class of drift (any
rule kind, not just sqli) cannot come back silently.

Docs also record the data-source limit the bug surfaced: Cloudflare collapses
dot-segments in the path, so `/../../etc/passwd` arrives as `/etc/passwd` and
path traversal is only detectable in a query string (or off-tunnel).
This commit is contained in:
Sirius DevOps 2026-11-27 03:13:16 -05:00
commit ef7e584d27
5 changed files with 867 additions and 18 deletions

View file

@ -32,13 +32,58 @@ Each tick runs exactly **two LogQL queries** over that window:
The prefilter is a deliberate **superset** (it matches `.gitignore` in a browse
URL, `.git/info/refs`, `backup-cronjob.yaml`, …). Loki drops non-matching bytes
cheaply; the precise decision is made in `http_rules.classify()` where real
semantics are available. Over-matching measured at ~1 100 of 35 445 lines/hour
(3 %), all of it filtered out in Python.
semantics are available. Over-matching measured 2026-11-27 06:00–07:00Z over
29 426 lines: **1 202 lines/h before, 1 548 lines/h after** the sqli fix
(+29 %), all of it filtered out in Python.
**The superset property is a test, not a comment.** `tests/fixtures/attack-shapes.json`
is one table of payload shapes; `TestPrefilterSuperset` iterates it twice — every
shape must be flagged by `classify()` *and* must match `config.HTTP_LINE_FILTER`
on its serialized access line. A shape the classifier knows but the prefilter
drops is invisible from Python: Loki never returns the line, so no unit test of
`classify()` can see the gap. `http-sqli` shipped unreachable for exactly that
reason — only `union select` was admitted and the canary used that shape
(t_a3604aab). Add a row when you add a rule or a payload family.
Two measured rules for editing the prefilter:
* **Every alternative is a plain or escaped literal.** Wrapping part of the
alternation in a `(?:…)` group made Loki's filter ~10× slower (22 s vs 2 s for
a 10-minute window) and `LOKI_TIMEOUT` is 25 s, i.e. the tick fails.
* **Keep the existing block first and append.** The identical set of
alternatives in a different order measured 20–60 s. Re-order only with a
`count_over_time` timing behind you; the 10-minute window costs ~3.9 s today.
`limit` on the line query is `HTTP_MAX_LINES = 600`; if a flood exceeds it the
tick reports `truncated` in the day counter and still classifies what arrived
(newest first).
## Data-source limit: Cloudflare normalises the path
More than one reader will assume "path traversal is covered". It is covered in
the **query string** only, and that is a property of the data source, not of the
rules:
```
probe sent GET /../../../../etc/passwd
arrives at envoy {"path":"/etc/passwd", …}
```
Cloudflare collapses dot-segments in the path before the origin ever sees the
request, so the traversal string does not exist anywhere in the access log and
no rule can match it. What does reach this layer, and is what the `traversal`
rule actually catches:
* traversal in a **query string** — `GET /?file=../../../etc/passwd` (answered
200 by the static landing page, hence MEDIUM not HIGH);
* requests that bypass Cloudflare — a WireGuard client hitting
`http://10.10.0.2:8080/` directly sends the path verbatim;
* percent-encoded traversal the edge does not decode-away — `/%2e%2e%2f…`.
So a traversal test through the public hostname proves only that Cloudflare
normalised it. Test path traversal against the Gateway origin, or in a query
string, and do not cite this rule as coverage for `../` in paths.
## The rules
Every finding is `SEV <fp12> <rule> <detail>`; the fingerprint is
@ -47,7 +92,7 @@ Every finding is `SEV <fp12> <rule> <detail>`; the fingerprint is
| Rule | Category | Severity | Entity | Fires when |
|---|---|---|---|---|
| `http-traversal` | `traversal` | HIGH served / MEDIUM attempt | `traversal\|host\|client` | `../`, `..\`, `%2e%2e`, `..%2f`, `%252e` in path or query |
| `http-sqli` | `sqli` | HIGH served / MEDIUM attempt | `sqli\|host\|client` | `union [all] select`, `' or 1=1`, `sleep(`, `benchmark(`, `information_schema`, `waitfor delay`, `load_file(`, `into outfile` |
| `http-sqli` | `sqli` | HIGH served / MEDIUM attempt | `sqli\|host\|client` | `union [all] select`; boolean `' or/and '`, `or <n>=<n>` in any encoding; time-based `sleep(`, `pg_sleep(`, `benchmark(`, `waitfor delay`; `information_schema`, `sysobjects`, `extractvalue(`, `updatexml(`, `load_file(`, `into outfile`; stacked `; drop/delete/insert/update` |
| `http-nul-byte` | `nul-byte` | HIGH served / MEDIUM attempt | `nul-byte\|host\|client` | `%00`, `%2500`, literal NUL |
| `http-secret-probe` | `secret-file` | HIGH served / MEDIUM attempt | `secret-file\|host\|client` | `.env*`, `.aws/{credentials,config}`, `.ssh/id_*`, `.docker/config.json`, `.kube/config`, `.npmrc`, `.netrc`, `.pgpass`, `*credentials*.{json,yml,…}`, `/meta-data/iam/security-credentials` |
| `http-vcs-probe` | `vcs-probe` | HIGH served / MEDIUM attempt | `vcs-probe\|host\|client` | `.git/config`, `.git/HEAD`, `.git/index`, `.git-credentials`, `.svn/`, `.hg/`, `.bzr/` (**not** the smart-HTTP endpoints) |
@ -149,6 +194,40 @@ MEDIUM 58d1f046a18a http-vcs-probe vcs-probe siriusdevops.com client=137.118.
Cloudflare reported it in `CF-Connecting-IP`. Same code path, both attribution
sources.
### sqli, end to end (2026-11-27 08:10Z, after the prefilter fix)
Four payloads fired at the public hostname (three, percent-encoded) and at the
Gateway origin `10.10.0.2:8080` (all four, so the `peer` path is exercised):
| payload | wire form | result |
|---|---|---|
| `/?id=1%27%20OR%20%271%27=%271` | as sent | 200, logged, `http-sqli` |
| `/?id=1' OR '1'='1` | 400 from envoy (a raw space is not a valid request target) | send `/?id=1'%20OR%20'1'='1` |
| `/?id=1%27%3BWAITFOR%20DELAY%20%270:0:5%27` | as sent | 200, logged, `http-sqli` |
| `/?q=1%27%20AND%20sleep(5)--` | as sent | 200, logged, `http-sqli` |
```
$ ./run.sh --http --no-prom --http-window 300 # live tick, 10.10.0.3 + CF client
MEDIUM 7d17b2f6b043 http-sqli sqli siriusdevops.com client=10.10.0.3 GET /?id=1'%20OR%20'1'='1 -> 200 (4x in 5m)
MEDIUM ff5178bdc5ff http-sqli sqli siriusdevops.com client=137.118.156.124 GET /?q=1%27%20AND%20sleep(5)-- -> 200 (3x in 5m)
```
Before the fix the same requests produced **nothing**: the four shapes were
dropped by the prefilter (the `union select` canary in the block above was the
only sqli shape it admitted). The table in `tests/fixtures/attack-shapes.json`
now pins 105 shapes × (classify, prefilter) and fails with 44 dropped shapes on
the pre-fix filter.
Cost, measured on the same hour (`06:00–07:00Z`, 29 426 lines): **1 202
lines/h → 1 548 lines/h** admitted (+346, +29 %), the 10-minute window 474 → 626
lines at 3.9 s per query. The 346 extra lines are Forgejo browse URLs
(`…/blame/commit/…`, `…/raw/commit/…` containing `secret`-named files and `.gz`
archives); `classify()` drops all of them (browse exclusion), and a calm window
`07:03:40–07:13:40Z` of 394 admitted lines produced **0 findings**. Over the
last 6 h the ordinary shapes are clean too: 381 `info/refs?service=git-*`, 83
browse (`/src/branch/master/…`), 6 `/book`, 6 `/lab`, 2 000 runner
`FetchTask` — **0 `http-*` findings**.
Behavioural contract on the same state file: first tick baselines silently
(`http.baselined_at`, 0 lines out), the next tick after the crafted requests
emits 10, the tick after that is silent, deleting one fingerprint re-emits