Building P7: An Autonomous CVE Patcher in a Weekend
A vulnerability scanner that finds CVEs is useful. A scanner that patches them for you, verifies the patch worked, and rolls back automatically when it breaks something is a different thing entirely. That is what P7 does.
I built it in a weekend. Here is how, and what I learned along the way.
The problem
My homelab already runs the full detection stack: Greenbone scans the network weekly, Trivy scans every container image, Semgrep/Bandit/Gitleaks scan the code, Wazuh ingests alerts from eight agents, and all of it funnels into DefectDojo on the homelab network. n8n polls DefectDojo and sends Telegram alerts when something is High or Critical.
Detection is solved. Remediation is still 100% manual. I read the alert on my phone, SSH in, run apt install, hope nothing breaks, maybe systemctl status the relevant service, and move on.
That is slow, error-prone, and does not scale. So I asked: what is the smallest end-to-end loop that closes the gap?
The loop
DefectDojo ββHigh+ findingsβββΊ semver classifier
β
βΌ
pct snapshot <ct> pre-patch
β
βΌ
apt/apk install pkg=fix
β
βΌ
systemctl is-active <svcs>
β
βΌ
trivy rescan β CVE still present?
β
βββββββββββββ΄βββββββββββ
βΌ βΌ
all green any failure
β β
mark mitigated pct rollback + reboot
in DefectDojo + Telegram alert
Every stage can abort the operation. The rollback point is a ZFS snapshot taken by pct snapshot <ctid> <snap-name> before any mutation. Rollback is pct rollback <ctid> <snap-name> and runs in about ten seconds on my hardware.
Why ZFS snapshots are the whole trick
The scariest part of “automatic patching” is not the patch. It is the state after a failed patch. If apt install leaves half a dependency chain in a weird state, hand-recovery can take hours.
With a ZFS snapshot, the system either works and I commit, or it does not and the entire container reverts to the exact bytes it had thirty seconds ago. No reasoning about dirty state. No partial recovery. Just a time machine.
This is what makes “move fast” safe. Without it, I would not run any of this unattended.
The classifier is boring on purpose
def classify(current: str, target: str) -> ChangeLevel:
cur = _parse(current)
tgt = _parse(target)
if cur is None or tgt is None:
return ChangeLevel.UNKNOWN
if cur[0] != tgt[0]:
return ChangeLevel.MAJOR
if cur[1] != tgt[1]:
return ChangeLevel.MINOR
if cur[2] != tgt[2]:
return ChangeLevel.PATCH
return ChangeLevel.UNKNOWN
Patch-level diffs (1.2.3 β 1.2.4) auto-apply. Minor and major are logged and skipped by default. The policy is configurable with --max-change, but my prior is: do not touch what you cannot semver-reason about.
A quick regex also collapses Debian-style versions like 1.2.3-1ubuntu0.4 to the semver triple, so apt-world versions still classify sanely.
Two backends, one interface
Running live against my homelab, DefectDojo reports 940 High+ findings on Container Images (Trivy) and 1,710 on Container Image Dependencies. The Trivy findings are Alpine apk packages (-r0 suffix). I added a second Patcher backend behind a Protocol:
PYTHONPATH=src python -m p7 run --target 205 --backend apk \
--product-id 2 --verify-cves --writeback
Same loop, different package manager. Trivy and apt code paths share orchestrator, snapshot, healthcheck, verify, write-back, and alerting.
Verify is non-negotiable
The step that elevates this from “I ran apt-get install for you” to “I remediated the CVE” is the Trivy rescan.
After patching, P7 runs trivy rootfs inside the CT and parses the JSON output. If the CVE being targeted is still reported, that is treated as a failed patch. Snapshot restores. Reboot. Telegram alert fires. The DefectDojo finding stays open.
40 tests, zero live infra required
Every subprocess call to pct, apt, apk, systemctl, and trivy goes through a Pct wrapper. In production it subprocess.runs. In tests, a FakePct captures the argv list and returns scripted PctResult tuples. That means the entire orchestrator β including snapshot, patch, healthcheck fail path, Trivy verify fail path, apk backend path, and DefectDojo write-back β is testable with zero live infra. Forty tests. Half-second run.
What I skipped on purpose
- Bi-directional Telegram approval buttons. Needs an n8n webhook workflow + bot callback plumbing. Day-2 work.
- Docker image rebuilds. Hard, slow, and the Trivy findings are inside running containers where
apk upgradeworks fine. - pip and language-specific managers. Not where my High+ findings live.
What is next
- Multi-host sweep (
--target all) with per-host concurrency bounds - Bi-directional Telegram HITL for minor and major updates
- Record patch-latency metrics and plot “mean time to mitigate” over time
The repo
github.com/PyHackSecGP/p7-autopatch β Apache 2.0.
The entire thing is 500 lines of Python plus tests. The real engineering is not the code. It is accepting that a rollback button is cheaper than a correctness proof.