netsec-scan: three-tick web-port gate (2 still leaked ~2 lines per CI build)

Measured on the 00:40 run: a second-tick gate cut a 21-line CDN burst to 2 lines,
still noise for a channel whose whole value is being quiet. A beacon/exfil
destination persists for hours (rule 8 catches periodicity at >=20 conns/2h), a
CDN endpoint used by one job does not survive 15 minutes. Stated cost documented
in config + README.
This commit is contained in:
sirius 2026-11-27 00:41:41 -05:00
commit f4001d589e
3 changed files with 44 additions and 28 deletions

View file

@ -94,14 +94,17 @@ destination (`…/dropped` in the entity, HIGH): a pod trying to reach a new hos
and being refused is a stronger signal than one that succeeded, and collapsing and being refused is a stronger signal than one that succeeded, and collapsing
them would hide it behind the forwarded tuple's fingerprint. them would hide it behind the forwarded tuple's fingerprint.
Rule 7 also keeps **web-port novelty** (80/443/8080/8443) on a second-tick gate: Rule 7 also keeps **web-port novelty** (80/443/8080/8443) on a **three-tick
a first-seen web destination is only worth a line once it appears in **two** (~15 min) persistence gate**: a first-seen web destination is only worth a line
distinct ticks. Measured 2026-11-27: one CI run produced 20 one-shot AWS/GitHub once it appears in three distinct ticks. Measured 2026-11-27: one CI run
CDN destinations inside a single 5-minute window, which is a 20-line message and produced 20 one-shot AWS/GitHub CDN destinations inside a single 5-minute window
exactly how a channel gets muted. CDN rotation is one-shot by nature; a beacon or (a 20-line message) and a two-tick gate still let ~2 per build through. CDN
an exfil destination is retried, so it still fires — a tick later. Non-web ports rotation is one-shot or short-lived; a beacon or an exfil destination persists.
and any `DROPPED` verdict alert on first sight, and a `DROPPED` web-port tuple is **Stated cost**: a smash-and-grab that contacts a brand-new host twice and stops
HIGH. Rationale and the two-tick evidence live in `config.EGRESS_WEB_MIN_TICKS`. 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). Non-web ports alert on first sight; a `DROPPED` web-port tuple is HIGH.
Rationale and the measured numbers live in `config.EGRESS_WEB_MIN_TICKS`.
**Deliberately not implemented** (and why): **Deliberately not implemented** (and why):

View file

@ -81,12 +81,15 @@ 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 # Severity for a first-seen egress tuple: standard web ports are common CDN
# churn, everything else is a much stronger beacon/exfil signal. # churn, everything else is a much stronger beacon/exfil signal.
WEB_PORTS = frozenset({80, 443, 8080, 8443}) WEB_PORTS = frozenset({80, 443, 8080, 8443})
# A first-seen *web-port* tuple must also be observed in a second tick before it # A first-seen *web-port* tuple must also be observed in three distinct ticks
# alerts. Measured 2026-11-27: one CI run produced 20 one-shot AWS/GitHub CDN # (~15 min) before it alerts. Measured 2026-11-27: one CI run produced 20
# destinations in a single 5-minute window, which is a 20-line message and # one-shot AWS/GitHub CDN destinations inside a single 5-minute window, and a
# exactly how a channel gets muted. CDN rotation is one-shot; a beacon or an # second-tick gate still let ~2 through per build. CDN rotation is one-shot or
# exfil destination is retried. Non-web ports still alert on first sight. # short-lived; a beacon or an exfil destination persists. Stated cost: a
EGRESS_WEB_MIN_TICKS = 2 # 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 MAX_EGRESS_COUNTS = 20000
# Re-alert cooldown for *state* tripwires (current-condition rules). Flow # Re-alert cooldown for *state* tripwires (current-condition rules). Flow

View file

