diff --git a/README.md b/README.md index 1ffeaa7..7ad145e 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ cd ~/Projects/netsec-agent && ./run.sh # silent tick, exit 0 ./run.sh --verbose # same, diagnostics on stderr ./run.sh --fixture tests/fixtures/beacon-shaped.jsonl --dry-run # what-would-I-say ./run.sh --http --flows-dir /tmp/empty --dry-run # gateway rules only, no SSH -python3 -m unittest discover -s tests # 89 tests, no network +python3 -m unittest discover -s tests # 93 tests, no network ``` ## Output contract @@ -303,13 +303,16 @@ credentials, no LLM call, no secret reads. ## Verified behaviour (2026-11-27) -* 90 unit tests pass against recorded fixtures (`tests/test_archives.py` covers +* 93 unit tests pass against recorded fixtures (`tests/test_archives.py` covers the gz path: read-once, torn archive, budget/pending, cold seed, warm-state archive absorption, single-fire; `tests/test_http_rules.py` covers the gateway rules: every category, the Forgejo browse/git FP shapes, `coalesce(client_ip, - peer)`, the auth-rate floors and the flood cap; `tests/test_seed.py` covers - seed silence: cold start, `--dry-run` cold start, a resume tick while still - unseeded, a held PromQL tripwire reported by the first warm tick, and + peer)`, the auth-rate floors and the flood cap, and the **prefilter superset** + property — `tests/fixtures/attack-shapes.json` is iterated by both + `classify()` and `config.HTTP_LINE_FILTER`, so a payload the classifier knows + but Loki drops fails the suite instead of going quiet; `tests/test_seed.py` + covers seed silence: cold start, `--dry-run` cold start, a resume tick while + still unseeded, a held PromQL tripwire reported by the first warm tick, and single-fire from a seeded state). * **Gateway rules (§4d) on the live stream**: calm window `07:03:40–07:13:40Z` (5 474 requests, 154 candidate lines) → **0 findings, stdout empty, exit 0**. @@ -319,6 +322,18 @@ credentials, no LLM call, no secret reads. kinds — including `http-auth-brute` on a 30× 403 burst (`40 x 401/403 in 5m, peak 33/min`). First tick baselined silently, the next emitted 10 findings, the next was silent; deleting one fingerprint re-emitted exactly that line. +* **`http-sqli` reachable end to end (t_a3604aab)**: the LogQL prefilter was not + a superset of `classify()` — only `union select` got through, so boolean and + time-based injection never reached Python. The four payloads from the bug + report now fire (`MEDIUM … http-sqli … client=10.10.0.3 / 137.118.156.124`, + `4x`/`3x in 5m`, live tick at 08:10Z; the literal-space form has to travel as + `%20` because envoy 400s a raw space). Over-match measured on the same hour + (06:00–07:00Z, 29 426 lines): 1 202 → 1 548 lines/h (+29 %), 10-minute query + 3.9 s. Clean windows stay clean: 394 prefilter-admitted lines in + `07:03:40–07:13:40Z` → 0 findings, and 6 h of `info/refs`, browse URLs, + `/book`, `/lab` and runner `FetchTask` → 0. Superset test: + `tests/fixtures/attack-shapes.json` (105 shapes) fails with 44 drops on the + old filter, passes on the new one. * **Cold start on the live store** (state moved aside, `--no-prom --verbose`): `5 file(s) (4 archive(s), 1 plain), 150225 flows, 116 tuple(s), 116 fingerprint(s), window 2.06h, 7.34s`, **stdout 0 bytes**, exit 0. The window is diff --git a/docs/gateway-http-rules.md b/docs/gateway-http-rules.md index 40433b1..9af21e7 100644 --- a/docs/gateway-http-rules.md +++ b/docs/gateway-http-rules.md @@ -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 `; the fingerprint is @@ -47,7 +92,7 @@ Every finding is `SEV `; 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 =` 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 diff --git a/netsec_scan/config.py b/netsec_scan/config.py index 5112cb2..598e907 100644 --- a/netsec_scan/config.py +++ b/netsec_scan/config.py @@ -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 =` 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" + ")", ) diff --git a/tests/fixtures/attack-shapes.json b/tests/fixtures/attack-shapes.json new file mode 100644 index 0000000..67a975b --- /dev/null +++ b/tests/fixtures/attack-shapes.json @@ -0,0 +1,659 @@ +{ + "_comment": "Contract table for the LogQL prefilter superset property (t_a3604aab). Every shape here must (a) be flagged by netsec_scan.http_rules.classify() with the recorded category/severity/rule and (b) match config.HTTP_LINE_FILTER on its serialized envoy access line. A shape classify() knows but the prefilter drops is invisible end to end: Loki never returns the line, so no test of classify() alone can catch it. Includes the four boolean/time-based sqli payloads that shipped unreachable.", + "shapes": [ + { + "path": "/?id=1%27%20OR%20%271%27=%271", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1' OR '1'='1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1%27%3BWAITFOR%20DELAY%20%270:0:5%27", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1%27%20AND%20sleep(5)--", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/index.php?q=union+select+1,2", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/item?id=1 union all select user,pass from users", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1%27+or+1%3D1--", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1 or 1=1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1%20or%201%3D1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1+or+1%3D1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1%2Bor%2B1%3D1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1)or 1=1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1%29or%201=1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1' and '1'='1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?id=1 or \"1\"=\"1\"", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/x.php?id=1;sleep(5)", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;benchmark(100000,md5(1))", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;pg_sleep(5)", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;drop table users", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1%3Bdrop%20table%20users", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=information_schema.tables", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=sysobjects", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=extractvalue(1,concat(0x7e))", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=updatexml(1,1)", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=load_file('/etc/passwd')", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1 into outfile '/tmp/x'", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;update users set x=1", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;insert into t values(1)", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/?q=1;delete from t", + "rule": "http-sqli", + "category": "sqli", + "severity": "MEDIUM" + }, + { + "path": "/../../etc/passwd", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/%2e%2e%2fetc%2fpasswd", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/..%2fetc/passwd", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/%252e%252e/etc/passwd", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/..\\windows\\win.ini", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/?file=../../../etc/passwd", + "rule": "http-traversal", + "category": "traversal", + "severity": "MEDIUM" + }, + { + "path": "/.env%00.txt", + "rule": "http-nul-byte", + "category": "nul-byte", + "severity": "MEDIUM" + }, + { + "path": "/a%2500b", + "rule": "http-nul-byte", + "category": "nul-byte", + "severity": "MEDIUM" + }, + { + "path": "/.env", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/backup/.env", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "./.envrc", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.aws/credentials", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.ssh/id_rsa", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.ssh/id_dsa", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.ssh/id_ecdsa", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.ssh/id_ed25519", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.ssh/authorized_keys", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.docker/config.json", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.kube/config", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.git-credentials", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.npmrc", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.pypirc", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.netrc", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.pgpass", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.my.cnf", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.htpasswd", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/client_secret.json", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/application_default_credentials.json", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/latest/meta-data/iam/security-credentials", + "rule": "http-secret-probe", + "category": "secret-file", + "severity": "MEDIUM" + }, + { + "path": "/.git/config", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.git/HEAD", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.git/index", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.git/packed-refs", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.git/logs/HEAD", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.git/credentials", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.svn/entries", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.hg/hgrc", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/.bzr/README", + "rule": "http-vcs-probe", + "category": "vcs-probe", + "severity": "MEDIUM" + }, + { + "path": "/backup.zip", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/db.sql", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/site.tar.gz", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/config.php.bak", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/credentials.old", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/backup.rar", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/db.7z", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/site.bz2", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/db.gz", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/x.swo", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/x.orig", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/x.save", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/x.swp", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/x.dump", + "rule": "http-backup-probe", + "category": "backup-file", + "severity": "LOW" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:sqlmap", + "severity": "HIGH", + "user_agent": "sqlmap/1.8#stable (https://sqlmap.org)" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:nikto", + "severity": "HIGH", + "user_agent": "Mozilla/5.00 (Nikto/2.5.0)" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:nuclei", + "severity": "HIGH", + "user_agent": "Nuclei - Open-source project (github.com/projectdiscovery/nuclei)" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:masscan", + "severity": "LOW", + "user_agent": "masscan/1.3" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:zgrab", + "severity": "LOW", + "user_agent": "zgrab/0.x" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:nmap", + "severity": "LOW", + "user_agent": "Nmap Scripting Engine" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:wpscan", + "severity": "HIGH", + "user_agent": "WPScan v3.8.25" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:gobuster", + "severity": "MEDIUM", + "user_agent": "gobuster/3.6" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:dirbuster", + "severity": "MEDIUM", + "user_agent": "dirbuster-1.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:dirb", + "severity": "MEDIUM", + "user_agent": "dirb/2.22" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:ffuf", + "severity": "MEDIUM", + "user_agent": "ffuf/2.1.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:feroxbuster", + "severity": "MEDIUM", + "user_agent": "feroxbuster/2.10.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:acunetix", + "severity": "HIGH", + "user_agent": "Acunetix-Product" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:netsparker", + "severity": "HIGH", + "user_agent": "Netsparker/6.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:nessus", + "severity": "HIGH", + "user_agent": "Nessus SOAP" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:openvas", + "severity": "HIGH", + "user_agent": "OpenVAS" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:hydra", + "severity": "HIGH", + "user_agent": "hydra/9.5" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:w3af", + "severity": "HIGH", + "user_agent": "w3af/1.7" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:zaproxy", + "severity": "HIGH", + "user_agent": "Zaproxy/2.14.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:commix", + "severity": "HIGH", + "user_agent": "commix/v3.9" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:jaeles", + "severity": "HIGH", + "user_agent": "jaeles/v1.0" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:ysoserial", + "severity": "HIGH", + "user_agent": "ysoserial" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:whatweb", + "severity": "LOW", + "user_agent": "whatweb/0.5.5" + }, + { + "path": "/", + "rule": "http-scanner-ua", + "category": "scanner-ua:gospider", + "severity": "LOW", + "user_agent": "gospider/1.1" + } + ] +} diff --git a/tests/test_http_rules.py b/tests/test_http_rules.py index d97632a..894feb1 100644 --- a/tests/test_http_rules.py +++ b/tests/test_http_rules.py @@ -8,7 +8,10 @@ ending in `.gitignore`, one ending in `.env.example` behind an RSS route, and a from __future__ import annotations +import json +import re import unittest +from pathlib import Path from netsec_scan import config, http_rules @@ -86,6 +89,66 @@ class TestClassification(unittest.TestCase): self.assertIsNone(http_rules.classify(rec(path, code=200, ua=ua)), path) +class TestPrefilterSuperset(unittest.TestCase): + """The LogQL prefilter must admit every shape `classify()` can flag. + + A too-narrow prefilter is invisible from Python: Loki never returns the + line, so no amount of testing `classify()` can catch it. That is how + `http-sqli` shipped unreachable for weeks — only `union select` reached the + classifier, and the canary happened to use exactly that shape. + + One table, iterated by both tests: `tests/fixtures/attack-shapes.json` is + the contract. Adding a rule or a payload shape to `http_rules` without + adding it here (and to `config.HTTP_LINE_FILTER`) is the drift this catches. + """ + + @staticmethod + def table(): + path = Path(__file__).with_name("fixtures") / "attack-shapes.json" + return json.loads(path.read_text(encoding="utf-8"))["shapes"] + + @staticmethod + def record(shape): + return { + "path": shape["path"], + "method": shape.get("method", "GET"), + "response_code": shape.get("response_code", 404), + "user_agent": shape.get("user_agent", "curl/8.23.0"), + "authority": shape.get("authority", "siriusdevops.com"), + "client_ip": shape.get("client_ip", "203.0.113.7"), + "peer": "10.10.0.1", + } + + def test_table_matches_classify(self): + for shape in self.table(): + with self.subTest(path=shape["path"]): + hit = http_rules.classify(self.record(shape)) + self.assertIsNotNone(hit, shape["path"]) + self.assertEqual( + hit, (shape["category"], shape["severity"], shape["rule"])) + + def test_logql_prefilter_admits_every_classified_shape(self): + prefilter = re.compile(config.HTTP_LINE_FILTER) + missed = [ + shape["path"] + for shape in self.table() + if not prefilter.search(json.dumps(self.record(shape), separators=(",", ":"))) + ] + self.assertEqual( + missed, [], + "classify() flags these, but the Loki prefilter drops them before " + "Python ever sees the line: %r" % missed[:8]) + + def test_the_four_reported_sqli_payloads_are_in_the_table(self): + """Regression pin for t_a3604aab: these four were dropped, verbatim.""" + shapes = {s["path"] for s in self.table()} + for path in ("/?id=1%27%20OR%20%271%27=%271", + "/?id=1' OR '1'='1", + "/?id=1%27%3BWAITFOR%20DELAY%20%270:0:5%27", + "/?q=1%27%20AND%20sleep(5)--"): + self.assertIn(path, shapes) + + class TestForgejoFalsePositives(unittest.TestCase): """The shapes that made a naive rule unusable on this stream."""