kanishcr7 commited on
Commit
00ed537
·
1 Parent(s): eefda8d

Merged from sub

Browse files
Files changed (3) hide show
  1. README.md +71 -56
  2. requirements.txt +1 -1
  3. server/app.py +40 -0
README.md CHANGED
@@ -10,19 +10,21 @@ pinned: false
10
 
11
  # 🦅 PatchHawk: Autonomous Supply-Chain Guard
12
 
13
- [![W&B](https://img.shields.io/badge/W%26B-patchhawk-blue?logo=weightsandbiases)](https://wandb.ai/ramprasathk07/patchhawk)
14
- [![HuggingFace](https://img.shields.io/badge/🤗_Model-patchhawk-yellow)](https://huggingface.co/ramprasathk07/patchhawk)
15
- [![Python 3.12](https://img.shields.io/badge/python-3.12-blue.svg)](https://python.org)
16
- [![OpenEnv](https://img.shields.io/badge/OpenEnv-Hackathon_Finalist-orange)](https://github.com/pytorch/openenv)
17
- [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://opensource.org/licenses/MIT)
18
 
19
- > **PatchHawk is an state-of-the-art autonomous DevSecOps agent powered by Group Relative Policy Optimization (GRPO). It goes beyond detection by validating vulnerabilities in isolated Docker sandboxes and generating verified, syntax-correct patches.**
 
 
20
 
21
  ---
22
 
23
- ## 📽️ The Vision: Cyber-Physical RL Loop
24
 
25
- Traditional security scanners often produce high signal-to-noise ratios and "hallucinated" vulnerabilities. PatchHawk bridges this gap by implementing a **Cyber-Physical Reinforcement Learning Loop**, where the model's reward is tied to the actual execution success of its patches in a real environment.
26
 
27
  ```mermaid
28
  graph TD
@@ -34,43 +36,46 @@ graph TD
34
  B -->|Patch| G[Verification Pipeline]
35
  G -->|Syntax Check| H{Success?}
36
  G -->|Unit Tests| I{Pass?}
37
- G -->|Re-Attack| J{Defeated?}
38
  H & I & J -->|All Pass| K[Positive Reward +3.0]
39
  H | I | J -->|Failure| L[Negative Penalty -1.5]
40
- K --> M[Model Update/Optimization]
 
41
  ```
42
 
 
 
43
  ---
44
 
45
  ## ✨ Key Features
46
 
47
- - 🛡️ **Autonomous Detection**: Sophisticated analysis of supply-chain vectors (typosquatting, backdoors, exfiltration).
48
- - 🐳 **Hardened Sandboxing**: High-fidelity Docker isolation with zero-network access and strict resource caps.
49
- - 🧠 **GRPO-Driven Learning**: Uses Group Relative Policy Optimization (DeepSeek-R1 style) for reasoning and trial-and-error mastery.
50
- - 🧩 **XML Reasoning**: Enforces a structured `<thought>...</thought>` chain for transparent decision-making.
51
- - 📊 **SOC Dashboard**: Real-time Streamlit interface for auditing agent behavior and reward telemetry.
52
- - ✅ **OpenEnv Compliant**: Fully integrated with the [PyTorch OpenEnv](https://github.com/pytorch/openenv) framework.
53
 
54
  ---
55
 
56
- ## 🛠 Project Structure
57
-
58
- The codebase is organized into modular components for training, inference, and environment simulation.
59
 
60
  ```text
61
  PatchHawk/
62
- ├── src/envs/patchhawk/ # 📦 Core OpenEnv Submission Package
63
  │ ├── server/ # FastAPI environment server
64
- │ ├── models.py # Type-safe contract definitions
65
  │ ├── client.py # Environment interaction client
66
  │ └── inference.py # Main agent execution loop
67
- ├── patchhawk/ # 🧠 Logic & Training
68
  │ ├── data/ # Scenario generation & datasets
69
- │ ├── training/ # GRPO/Unsloth training scripts
70
  │ └── app/ # Streamlit SOC Dashboard
71
  ├── docker/ # 🐳 Container configurations
72
  ├── config.yaml # Environment & Agent configuration
73
- ── openenv.yaml # OpenEnv metadata
 
 
74
  ```
75
 
76
  ---
@@ -79,9 +84,10 @@ PatchHawk/
79
 
80
  ### Prerequisites
81
 
82
- - **Python 3.12+**
83
- - **Docker Engine** (running locally)
84
- - **Nvidia GPU** (8GB+ VRAM recommended for local training/inference)
 
85
 
86
  ### 1. Installation
87
 
@@ -90,79 +96,88 @@ PatchHawk/
90
  git clone https://github.com/ramprasathk07/PatchHawk.git
91
  cd PatchHawk
92
 
93
- # Create virtual environment and install core dependencies
94
  python -m venv .venv
95
- source .venv/bin/activate # Windows: .venv\Scripts\activate
 
 
96
  pip install -e .
97
  ```
98
 
99
  ### 2. Environment Setup
100
 
101
  ```bash
102
- # Setup environment variables
103
  cp .env.example .env
104
- # Edit .env to include your HF_TOKEN and OpenAI/Anthropic keys
105
 
106
- # Build the validation sandbox
107
  docker build -t patchhawk-sandbox:latest -f docker/Dockerfile.sandbox .
108
  ```
109
 
110
  ### 3. Running the Agent (Dry Run)
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 (Action Space)
123
 
124
- PatchHawk implements a granular scoring system to guide the agent toward safe and effective decisions.
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` | Rewarded for malware; penalized for False Positives. |
131
- | **3** | `SUBMIT_PATCH` | `+3.0 / -1.5` | **The Goal.** Requires pass in Syntax -> Test -> Re-Attack. |
132
- | **4** | `ESCALATE` | `0.0` | Hand off to human expert if uncertainty is high. |
133
 
134
- ### Dynamic Scaling
135
- - **Risk Accuracy**: Agent receives up to `+2.0` bonus for predicting the exact risk score.
136
- - **Safety Multiplier**: Frequent failed syntax checks trigger a decay factor on all rewards.
137
 
138
  ---
139
 
140
  ## 📈 Dashboard & UI
141
 
142
- Launch the **Security Operations Center (SOC)** to watch the agent reason in real-time.
143
 
144
  ```bash
145
  streamlit run patchhawk/app/dashboard.py
146
  ```
147
 
148
- - **Terminal Trace**: Live XML reasoning logs.
149
- - **Docker Monitor**: Real-time stdout/stderr from the sandbox.
150
- - **Reward Audit**: Detailed breakdown of why specific points were awarded.
 
151
 
152
  ---
153
 
154
- ## 🗺️ Roadmap
155
 
156
- - [ ] **Multi-Agent Coordination**: Deploying "Attacker" vs "Defender" models for automated red-teaming.
157
- - [ ] **CVE Ingestion**: Automated generation of training scenarios from current NVD databases.
158
- - [ ] **Cross-Language Support**: Expanding beyond Python to Go, Javascript, and Rust.
159
- - [ ] **Kubernetes Native**: Orchestrating sandboxes at scale using K8s instead of local Docker.
 
 
 
 
 
 
 
160
 
161
  ---
162
 
163
  ## 📝 License
164
 
165
- Distributed under the **MIT License**. See `LICENSE` or the project root for more information.
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
+ [![Weights & Biases](https://img.shields.io/badge/Weights%20%26%20Biases-FFBE00?logo=weightsandbiases&logoColor=black)](https://wandb.ai)
14
+ [![Hugging Face](https://img.shields.io/badge/Hugging%20Face-FFD21E?logo=huggingface&logoColor=black)](https://huggingface.co)
15
+ [![Python 3.12](https://img.shields.io/badge/Python-3.12-blue?logo=python&logoColor=white)](https://python.org)
16
+ [![OpenEnv](https://img.shields.io/badge/OpenEnv-Compliant-2ea44f)](https://openenv.dev)
17
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](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: CyberPhysical 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 -->|ReAttack| 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 supplychain analysis identifying typosquatting, backdoors, data exfiltration, and malicious logic in dependencies.
53
+ - 🐳 **Hardened Sandboxing**: Highfidelity Docker isolation with network‑disabled execution, strict resource caps, and ephemeral file systems to safely detonate suspicious code.
54
+ - 🧠 **GRPODriven Learning**: Group Relative Policy Optimization (inspired by DeepSeekR1) enables trialanderror 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 decisionmaking process.
56
+ - 📊 **SOC Dashboard**: Realtime 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 # Typesafe 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
+ - [ ] **MultiAgent Coordination**: Deploy attacker and defender models for automated redteaming 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."""