@ -24,17 +24,19 @@ class TestFirstSeenEgress(unittest.TestCase):
now or self.NOW, 0.5, tick) now or self.NOW, 0.5, tick)
def warmed(self, flows): def warmed(self, flows):
"""Baseline tick: populates counts, emits nothing for web ports.""" """Run the ticks *before* the gate fires; emit nothing, fill counts."""
counts = {} counts = {}
self.assertEqual(self.evaluate(flows, {}, counts, tick=1), []) for tick in range(1, config.EGRESS_WEB_MIN_TICKS):
self.assertEqual(self.evaluate(flows, {}, counts, tick=tick), [])
return counts return counts
def test_web_port_tuple_needs_a_second_tick(self): def test_web_port_tuple_needs_three_ticks(self):
"""One-shot CDN destinations must not produce a line on first sight.""" """One-shot / short-lived CDN destinations must not produce a line."""
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
counts = {} counts = {}
self.assertEqual(self.evaluate(flows, {}, counts, tick=1), []) self.assertEqual(self.evaluate(flows, {}, counts, tick=1), [])
findings = self.evaluate(flows, {}, counts, tick=2) self.assertEqual(self.evaluate(flows, {}, counts, tick=2), [])
findings = self.evaluate(flows, {}, counts, tick=3)
self.assertTrue(findings) self.assertTrue(findings)
self.assertTrue(all(f.rule == "egress-new-dst" for f in findings)) self.assertTrue(all(f.rule == "egress-new-dst" for f in findings))
self.assertTrue(all(f.severity == "MEDIUM" for f in findings)) self.assertTrue(all(f.severity == "MEDIUM" for f in findings))
@ -60,27 +62,29 @@ class TestFirstSeenEgress(unittest.TestCase):
def test_seen_fingerprint_suppresses_the_rule(self): def test_seen_fingerprint_suppresses_the_rule(self):
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
counts = self.warmed(flows) counts = self.warmed(flows)
first = self.evaluate(flows, {}, counts, tick=2) first = self.evaluate(flows, {}, counts, tick=config.EGRESS_WEB_MIN_TICKS)
self.assertTrue(first) self.assertTrue(first)
seen = {f.fingerprint: {"rule": f.rule} for f in first} seen = {f.fingerprint: {"rule": f.rule} for f in first}
self.assertEqual(self.evaluate(flows, seen, counts, tick=3), []) self.assertEqual(self.evaluate(flows, seen, counts,
tick=config.EGRESS_WEB_MIN_TICKS + 1), [])
def test_single_fire_then_rearm_when_one_key_is_deleted(self): def test_single_fire_then_rearm_when_one_key_is_deleted(self):
"""The acceptance procedure: delete exactly one fp -> exactly one line.""" """The acceptance procedure: delete exactly one fp -> exactly one line."""
from netsec_scan.__main__ import apply_dedupe from netsec_scan.__main__ import apply_dedupe
fires = config.EGRESS_WEB_MIN_TICKS
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
counts = self.warmed(flows) counts = self.warmed(flows)
seen = {} seen = {}
first = apply_dedupe(self.evaluate(flows, seen, counts, tick=2), first = apply_dedupe(self.evaluate(flows, seen, counts, tick=fires),
seen, self.NOW.timestamp(), False) seen, self.NOW.timestamp(), False)
self.assertTrue(first) self.assertTrue(first)
again = apply_dedupe(self.evaluate(flows, seen, counts, tick=3), again = apply_dedupe(self.evaluate(flows, seen, counts, tick=fires + 1),
seen, self.NOW.timestamp(), False) seen, self.NOW.timestamp(), False)
self.assertEqual(again, []) self.assertEqual(again, [])
target = first[0].fingerprint target = first[0].fingerprint
del seen[target] del seen[target]
rearmed = apply_dedupe(self.evaluate(flows, seen, counts, tick=4), rearmed = apply_dedupe(self.evaluate(flows, seen, counts, tick=fires + 2),
seen, self.NOW.timestamp(), False) seen, self.NOW.timestamp(), False)
self.assertEqual([f.fingerprint for f in rearmed], [target]) self.assertEqual([f.fingerprint for f in rearmed], [target])
@ -89,20 +93,26 @@ class TestFirstSeenEgress(unittest.TestCase):
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
seen = {"deadbeef0000": {"rule": "egress-new-dst", "kind": "novelty"}} seen = {"deadbeef0000": {"rule": "egress-new-dst", "kind": "novelty"}}
apply_dedupe(self.evaluate(flows, seen, self.warmed(flows), tick=2), apply_dedupe(self.evaluate(flows, seen, self.warmed(flows),
tick=config.EGRESS_WEB_MIN_TICKS),
seen, self.NOW.timestamp(), False) seen, self.NOW.timestamp(), False)
self.assertIn("deadbeef0000", seen) self.assertIn("deadbeef0000", seen)
self.assertGreater(len(seen), 1) self.assertGreater(len(seen), 1)
def test_fingerprint_is_stable_across_runs(self): def test_fingerprint_is_stable_across_runs(self):
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
a = self.evaluate(flows, {}, self.warmed(flows), tick=2, now=utc(2026, 11, 27, 5, 20)) fires = config.EGRESS_WEB_MIN_TICKS
b = self.evaluate(flows, {}, self.warmed(flows), tick=2, now=utc(2027, 1, 2, 9, 0)) a = self.evaluate(flows, {}, self.warmed(flows), tick=fires,
now=utc(2026, 11, 27, 5, 20))
b = self.evaluate(flows, {}, self.warmed(flows), tick=fires,
now=utc(2027, 1, 2, 9, 0))
self.assertEqual([f.fingerprint for f in a], [f.fingerprint for f in b]) self.assertEqual([f.fingerprint for f in a], [f.fingerprint for f in b])
def test_nonexternal_and_reserved_are_ignored(self): def test_nonexternal_and_reserved_are_ignored(self):
flows = fixture_flows("egress-sample.jsonl") flows = fixture_flows("egress-sample.jsonl")
findings = self.evaluate(flows, {}, self.warmed(flows), tick=2) findings = self.evaluate(flows, {}, self.warmed(flows),
tick=config.EGRESS_WEB_MIN_TICKS)
self.assertTrue(findings)
for f in findings: for f in findings:
self.assertNotIn("reserved:", f.entity) self.assertNotIn("reserved:", f.entity)
self.assertNotIn("192.168.", f.entity) self.assertNotIn("192.168.", f.entity)