netsec-scan: HTTP attack-shape rules over the gateway access log (§4d)
F7 (t_e662b03a) gave every Gateway listener a JSON access line and shipped it to Loki; this is the §4d consumer that had none. New `http_rules.py` classifies candidate lines and `loki.py` reads them through the logging/loki Service proxy with two LogQL queries per tick - a `|~` prefilter for attack-shaped lines and a `sum by (client_ip, peer)` 1-minute matrix for 401/403. Loki does the volume reduction: a calm 10-minute window (5 474 requests / 3.5 MB) puts ~150 lines on the wire. Rules (one line per host+client+category, fingerprint excludes the path): http-traversal, -sqli, -nul-byte, -secret-probe, -vcs-probe, -backup-probe, -scanner-ua, http-auth-brute (401/403 only, >=20/min and >=30 in 5m per coalesce(client_ip, peer)), http-flood (cap 12 new fingerprints/tick). FP control is the point: Forgejo is 93.5 % of the stream, so smart-HTTP git endpoints and browse/archive/RSS URLs on the Forgejo hosts are excluded from path rules, and severity escalates only when the shape is in the path component AND the origin answered 2xx (a 200 on `/?file=../../.env` is the landing page). Measured: 17 639 of 35 445 lines/hour are 303 -> /user/login (3xx, never counted); 293 are 404 crawler churn; the noisiest legitimate 401/403 client does 3/min. Verified on the live stream: calm window 07:03:40-07:13:40Z -> 0 findings, empty stdout, exit 0; the 06:48-06:53Z 91.92.47.0/24 burst -> 8 findings; crafted requests fired all nine rule kinds (public via Cloudflare and direct to the overlay for the peer fallback); first tick baselines silently, next emits, next is silent, deleting one fingerprint re-emits exactly one line. 85 unit tests. Docs: docs/gateway-http-rules.md. run.sh now cds to its own directory so a worktree runs its own code.
This commit is contained in:
parent
bde343c75f
commit
63bac6e06f
9 changed files with 1216 additions and 16 deletions
|
|
@ -151,3 +151,60 @@ NON_EGRESS_NETS = (
|
|||
|
||||
KUBECTL_CANDIDATES = ("kubectl", "/usr/sbin/kubectl", "/usr/bin/kubectl", "/usr/local/bin/kubectl")
|
||||
SSH_CANDIDATES = ("ssh", "/usr/sbin/ssh", "/usr/bin/ssh")
|
||||
|
||||
# ------------------------------------------------- gateway access log ----
|
||||
# `cilium-envoy` writes one JSON access line per request (t_e662b03a); the
|
||||
# `logging` Alloy DaemonSet ships it to Loki with 192 h retention. The sensor
|
||||
# reads it **only** through LogQL so the filtering runs in Loki, not here.
|
||||
# Measured 2026-11-27: 641 B/line, 9.54 lines/s, 528 MB/day raw, 93.5 % Forgejo.
|
||||
LOKI_PROXY = (
|
||||
"/api/v1/namespaces/logging/services/loki:3100/proxy"
|
||||
)
|
||||
LOKI_TIMEOUT = int(os.environ.get("NETSEC_SCAN_LOKI_TIMEOUT", "25"))
|
||||
# Exact stream selector from the F7 handoff. Narrow on purpose: it is the
|
||||
# difference between reading one pod's stream on one node and the whole cluster.
|
||||
LOKI_STREAM = os.environ.get(
|
||||
"NETSEC_SCAN_LOKI_STREAM",
|
||||
'{namespace="kube-system", container="cilium-envoy", job="kube/pods", node="siriussec"}',
|
||||
)
|
||||
|
||||
# How far back each tick looks. The cron interval is 5 min, so 10 min guarantees
|
||||
# overlap across ticks (a line can never fall between two windows) and the
|
||||
# fingerprint dedupe absorbs the overlap. 10 min ≈ 3.6 MB scanned per query.
|
||||
HTTP_WINDOW = int(os.environ.get("NETSEC_SCAN_HTTP_WINDOW", "600"))
|
||||
HTTP_RATE_WINDOW = int(os.environ.get("NETSEC_SCAN_HTTP_RATE_WINDOW", "300"))
|
||||
HTTP_MAX_LINES = int(os.environ.get("NETSEC_SCAN_HTTP_MAX_LINES", "600"))
|
||||
# A tick may announce at most this many *new* client/path fingerprints; the rest
|
||||
# collapse into one `http-flood` state line. IP-rotating scanners would otherwise
|
||||
# mint one line per source address.
|
||||
HTTP_MAX_NEW_PER_TICK = int(os.environ.get("NETSEC_SCAN_HTTP_MAX_NEW", "12"))
|
||||
|
||||
# Severity control. Cloudflare replaces the socket peer with the tunnel
|
||||
# (10.10.0.1 on the Pi); `client_ip` is the real client there. A request with
|
||||
# neither is unattributable and is never used as a per-client key.
|
||||
HTTP_EDGE_PEERS = frozenset({"10.10.0.1"})
|
||||
# Only these hosts serve Forgejo browse URLs; on any other host the same path
|
||||
# shape (e.g. `/<a>/<b>/raw/.../.env`) is a probe, not a repository view.
|
||||
HTTP_FORGEJO_HOSTS = frozenset({"forgejo.siriusdevops.com"})
|
||||
|
||||
# 401/403 brute force per client. Measured on a calm hour 2026-11-27: 20 auth
|
||||
# failures *total*, all from the CI git client (git/2.56.0 retrying a push),
|
||||
# peak 1/min. Forgejo's legitimate 303 -> /user/login bulk is 3xx and never
|
||||
# counted. So 20 failures in one minute plus 30 over five minutes is a real
|
||||
# attempt, two orders of magnitude above the CI baseline.
|
||||
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.
|
||||
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)",
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue