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

@ -24,17 +24,19 @@ class TestFirstSeenEgress(unittest.TestCase):
now or self.NOW, 0.5, tick)
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 = {}
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
def test_web_port_tuple_needs_a_second_tick(self):
"""One-shot CDN destinations must not produce a line on first sight."""
def test_web_port_tuple_needs_three_ticks(self):
"""One-shot / short-lived CDN destinations must not produce a line."""
flows = fixture_flows("egress-sample.jsonl")
counts = {}
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(all(f.rule == "egress-new-dst" 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):
flows = fixture_flows("egress-sample.jsonl")
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)
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):
"""The acceptance procedure: delete exactly one fp -> exactly one line."""
from netsec_scan.__main__ import apply_dedupe
fires = config.EGRESS_WEB_MIN_TICKS
flows = fixture_flows("egress-sample.jsonl")
counts = self.warmed(flows)
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)
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)
self.assertEqual(again, [])
target = first[0].fingerprint
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)
self.assertEqual([f.fingerprint for f in rearmed], [target])
@ -89,20 +93,26 @@ class TestFirstSeenEgress(unittest.TestCase):
flows = fixture_flows("egress-sample.jsonl")
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)
self.assertIn("deadbeef0000", seen)
self.assertGreater(len(seen), 1)
def test_fingerprint_is_stable_across_runs(self):
flows = fixture_flows("egress-sample.jsonl")
a = self.evaluate(flows, {}, self.warmed(flows), tick=2, now=utc(2026, 11, 27, 5, 20))
b = self.evaluate(flows, {}, self.warmed(flows), tick=2, now=utc(2027, 1, 2, 9, 0))
fires = config.EGRESS_WEB_MIN_TICKS
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])
def test_nonexternal_and_reserved_are_ignored(self):
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:
self.assertNotIn("reserved:", f.entity)
self.assertNotIn("192.168.", f.entity)