Skip to content

feat: agent hardening: DPAPI and TPM keys, signed self-update in rings, quiet agent and sensor alerts, code signing prep - #68

Merged
jusso-dev merged 8 commits into
mainfrom
feat/agent-hardening
Nov 9, 2026
Merged

jusso-dev merged 8 commits into
mainfrom
feat/agent-hardening

Conversation

@jusso-dev

Copy link
Copy Markdown
Owner

Closes #58

Stacked on #67 (base feat/windows-response). Merge #67 and the stack below it first.

This PR adds quiet agent and quiet sensor alerts, tamper checks, DPAPI keys on Windows with an optional TPM wrap, and minisign-verified self-update in rings. It also prepares code signing. Nothing was bought or registered, and no real release key exists. The compiled-in release key is a placeholder, so agents refuse every update until Justin provides one.

Decisions for Justin are in the issue comment: #58 (comment)

Acceptance criteria

Criterion Status
Windows JWT and device seed in DPAPI (TPM-wrapped when present), no plaintext after migration Met in code; the Windows CI step proves the DPAPI path. keystore.zig now has a windows_dpapi backend (platform/windows/dpapi.zig): CryptProtectData with CRYPTPROTECT_LOCAL_MACHINE and fixed entropy, one <state dir>\<secret>.dpapi file per secret, and a protected DACL for SYSTEM and Administrators applied before the rename. When the Microsoft Platform Crypto Provider opens, the blob is wrapped with RSA-OAEP SHA-256 under a non-exportable machine key, and the wrap is checked by unwrapping before it is trusted. Otherwise it falls back to DPAPI alone. The existing migration path (move, read back, scrub, delete) is reused. --keystore-selftest covers the round trip, the plaintext seed being migrated and removed, and no plaintext in the blob. The TPM wrap has not run live, because hosted runners have no usable TPM, so it is compiled and falls back cleanly. Linux stays on a 0600 file; tpm2-tss is not done (documented).
Agent updates from a signed manifest, refuses bad signatures, bad hashes and downgrades, rolls back on a failed health check Met with a throwaway key; off in shipped builds. minisign.zig verifies ED (BLAKE2b-512 prehashed) and legacy Ed signatures plus the trusted comment's global signature. I also checked it once against a signature made by an independent implementation (Python cryptography plus hashlib). updater.zig does decide, verify size and SHA-256, stage, swap with .previous (renames the running exe), exit non-zero so the service manager restarts, open a 5 minute health window, then commit or roll back. It refuses a downgrade except through a signed rollback, and never retries a version it rolled back from. Tests and --update-selftest cover a good update, bad signature, bad hash, refused downgrade and failed-health rollback through real renames, in CI on all three OSes. A live restart through SCM, systemd or launchd after a swap has not run.
Ring assignment stable per agent, promotion gated Met. Bucket = sha256(lower-case agent id) first 4 bytes BE mod 100, with the same test vector on agent and server. update_ring override; rings canary, internal, 10, 50, 100. POST /api/agent/update-check picks the ring on the server, and /api/releases/latest takes ring=. New releases start in canary. Promotion needs a 24 h soak with 5% or less rolled back or failed. DB test covers ring gating and override.
Offline without a shutdown notice raises an alert; a quiet sensor raises an alert Met. The clean shutdown marker (<state>.clean) and a final shutdown heartbeat are sent on SCM stop or shutdown and on SIGTERM/SIGINT. A start without the marker sends agent_tamper unexpected_stop. jobs/agent_alerts.zig raises Agent silent with the hostname and last heartbeat. The next heartbeat closes it, and revoked agents are skipped. Sensor silent fires when a running sensor stops, fails, or (Sysmon, Security) goes 15 minutes without events. Delivering again closes it. Tamper checks cover the binary hash, config and identity changes, Sysmon stopping, and Defender 5007 exclusions over Tawny paths. install.ps1 runs sc.exe sdset, and the Windows CI step applies the same SDDL to a probe service and reads it back. DB tests cover all three alert kinds.
Release signing steps skip cleanly without secrets; docs/code-signing.md lists options; nothing bought Met; release workflow not run (it runs on tags only). sign-windows (signtool), notarize-macos (codesign plus notarytool) and release-manifests (minisign, protected release-signing environment) each gate on a job-level env flag. The doc gives no prices. It names the vendor pages to check.
CI green on the PR See checks below.

No mTLS. The decision is recorded in docs/threat-model.md.

