File size: 9,592 Bytes
9452af2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | """
Rhodawk AI — Vulnerability Chain Analyzer
==========================================
Documents how primitive findings (individual assumption gaps + PoC results)
might combine into higher-severity chains.
ETHICAL CONSTRAINTS:
- Chains are THEORETICAL proposals documented for human review
- No chain is automatically executed
- All chain proposals are stored with status PENDING_HUMAN_REVIEW
- Human operator must approve or reject every chain before any further action
Orchestrated by Nous Hermes 3 via OpenRouter.
"""
from __future__ import annotations
import hashlib
import json
import os
import re
import sqlite3
import time
from typing import Optional
import requests
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY", "")
HERMES_MODEL = os.getenv(
"RHODAWK_RESEARCH_MODEL",
"nousresearch/hermes-3-llama-3.1-405b:free",
)
CHAIN_DB = os.getenv("RHODAWK_CHAIN_DB", "/data/chain_memory.sqlite")
def _init_db() -> None:
os.makedirs(os.path.dirname(CHAIN_DB), exist_ok=True)
conn = sqlite3.connect(CHAIN_DB)
conn.executescript("""
CREATE TABLE IF NOT EXISTS primitive_findings (
id TEXT PRIMARY KEY,
repo TEXT NOT NULL,
gap_id TEXT NOT NULL,
severity TEXT,
description TEXT,
triggered INTEGER DEFAULT 0,
confidence TEXT DEFAULT 'UNKNOWN',
created_at REAL,
harness_out TEXT
);
CREATE TABLE IF NOT EXISTS chains (
id TEXT PRIMARY KEY,
repo TEXT NOT NULL,
primitive_ids TEXT NOT NULL,
description TEXT,
chained_severity TEXT,
confidence TEXT,
conditions TEXT,
theoretical_impact TEXT,
human_notes TEXT,
status TEXT DEFAULT 'PENDING_HUMAN_REVIEW',
created_at REAL,
human_approved INTEGER DEFAULT 0,
human_reviewer TEXT,
reviewed_at REAL
);
""")
conn.commit()
conn.close()
def store_primitive(
repo: str,
gap_id: str,
severity: str,
description: str,
triggered: bool,
confidence: str = "UNKNOWN",
harness_result: Optional[dict] = None,
) -> str:
"""Persist a primitive finding from the harness execution."""
_init_db()
finding_id = hashlib.sha256(
f"{repo}:{gap_id}:{time.time()}".encode()
).hexdigest()[:16]
conn = sqlite3.connect(CHAIN_DB)
conn.execute(
"""INSERT OR REPLACE INTO primitive_findings
(id, repo, gap_id, severity, description, triggered, confidence, created_at, harness_out)
VALUES (?,?,?,?,?,?,?,?,?)""",
(
finding_id, repo, gap_id, severity, description,
1 if triggered else 0, confidence, time.time(),
json.dumps(harness_result or {}),
),
)
conn.commit()
conn.close()
return finding_id
def analyze_chains(repo: str) -> list[dict]:
"""
Ask Hermes to propose vulnerability chains from stored primitives.
Returns THEORETICAL proposals — all tagged PENDING_HUMAN_REVIEW.
Nothing is executed automatically.
"""
_init_db()
conn = sqlite3.connect(CHAIN_DB)
rows = conn.execute(
"""SELECT id, gap_id, severity, description, triggered, confidence
FROM primitive_findings WHERE repo = ?
ORDER BY created_at DESC""",
(repo,),
).fetchall()
conn.close()
if len(rows) < 2:
return []
primitives_text = "\n".join(
f"- [{r[0]}] Gap: {r[1]} | Sev: {r[2]} | Triggered: {bool(r[4])} "
f"| Confidence: {r[5]} | {r[3][:120]}"
for r in rows
)
headers = {
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "https://rhodawk.ai",
}
payload = {
"model": HERMES_MODEL,
"messages": [
{
"role": "system",
"content": (
"You are a senior security researcher conducting responsible vulnerability research. "
"Analyse primitive findings and propose THEORETICAL vulnerability chains for human review. "
"Be rigorous and conservative — only propose chains that are logically sound based on "
"the available evidence. Mark any speculation clearly. Output valid JSON only."
),
},
{
"role": "user",
"content": (
f"Analyse these primitive findings from {repo} and identify plausible chains.\n\n"
f"PRIMITIVES:\n{primitives_text}\n\n"
"Output ONLY this JSON:\n"
'{"chains": [{'
'"primitive_ids": ["id1","id2"],'
'"description": "Step-by-step logical chain",'
'"chained_severity": "P1|P2|P3",'
'"confidence": "HIGH|MEDIUM|LOW",'
'"required_conditions": ["condition1"],'
'"theoretical_impact": "What an attacker could theoretically achieve",'
'"human_verification_needed": "What a human researcher must manually verify before treating this as real"'
"}]}"
),
},
],
"max_tokens": 2048,
"temperature": 0.1,
}
try:
resp = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers, json=payload, timeout=120,
)
resp.raise_for_status()
raw = resp.json()["choices"][0]["message"]["content"]
match = re.search(r"\{[\s\S]*\}", raw)
if not match:
return []
data = json.loads(match.group())
chains = data.get("chains", [])
conn = sqlite3.connect(CHAIN_DB)
for chain in chains:
chain_id = hashlib.sha256(
f"{repo}:{':'.join(chain.get('primitive_ids', []))}:{time.time()}".encode()
).hexdigest()[:16]
chain["id"] = chain_id
conn.execute(
"""INSERT OR IGNORE INTO chains
(id, repo, primitive_ids, description, chained_severity, confidence,
conditions, theoretical_impact, status, created_at)
VALUES (?,?,?,?,?,?,?,?,'PENDING_HUMAN_REVIEW',?)""",
(
chain_id, repo,
json.dumps(chain.get("primitive_ids", [])),
chain.get("description", ""),
chain.get("chained_severity", "P3"),
chain.get("confidence", "LOW"),
json.dumps(chain.get("required_conditions", [])),
chain.get("theoretical_impact", ""),
time.time(),
),
)
conn.commit()
conn.close()
return chains
except Exception as e:
return [{"error": str(e)}]
def get_pending_chains(repo: Optional[str] = None) -> list[dict]:
_init_db()
conn = sqlite3.connect(CHAIN_DB)
if repo:
rows = conn.execute(
"""SELECT id, repo, description, chained_severity, confidence, status, created_at
FROM chains WHERE repo=? AND status='PENDING_HUMAN_REVIEW'
ORDER BY created_at DESC""",
(repo,),
).fetchall()
else:
rows = conn.execute(
"""SELECT id, repo, description, chained_severity, confidence, status, created_at
FROM chains WHERE status='PENDING_HUMAN_REVIEW'
ORDER BY created_at DESC""",
).fetchall()
conn.close()
return [
{
"id": r[0], "repo": r[1], "description": r[2],
"severity": r[3], "confidence": r[4], "status": r[5], "created_at": r[6],
}
for r in rows
]
def get_all_primitives(repo: Optional[str] = None) -> list[dict]:
_init_db()
conn = sqlite3.connect(CHAIN_DB)
if repo:
rows = conn.execute(
"SELECT id, repo, gap_id, severity, description, triggered, confidence, created_at "
"FROM primitive_findings WHERE repo=? ORDER BY created_at DESC",
(repo,),
).fetchall()
else:
rows = conn.execute(
"SELECT id, repo, gap_id, severity, description, triggered, confidence, created_at "
"FROM primitive_findings ORDER BY created_at DESC",
).fetchall()
conn.close()
return [
{
"id": r[0], "repo": r[1], "gap_id": r[2], "severity": r[3],
"description": r[4], "triggered": bool(r[5]),
"confidence": r[6], "created_at": r[7],
}
for r in rows
]
def approve_chain(chain_id: str, reviewer: str) -> bool:
_init_db()
conn = sqlite3.connect(CHAIN_DB)
conn.execute(
"UPDATE chains SET status='HUMAN_APPROVED', human_approved=1, "
"human_reviewer=?, reviewed_at=? WHERE id=?",
(reviewer, time.time(), chain_id),
)
conn.commit()
conn.close()
return True
def reject_chain(chain_id: str, reviewer: str) -> bool:
_init_db()
conn = sqlite3.connect(CHAIN_DB)
conn.execute(
"UPDATE chains SET status='HUMAN_REJECTED', human_reviewer=?, reviewed_at=? WHERE id=?",
(reviewer, time.time(), chain_id),
)
conn.commit()
conn.close()
return True
|