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

@ -195,16 +195,49 @@ HTTP_FORGEJO_HOSTS = frozenset({"forgejo.siriusdevops.com"})
HTTP_AUTH_PER_MIN = int(os.environ.get("NETSEC_SCAN_HTTP_AUTH_PER_MIN", "20"))
HTTP_AUTH_WINDOW_TOTAL = int(os.environ.get("NETSEC_SCAN_HTTP_AUTH_TOTAL", "30"))
# LogQL prefilter for attack-shaped *lines*. Deliberately a superset: Loki
# drops non-matching bytes cheaply, and `http_rules.classify()` makes the precise
# decision in Python (semantics the regex cannot express, e.g. Forgejo browse
# URLs and smart-HTTP git). Measured over-matching: ~1 090 of 35 445 lines/h.
# LogQL prefilter for attack-shaped *lines*. Deliberately a superset of what
# `http_rules.classify()` can flag: Loki drops non-matching bytes cheaply, and
# `classify()` makes the precise decision in Python (semantics the regex cannot
# express, e.g. Forgejo browse URLs and smart-HTTP git). The superset property is
# enforced by tests/fixtures/attack-shapes.json — every shape that table says
# classify() flags must also match this regex. `http-sqli` was unreachable for
# weeks because nobody asserted it: the prefilter admitted only `union select`
# and the canary happened to use that shape (t_a3604aab).
#
# Two rules for edits, both measured on this deployment:
# * every alternative is a plain or escaped literal. Wrapping part of this in a
# `(?:...)` group made Loki's filter ~10x slower (22 s vs 2 s for a 10-minute
# window; `LOKI_TIMEOUT` is 25 s), so payment for "readability" here is a
# timed-out tick.
# * keep the existing block first and append new literals. The same set of
# alternatives in a different order measured 20-60 s, so re-order only with a
# `count_over_time` timing behind you.
#
# Measured over-match, 2026-11-27 06:00-07:00Z on 29 426 lines: 1 202 lines/h
# before, 1 548 lines/h after (+29 %).
HTTP_LINE_FILTER = os.environ.get(
"NETSEC_SCAN_HTTP_LINE_FILTER",
"(?i)(\\.\\.|%2e%2e|union|select|%00|%2500|\\.env|\\.git|\\.svn|\\.hg|"
"credential|\\.aws|\\.ssh|id_rsa|\\.kube|\\.npmrc|\\.netrc|\\.htpasswd|"
"secrets|service-account|\\.bak|\\.old|\\.orig|\\.save|\\.swp|\\.sql|"
"\\.dump|\\.zip|\\.tar|\\.tgz|backup|sqlmap|nikto|nuclei|masscan|zgrab|"
"nmap|wpscan|gobuster|dirbuster|ffuf|ferox|acunetix|netsparker|nessus|"
"openvas|hydra|w3af|zaproxy)",
"(?i)("
r"\.\.|%2e%2e|union|select|%00|%2500|\.env|\.git|\.svn|\.hg|"
r"credential|\.aws|\.ssh|id_rsa|\.kube|\.npmrc|\.netrc|\.htpasswd|"
r"secrets|service-account|\.bak|\.old|\.orig|\.save|\.swp|\.sql|"
r"\.dump|\.zip|\.tar|\.tgz|backup|sqlmap|nikto|nuclei|masscan|zgrab|"
r"nmap|wpscan|gobuster|dirbuster|ffuf|ferox|acunetix|netsparker|nessus|"
r"openvas|hydra|w3af|zaproxy|"
# Shapes classify() already knew but this filter dropped: double-encoded
# traversal, secret files it did not name, the VCS/backup extensions and
# scanner families it did not list.
r"%252e|\\u0000|\.bzr|secret|id_dsa|id_ecdsa|id_ed25519|authorized_keys|"
r"\.docker|\.pypirc|\.pgpass|\.my\.cnf|\.swo|\.gz|\.bz2|\.rar|\.7z|"
r"dirb|commix|jaeles|ysoserial|whatweb|gospider|"
# SQL injection. Any quote is the cheapest complete superset for the
# boolean/stacked families (`' OR '1'='1`, `%27%20OR%20%271%27=%271`,
# `';WAITFOR DELAY`) whatever the encoding; the time-based and function
# keywords carry no quote at all; the unquoted `or <n>=<n>` shape needs its
# separator spellings because this filter scans raw bytes, before decoding.
r"%27|'|sleep|benchmark|waitfor|pg_sleep|information_schema|sysobjects|"
r"extractvalue|updatexml|load_file|outfile|"
r"%20or|or%20|\+or|or\+|%2Bor|or%2B|\)or|%29or| or |"
r";drop|;delete|;insert|;update|%3bdrop|%3bdelete|%3binsert|%3bupdate"
")",
)