Not done

  • tpm2-tss sealing on Linux (documented as not done).
  • ETW session checks: Sysmon service state stands in. The agent polls file hashes every 60 s instead of using directory change notifications.
  • agent_health CPU, RSS, disk, per-priority queues, and a healthy/degraded/silent/tampered verdict.
  • No UI for update rings or sensor status yet.
  • The PFX-based signtool step suits an exportable key. A token or cloud signer needs that vendor's tool once one is chosen (noted in the doc).

What ran where

Mac (macOS arm64, Zig 0.17.0):

agent: zig build -Doptimize=ReleaseSafe && zig build test --summary all -> Build Summary: 4/4 steps succeeded; 177/184 tests passed (7 skipped)
agent: zig build -Doptimize=ReleaseSafe -Dtarget={x86_64-windows,x86_64-linux,aarch64-linux,x86_64-macos} -> all build
agent: zig build test -Dtarget=x86_64-windows -> compiles (only "unable to execute" from the host)
agent: tawny-agent --update-selftest <tmp> -> good=swapped bad_signature=refused bad_hash=refused downgrade=refused rollback=restored result=ok
server: zig test src/main.zig -lc -> All 158 tests passed. (0 SafeAllocator lines; DB tests ran)
server: zig test src/main.zig -lc --test-filter fuzz -> All 2 tests passed.
server: zig build -Doptimize=ReleaseSafe; /tmp/run-contract.sh $PWD -> Ran 42 tests ... OK (skipped=9)
ui: node --check app.js && node ci/ui-fallback.mjs -> ui fallback ok assets=2 routes=12
yaml: /tmp/yamlcheck.sh .github/workflows/*.yml -> yaml ok
pwsh: ParseFile install.ps1 and the new CI step -> 0 errors

/tmp/run-e2e.sh on the Mac got to heartbeat_ok, event_ok=system_info and schema_v2_ok. It then hit timeout waiting for isolate result because ci/agent_e2e.py expects the Linux "not supported on linux" message. The base commit 5bd105a gives the same result on this Mac, and the Linux agent-e2e CI job is the one that counts.

Linux container (ubuntu:26.04, aarch64):

agent: zig build test --summary all -> 168 pass, 10 skip (178 total)

CI only: the Windows DPAPI keystore self-test and service DACL step, and the update self-test on Linux, macOS and Windows.

🤖 Generated with Claude Code

jusso-dev and others added 8 commits November 10, 2026 04:55
Migration 0007 adds the clean shutdown notice, update ring and state
columns, release ring columns and a per-sensor status table.

- jobs/agent_alerts.zig: an agent offline without a clean shutdown
  notice raises Agent silent (never for revoked agents); the next
  heartbeat closes it. A sensor that was running and stops, or a
  continuous sensor quiet for 15 minutes, raises Sensor silent; delivery
  closes it. Every agent_tamper event raises Agent tamper.
- Heartbeat accepts shutdown and shutdown_reason. Ingest feeds
  sensor_health and agent_health into sensor status and records
  key_protection and update state.
- agent_tamper schema; agent_health gains key_protection, sensors and
  update. New types appended to the backup and sink name lists.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- GET /api/releases/latest takes an optional ring query.
- POST /api/agent/update-check returns the signed manifest, signature
  and download URL when the latest release is open to the agent's ring
  (update_ring override, else sha256(agent_id) mod 100).
- The release sync stores published .manifest.json and .minisig assets
  and starts new releases in canary. promote() widens a ring after a
  24 hour soak while no more than 5% of reporting agents rolled back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- windows_dpapi backend: CryptProtectData machine scope with fixed
  entropy, one <state dir>\\<secret>.dpapi file per secret, protected
  DACL for SYSTEM and Administrators set before the rename.
- When the Microsoft Platform Crypto Provider opens, the DPAPI blob is
  wrapped with RSA-OAEP SHA-256 under a non-exportable machine key and
  checked by unwrapping before it is trusted; otherwise DPAPI alone.
  TAWNY_KEYSTORE_TPM=off turns the wrap off.
- Existing migration moves plaintext JWT and seed in, reads back, then
  deletes. Store.protection() reports keychain, dpapi, tpm or file.
- --keystore-selftest for CI. Link crypt32 and ncrypt. Constants pinned
  against windows-rs in a test that runs on every OS.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- minisign.zig verifies ED (BLAKE2b-512 prehashed) and legacy Ed
  signatures and the trusted comment's global signature. Cross-checked
  against a signature made by an independent implementation.
- update_key.zig holds an obvious placeholder; the updater refuses every
  update while it is there (state disabled_no_release_key).
- updater.zig: decide (platform, ring, min_from, downgrade refused
  unless a signed rollback, never retry a rolled-back version), verify
  size and SHA-256, stage, swap with .previous (rename the running exe),
  restart through the service manager, 5 minute health window, commit
  or roll back. Tests sign with a key made inside the test.
- --update-selftest runs the same scenarios through real file renames.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… DACL

- lifecycle: stop reasons from the SCM handler and signals; on a clean
  stop the agent flushes, sends a shutdown heartbeat and writes
  <state>.clean. A start without it sends agent_tamper unexpected_stop.
- tamper.zig: binary hash (against the signed manifest after an update),
  config and identity file changes outside the agent, Sysmon service
  stopped, Defender Operational 5007 exclusions covering Tawny paths.
- agent_health carries key_protection, sensors and update state.
- Run loop checks for signed updates every 6 hours (off while the
  release key is the placeholder) and exits non-zero after a swap so the
  service manager starts the new binary.
- install.ps1 sets a service DACL so only SYSTEM and Administrators can
  stop, reconfigure or delete TawnyAgent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…n Windows

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…without secrets

sign-windows, notarize-macos and release-manifests each gate on a
job-level env flag set from their secrets, so releases still publish
unsigned binaries when none are configured. The minisign key is read
only from the protected release-signing environment. docs/code-signing.md
lists the Windows and macOS options to choose from, without prices.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Closes #58

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 9, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 59 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 8f20a32b-72a6-4989-851c-c64a3da5b63c
Commits

Reviewing files that changed from the base of the PR and between 174b120 and ecd4929.

Files selected for processing: 40

  • .github/workflows/ci.yml
  • .github/workflows/release.yml
  • agent/build.zig
  • agent/install/install.ps1
  • agent/src/collectors/windows_events.zig
  • agent/src/config.zig
  • agent/src/keystore.zig
  • agent/src/keystore_debug.zig
  • agent/src/lifecycle.zig
  • agent/src/main.zig
  • agent/src/minisign.zig
  • agent/src/platform/windows/dpapi.zig
  • agent/src/platform/windows/scm.zig
  • agent/src/platform/windows/service.zig
  • agent/src/schema_check.zig
  • agent/src/tamper.zig
  • agent/src/transport/http.zig
  • agent/src/update_key.zig
  • agent/src/updater.zig
  • docs/api.md
  • docs/architecture/target-edr.md
  • docs/code-signing.md
  • docs/production-agent-hardening.md
  • docs/threat-model.md
  • schemas/events/agent_health.v1.json
  • schemas/events/agent_tamper.v1.json
  • schemas/events/golden/agent_health.json
  • schemas/events/golden/agent_tamper.json
  • server/src/db/migrate.zig
  • server/src/db/migrations/0007_agent_hardening.sql
  • server/src/http/dispatch.zig
  • server/src/ingest/schema_check.zig
  • server/src/ingest/v2.zig
  • server/src/jobs/agent_alerts.zig
  • server/src/jobs/backup.zig
  • server/src/jobs/deliver.zig
  • server/src/jobs/releases.zig
  • server/src/main.zig
  • server/src/routes/agent_releases.zig
  • server/src/routes/agents.zig
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@jusso-dev

Copy link
Copy Markdown
Owner Author

Status: all 11 CI checks pass on ecd4929.

Windows runner, live:

KEYSTORE  backend=windows_dpapi
KEYSTORE  round_trip=ok
KEYSTORE  migration=ok  plaintext_removed=true
KEYSTORE  blob_bytes=290  key_protection=dpapi
service DACL OK

So DPAPI round-trips, a plaintext seed is migrated and removed, and the service stop and delete DACL is in place. Hosted runners have no TPM, so key_protection=dpapi there. The TPM wrap path hasn't run on real hardware.

Self-update is built and tested with a throwaway in-test key, but it's off in practice: agent/src/update_key.zig holds a placeholder, and agents refuse every update until Justin's real minisign public key is committed. The release workflow signing steps skip without secrets, and they only run on tags.

Decisions for Justin are on #58: #58 (comment) (Windows signing option, Apple Developer enrolment, the release key and protected environment, and the promotion soak and threshold).

Stacked on #67. Merge order: #62 to #67, then this. #59 and #60 will stack on this one.

@jusso-dev
jusso-dev changed the base branch from feat/windows-response to main November 9, 2026 22:44
@jusso-dev
jusso-dev merged commit 171a4b0 into main Nov 9, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EDR 6: Agent hardening, signed self-update in rings, DPAPI and TPM keys, quiet agent alerts, code signing

1 participant