forked from sooryathejas/METATRON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm.py
More file actions
372 lines (295 loc) · 13 KB
/
Copy pathllm.py
File metadata and controls
372 lines (295 loc) · 13 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python3
"""
METATRON - llm.py
Ollama interface for metatron-qwen model.
Builds prompts, handles AI responses, runs tool dispatch loop.
Model: metatron-qwen (fine-tuned from huihui_ai/qwen3.5-abliterated:9b)
"""
import re
import requests
import json
from tools import run_tool_by_command, run_nmap, run_curl_headers
from search import handle_search_dispatch
OLLAMA_URL = "http://localhost:11434/api/generate"
MODEL_NAME = "metatron-qwen"
MAX_TOKENS = 4096
MAX_TOOL_LOOPS = 9 # max times AI can call tools per session
OLLAMA_TIMEOUT = 600
# ─────────────────────────────────────────────
# SYSTEM PROMPT
# ─────────────────────────────────────────────
SYSTEM_PROMPT = """You are METATRON, an elite AI penetration testing assistant running on Parrot OS.
You are precise, technical, and direct. No fluff.
You have access to real tools. To use them, write tags in your response:
[TOOL: nmap -sV 192.168.1.1] → runs nmap or any CLI tool
[SEARCH: CVE-2021-44228 exploit] → searches the web via DuckDuckGo
Rules:
- Always analyze scan data thoroughly before suggesting exploits
- List vulnerabilities with: name, severity (critical/high/medium/low), port, service
- For each vulnerability, suggest a concrete fix
- If you need more information, use [SEARCH:] or [TOOL:]
- Format vulnerabilities clearly so they can be saved to a database
- Be specific about CVE IDs when you know them
- Always give a final risk rating: CRITICAL / HIGH / MEDIUM / LOW
Output format for vulnerabilities (use this exactly):
VULN: <name> | SEVERITY: <level> | PORT: <port> | SERVICE: <service>
DESC: <description>
FIX: <fix recommendation>
Output format for exploits:
EXPLOIT: <name> | TOOL: <tool> | PAYLOAD: <payload or description>
RESULT: <expected result>
NOTES: <any notes>
End your analysis with:
RISK_LEVEL: <CRITICAL|HIGH|MEDIUM|LOW>
SUMMARY: <2-3 sentence overall summary>
"""
# ─────────────────────────────────────────────
# OLLAMA API CALL
# ─────────────────────────────────────────────
def ask_ollama(prompt: str, context: list = None) -> str:
"""
Send a prompt to metatron-qwen via Ollama API.
context: list of previous message dicts for multi-turn conversation.
Returns the AI response string.
"""
try:
payload = {
"model": MODEL_NAME,
"prompt": prompt,
"stream": False,
"options": {
"num_predict": MAX_TOKENS,
"temperature": 0.7,
"top_p": 0.9,
}
}
print(f"\n[*] Sending to {MODEL_NAME}...")
resp = requests.post(OLLAMA_URL, json=payload, timeout=OLLAMA_TIMEOUT)
resp.raise_for_status()
data = resp.json()
response = data.get("response", "").strip()
if not response:
return "[!] Model returned empty response."
return response
except requests.exceptions.ConnectionError:
return "[!] Cannot connect to Ollama. Is it running? Try: ollama serve"
except requests.exceptions.Timeout:
return "[!] Ollama timed out. Model may be loading, try again."
except requests.exceptions.HTTPError as e:
return f"[!] Ollama HTTP error: {e}"
except Exception as e:
return f"[!] Unexpected error: {e}"
# ─────────────────────────────────────────────
# TOOL DISPATCH
# ─────────────────────────────────────────────
def extract_tool_calls(response: str) -> list:
"""
Extract all [TOOL: ...] and [SEARCH: ...] tags from AI response.
Returns list of tuples: [("TOOL", "nmap -sV x.x.x.x"), ("SEARCH", "CVE...")]
"""
calls = []
tool_matches = re.findall(r'\[TOOL:\s*(.+?)\]', response)
search_matches = re.findall(r'\[SEARCH:\s*(.+?)\]', response)
for m in tool_matches:
calls.append(("TOOL", m.strip()))
for m in search_matches:
calls.append(("SEARCH", m.strip()))
return calls
def run_tool_calls(calls: list) -> str:
"""
Execute all tool/search calls and return combined results string.
"""
if not calls:
return ""
results = ""
for call_type, call_content in calls:
print(f"\n [DISPATCH] {call_type}: {call_content}")
if call_type == "TOOL":
output = run_tool_by_command(call_content)
elif call_type == "SEARCH":
output = handle_search_dispatch(call_content)
else:
output = f"[!] Unknown call type: {call_type}"
results += f"\n[{call_type} RESULT: {call_content}]\n"
results += "─" * 40 + "\n"
results += output.strip() + "\n"
return results
# ─────────────────────────────────────────────
# PARSER — extract structured data from AI output
# ─────────────────────────────────────────────
def parse_vulnerabilities(response: str) -> list:
"""
Parse VULN: lines from AI response into dicts.
Returns list of vulnerability dicts ready for db.save_vulnerability()
"""
vulns = []
lines = response.splitlines()
i = 0
while i < len(lines):
line = lines[i].strip()
if line.startswith("VULN:"):
vuln = {
"vuln_name": "",
"severity": "medium",
"port": "",
"service": "",
"description": "",
"fix": ""
}
# parse header line: VULN: name | SEVERITY: x | PORT: x | SERVICE: x
parts = line.split("|")
for part in parts:
part = part.strip()
if part.startswith("VULN:"):
vuln["vuln_name"] = part.replace("VULN:", "").strip()
elif part.startswith("SEVERITY:"):
vuln["severity"] = part.replace("SEVERITY:", "").strip().lower()
elif part.startswith("PORT:"):
vuln["port"] = part.replace("PORT:", "").strip()
elif part.startswith("SERVICE:"):
vuln["service"] = part.replace("SERVICE:", "").strip()
# look ahead for DESC: and FIX: lines
j = i + 1
while j < len(lines) and j <= i + 5:
next_line = lines[j].strip()
if next_line.startswith("DESC:"):
vuln["description"] = next_line.replace("DESC:", "").strip()
elif next_line.startswith("FIX:"):
vuln["fix"] = next_line.replace("FIX:", "").strip()
j += 1
if vuln["vuln_name"]:
vulns.append(vuln)
i += 1
return vulns
def parse_exploits(response: str) -> list:
"""
Parse EXPLOIT: lines from AI response into dicts.
Returns list of exploit dicts ready for db.save_exploit()
"""
exploits = []
lines = response.splitlines()
i = 0
while i < len(lines):
line = lines[i].strip()
if line.startswith("EXPLOIT:"):
exploit = {
"exploit_name": "",
"tool_used": "",
"payload": "",
"result": "unknown",
"notes": ""
}
parts = line.split("|")
for part in parts:
part = part.strip()
if part.startswith("EXPLOIT:"):
exploit["exploit_name"] = part.replace("EXPLOIT:", "").strip()
elif part.startswith("TOOL:"):
exploit["tool_used"] = part.replace("TOOL:", "").strip()
elif part.startswith("PAYLOAD:"):
exploit["payload"] = part.replace("PAYLOAD:", "").strip()
j = i + 1
while j < len(lines) and j <= i + 4:
next_line = lines[j].strip()
if next_line.startswith("RESULT:"):
exploit["result"] = next_line.replace("RESULT:", "").strip()
elif next_line.startswith("NOTES:"):
exploit["notes"] = next_line.replace("NOTES:", "").strip()
j += 1
if exploit["exploit_name"]:
exploits.append(exploit)
i += 1
return exploits
def parse_risk_level(response: str) -> str:
"""Extract RISK_LEVEL from AI response."""
match = re.search(r'RISK_LEVEL:\s*(CRITICAL|HIGH|MEDIUM|LOW)', response, re.IGNORECASE)
return match.group(1).upper() if match else "UNKNOWN"
def parse_summary(response: str) -> str:
"""Extract SUMMARY line from AI response."""
match = re.search(r'SUMMARY:\s*(.+)', response, re.IGNORECASE)
return match.group(1).strip() if match else response[:500]
# ─────────────────────────────────────────────
# MAIN ANALYSIS FUNCTION
# ─────────────────────────────────────────────
def analyse_target(target: str, raw_scan: str) -> dict:
"""
Full analysis pipeline:
1. Build initial prompt with scan data
2. Send to metatron-qwen
3. Run tool dispatch loop if AI requests tools
4. Parse structured output
5. Return everything ready for db.py to save
Returns dict with:
- full_response : complete AI text
- vulnerabilities : list of parsed vuln dicts
- exploits : list of parsed exploit dicts
- risk_level : CRITICAL/HIGH/MEDIUM/LOW
- summary : short summary text
- raw_scan : original scan dump
"""
# ── Step 1: initial prompt ──────────────────
initial_prompt = f"""{SYSTEM_PROMPT}
TARGET: {target}
RECON DATA:
{raw_scan}
Analyze this target completely. Use [TOOL:] or [SEARCH:] if you need more information.
List all vulnerabilities, fixes, and suggest exploits where applicable.
"""
full_conversation = initial_prompt
final_response = ""
# ── Step 2: tool dispatch loop ──────────────
for loop in range(MAX_TOOL_LOOPS):
response = ask_ollama(full_conversation)
print(f"\n{'─'*60}")
print(f"[METATRON - Round {loop + 1}]")
print(f"{'─'*60}")
print(response)
final_response = response
# check for tool calls
tool_calls = extract_tool_calls(response)
if not tool_calls:
print("\n[*] No tool calls. Analysis complete.")
break
# run all tool calls
tool_results = run_tool_calls(tool_calls)
# feed results back into conversation
full_conversation = (
f"{full_conversation}\n\n"
f"[YOUR PREVIOUS RESPONSE]\n{response}\n\n"
f"[TOOL RESULTS]\n{tool_results}\n\n"
f"Continue your analysis with this new information. "
f"If analysis is complete, give the final RISK_LEVEL and SUMMARY."
)
# ── Step 3: parse structured output ─────────
vulnerabilities = parse_vulnerabilities(final_response)
exploits = parse_exploits(final_response)
risk_level = parse_risk_level(final_response)
summary = parse_summary(final_response)
print(f"\n[+] Parsed: {len(vulnerabilities)} vulns, {len(exploits)} exploits | Risk: {risk_level}")
return {
"full_response": final_response,
"vulnerabilities": vulnerabilities,
"exploits": exploits,
"risk_level": risk_level,
"summary": summary,
"raw_scan": raw_scan
}
# ─────────────────────────────────────────────
# QUICK TEST
# ─────────────────────────────────────────────
if __name__ == "__main__":
print("[ llm.py test — direct AI query ]\n")
# test if ollama is reachable
try:
r = requests.get("http://localhost:11434", timeout=5)
print("[+] Ollama is running.")
except Exception:
print("[!] Ollama not reachable. Run: ollama serve")
exit(1)
target = input("Test target: ").strip()
test_scan = f"Test recon for {target} — nmap and whois data would appear here."
result = analyse_target(target, test_scan)
print(f"\nRisk Level : {result['risk_level']}")
print(f"Summary : {result['summary']}")
print(f"Vulns found: {len(result['vulnerabilities'])}")
print(f"Exploits : {len(result['exploits'])}")