Spaces:
Running
Running
Merged from sub
Browse files- README.md +71 -56
- requirements.txt +1 -1
- server/app.py +40 -0
README.md
CHANGED
|
@@ -10,19 +10,21 @@ pinned: false
|
|
| 10 |
|
| 11 |
# 🦅 PatchHawk: Autonomous Supply-Chain Guard
|
| 12 |
|
| 13 |
-
[
|
| 111 |
|
| 112 |
```bash
|
| 113 |
-
# Start the environment server
|
| 114 |
python -m server.app --port 8000
|
| 115 |
|
| 116 |
-
# Execute the inference loop
|
| 117 |
python src/envs/patchhawk/inference.py --env-url http://localhost:8000
|
| 118 |
```
|
| 119 |
|
| 120 |
---
|
| 121 |
|
| 122 |
-
## 💎 Reward Rubric
|
| 123 |
|
| 124 |
-
|
| 125 |
|
| 126 |
| Action ID | Action Name | Base Reward | Success Criteria |
|
| 127 |
| :--- | :--- | :--- | :--- |
|
| 128 |
-
| **0** | `ANALYZE` | `0.0` | Observation step; used for data gathering. |
|
| 129 |
-
| **1** | `DETONATE` | `+0.1` | Successfully extract telemetry from Docker. |
|
| 130 |
-
| **2** | `BLOCK_PR` | `+2.0 / -1.0` |
|
| 131 |
-
| **3** | `SUBMIT_PATCH` | `+3.0 / -1.5` |
|
| 132 |
-
| **4** | `ESCALATE` | `0.0` |
|
| 133 |
|
| 134 |
-
### Dynamic Scaling
|
| 135 |
-
- **Risk Accuracy**:
|
| 136 |
-
- **Safety Multiplier**:
|
| 137 |
|
| 138 |
---
|
| 139 |
|
| 140 |
## 📈 Dashboard & UI
|
| 141 |
|
| 142 |
-
Launch the **Security Operations Center (SOC)** to
|
| 143 |
|
| 144 |
```bash
|
| 145 |
streamlit run patchhawk/app/dashboard.py
|
| 146 |
```
|
| 147 |
|
| 148 |
-
|
| 149 |
-
-
|
| 150 |
-
-
|
|
|
|
| 151 |
|
| 152 |
---
|
| 153 |
|
| 154 |
-
## 🗺️ Roadmap
|
| 155 |
|
| 156 |
-
- [ ] **Multi
|
| 157 |
-
- [ ] **CVE Ingestion**:
|
| 158 |
-
- [ ] **Cross-Language Support**:
|
| 159 |
-
- [ ] **Kubernetes Native**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
---
|
| 162 |
|
| 163 |
## 📝 License
|
| 164 |
|
| 165 |
-
Distributed under the **MIT License**. See
|
| 166 |
|
| 167 |
-
Developed with ❤️ by **Ramprasath K & The PatchHawk Team**
|
| 168 |
-
Ramprasath K & The PatchHawk Team
|
|
|
|
| 10 |
|
| 11 |
# 🦅 PatchHawk: Autonomous Supply-Chain Guard
|
| 12 |
|
| 13 |
+
[](https://wandb.ai)
|
| 14 |
+
[](https://huggingface.co)
|
| 15 |
+
[](https://python.org)
|
| 16 |
+
[](https://openenv.dev)
|
| 17 |
+
[](https://opensource.org/licenses/MIT)
|
| 18 |
|
| 19 |
+
**Built for the OpenEnv Hackathon 2026 by Meta**
|
| 20 |
+
|
| 21 |
+
PatchHawk is an autonomous DevSecOps agent powered by Group Relative Policy Optimization (GRPO). It moves beyond static vulnerability detection by validating findings inside isolated Docker sandboxes and generating verified, syntactically correct patches. The system closes the loop between detection, validation, and remediation through a cyber‑physical reinforcement learning feedback cycle.
|
| 22 |
|
| 23 |
---
|
| 24 |
|
| 25 |
+
## 📽️ The Vision: Cyber‑Physical RL Loop
|
| 26 |
|
| 27 |
+
Traditional security scanners suffer from high false‑positive rates and often report vulnerabilities that cannot be exploited or fixed in practice. PatchHawk addresses this by implementing a reinforcement learning loop where the model's reward is tied directly to the success of its patches inside a real execution environment.
|
| 28 |
|
| 29 |
```mermaid
|
| 30 |
graph TD
|
|
|
|
| 36 |
B -->|Patch| G[Verification Pipeline]
|
| 37 |
G -->|Syntax Check| H{Success?}
|
| 38 |
G -->|Unit Tests| I{Pass?}
|
| 39 |
+
G -->|Re‑Attack| J{Defeated?}
|
| 40 |
H & I & J -->|All Pass| K[Positive Reward +3.0]
|
| 41 |
H | I | J -->|Failure| L[Negative Penalty -1.5]
|
| 42 |
+
K --> M[Model Update / Optimization]
|
| 43 |
+
L --> M
|
| 44 |
```
|
| 45 |
|
| 46 |
+
The agent learns to produce patches that not only compile but also withstand re‑execution of the original exploit vector.
|
| 47 |
+
|
| 48 |
---
|
| 49 |
|
| 50 |
## ✨ Key Features
|
| 51 |
|
| 52 |
+
- 🛡️ **Autonomous Detection**: Sophisticated supply‑chain analysis identifying typosquatting, backdoors, data exfiltration, and malicious logic in dependencies.
|
| 53 |
+
- 🐳 **Hardened Sandboxing**: High‑fidelity Docker isolation with network‑disabled execution, strict resource caps, and ephemeral file systems to safely detonate suspicious code.
|
| 54 |
+
- 🧠 **GRPO‑Driven Learning**: Group Relative Policy Optimization (inspired by DeepSeek‑R1) enables trial‑and‑error mastery and structured reasoning without a separate critic model.
|
| 55 |
+
- 🧩 **XML Reasoning Traces**: All agent decisions are accompanied by a machine‑readable `<thought>...</thought>` block, providing full auditability of the decision‑making process.
|
| 56 |
+
- 📊 **SOC Dashboard**: Real‑time Streamlit interface for monitoring agent behavior, sandbox telemetry, and reward breakdowns.
|
| 57 |
+
- ✅ **OpenEnv Compliance**: Fully integrated with the PyTorch OpenEnv framework, ensuring reproducible and shareable reinforcement learning environments.
|
| 58 |
|
| 59 |
---
|
| 60 |
|
| 61 |
+
## 🛠️ Project Structure
|
|
|
|
|
|
|
| 62 |
|
| 63 |
```text
|
| 64 |
PatchHawk/
|
| 65 |
+
├── src/envs/patchhawk/ # 📦 OpenEnv Submission Package
|
| 66 |
│ ├── server/ # FastAPI environment server
|
| 67 |
+
│ ├── models.py # Type‑safe contract definitions
|
| 68 |
│ ├── client.py # Environment interaction client
|
| 69 |
│ └── inference.py # Main agent execution loop
|
| 70 |
+
├── patchhawk/ # 🧠 Core Logic & Training
|
| 71 |
│ ├── data/ # Scenario generation & datasets
|
| 72 |
+
│ ├── training/ # GRPO / Unsloth training scripts
|
| 73 |
│ └── app/ # Streamlit SOC Dashboard
|
| 74 |
├── docker/ # 🐳 Container configurations
|
| 75 |
├── config.yaml # Environment & Agent configuration
|
| 76 |
+
├── openenv.yaml # OpenEnv metadata
|
| 77 |
+
├── .env.example # Environment variable template
|
| 78 |
+
└── README.md
|
| 79 |
```
|
| 80 |
|
| 81 |
---
|
|
|
|
| 84 |
|
| 85 |
### Prerequisites
|
| 86 |
|
| 87 |
+
- Python 3.12 or higher
|
| 88 |
+
- Docker Engine (running locally, with buildx available)
|
| 89 |
+
- NVIDIA GPU (8 GB VRAM or more recommended for training and inference)
|
| 90 |
+
- Hugging Face account and token (for model access)
|
| 91 |
|
| 92 |
### 1. Installation
|
| 93 |
|
|
|
|
| 96 |
git clone https://github.com/ramprasathk07/PatchHawk.git
|
| 97 |
cd PatchHawk
|
| 98 |
|
| 99 |
+
# Create and activate a virtual environment
|
| 100 |
python -m venv .venv
|
| 101 |
+
source .venv/bin/activate # On Windows: .venv\Scripts\activate
|
| 102 |
+
|
| 103 |
+
# Install core dependencies
|
| 104 |
pip install -e .
|
| 105 |
```
|
| 106 |
|
| 107 |
### 2. Environment Setup
|
| 108 |
|
| 109 |
```bash
|
| 110 |
+
# Copy the environment template and populate your keys
|
| 111 |
cp .env.example .env
|
| 112 |
+
# Edit .env to include HF_TOKEN, OPENAI_API_KEY, ANTHROPIC_API_KEY, etc.
|
| 113 |
|
| 114 |
+
# Build the validation sandbox Docker image
|
| 115 |
docker build -t patchhawk-sandbox:latest -f docker/Dockerfile.sandbox .
|
| 116 |
```
|
| 117 |
|
| 118 |
### 3. Running the Agent (Dry Run)
|
| 119 |
|
| 120 |
```bash
|
| 121 |
+
# Start the environment server (in one terminal)
|
| 122 |
python -m server.app --port 8000
|
| 123 |
|
| 124 |
+
# Execute the inference loop (in another terminal)
|
| 125 |
python src/envs/patchhawk/inference.py --env-url http://localhost:8000
|
| 126 |
```
|
| 127 |
|
| 128 |
---
|
| 129 |
|
| 130 |
+
## 💎 Reward Rubric
|
| 131 |
|
| 132 |
+
The agent is guided by a granular reward structure that encourages safe, effective, and verifiable actions.
|
| 133 |
|
| 134 |
| Action ID | Action Name | Base Reward | Success Criteria |
|
| 135 |
| :--- | :--- | :--- | :--- |
|
| 136 |
+
| **0** | `ANALYZE` | `0.0` | Observation step; used solely for data gathering. |
|
| 137 |
+
| **1** | `DETONATE` | `+0.1` | Successfully extract telemetry from the Docker sandbox. |
|
| 138 |
+
| **2** | `BLOCK_PR` | `+2.0 / -1.0` | Positive reward when correctly blocking a malicious PR; negative penalty for false positives. |
|
| 139 |
+
| **3** | `SUBMIT_PATCH` | `+3.0 / -1.5` | The primary goal. Reward requires passing syntax check, unit tests, and a re‑attack validation. |
|
| 140 |
+
| **4** | `ESCALATE` | `0.0` | Hands off to a human expert when uncertainty exceeds a configurable threshold. |
|
| 141 |
|
| 142 |
+
### Dynamic Scaling Factors
|
| 143 |
+
- **Risk Accuracy Bonus**: Up to `+2.0` additional reward for accurately predicting the risk score of a vulnerability.
|
| 144 |
+
- **Safety Multiplier**: Repeated syntax check failures apply a decay factor to all future rewards.
|
| 145 |
|
| 146 |
---
|
| 147 |
|
| 148 |
## 📈 Dashboard & UI
|
| 149 |
|
| 150 |
+
Launch the **Security Operations Center (SOC)** dashboard to observe the agent's reasoning in real time.
|
| 151 |
|
| 152 |
```bash
|
| 153 |
streamlit run patchhawk/app/dashboard.py
|
| 154 |
```
|
| 155 |
|
| 156 |
+
The dashboard provides:
|
| 157 |
+
- Live XML reasoning logs from the agent.
|
| 158 |
+
- Real‑time stdout/stderr streams from the Docker sandbox.
|
| 159 |
+
- Detailed audit trail of reward assignments and verification outcomes.
|
| 160 |
|
| 161 |
---
|
| 162 |
|
| 163 |
+
## 🗺️ Roadmap & Future Work
|
| 164 |
|
| 165 |
+
- [ ] **Multi‑Agent Coordination**: Deploy attacker and defender models for automated red‑teaming exercises.
|
| 166 |
+
- [ ] **CVE Ingestion**: Automatically generate training scenarios from the National Vulnerability Database (NVD).
|
| 167 |
+
- [ ] **Cross-Language Support**: Expand beyond Python to Go, JavaScript, Rust, and Java.
|
| 168 |
+
- [ ] **Kubernetes Native**: Orchestrate sandboxes at scale using Kubernetes instead of local Docker.
|
| 169 |
+
- [ ] **Fine‑Tuned Vulnerability Model**: Train a specialized 7B parameter LLM (e.g., VulnLLM‑R) on vulnerability‑fixing commits.
|
| 170 |
+
- [ ] **Context‑Aware Analysis**: Integrate Code Property Graph (CPG) slicing for LLM‑based semantic vulnerability detection.
|
| 171 |
+
- [ ] **Silent Patch Detection**: Identify security‑relevant commits that were not publicly disclosed.
|
| 172 |
+
- [ ] **AI‑Generated Code Audit**: Trace vulnerabilities back to AI coding assistants (e.g., GitHub Copilot, ChatGPT).
|
| 173 |
+
- [ ] **Automated PR Remediation**: Generate and submit fix‑containing pull requests for detected vulnerabilities.
|
| 174 |
+
- [ ] **Adversarial Training Loop**: Implement a self‑improving LLM‑vs‑LLM red‑team / blue‑team training regimen.
|
| 175 |
+
- [ ] **Supply‑Chain Malware Detection**: Extend dependency analysis to identify novel, unpublished attack patterns.
|
| 176 |
|
| 177 |
---
|
| 178 |
|
| 179 |
## 📝 License
|
| 180 |
|
| 181 |
+
Distributed under the **MIT License**. See the LICENSE file in the repository root for full details.
|
| 182 |
|
| 183 |
+
Developed with ❤️ by **Ramprasath K & The PatchHawk Team** for the OpenEnv Hackathon 2026 hosted by Meta.
|
|
|
requirements.txt
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
# Core
|
| 2 |
-
openenv-core>=0.2.0
|
| 3 |
openai>=1.0.0
|
| 4 |
numpy>=1.24.0
|
| 5 |
PyYAML>=6.0
|
|
|
|
| 1 |
# Core
|
| 2 |
+
openenv-core[ui]>=0.2.0
|
| 3 |
openai>=1.0.0
|
| 4 |
numpy>=1.24.0
|
| 5 |
PyYAML>=6.0
|
server/app.py
CHANGED
|
@@ -30,6 +30,7 @@ from openenv.core import create_app
|
|
| 30 |
|
| 31 |
from patchhawk.agent.environment import PatchHawkEnv
|
| 32 |
from patchhawk.env_models import PatchHawkAction, PatchHawkObservation
|
|
|
|
| 33 |
|
| 34 |
|
| 35 |
def _env_factory() -> PatchHawkEnv:
|
|
@@ -50,6 +51,45 @@ def create_openenv_app():
|
|
| 50 |
|
| 51 |
app = create_openenv_app()
|
| 52 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def main(port: int | None = None) -> None:
|
| 55 |
"""Start the PatchHawk OpenEnv server."""
|
|
|
|
| 30 |
|
| 31 |
from patchhawk.agent.environment import PatchHawkEnv
|
| 32 |
from patchhawk.env_models import PatchHawkAction, PatchHawkObservation
|
| 33 |
+
from fastapi.responses import HTMLResponse
|
| 34 |
|
| 35 |
|
| 36 |
def _env_factory() -> PatchHawkEnv:
|
|
|
|
| 51 |
|
| 52 |
app = create_openenv_app()
|
| 53 |
|
| 54 |
+
@app.get("/", response_class=HTMLResponse)
|
| 55 |
+
def root_dashboard():
|
| 56 |
+
return """
|
| 57 |
+
<!DOCTYPE html>
|
| 58 |
+
<html>
|
| 59 |
+
<head>
|
| 60 |
+
<title>PatchHawk | Autonomous DevSecOps SOC</title>
|
| 61 |
+
<style>
|
| 62 |
+
body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: #0d1117; color: #c9d1d9; display: flex; flex-direction: column; align-items: center; justify-content: center; height: 100vh; margin: 0; }
|
| 63 |
+
.container { background: #161b22; padding: 40px; border-radius: 12px; border: 1px solid #30363d; box-shadow: 0 10px 30px rgba(0,0,0,0.5); text-align: center; max-width: 600px; }
|
| 64 |
+
h1 { color: #58a6ff; margin-bottom: 10px; }
|
| 65 |
+
p { font-size: 1.1em; color: #8b949e; line-height: 1.6; }
|
| 66 |
+
.status { display: inline-block; padding: 5px 15px; border-radius: 20px; background: #238636; color: white; font-weight: bold; margin: 20px 0; }
|
| 67 |
+
.links { display: flex; gap: 10px; justify-content: center; margin-top: 30px; }
|
| 68 |
+
.btn { text-decoration: none; padding: 12px 25px; border-radius: 6px; font-weight: bold; transition: 0.3s; }
|
| 69 |
+
.btn-blue { background: #1f6feb; color: white; }
|
| 70 |
+
.btn-blue:hover { background: #388bfd; }
|
| 71 |
+
.btn-outline { border: 1px solid #30363d; color: #58a6ff; }
|
| 72 |
+
.btn-outline:hover { background: #30363d; }
|
| 73 |
+
.badge { background: #30363d; padding: 4px 10px; border-radius: 4px; font-family: monospace; }
|
| 74 |
+
</style>
|
| 75 |
+
</head>
|
| 76 |
+
<body>
|
| 77 |
+
<div class="container">
|
| 78 |
+
<h1>🦅 PatchHawk SOC</h1>
|
| 79 |
+
<p>Autonomous Supply-Chain Vulnerability & Patching Agent</p>
|
| 80 |
+
<div class="status">● ENVIRONMENT LIVE</div>
|
| 81 |
+
<p>The OpenEnv API Spec is running correctly at <span class="badge">port: 7860</span>.</p>
|
| 82 |
+
|
| 83 |
+
<div class="links">
|
| 84 |
+
<a href="/web" class="btn btn-blue">Open Env Explorer</a>
|
| 85 |
+
<a href="/docs" class="btn btn-outline">API Docs (Swagger)</a>
|
| 86 |
+
</div>
|
| 87 |
+
<p style="margin-top:20px; font-size:0.9em;">Evaluation URL: <span class="badge">/reset</span></p>
|
| 88 |
+
</div>
|
| 89 |
+
</body>
|
| 90 |
+
</html>
|
| 91 |
+
"""
|
| 92 |
+
|
| 93 |
|
| 94 |
def main(port: int | None = None) -> None:
|
| 95 |
"""Start the PatchHawk OpenEnv server."""
|