panopticon-mcp-server 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- panopticon_mcp_server-0.1.0/.env.example +4 -0
- panopticon_mcp_server-0.1.0/.gitignore +74 -0
- panopticon_mcp_server-0.1.0/PKG-INFO +33 -0
- panopticon_mcp_server-0.1.0/ai_agent.py +431 -0
- panopticon_mcp_server-0.1.0/config.py +36 -0
- panopticon_mcp_server-0.1.0/db.py +119 -0
- panopticon_mcp_server-0.1.0/get_vendors.py +54 -0
- panopticon_mcp_server-0.1.0/global_search.py +134 -0
- panopticon_mcp_server-0.1.0/graph_builder.py +259 -0
- panopticon_mcp_server-0.1.0/ingestor.py +246 -0
- panopticon_mcp_server-0.1.0/mcp_server.py +608 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/README.md +17 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/__init__.py +3 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/config.py +53 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/context.py +90 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/dbreader.py +114 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/kev.py +69 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/nvd.py +299 -0
- panopticon_mcp_server-0.1.0/panopticon_mcp/server.py +140 -0
- panopticon_mcp_server-0.1.0/pyproject.toml +30 -0
- panopticon_mcp_server-0.1.0/requirements.txt +13 -0
- panopticon_mcp_server-0.1.0/ruff.toml +5 -0
- panopticon_mcp_server-0.1.0/save_report.py +277 -0
- panopticon_mcp_server-0.1.0/seed_mock.py +58 -0
- panopticon_mcp_server-0.1.0/utils.py +83 -0
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
NVD_API_KEY=your_nvd_api_key_here
|
|
2
|
+
OPENROUTER_API_KEY=your_openrouter_api_key_here
|
|
3
|
+
AVAILABLE_MODELS=nvidia/nemotron-3.5-lightning:free,nvidia/nemotron-3.5-super-vl:free,nvidia/nemotron-3.5-content-safety:free,nvidia/nemotron-3-ultra-550b-a55b:free,nvidia/nemotron-3-nano-omni-30b-a3b-reasoning:free,nvidia/nemotron-3-super-120b-a12b:free,poolside/laguna-s-2.1:free,dots-studio/dots-3-note-preview:free,thinkingmachines/inkling:free,cohere/north-mini-code:free,thinkingmachines/inkling-small:free,poolside/laguna-xs-2.1:free
|
|
4
|
+
DEFAULT_MODEL=nvidia/nemotron-3.5-lightning:free
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Logs
|
|
2
|
+
logs
|
|
3
|
+
*.log
|
|
4
|
+
npm-debug.log*
|
|
5
|
+
yarn-debug.log*
|
|
6
|
+
yarn-error.log*
|
|
7
|
+
pnpm-debug.log*
|
|
8
|
+
lerna-debug.log*
|
|
9
|
+
|
|
10
|
+
node_modules
|
|
11
|
+
dist
|
|
12
|
+
dist-ssr
|
|
13
|
+
*.local
|
|
14
|
+
|
|
15
|
+
# Editor directories and files
|
|
16
|
+
.vscode/*
|
|
17
|
+
!.vscode/extensions.json
|
|
18
|
+
.idea
|
|
19
|
+
.DS_Store
|
|
20
|
+
*.suo
|
|
21
|
+
*.ntvs*
|
|
22
|
+
*.njsproj
|
|
23
|
+
*.sln
|
|
24
|
+
*.sw?
|
|
25
|
+
|
|
26
|
+
# Environment variables
|
|
27
|
+
.env
|
|
28
|
+
*.env
|
|
29
|
+
panopticon-python/.env
|
|
30
|
+
|
|
31
|
+
# Database
|
|
32
|
+
panopticon.db
|
|
33
|
+
*.db
|
|
34
|
+
panopticon-python/*.db
|
|
35
|
+
|
|
36
|
+
# Python
|
|
37
|
+
panopticon-python/venv/
|
|
38
|
+
__pycache__/
|
|
39
|
+
*.py[cod]
|
|
40
|
+
*.pyo
|
|
41
|
+
venv/
|
|
42
|
+
.venv/
|
|
43
|
+
|
|
44
|
+
# Node
|
|
45
|
+
node_modules/
|
|
46
|
+
dist/
|
|
47
|
+
|
|
48
|
+
# Rust
|
|
49
|
+
src-tauri/target/
|
|
50
|
+
|
|
51
|
+
# IDE
|
|
52
|
+
.vscode/
|
|
53
|
+
.idea/
|
|
54
|
+
*.swp
|
|
55
|
+
*.swo
|
|
56
|
+
|
|
57
|
+
# Logs
|
|
58
|
+
*.log
|
|
59
|
+
.aider*
|
|
60
|
+
|
|
61
|
+
# panopticon-mcp-server: build artifacts
|
|
62
|
+
panopticon-python/dist/
|
|
63
|
+
panopticon-python/*.egg-info/
|
|
64
|
+
panopticon-python/.ruff_cache/
|
|
65
|
+
panopticon-python/panopticon_mcp/.ruff_cache/
|
|
66
|
+
panopticon-python/uv.lock
|
|
67
|
+
|
|
68
|
+
# panopticon-mcp-server: debug/test files
|
|
69
|
+
test_mcp_requests.txt
|
|
70
|
+
test_mcp_client.py
|
|
71
|
+
test_all_tools.py
|
|
72
|
+
test_specific_tool.py
|
|
73
|
+
mcp_out.jsonl
|
|
74
|
+
mcp_err.log
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: panopticon-mcp-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: MCP server for CVE intelligence: local SQLite cache, live NVD fallback, CISA KEV flags
|
|
5
|
+
License: MIT
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: aiosqlite>=0.20
|
|
8
|
+
Requires-Dist: httpx>=0.27
|
|
9
|
+
Requires-Dist: mcp<2,>=1.2.0
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: mcp[cli]<2,>=1.2.0; extra == 'dev'
|
|
12
|
+
Requires-Dist: pytest-asyncio>=0.24; extra == 'dev'
|
|
13
|
+
Requires-Dist: pytest>=8; extra == 'dev'
|
|
14
|
+
Requires-Dist: ruff>=0.7; extra == 'dev'
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# panopticon-mcp-server
|
|
18
|
+
|
|
19
|
+
MCP server for CVE intelligence: local SQLite cache (optional), live NVD fallback,
|
|
20
|
+
CISA KEV flags, CWE pattern analysis.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
pip install panopticon-mcp-server
|
|
25
|
+
|
|
26
|
+
## Run
|
|
27
|
+
|
|
28
|
+
panopticon-mcp-server # live-only mode (NVD + CISA KEV)
|
|
29
|
+
panopticon-mcp-server --db-path PATH # local+live mode with Panopticon SQLite DB
|
|
30
|
+
set PANOPTICON_DB_PATH=PATH # same via env var
|
|
31
|
+
|
|
32
|
+
Tools: get_cve_details, search_cves, is_cve_exploited, get_cwe_hierarchy,
|
|
33
|
+
get_cve_context_for_mitigation, db_status.
|
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Panopticon CVE AI Agent.
|
|
3
|
+
Handles mitigation generation and chat with history via OpenRouter and MCP server.
|
|
4
|
+
Includes response caching and structured error handling.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import asyncio
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
import os
|
|
11
|
+
import sys
|
|
12
|
+
from contextlib import AsyncExitStack
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
|
|
15
|
+
import httpx
|
|
16
|
+
from dotenv import load_dotenv
|
|
17
|
+
from mcp import ClientSession, StdioServerParameters
|
|
18
|
+
from mcp.client.stdio import stdio_client
|
|
19
|
+
|
|
20
|
+
from utils import sanitize_degenerate
|
|
21
|
+
|
|
22
|
+
# Global state for daemon streaming
|
|
23
|
+
_current_daemon_request_id: int | None = None
|
|
24
|
+
|
|
25
|
+
load_dotenv()
|
|
26
|
+
|
|
27
|
+
SCRIPT_DIR = Path(__file__).resolve().parent
|
|
28
|
+
sys.path.insert(0, str(SCRIPT_DIR))
|
|
29
|
+
|
|
30
|
+
# Logging configuration
|
|
31
|
+
logging.basicConfig(
|
|
32
|
+
level=logging.INFO,
|
|
33
|
+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
|
34
|
+
datefmt="%H:%M:%S",
|
|
35
|
+
stream=sys.stderr,
|
|
36
|
+
)
|
|
37
|
+
logger = logging.getLogger("ai_agent")
|
|
38
|
+
|
|
39
|
+
OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY")
|
|
40
|
+
if not OPENROUTER_API_KEY:
|
|
41
|
+
logger.error("OPENROUTER_API_KEY not found in .env!")
|
|
42
|
+
sys.exit(1)
|
|
43
|
+
|
|
44
|
+
OPENROUTER_BASE_URL = "https://openrouter.ai/api/v1"
|
|
45
|
+
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "nvidia/nemotron-3.5-lightning:free")
|
|
46
|
+
|
|
47
|
+
# In-memory LLM response cache (key: hash of model+messages, value: response text)
|
|
48
|
+
# Max 50 entries to prevent unbounded memory growth
|
|
49
|
+
_response_cache: dict[str, tuple[str, float]] = {}
|
|
50
|
+
CACHE_MAX_SIZE = 50
|
|
51
|
+
CACHE_TTL_SECONDS = 3600 # 1 hour
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class CVEAgent:
|
|
55
|
+
"""Manages LLM sessions and MCP tool calls for CVE analysis."""
|
|
56
|
+
|
|
57
|
+
def __init__(self, model_name: str):
|
|
58
|
+
self.model_name = model_name
|
|
59
|
+
self.mcp_session = None
|
|
60
|
+
self._exit_stack = None
|
|
61
|
+
self.http_client = httpx.AsyncClient(
|
|
62
|
+
base_url=OPENROUTER_BASE_URL,
|
|
63
|
+
headers={
|
|
64
|
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
65
|
+
"Content-Type": "application/json",
|
|
66
|
+
"HTTP-Referer": "http://localhost:1420",
|
|
67
|
+
"X-Title": "Panopticon CVE Map",
|
|
68
|
+
},
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
async def connect_to_mcp(self):
|
|
72
|
+
"""Initialize connection to the local MCP server."""
|
|
73
|
+
server_params = StdioServerParameters(
|
|
74
|
+
command=sys.executable, args=[str(SCRIPT_DIR / "mcp_server.py")]
|
|
75
|
+
)
|
|
76
|
+
self._exit_stack = AsyncExitStack()
|
|
77
|
+
stdio_transport = await self._exit_stack.enter_async_context(
|
|
78
|
+
stdio_client(server_params)
|
|
79
|
+
)
|
|
80
|
+
read_stream, write_stream = stdio_transport
|
|
81
|
+
self.mcp_session = await self._exit_stack.enter_async_context(
|
|
82
|
+
ClientSession(read_stream, write_stream)
|
|
83
|
+
)
|
|
84
|
+
await self.mcp_session.initialize()
|
|
85
|
+
logger.info("MCP connection established")
|
|
86
|
+
|
|
87
|
+
async def get_cve_context(self, cve_id: str) -> str:
|
|
88
|
+
"""Retrieve CVE context via MCP tool call."""
|
|
89
|
+
if not self.mcp_session:
|
|
90
|
+
await self.connect_to_mcp()
|
|
91
|
+
try:
|
|
92
|
+
result = await self.mcp_session.call_tool(
|
|
93
|
+
"get_cve_context_for_mitigation", arguments={"cve_id": cve_id}
|
|
94
|
+
)
|
|
95
|
+
if getattr(result, "isError", False):
|
|
96
|
+
error_msg = (
|
|
97
|
+
result.content[0].text if result.content else "Unknown MCP error"
|
|
98
|
+
)
|
|
99
|
+
logger.error(f"MCP tool error for {cve_id}: {error_msg}")
|
|
100
|
+
return f"MCP Error: {error_msg}"
|
|
101
|
+
if result.content:
|
|
102
|
+
for content_item in result.content:
|
|
103
|
+
if getattr(content_item, "type", "text") == "text":
|
|
104
|
+
logger.debug(f"MCP context retrieved for {cve_id}")
|
|
105
|
+
return content_item.text
|
|
106
|
+
return str(result.content[0].text)
|
|
107
|
+
logger.warning(f"Empty MCP response for {cve_id}")
|
|
108
|
+
return "Error: Empty response from MCP server"
|
|
109
|
+
except Exception as e:
|
|
110
|
+
logger.exception(f"MCP tool call failed for {cve_id}")
|
|
111
|
+
return f"Exception calling MCP tool: {e}"
|
|
112
|
+
|
|
113
|
+
async def _call_llm(
|
|
114
|
+
self,
|
|
115
|
+
messages: list,
|
|
116
|
+
temperature: float = 0.7,
|
|
117
|
+
max_tokens: int = 4096,
|
|
118
|
+
stream: bool = False,
|
|
119
|
+
) -> str:
|
|
120
|
+
"""
|
|
121
|
+
Call OpenRouter API. If stream=True, yields tokens via stdout line-by-line.
|
|
122
|
+
Falls back to non-streaming if provider doesn't support it.
|
|
123
|
+
"""
|
|
124
|
+
headers = {
|
|
125
|
+
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
126
|
+
"Content-Type": "application/json",
|
|
127
|
+
"HTTP-Referer": "https://github.com/sleepti3ht/Panopticon",
|
|
128
|
+
"X-Title": "Panopticon CVE Intelligence Map",
|
|
129
|
+
}
|
|
130
|
+
payload = {
|
|
131
|
+
"model": self.model_name,
|
|
132
|
+
"messages": messages,
|
|
133
|
+
"temperature": temperature,
|
|
134
|
+
"max_tokens": max_tokens,
|
|
135
|
+
"top_p": 0.9,
|
|
136
|
+
"frequency_penalty": 0.4,
|
|
137
|
+
"presence_penalty": 0.3,
|
|
138
|
+
"stream": stream,
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async with httpx.AsyncClient(timeout=120.0) as client:
|
|
142
|
+
if not stream:
|
|
143
|
+
resp = await client.post(
|
|
144
|
+
f"{OPENROUTER_BASE_URL}/chat/completions",
|
|
145
|
+
json=payload,
|
|
146
|
+
headers=headers,
|
|
147
|
+
)
|
|
148
|
+
resp.raise_for_status()
|
|
149
|
+
data = resp.json()
|
|
150
|
+
|
|
151
|
+
if "error" in data:
|
|
152
|
+
err_msg = data["error"].get("message", str(data["error"]))
|
|
153
|
+
if "credit" in err_msg.lower():
|
|
154
|
+
return f"Error: Provider credit issue - {err_msg}"
|
|
155
|
+
elif "rate limit" in err_msg.lower():
|
|
156
|
+
return f"Error: Rate limit - {err_msg}"
|
|
157
|
+
return f"Error: {err_msg}"
|
|
158
|
+
|
|
159
|
+
return data["choices"][0]["message"]["content"]
|
|
160
|
+
|
|
161
|
+
# Streaming mode: parse SSE and write tokens to stdout
|
|
162
|
+
async with client.stream(
|
|
163
|
+
"POST",
|
|
164
|
+
f"{OPENROUTER_BASE_URL}/chat/completions",
|
|
165
|
+
json=payload,
|
|
166
|
+
headers=headers,
|
|
167
|
+
) as resp:
|
|
168
|
+
resp.raise_for_status()
|
|
169
|
+
full_text = ""
|
|
170
|
+
async for line in resp.aiter_lines():
|
|
171
|
+
if not line.startswith("data: "):
|
|
172
|
+
continue
|
|
173
|
+
data_str = line[6:].strip()
|
|
174
|
+
if data_str == "[DONE]":
|
|
175
|
+
break
|
|
176
|
+
try:
|
|
177
|
+
chunk = json.loads(data_str)
|
|
178
|
+
if "choices" in chunk and len(chunk["choices"]) > 0:
|
|
179
|
+
delta = chunk["choices"][0].get("delta", {})
|
|
180
|
+
token = delta.get("content", "")
|
|
181
|
+
if token:
|
|
182
|
+
full_text += token
|
|
183
|
+
# If in daemon mode, emit JSON stream chunk; else raw text
|
|
184
|
+
global _current_daemon_request_id # noqa: PLW0602
|
|
185
|
+
if _current_daemon_request_id is not None:
|
|
186
|
+
print(
|
|
187
|
+
json.dumps(
|
|
188
|
+
{
|
|
189
|
+
"id": _current_daemon_request_id,
|
|
190
|
+
"stream": token,
|
|
191
|
+
}
|
|
192
|
+
),
|
|
193
|
+
flush=True,
|
|
194
|
+
)
|
|
195
|
+
else:
|
|
196
|
+
print(token, end="", flush=True)
|
|
197
|
+
except json.JSONDecodeError:
|
|
198
|
+
continue
|
|
199
|
+
return full_text
|
|
200
|
+
|
|
201
|
+
async def generate_mitigation(self, cve_id: str) -> str:
|
|
202
|
+
"""Generate a structured mitigation plan for a given CVE."""
|
|
203
|
+
logger.info(f"Generating mitigation for {cve_id}")
|
|
204
|
+
context = await self.get_cve_context(cve_id)
|
|
205
|
+
if "error" in context.lower() or "exception" in context.lower():
|
|
206
|
+
return context
|
|
207
|
+
|
|
208
|
+
prompt = f"""You are a cybersecurity expert specializing in vulnerability remediation.
|
|
209
|
+
IMPORTANT: Detect the language of the user's interface or query. If the user interacts in Russian, respond in Russian. Otherwise, default to English.
|
|
210
|
+
|
|
211
|
+
Context of the vulnerability:
|
|
212
|
+
{context}
|
|
213
|
+
|
|
214
|
+
CRITICAL INSTRUCTIONS:
|
|
215
|
+
- Output ONLY the final formatted response.
|
|
216
|
+
- DO NOT output your thinking process, internal monologue, or reasoning steps.
|
|
217
|
+
- DO NOT start with "Here is the analysis" or "Sure". Start directly with "1. **Risk Assessment**".
|
|
218
|
+
- Provide your response in the following exact format:
|
|
219
|
+
1. **Risk Assessment**: Brief summary of the threat
|
|
220
|
+
2. **Immediate Actions**: Steps to take right now (patching, workarounds)
|
|
221
|
+
3. **Long-term Recommendations**: Architectural improvements
|
|
222
|
+
4. **Specific Commands**: Exact commands or configuration changes
|
|
223
|
+
5. **References**: Links to official advisories
|
|
224
|
+
- Be concise, technical, and comprehensive. Use markdown formatting."""
|
|
225
|
+
|
|
226
|
+
messages = [
|
|
227
|
+
{
|
|
228
|
+
"role": "system",
|
|
229
|
+
"content": "You are a strict, professional cybersecurity expert. Output only the final answer, no thinking process.",
|
|
230
|
+
},
|
|
231
|
+
{"role": "user", "content": prompt},
|
|
232
|
+
]
|
|
233
|
+
|
|
234
|
+
return await self._call_llm(messages, temperature=0.1, max_tokens=2000)
|
|
235
|
+
|
|
236
|
+
async def close(self):
|
|
237
|
+
"""Clean up resources, suppressing known anyio teardown quirks."""
|
|
238
|
+
if self._exit_stack:
|
|
239
|
+
try:
|
|
240
|
+
await self._exit_stack.aclose()
|
|
241
|
+
except (RuntimeError, ExceptionGroup):
|
|
242
|
+
logger.debug("Suppressed anyio teardown noise during MCP close")
|
|
243
|
+
except Exception as e: # noqa: BLE001
|
|
244
|
+
logger.warning(f"Non-fatal error during agent cleanup: {e}")
|
|
245
|
+
|
|
246
|
+
await self.http_client.aclose()
|
|
247
|
+
logger.debug("Agent resources cleaned up")
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
# ==========================================
|
|
251
|
+
# Functions for chat with conversation history
|
|
252
|
+
# ==========================================
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
async def chat_with_history(
|
|
256
|
+
cve_id: str, messages_json: str, model: str = DEFAULT_MODEL
|
|
257
|
+
) -> str:
|
|
258
|
+
"""
|
|
259
|
+
Chat with history. Returns full response.
|
|
260
|
+
In streaming mode, tokens are written to stdout as they arrive.
|
|
261
|
+
"""
|
|
262
|
+
try:
|
|
263
|
+
messages = json.loads(messages_json)
|
|
264
|
+
except json.JSONDecodeError as e:
|
|
265
|
+
return f"Error: Invalid messages JSON - {e}"
|
|
266
|
+
|
|
267
|
+
agent = CVEAgent(model)
|
|
268
|
+
cve_context = await agent.get_cve_context(cve_id)
|
|
269
|
+
|
|
270
|
+
system_prompt = f"""You are a cybersecurity analyst specializing in CVE triage.
|
|
271
|
+
CVE ID: {cve_id}
|
|
272
|
+
Context: {cve_context}
|
|
273
|
+
|
|
274
|
+
Provide structured mitigation plans with:
|
|
275
|
+
1. Risk assessment (CVSS, severity, exploitation likelihood)
|
|
276
|
+
2. Immediate actions (version check, emergency mitigation)
|
|
277
|
+
3. Long-term recommendations
|
|
278
|
+
4. Specific commands (version check, upgrade, workaround)
|
|
279
|
+
5. References (vendor advisories, CVE links)
|
|
280
|
+
|
|
281
|
+
Respond in the same language as the user's question."""
|
|
282
|
+
|
|
283
|
+
full_messages = [{"role": "system", "content": system_prompt}] + messages
|
|
284
|
+
|
|
285
|
+
# Try streaming first, fallback to non-streaming
|
|
286
|
+
try:
|
|
287
|
+
response = await agent._call_llm(full_messages, stream=True)
|
|
288
|
+
except Exception: # noqa: BLE001
|
|
289
|
+
# Fallback: non-streaming mode
|
|
290
|
+
response = await agent._call_llm(full_messages, stream=False)
|
|
291
|
+
|
|
292
|
+
# Sanitize before returning
|
|
293
|
+
if sanitize_degenerate(response) != response:
|
|
294
|
+
print("\n*[Output truncated: degenerate repetition detected]*", flush=True)
|
|
295
|
+
response = sanitize_degenerate(response)
|
|
296
|
+
|
|
297
|
+
return response
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
async def run_mitigation(cve_id: str, model_name: str):
|
|
301
|
+
"""Entry point for single-shot mitigation generation."""
|
|
302
|
+
logger.info(f"Running mitigation for {cve_id} with model {model_name}")
|
|
303
|
+
agent = CVEAgent(model_name)
|
|
304
|
+
try:
|
|
305
|
+
await agent.connect_to_mcp()
|
|
306
|
+
result = await agent.generate_mitigation(cve_id)
|
|
307
|
+
print(result)
|
|
308
|
+
finally:
|
|
309
|
+
await agent.close()
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
# ==========================================
|
|
313
|
+
# Daemon mode: persistent process with JSON-RPC-like protocol
|
|
314
|
+
# ==========================================
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
async def daemon_loop():
|
|
318
|
+
"""
|
|
319
|
+
Read JSON requests from stdin, dispatch to methods, write JSON responses to stdout.
|
|
320
|
+
"""
|
|
321
|
+
logger.info("Daemon started, waiting for requests...")
|
|
322
|
+
|
|
323
|
+
while True:
|
|
324
|
+
try:
|
|
325
|
+
line = await asyncio.get_event_loop().run_in_executor(
|
|
326
|
+
None, sys.stdin.readline
|
|
327
|
+
)
|
|
328
|
+
if not line:
|
|
329
|
+
logger.info("stdin closed, daemon exiting")
|
|
330
|
+
break
|
|
331
|
+
|
|
332
|
+
line = line.strip()
|
|
333
|
+
if not line:
|
|
334
|
+
continue
|
|
335
|
+
|
|
336
|
+
try:
|
|
337
|
+
request = json.loads(line)
|
|
338
|
+
except json.JSONDecodeError as e:
|
|
339
|
+
error_response = {"error": f"Invalid JSON: {e}"}
|
|
340
|
+
print(json.dumps(error_response), flush=True)
|
|
341
|
+
continue
|
|
342
|
+
|
|
343
|
+
request_id = request.get("id", 0)
|
|
344
|
+
method = request.get("method", "")
|
|
345
|
+
params = request.get("params", {})
|
|
346
|
+
|
|
347
|
+
logger.info(f"Daemon received request id={request_id} method={method}")
|
|
348
|
+
|
|
349
|
+
if method == "chat":
|
|
350
|
+
cve_id = params.get("cve_id", "")
|
|
351
|
+
messages = params.get("messages", [])
|
|
352
|
+
model = params.get("model", DEFAULT_MODEL)
|
|
353
|
+
|
|
354
|
+
global _current_daemon_request_id
|
|
355
|
+
_current_daemon_request_id = request_id
|
|
356
|
+
|
|
357
|
+
try:
|
|
358
|
+
response = await chat_with_history(
|
|
359
|
+
cve_id, json.dumps(messages), model
|
|
360
|
+
)
|
|
361
|
+
finally:
|
|
362
|
+
_current_daemon_request_id = None
|
|
363
|
+
|
|
364
|
+
# Send final result
|
|
365
|
+
response_obj = {"id": request_id, "result": response}
|
|
366
|
+
print(json.dumps(response_obj), flush=True)
|
|
367
|
+
|
|
368
|
+
elif method == "mitigate":
|
|
369
|
+
cve_id = params.get("cve_id", "")
|
|
370
|
+
model = params.get("model", DEFAULT_MODEL)
|
|
371
|
+
|
|
372
|
+
agent = CVEAgent(model)
|
|
373
|
+
try:
|
|
374
|
+
await agent.connect_to_mcp()
|
|
375
|
+
result = await agent.generate_mitigation(cve_id)
|
|
376
|
+
finally:
|
|
377
|
+
await agent.close()
|
|
378
|
+
|
|
379
|
+
response_obj = {"id": request_id, "result": result}
|
|
380
|
+
print(json.dumps(response_obj), flush=True)
|
|
381
|
+
|
|
382
|
+
else:
|
|
383
|
+
error_response = {
|
|
384
|
+
"id": request_id,
|
|
385
|
+
"error": f"Unknown method: {method}",
|
|
386
|
+
}
|
|
387
|
+
print(json.dumps(error_response), flush=True)
|
|
388
|
+
|
|
389
|
+
except Exception as e:
|
|
390
|
+
logger.exception(f"Daemon loop error: {e}") # noqa: TRY401
|
|
391
|
+
error_response = {"error": str(e)}
|
|
392
|
+
print(json.dumps(error_response), flush=True)
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
# ==========================================
|
|
396
|
+
# Application entry point
|
|
397
|
+
# ==========================================
|
|
398
|
+
|
|
399
|
+
if __name__ == "__main__":
|
|
400
|
+
if sys.stdout.encoding != "utf-8":
|
|
401
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
402
|
+
if sys.stderr.encoding != "utf-8":
|
|
403
|
+
sys.stderr.reconfigure(encoding="utf-8")
|
|
404
|
+
if sys.stdin.encoding != "utf-8":
|
|
405
|
+
sys.stdin.reconfigure(encoding="utf-8")
|
|
406
|
+
|
|
407
|
+
# --daemon: persistent process with JSON-RPC-like protocol
|
|
408
|
+
if len(sys.argv) >= 2 and sys.argv[1] == "--daemon":
|
|
409
|
+
asyncio.run(daemon_loop())
|
|
410
|
+
|
|
411
|
+
# --chat reads conversation history from stdin (bypasses Windows 32KB CLI limit)
|
|
412
|
+
elif len(sys.argv) >= 3 and sys.argv[1] == "--chat":
|
|
413
|
+
cve_id = sys.argv[2]
|
|
414
|
+
model = sys.argv[3] if len(sys.argv) > 3 else DEFAULT_MODEL
|
|
415
|
+
messages_json = sys.stdin.read()
|
|
416
|
+
|
|
417
|
+
result = asyncio.run(chat_with_history(cve_id, messages_json, model))
|
|
418
|
+
print(result)
|
|
419
|
+
|
|
420
|
+
elif len(sys.argv) >= 3 and sys.argv[1] == "--mitigate":
|
|
421
|
+
cve_id = sys.argv[2]
|
|
422
|
+
model = sys.argv[3] if len(sys.argv) > 3 else DEFAULT_MODEL
|
|
423
|
+
asyncio.run(run_mitigation(cve_id, model))
|
|
424
|
+
else:
|
|
425
|
+
logger.error("Invalid arguments")
|
|
426
|
+
print(
|
|
427
|
+
"Usage: ai_agent.py --daemon OR "
|
|
428
|
+
"ai_agent.py --mitigate <cve_id> [model] OR "
|
|
429
|
+
"ai_agent.py --chat <cve_id> [model] (history via stdin)",
|
|
430
|
+
file=sys.stderr,
|
|
431
|
+
)
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Configuration for Panopticon CVE Map.
|
|
3
|
+
Loads environment variables and defines application constants.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import os
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
|
|
9
|
+
from dotenv import load_dotenv
|
|
10
|
+
|
|
11
|
+
load_dotenv()
|
|
12
|
+
|
|
13
|
+
# NVD API Configuration
|
|
14
|
+
NVD_API_KEY = os.getenv("NVD_API_KEY")
|
|
15
|
+
NVD_BASE_URL = "https://services.nvd.nist.gov/rest/json/cves/2.0"
|
|
16
|
+
|
|
17
|
+
# Rate limiting (requests per 30s window)
|
|
18
|
+
NVD_RATE_LIMIT = 50 if NVD_API_KEY else 5
|
|
19
|
+
NVD_RATE_WINDOW = 30
|
|
20
|
+
NVD_REQUEST_DELAY = NVD_RATE_WINDOW / NVD_RATE_LIMIT
|
|
21
|
+
|
|
22
|
+
# Graph rendering limits
|
|
23
|
+
MAX_GRAPH_NODES = 100 # Reduced from 300 for readability
|
|
24
|
+
MAX_EGO_DEPTH = 2
|
|
25
|
+
|
|
26
|
+
# Filter by publication year (only CVEs after this year)
|
|
27
|
+
MIN_PUBLISH_YEAR = 2020
|
|
28
|
+
INGEST_LOOKBACK_DAYS = 90
|
|
29
|
+
INGEST_CHUNK_DAYS = 110
|
|
30
|
+
|
|
31
|
+
# Database (absolute path, independent of the current working directory)
|
|
32
|
+
DB_PATH = str(Path(__file__).resolve().parent / "panopticon.db")
|
|
33
|
+
|
|
34
|
+
# Debug helper (NOT for production):
|
|
35
|
+
# from utils import mask_api_key
|
|
36
|
+
# logger.debug(f"NVD API Key: {mask_api_key(NVD_API_KEY)}")
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Database schema and persistence layer for Panopticon CVE Map.
|
|
3
|
+
Uses aiosqlite for async SQLite operations.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import aiosqlite
|
|
7
|
+
|
|
8
|
+
from config import DB_PATH
|
|
9
|
+
|
|
10
|
+
SCHEMA = """
|
|
11
|
+
CREATE TABLE IF NOT EXISTS cves (
|
|
12
|
+
cve_id TEXT PRIMARY KEY,
|
|
13
|
+
description TEXT,
|
|
14
|
+
cvss_score REAL DEFAULT 0.0,
|
|
15
|
+
cvss_vector TEXT,
|
|
16
|
+
severity TEXT,
|
|
17
|
+
published_date TEXT,
|
|
18
|
+
last_modified TEXT
|
|
19
|
+
);
|
|
20
|
+
|
|
21
|
+
CREATE TABLE IF NOT EXISTS affected_products (
|
|
22
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
23
|
+
cve_id TEXT REFERENCES cves(cve_id),
|
|
24
|
+
vendor TEXT NOT NULL,
|
|
25
|
+
product TEXT NOT NULL,
|
|
26
|
+
version TEXT,
|
|
27
|
+
cpe_string TEXT,
|
|
28
|
+
UNIQUE(cve_id, cpe_string)
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
CREATE TABLE IF NOT EXISTS cwe_mapping (
|
|
32
|
+
cve_id TEXT REFERENCES cves(cve_id),
|
|
33
|
+
cwe_id TEXT NOT NULL,
|
|
34
|
+
PRIMARY KEY (cve_id, cwe_id)
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
CREATE TABLE IF NOT EXISTS references_links (
|
|
38
|
+
cve_id TEXT REFERENCES cves(cve_id),
|
|
39
|
+
url TEXT NOT NULL,
|
|
40
|
+
source TEXT,
|
|
41
|
+
tags TEXT,
|
|
42
|
+
UNIQUE(cve_id, url)
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
-- Indexes for fast queries
|
|
46
|
+
CREATE INDEX IF NOT EXISTS idx_vendor ON affected_products(vendor);
|
|
47
|
+
CREATE INDEX IF NOT EXISTS idx_product ON affected_products(product);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_cvss ON cves(cvss_score);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_severity ON cves(severity);
|
|
50
|
+
CREATE INDEX IF NOT EXISTS idx_cwe ON cwe_mapping(cwe_id);
|
|
51
|
+
CREATE INDEX IF NOT EXISTS idx_published ON cves(published_date);
|
|
52
|
+
"""
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
async def init_db():
|
|
56
|
+
"""Initialize database and run migrations."""
|
|
57
|
+
async with aiosqlite.connect(DB_PATH) as db:
|
|
58
|
+
await db.executescript(SCHEMA)
|
|
59
|
+
await db.commit()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def insert_cve(cve_data: dict):
|
|
63
|
+
"""Atomic CVE insertion with duplicate ignoring (INSERT OR IGNORE)."""
|
|
64
|
+
async with aiosqlite.connect(DB_PATH) as db:
|
|
65
|
+
await db.execute(
|
|
66
|
+
"""
|
|
67
|
+
INSERT OR IGNORE INTO cves
|
|
68
|
+
(cve_id, description, cvss_score, cvss_vector, severity, published_date, last_modified)
|
|
69
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
70
|
+
""",
|
|
71
|
+
(
|
|
72
|
+
cve_data["cve_id"],
|
|
73
|
+
cve_data["description"],
|
|
74
|
+
cve_data["cvss_score"],
|
|
75
|
+
cve_data.get("cvss_vector"),
|
|
76
|
+
cve_data["severity"],
|
|
77
|
+
cve_data["published_date"],
|
|
78
|
+
cve_data["last_modified"],
|
|
79
|
+
),
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
# Affected products (CPE)
|
|
83
|
+
for cpe in cve_data.get("affected_products", []):
|
|
84
|
+
await db.execute(
|
|
85
|
+
"""
|
|
86
|
+
INSERT OR IGNORE INTO affected_products
|
|
87
|
+
(cve_id, vendor, product, version, cpe_string)
|
|
88
|
+
VALUES (?, ?, ?, ?, ?)
|
|
89
|
+
""",
|
|
90
|
+
(
|
|
91
|
+
cve_data["cve_id"],
|
|
92
|
+
cpe["vendor"],
|
|
93
|
+
cpe["product"],
|
|
94
|
+
cpe.get("version"),
|
|
95
|
+
cpe.get("cpe_string"),
|
|
96
|
+
),
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
# CWE mapping
|
|
100
|
+
for cwe_id in cve_data.get("cwe_ids", []):
|
|
101
|
+
await db.execute(
|
|
102
|
+
"""
|
|
103
|
+
INSERT OR IGNORE INTO cwe_mapping (cve_id, cwe_id)
|
|
104
|
+
VALUES (?, ?)
|
|
105
|
+
""",
|
|
106
|
+
(cve_data["cve_id"], cwe_id),
|
|
107
|
+
)
|
|
108
|
+
|
|
109
|
+
# References (advisories, patches, exploits)
|
|
110
|
+
for ref in cve_data.get("references", []):
|
|
111
|
+
await db.execute(
|
|
112
|
+
"""
|
|
113
|
+
INSERT OR IGNORE INTO references_links (cve_id, url, source, tags)
|
|
114
|
+
VALUES (?, ?, ?, ?)
|
|
115
|
+
""",
|
|
116
|
+
(cve_data["cve_id"], ref["url"], ref.get("source"), ref.get("tags")),
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
await db.commit()
|