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:
parent
68b2616cc5
commit
c31d46a037
6 changed files with 174 additions and 52 deletions
|
|
@ -16,25 +16,47 @@ def utc(*args):
|
|||
|
||||
|
||||
class TestFirstSeenEgress(unittest.TestCase):
|
||||
def test_same_tuple_fires_once(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
seen = {}
|
||||
now = utc(2026, 11, 27, 5, 20)
|
||||
first = rules.rule_egress_first_seen(flows, seen, now)
|
||||
self.assertTrue(first)
|
||||
self.assertTrue(all(f.rule == "egress-new-dst" for f in first))
|
||||
# second evaluation over the same flows must be silent
|
||||
second = rules.rule_egress_first_seen(flows, seen, now)
|
||||
self.assertEqual(second, [])
|
||||
NOW = utc(2026, 11, 27, 5, 20)
|
||||
|
||||
def test_new_tuples_produce_findings(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
findings = rules.rule_egress_first_seen(flows, {}, self.NOW)
|
||||
self.assertTrue(findings)
|
||||
self.assertTrue(all(f.rule == "egress-new-dst" for f in findings))
|
||||
|
||||
def test_seen_fingerprint_suppresses_the_rule(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
first = rules.rule_egress_first_seen(flows, {}, self.NOW)
|
||||
seen = {f.fingerprint: {"rule": f.rule} for f in first}
|
||||
self.assertEqual(rules.rule_egress_first_seen(flows, seen, self.NOW), [])
|
||||
|
||||
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
|
||||
|
||||
def test_union_semantics_keeps_history(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
seen = {}
|
||||
rules.rule_egress_first_seen(flows, seen, utc(2026, 11, 27, 5, 20))
|
||||
before = dict(seen)
|
||||
# a later, unrelated evaluation must not shrink the seen-set
|
||||
rules.rule_egress_first_seen([], seen, utc(2026, 11, 27, 5, 25))
|
||||
self.assertEqual(seen, before)
|
||||
first = apply_dedupe(rules.rule_egress_first_seen(flows, seen, self.NOW),
|
||||
seen, self.NOW.timestamp(), False)
|
||||
self.assertTrue(first)
|
||||
again = apply_dedupe(rules.rule_egress_first_seen(flows, seen, self.NOW),
|
||||
seen, self.NOW.timestamp(), False)
|
||||
self.assertEqual(again, [])
|
||||
target = first[0].fingerprint
|
||||
del seen[target]
|
||||
rearmed = apply_dedupe(rules.rule_egress_first_seen(flows, seen, self.NOW),
|
||||
seen, self.NOW.timestamp(), False)
|
||||
self.assertEqual([f.fingerprint for f in rearmed], [target])
|
||||
|
||||
def test_seen_set_is_union_only(self):
|
||||
from netsec_scan.__main__ import apply_dedupe
|
||||
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
seen = {"deadbeef0000": {"rule": "egress-new-dst", "kind": "novelty"}}
|
||||
apply_dedupe(rules.rule_egress_first_seen(flows, seen, self.NOW),
|
||||
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")
|
||||
|
|
@ -44,7 +66,7 @@ class TestFirstSeenEgress(unittest.TestCase):
|
|||
|
||||
def test_severity_by_port(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
findings = rules.rule_egress_first_seen(flows, {}, utc(2026, 11, 27, 5, 20))
|
||||
findings = rules.rule_egress_first_seen(flows, {}, self.NOW)
|
||||
for f in findings:
|
||||
port = int(f.entity.rsplit(":", 1)[1].split("/")[0])
|
||||
want = "MEDIUM" if port in config.WEB_PORTS else "HIGH"
|
||||
|
|
@ -52,7 +74,7 @@ class TestFirstSeenEgress(unittest.TestCase):
|
|||
|
||||
def test_nonexternal_and_reserved_are_ignored(self):
|
||||
flows = fixture_flows("egress-sample.jsonl")
|
||||
findings = rules.rule_egress_first_seen(flows, {}, utc(2026, 11, 27, 5, 20))
|
||||
findings = rules.rule_egress_first_seen(flows, {}, self.NOW)
|
||||
for f in findings:
|
||||
self.assertNotIn("reserved:", f.entity)
|
||||
self.assertNotIn("192.168.", f.entity)
|
||||
|
|
@ -154,6 +176,20 @@ class TestPromRules(unittest.TestCase):
|
|||
self.assertTrue(rules.rule_http_errors(busy))
|
||||
|
||||
|
||||
class TestDayCounters(unittest.TestCase):
|
||||
def test_egress_tuples_union_across_ticks(self):
|
||||
from netsec_scan.__main__ import record_counters
|
||||
from netsec_scan import state as state_mod
|
||||
|
||||
st = state_mod.empty()
|
||||
record_counters(st, 5, [], ["a->1.2.3.4:443/tcp"], 0)
|
||||
record_counters(st, 7, [], ["a->1.2.3.4:443/tcp", "b->5.6.7.8:80/tcp"], 1)
|
||||
self.assertEqual(st["day"]["flows"], 12)
|
||||
self.assertEqual(st["day"]["egress"], ["a->1.2.3.4:443/tcp", "b->5.6.7.8:80/tcp"])
|
||||
self.assertEqual(st["totals"]["ticks"], 2)
|
||||
self.assertEqual(st["totals"]["malformed"], 1)
|
||||
|
||||
|
||||
class TestOutputShape(unittest.TestCase):
|
||||
def test_render_is_bounded_and_starts_with_severity_and_fingerprint(self):
|
||||
from netsec_scan.__main__ import render
|
||||
|
|
|
|||
|
|
@ -45,13 +45,13 @@ class TestStateFile(unittest.TestCase):
|
|||
|
||||
def test_missing_key_is_added_not_replaced(self):
|
||||
st = state_mod.load(self.path)
|
||||
st["egress"]["a->1.2.3.4:443/tcp"] = "2026-11-27T00:00:00+00:00"
|
||||
st["seen"]["aaaa"] = {"rule": "x", "first": "2026-11-27T00:00:00+00:00"}
|
||||
state_mod.save(self.path, st)
|
||||
again = state_mod.load(self.path)
|
||||
again["egress"]["b->5.6.7.8:80/tcp"] = "2026-11-27T00:01:00+00:00"
|
||||
again["seen"]["bbbb"] = {"rule": "x", "first": "2026-11-27T00:01:00+00:00"}
|
||||
state_mod.save(self.path, again)
|
||||
final = json.loads(self.path.read_text())
|
||||
self.assertEqual(len(final["egress"]), 2)
|
||||
self.assertEqual(sorted(final["seen"]), ["aaaa", "bbbb"])
|
||||
|
||||
def test_prune_bounds_the_seen_set(self):
|
||||
st = state_mod.load(self.path)
|
||||
|
|
|
|||
Loading…
Reference in a new issue