The collector gzips on rotation, so FLOW_PATTERN 'flows-*.jsonl' saw 1 of 5 files (measured: 1/72 of the retained window). Archives are immutable, so each is read once, whole, decrypted lazily and recorded in state['archives']. - archives are bounded (ARCHIVE_MAX_SECONDS/BYTES/FILES, newest first) and flows are consumed in batches; a partial pass leaves state seeded=false and reports the unread files as pending, so the next tick continues instead of alerting from a window it has not examined - the alert window comes from the coverage start in the consumed filenames (monotone), so 'no prior in ~Xh of observed flows' is now verifiable - the seed absorbs every egress tuple in the window, not just findings (a web-port tuple never becomes one on its first tick); a warm state meeting archives for the first time absorbs them the same way - a torn .gz keeps its readable prefix, is noted in state/seed.failed, and does not abort the tick; in-flight *.gz.tmp.* markers are never read - report.json carries window_h/covered_since/seed; 12 new tests
153 lines
7.1 KiB
Python
153 lines
7.1 KiB
Python
"""Every tunable in one place, with the measurement it came from.
|
|
|
|
Thresholds are deliberately conservative in the sense of *few, quiet rules*
|
|
rather than tight numbers: a rule that fires on ordinary traffic gets the whole
|
|
channel muted. Each value below was checked against live data on 2026-11-27
|
|
(see README "Baselines").
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
HOME = Path.home()
|
|
|
|
# ---------------------------------------------------------------- paths ----
|
|
# The state file must NOT live inside a {job_id} dir: the scheduler reclaims
|
|
# those. ~/.hermes/cron/ is stable.
|
|
STATE_FILE = Path(
|
|
os.environ.get("NETSEC_SCAN_STATE", HOME / ".hermes" / "cron" / "netsec-scan-state.json")
|
|
)
|
|
REPORT_DIR = Path(
|
|
os.environ.get("NETSEC_SCAN_REPORTS", HOME / "Projects" / "netsec-agent" / "reports")
|
|
)
|
|
|
|
# ------------------------------------------------- collector (remote) ----
|
|
# The collector is a DaemonSet on siriussec writing hostPath /var/log/netsec.
|
|
# The sensor runs on the admin host, so it reads those files over SSH with
|
|
# `sudo -n` (NOPASSWD is configured for Lancelot). Never poll hubble directly:
|
|
# the agent ring buffer is ~28 s at 148 flows/s, so a poll is lossy by design.
|
|
SSH_HOST = os.environ.get("NETSEC_SCAN_SSH_HOST", "[email protected]")
|
|
SSH_KEY = os.environ.get("NETSEC_SCAN_SSH_KEY", str(HOME / ".ssh" / "lancelot_priv"))
|
|
SSH_TIMEOUT = int(os.environ.get("NETSEC_SCAN_SSH_TIMEOUT", "60"))
|
|
FLOW_DIR = os.environ.get("NETSEC_SCAN_FLOW_DIR", "/var/log/netsec")
|
|
# The collector gzips a file the moment it rotates (t_cfea18ac), so the store is
|
|
# `flows-<stamp>-<n>.jsonl` (the live file) plus `flows-<stamp>-<n>.jsonl.gz`
|
|
# (closed, immutable). Without the trailing `*` the sensor can only see the live
|
|
# file — i.e. minutes of the ~74 h the 960 MiB cap actually retains.
|
|
FLOW_PATTERN = os.environ.get("NETSEC_SCAN_FLOW_PATTERN", "flows-*.jsonl*")
|
|
FLOW_GLOB = FLOW_DIR + "/" + FLOW_PATTERN
|
|
|
|
# ------------------------------------------------- archival seed budget ----
|
|
# Archives are read once, whole, and only at cold start / right after a
|
|
# rotation. Unbounded is not an option: 960 MiB of gzip is ~16 GB of JSON at the
|
|
# measured 17.5x ratio, and holding that as Flow objects would exhaust memory
|
|
# long before it finished. Measured on the admin host 2026-11-27: json.loads +
|
|
# parse_flow + egress-key extraction runs at ~140 MB/s of decompressed JSON, so
|
|
# these defaults are sized for a one-time seed of ~12 GB (~55 h of current
|
|
# volume) and a warm tick, which reads zero or one archive, finishes in ~1 s.
|
|
# Whichever budget binds first wins; the window actually covered is printed and
|
|
# persisted, never implied.
|
|
ARCHIVE_MAX_SECONDS = float(os.environ.get("NETSEC_SCAN_ARCHIVE_SECONDS", "90"))
|
|
ARCHIVE_MAX_BYTES = int(os.environ.get("NETSEC_SCAN_ARCHIVE_BYTES", str(8 * 1024 ** 3)))
|
|
ARCHIVE_MAX_FILES = int(os.environ.get("NETSEC_SCAN_ARCHIVE_FILES", "40"))
|
|
|
|
# Flows are consumed in batches so a seed never holds the whole window in RAM as
|
|
# Flow objects (16 GB of JSON is millions of them).
|
|
FLOW_BATCH = int(os.environ.get("NETSEC_SCAN_FLOW_BATCH", "20000"))
|
|
|
|
# ----------------------------------------------------- prometheus ----
|
|
PROM_PROXY = (
|
|
"/api/v1/namespaces/monitoring/services/"
|
|
"http:kube-prometheus-stack-prometheus:9090/proxy"
|
|
)
|
|
PROM_TIMEOUT = int(os.environ.get("NETSEC_SCAN_PROM_TIMEOUT", "25"))
|
|
|
|
# ----------------------------------------------------- thresholds ----
|
|
# Hubble pipeline loss: events/s over 15m. Measured 0.60/s siriussec, 0.13/s
|
|
# linode, 0.00/s rpi at idle — i.e. the sensor is already blind to ~0.7 % of
|
|
# traffic. This rule exists so the other rules cannot lie about being calm.
|
|
LOST_EVENTS_RATE = 0.05
|
|
|
|
# TCP/UDP drops (ICMPv6 UNSUPPORTED_L3_PROTOCOL excluded — it is constant
|
|
# neighbour-discovery noise at ~0.39/s and would mask a real spike).
|
|
DROP_FLOOR = 0.05 # events/s; below this a "spike" is one packet
|
|
DROP_RATIO = 3.0 # vs the 1h baseline rate
|
|
|
|
# SYN without SYN-ACK (connect sweep). Measured 1.59 at idle; ring-buffer loss
|
|
# also nudges it up, hence the 3x gate and the absolute floor.
|
|
SYN_RATIO = 3.0
|
|
SYN_FLOOR = 1.0 # SYN/s
|
|
|
|
# RST storm: 10x the 6h average. Measured 2.5/s at idle (ephemeral-port churn).
|
|
RST_RATIO = 10.0
|
|
RST_FLOOR = 2.0 # RST/s
|
|
|
|
# L7 error share. Measured 0.15/s errors of 6.9/s requests (2.2 %).
|
|
HTTP_ERROR_RATIO = 0.5
|
|
HTTP_MIN_RATE = 0.5 # requests/s, so a single 404 in a quiet minute cannot trip it
|
|
|
|
# Ports below the Linux ephemeral range (32768-60999) that appear in
|
|
# hubble_port_distribution_total for the first time in 24h are treated as new
|
|
# listeners. Without the cut, every client source port is a "new port".
|
|
NEW_PORT_CEILING = 32768
|
|
NEW_PORT_LOOKBACK = "24h"
|
|
|
|
# Flow-store novelty.
|
|
BEACON_MIN_CONNS = 20 # connections (distinct client sockets) from one workload to one dst:port
|
|
BEACON_MAX_CV = 0.15 # coefficient of variation of inter-arrival times
|
|
BEACON_WINDOW = 7200 # seconds of connection history kept per tuple (2h)
|
|
BEACON_MAX_INTERVAL = 900.0 # mean interval ceiling; 30s beacons fit, 1h cron-ish traffic does not
|
|
|
|
# Severity for a first-seen egress tuple: standard web ports are common CDN
|
|
# churn, everything else is a much stronger beacon/exfil signal.
|
|
WEB_PORTS = frozenset({80, 443, 8080, 8443})
|
|
# A first-seen *web-port* tuple must also be observed in three distinct ticks
|
|
# (~15 min) before it alerts. Measured 2026-11-27: one CI run produced 20
|
|
# one-shot AWS/GitHub CDN destinations inside a single 5-minute window, and a
|
|
# second-tick gate still let ~2 through per build. CDN rotation is one-shot or
|
|
# short-lived; a beacon or an exfil destination persists. Stated cost: a
|
|
# smash-and-grab that contacts a brand-new host twice and stops is NOT alerted
|
|
# on 80/443/8080/8443 (it is on any other port, and on any DROPPED verdict),
|
|
# and periodic C2 is rule 8's job (`beacon-shape`, >= 20 conns in 2 h).
|
|
EGRESS_WEB_MIN_TICKS = 3
|
|
MAX_EGRESS_COUNTS = 20000
|
|
|
|
# Re-alert cooldown for *state* tripwires (current-condition rules). Flow
|
|
# novelty is permanent: a tuple fires exactly once, ever. A state rule gets to
|
|
# fire again after this window so "it broke, was fixed, and broke again" is not
|
|
# silently forgotten.
|
|
STATE_COOLDOWN = 86400 # 24h
|
|
|
|
# Sensor degradation memo: the same class of failure is announced once per
|
|
# window, not every 5 minutes.
|
|
DEGRADED_MEMO = 6 * 3600
|
|
|
|
# State bounds (a cumulative seen-set cannot grow forever).
|
|
MAX_SEEN = 20000
|
|
MAX_EGRESS = 20000
|
|
MAX_BEACON_TUPLES = 2000
|
|
MAX_DAY_EGRESS = 500
|
|
|
|
# Output contract.
|
|
MAX_LINE = 200
|
|
|
|
# Local network space. Anything not "global" is not egress; these are explicit
|
|
# so the rule reads correctly even if a future Python changes is_global.
|
|
NON_EGRESS_NETS = (
|
|
"0.0.0.0/8",
|
|
"10.0.0.0/8", # includes pod 10.244/16, mesh 10.10/24, service 10.96/12
|
|
"100.64.0.0/10", # CGNAT (tailnet)
|
|
"127.0.0.0/8",
|
|
"169.254.0.0/16",
|
|
"172.16.0.0/12",
|
|
"192.168.0.0/16", # homelab LAN
|
|
"::1/128",
|
|
"fc00::/7",
|
|
"fe80::/10",
|
|
"ff00::/8",
|
|
)
|
|
|
|
KUBECTL_CANDIDATES = ("kubectl", "/usr/sbin/kubectl", "/usr/bin/kubectl", "/usr/local/bin/kubectl")
|
|
SSH_CANDIDATES = ("ssh", "/usr/sbin/ssh", "/usr/bin/ssh")
|