128 lines
5.4 KiB
Python
128 lines
5.4 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")
|
||
|
|
FLOW_PATTERN = os.environ.get("NETSEC_SCAN_FLOW_PATTERN", "flows-*.jsonl")
|
||
|
|
FLOW_GLOB = FLOW_DIR + "/" + FLOW_PATTERN
|
||
|
|
|
||
|
|
# ----------------------------------------------------- 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 a second tick before it
|
||
|
|
# alerts. Measured 2026-11-27: one CI run produced 20 one-shot AWS/GitHub CDN
|
||
|
|
# destinations in a single 5-minute window, which is a 20-line message and
|
||
|
|
# exactly how a channel gets muted. CDN rotation is one-shot; a beacon or an
|
||
|
|
# exfil destination is retried. Non-web ports still alert on first sight.
|
||
|
|
EGRESS_WEB_MIN_TICKS = 2
|
||
|
|
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")
|