netsec-scan: second-tick gate for web-port egress novelty (kill CDN churn)

One CI run produced 20 one-shot AWS/GitHub CDN destinations in a single 5-minute
tick (21-line message, nothing reached the operator only because delivery was
unresolved). First-seen tuples now carry per-tick observation counts in state:
web ports (80/443/8080/8443) need two distinct ticks before alerting, non-web
ports and any DROPPED verdict still alert on first sight. A beacon/exfil
destination is retried; CDN rotation is not.

Also: cron deliver switched to local because this profile has
platforms.telegram.enabled=false (deliver=telegram errors 'no delivery target
resolved'), and the README now documents that constraint.
This commit is contained in:
sirius 2026-11-27 00:32:25 -05:00
commit be60d74159
6 changed files with 94 additions and 33 deletions

View file

@ -244,7 +244,9 @@ def main(argv: Optional[List[str]] = None) -> int:
seeding = not st.get("seeded")
window_h = track_window(st, flows)
new_egress = rules.rule_egress_first_seen(flows, st["seen"], _utcnow(), window_h)
tick = int(st["totals"].get("ticks", 0)) + 1
new_egress = rules.rule_egress_first_seen(
flows, st["seen"], st["egress_counts"], _utcnow(), window_h, tick)
beacon = rules.rule_beacon(flows, st["beacon"], now)
rules.prune_beacon(st["beacon"], now)
flow_findings = new_egress + beacon

View file

@ -81,6 +81,13 @@ BEACON_MAX_INTERVAL = 900.0 # mean interval ceiling; 30s beacons fit, 1h cron-i
# 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

View file

@ -196,7 +196,8 @@ def _window_label(hours: float) -> str:
def rule_egress_first_seen(flows: Iterable[Flow], seen: Dict[str, dict],
now: datetime, window_h: float = None) -> List[Finding]:
counts: Dict[str, dict], now: datetime,
window_h: float = None, tick: int = 0) -> List[Finding]:
"""First-seen egress tuple = candidate beacon/exfil.
Novelty is gated on the **cumulative seen-set only** (fingerprint presence),
@ -204,14 +205,14 @@ def rule_egress_first_seen(flows: Iterable[Flow], seen: Dict[str, dict],
fingerprint from `seen` re-arms the rule, which is what the acceptance
procedure exercises.
The alert says how long this sensor has actually been watching
(`window_h`): the collector caps at 1 GiB, which at the measured ~2 MB/min
is days, not the month the design note assumed. Never imply a longer
baseline than was observed.
`counts` (tuple -> {n, first, last_tick}) exists only to separate one-shot
CDN rotation from a repeat destination: a web-port tuple must be seen in
`EGRESS_WEB_MIN_TICKS` distinct ticks before it is worth a line. Non-web
ports alert immediately, and a `DROPPED` verdict is always HIGH.
A `DROPPED` verdict on a new to-world tuple is its own signal (the pod tried
something that policy refused), so it is keyed and graded separately from
the forwarded traffic for the same destination.
The alert says how long this sensor has actually been watching
(`window_h`): the collector caps at 1 GiB, which is days, not the month the
design note assumed. Never imply a longer baseline than was observed.
"""
out: List[Finding] = []
window = (" (no prior in ~%s of observed flows)" % _window_label(window_h)
@ -220,13 +221,21 @@ def rule_egress_first_seen(flows: Iterable[Flow], seen: Dict[str, dict],
key = _egress_key(flow)
if not key:
continue
entry = counts.get(key)
if entry is None:
entry = {"n": 1, "first": (flow.time or now).isoformat(timespec="seconds"),
"last_tick": tick}
counts[key] = entry
elif entry.get("last_tick") != tick:
entry["n"] = int(entry.get("n", 1)) + 1
entry["last_tick"] = tick
dropped = flow.verdict == "DROPPED"
if dropped:
key += "/dropped"
if flow.dst_port in config.WEB_PORTS and not dropped:
severity = "MEDIUM"
else:
severity = "HIGH"
web = flow.dst_port in config.WEB_PORTS
if web and not dropped and int(entry.get("n", 1)) < config.EGRESS_WEB_MIN_TICKS:
continue # one-shot CDN destination: not worth a line yet
severity = "MEDIUM" if web and not dropped else "HIGH"
finding = Finding(
rule="egress-new-dst", kind="novelty", severity=severity, entity=key,
detail="%s first-seen %s%s" % (key, _ts(flow.time), window),

View file

@ -34,6 +34,7 @@ def empty() -> Dict[str, Any]:
"seeded": False, # flow novelty has been baselined once
"seen": {}, # fp -> {rule, kind, entity, severity, first, last_alert}
"beacon": {}, # tuple -> {socket: ts}
"egress_counts": {}, # tuple -> {n ticks seen, first, last_tick}
"observed_since": None, # earliest flow timestamp ever observed (ISO)
"files": {}, # name -> {offset, carry, size}
"degraded": {}, # failure class -> iso of last announcement
@ -56,7 +57,7 @@ def load(path: Path) -> Dict[str, Any]:
base = empty()
for key, value in base.items():
data.setdefault(key, value)
for key in ("seen", "beacon", "files", "degraded"):
for key in ("seen", "beacon", "egress_counts", "files", "degraded"):
if not isinstance(data.get(key), dict):
data[key] = {}
return data
@ -89,6 +90,11 @@ def prune(state: Dict[str, Any]) -> None:
key=lambda kv: max(kv[1].values()) if kv[1] else 0)
for key, _ in ordered[: len(ordered) - config.MAX_BEACON_TUPLES + 200]:
beacon.pop(key, None)
counts = state.get("egress_counts") or {}
if len(counts) > config.MAX_EGRESS_COUNTS:
ordered = sorted(counts.items(), key=lambda kv: int(kv[1].get("last_tick", 0)))
for key, _ in ordered[: len(ordered) - config.MAX_EGRESS_COUNTS + 2000]:
counts.pop(key, None)
day = state["day"]
if len(day.get("egress", [])) > config.MAX_DAY_EGRESS:
day["egress"] = day["egress"][-config.MAX_DAY_EGRESS:]