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

@ -51,8 +51,8 @@ python3 -m unittest discover -s tests # 40 tests, no network
Example:
```
HIGH a3f19c02b7de egress-new-dst osint/osint-app->203.0.113.9:4444/tcp first-seen 03:12Z (no prior in retained window)
MEDIUM 0fbb2e28bb07 egress-new-dst flux-system/source-controller->104.18.30.120:443/tcp first-seen 05:10Z (no prior in retained window)
HIGH a3f19c02b7de egress-new-dst osint/osint-app->203.0.113.9:4444/tcp first-seen 03:12Z (no prior in ~20m of observed flows)
MEDIUM 0fbb2e28bb07 egress-new-dst flux-system/source-controller->104.18.30.120:443/tcp first-seen 05:10Z (no prior in ~2.1d of observed flows)
```
**Fingerprint** = `sha256(rule + entity)[:12]`. Never a timestamp, never a
@ -86,9 +86,14 @@ and two flow-store rules (`/var/log/netsec/*.jsonl` on siriussec).
| 4 | `rst-storm` | MEDIUM | refused/reset connection storm | RST > **2/s** and > **10×** 6 h average | 1 RST series, 2.5/s at idle |
| 5 | `new-port` | LOW | a server-range port (1-32767) that carried flows now but none 24 h ago | `count by(port) … unless … offset 24h` | 131 port series; 4 named ports in 24 h at deploy |
| 6 | `http-error-ratio` | MEDIUM | L7 4xx/5xx share shift (public gateway) | > **0.5** of ≥ **0.5 req/s** (15 m) | 20 status series; 2.2 % at idle |
| 7 | `egress-new-dst` | HIGH (non-web port) / MEDIUM (80/443) | first-seen `(workload, dst_ip, dst_port)` outside pod/mesh/LAN space — candidate beacon or exfil | no prior in the retained window (cumulative seen-set) | 21 workload→world tuples in the first 15 min captured |
| 7 | `egress-new-dst` | HIGH (non-web port, or any `DROPPED` verdict) / MEDIUM (80/443 forwarded) | first-seen `(workload, dst_ip, dst_port)` outside pod/mesh/LAN space — candidate beacon or exfil | no prior in the cumulative seen-set; the line states the observation window (e.g. `~20m of observed flows`) | 21 workload→world tuples in the first 15 min captured |
| 8 | `beacon-shape` | HIGH | periodic connections from one workload to one `dst:port` — C2 shape | ≥ **20** distinct client sockets in 2 h, inter-arrival **CV < 0.15**, mean interval ≤ 900 s | requires ≥ 20 conns; the busiest real tuple measured 9 sockets / CV 1.57 |
Rule 7 keeps a `DROPPED` attempt **separate** from forwarded traffic to the same
destination (`…/dropped` in the entity, HIGH): a pod trying to reach a new host
and being refused is a stronger signal than one that succeeded, and collapsing
them would hide it behind the forwarded tuple's fingerprint.
**Deliberately not implemented** (and why):
* **Per-workload PromQL rules.** The `pod` label on every `hubble_*` metric is
@ -152,6 +157,17 @@ UTC day rollover. No alert is ever raised from it.
**per-file byte offset** kept in state — a tick pulls only new bytes. It never
polls `hubble observe`: the agent ring buffer is ~28 s at 148 flows/s, so a
poll is lossy by design.
* **Measured volume**: 41 MB in ~20 min at collector start (the first file
carries a 37.5 MB startup burst), lower in steady state, against a 1 GiB cap —
so the file window is **days, not the month the design note assumed**. A
"not seen before" judgement therefore comes from this sensor's own cumulative
seen-set (which outlives the files), and the alert says how long that window
actually is rather than implying a longer baseline.
* Only `*.jsonl` counts toward the volume; rotation tombstones in the directory
are ignored. A partial final line in a file that is still being appended is
**normal**: the parser keeps the incomplete tail and re-reads it next tick
from the last complete newline (a `JSONDecodeError` on that tail is expected,
not corruption).
* **Prometheus**: the monitoring Service through the API proxy, read-only, no
port-forward, no new credentials.
@ -175,6 +191,7 @@ UTC day rollover. No alert is ever raised from it.
|---|---|
| repo | `sirius/netsec-agent` on Forgejo, cloned at `~/Projects/netsec-agent` |
| shim | `~/.hermes/scripts/netsec-scan.sh` → `exec /home/lancelot/Projects/netsec-agent/run.sh "$@"` (`chmod +x`: a non-executable shim fails with `Permission denied` **on empty stdout**, which looks exactly like a healthy silent tick) |
| scheduler shim | `~/.hermes/profiles/devops/scripts/netsec-scan.sh` → execs the canonical shim. The cron scheduler only accepts a `script` path that resolves inside `<HERMES_HOME>/scripts/`, so the profile copy exists purely for that guard; the real entrypoint stays in one place. |
| cron | Hermes job `netsec-scan`, `no_agent` script job, every 5 minutes, `workdir ~/Projects/netsec-agent`, `script netsec-scan.sh`, `deliver telegram` |
| state | `~/.hermes/cron/netsec-scan-state.json` (+ `.memo.json` for degradation wording) |
| reports | `~/Projects/netsec-agent/reports/YYYY-MM-DD.json` |
@ -184,10 +201,14 @@ credentials, no LLM call, no secret reads.
## Verified behaviour (2026-11-27)
* 40 unit tests pass against recorded fixtures.
* First live tick seeded 21 egress tuples and 21 beacon buckets silently, wrote
the state file and `reports/2026-11-27.json`.
* Single-fire: deleting one key from `seen` re-emits exactly one line, and the
immediately following run is silent.
* 43 unit tests pass against recorded fixtures.
* Baseline tick seeded the retained window silently (25 fingerprints, 21 beacon
buckets), wrote the state file and `reports/2026-11-27.json`; the immediately
following tick was empty with exit 0 **and a changed state mtime**.
* Single-fire: deleting one fingerprint from `seen` re-emitted exactly one line
naming that tuple, and the next run was silent; the state was then restored.
* Cron delivery proven through `~/.hermes/cron/executions.db`
(`delivery_outcome=delivered`) — see the task handoff for the transcript.
* Live tripwire values at commissioning: hubble-loss HIGH on siriussec (0.42-0.60
events/s) and linode (0.13-3.2 events/s); `new-port` LOW for 3100/9093; the
rest quiet.