netsec-scan: honest observation window, /dropped tuples, report baseline counts

- first-seen egress is gated by the cumulative seen-set only (deleting a
  fingerprint re-arms the rule); the alert states the observed window
  (~20m / 3.2h / 2.1d) instead of implying the collector holds weeks.
- a DROPPED attempt on a new to-world tuple is keyed separately from forwarded
  traffic to the same destination and graded HIGH.
- daily report counts every external tuple seen that day, not just novel ones.
- drop-prune of the dead state['egress'] map; tests cover the single-fire rearm,
  day counters and the egress union.
- README: measured collector volume/tombstones/partial-tail behaviour, scheduler
  shim path.
This commit is contained in:
sirius 2026-11-27 00:24:22 -05:00
commit c31d46a037
6 changed files with 174 additions and 52 deletions

View file

@ -165,6 +165,16 @@ def _egress_key(flow: Flow) -> Optional[str]:
return "%s->%s:%d/%s" % (flow.src_workload, flow.dst_ip, flow.dst_port, flow.proto.lower())
def egress_keys(flows: Iterable[Flow]) -> List[str]:
"""Every external destination tuple in this slice (novel or not)."""
keys = []
for flow in flows:
key = _egress_key(flow)
if key and key not in keys:
keys.append(key)
return keys
def prune_beacon(beacon: Dict[str, Dict[str, float]], now: float) -> None:
"""Drop sockets that fell out of the window from every tracked tuple."""
cutoff = now - config.BEACON_WINDOW
@ -176,25 +186,54 @@ def prune_beacon(beacon: Dict[str, Dict[str, float]], now: float) -> None:
beacon.pop(key, None)
def rule_egress_first_seen(flows: Iterable[Flow], egress_seen: Dict[str, str],
now: datetime) -> List[Finding]:
def _window_label(hours: float) -> str:
"""Observation window in the shortest honest form."""
if hours < 1:
return "%dm" % max(1, int(round(hours * 60)))
if hours < 48:
return "%.1fh" % hours
return "%.1fd" % (hours / 24.0)
def rule_egress_first_seen(flows: Iterable[Flow], seen: Dict[str, dict],
now: datetime, window_h: float = None) -> List[Finding]:
"""First-seen egress tuple = candidate beacon/exfil.
The seen-set is cumulative and seeded silently on the first run, so
deploying this sensor cannot dump the whole retained history into chat.
Novelty is gated on the **cumulative seen-set only** (fingerprint presence),
so there is exactly one place that decides "already reported": deleting a
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.
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.
"""
out: List[Finding] = []
window = (" (no prior in ~%s of observed flows)" % _window_label(window_h)
if window_h else " (no prior in observed flows)")
for flow in flows:
key = _egress_key(flow)
if not key or key in egress_seen:
if not key:
continue
egress_seen[key] = (flow.time or now).astimezone(timezone.utc).isoformat()
port = flow.dst_port
severity = "MEDIUM" if port in config.WEB_PORTS else "HIGH"
out.append(Finding(
dropped = flow.verdict == "DROPPED"
if dropped:
key += "/dropped"
if flow.dst_port in config.WEB_PORTS and not dropped:
severity = "MEDIUM"
else:
severity = "HIGH"
finding = Finding(
rule="egress-new-dst", kind="novelty", severity=severity, entity=key,
detail="%s first-seen %s (no prior in retained window)" % (key, _ts(flow.time)),
))
detail="%s first-seen %s%s" % (key, _ts(flow.time), window),
)
if finding.fingerprint in seen:
continue
out.append(finding)
return out