-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
422 lines (352 loc) · 13 KB
/
Copy pathclient.py
File metadata and controls
422 lines (352 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env python
"""
ModTester CLI Client
Penetration testing powered by LLM + MCP tools.
Usage:
python client.py # Interactive mode
python client.py "scan example.com" # Single query
Supports: AWS Bedrock (Claude) and Ollama-compatible APIs.
"""
import boto3
import httpx
import sys
import os
import json
import time
import threading
import argparse
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from botocore.config import Config
from tools.display import get_action_message
from config import (
MCP_SERVER_URL,
AI_BACKEND,
AWS_REGION,
BEDROCK_MODEL,
BEDROCK_FALLBACK_MODELS,
OLLAMA_URL,
OLLAMA_MODEL,
OLLAMA_API_KEY,
OLLAMA_IS_CLOUD,
SYSTEM_PROMPT,
SHOW_TOOL_NAMES,
)
# =============================================================================
# TERMINAL COLORS
# =============================================================================
CYAN = "\033[36m"
GREEN = "\033[32m"
YELLOW = "\033[33m"
RED = "\033[31m"
DIM = "\033[2m"
BOLD = "\033[1m"
RESET = "\033[0m"
MAGENTA = "\033[35m"
# =============================================================================
# STATUS DISPLAY
# =============================================================================
class StatusLine:
"""Animated spinner showing what the AI is doing."""
FRAMES = "⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"
def __init__(self, phase: str, message: str):
self.phase = phase
self.message = message
self._running = False
self._thread = None
self._start_time = 0
def start(self):
self._running = True
self._start_time = time.time()
self._thread = threading.Thread(target=self._spin, daemon=True)
self._thread.start()
def stop(self):
self._running = False
if self._thread:
self._thread.join(timeout=1)
# Clear the spinner line
sys.stdout.write("\r\033[K")
sys.stdout.flush()
def _spin(self):
i = 0
while self._running:
elapsed = time.time() - self._start_time
frame = self.FRAMES[i % len(self.FRAMES)]
phase_str = f"[{self.phase}] " if self.phase else ""
line = f"\r {CYAN}{frame} {phase_str}{self.message} [{elapsed:.0f}s]{RESET}"
sys.stdout.write(line)
sys.stdout.flush()
time.sleep(0.1)
i += 1
def print_status(phase: str, message: str, result: str = ""):
p = f"{CYAN}[{phase}]{RESET}" if phase else f"{CYAN}•{RESET}"
if result:
print(f" {p} {message} {DIM}→ {result}{RESET}", flush=True)
else:
print(f" {p} {message}", flush=True)
# =============================================================================
# MCP TOOL CALLING
# =============================================================================
def call_mcp_tool(tool_name: str, arguments: dict) -> str:
"""Call an MCP tool on the server and return the result as text."""
url = f"{MCP_SERVER_URL}/mcp/v1/tools/{tool_name}"
try:
r = httpx.post(url, json=arguments, timeout=600.0)
if r.status_code == 200:
data = r.json()
return data.get("result", json.dumps(data))
return f"Error {r.status_code}: {r.text[:500]}"
except httpx.TimeoutException:
return f"Timeout calling {tool_name}"
except Exception as e:
return f"Error: {e}"
def get_tools_from_server() -> list:
"""Fetch tool definitions from server."""
try:
r = httpx.get(f"{MCP_SERVER_URL}/tools", timeout=10.0)
if r.status_code == 200:
return r.json()
except Exception:
pass
return []
# =============================================================================
# AI BACKENDS
# =============================================================================
def _format_tools_bedrock(tools: list) -> dict:
"""Convert server tools to Bedrock toolConfig format."""
tool_specs = []
for t in tools:
tool_specs.append({
"toolSpec": {
"name": t["name"],
"description": t["description"],
"inputSchema": {"json": t["input_schema"]},
}
})
return {"tools": tool_specs}
def _format_tools_ollama(tools: list) -> list:
"""Convert server tools to Ollama/OpenAI format."""
result = []
for t in tools:
result.append({
"type": "function",
"function": {
"name": t["name"],
"description": t["description"],
"parameters": t["input_schema"],
},
})
return result
def chat_bedrock(user_message: str, conversation: list, tools: list) -> tuple:
"""Send a message via AWS Bedrock and handle tool calls."""
config = Config(region_name=AWS_REGION, retries={"max_attempts": 3, "mode": "adaptive"})
client = boto3.client("bedrock-runtime", config=config)
tc = _format_tools_bedrock(tools)
system = [{"text": SYSTEM_PROMPT}]
conversation.append({"role": "user", "content": [{"text": user_message}]})
sl = StatusLine("THINKING", "Analyzing request...")
sl.start()
model = BEDROCK_MODEL
try:
resp = client.converse(
modelId=model, system=system, messages=conversation,
toolConfig=tc, inferenceConfig={"maxTokens": 8192},
)
sl.stop()
except Exception as e:
sl.stop()
# Try fallbacks
for fallback in BEDROCK_FALLBACK_MODELS:
try:
resp = client.converse(
modelId=fallback, system=system, messages=conversation,
toolConfig=tc, inferenceConfig={"maxTokens": 8192},
)
model = fallback
break
except Exception:
continue
else:
return f"Error: {e}", conversation
# Process response loop (handle tool use)
while resp.get("stopReason") == "tool_use":
am = resp["output"]["message"]
conversation.append(am)
# Print any text before tool calls
for b in am["content"]:
if "text" in b:
t = b["text"].strip()
if t:
print(f"\n{t}", flush=True)
tr = []
for b in am["content"]:
if "toolUse" in b:
tu = b["toolUse"]
tn, ti, tid = tu["name"], tu["input"], tu["toolUseId"]
# Display action message (never tool names)
action_msg = get_action_message(tn, ti)
print_status("", action_msg)
sl = StatusLine("", action_msg)
sl.start()
result = call_mcp_tool(tn, ti)
sl.stop()
# Show truncated result
rp = result[:3000]
print(f"{DIM}{rp}{RESET}", flush=True)
if len(result) > 3000:
print(f"{DIM} ... ({len(result)} chars total){RESET}", flush=True)
tr.append({"toolResult": {"toolUseId": tid, "content": [{"text": result}]}})
conversation.append({"role": "user", "content": tr})
sl = StatusLine("ANALYZING", "Processing results...")
sl.start()
try:
resp = client.converse(
modelId=model, system=system, messages=conversation,
toolConfig=tc, inferenceConfig={"maxTokens": 8192},
)
sl.stop()
except Exception as e:
sl.stop()
return f"Error during analysis: {e}", conversation
# Extract final text
fm = resp["output"]["message"]
conversation.append(fm)
rt = ""
for b in fm["content"]:
if "text" in b:
rt += b["text"]
return rt, conversation
def chat_ollama(user_message: str, conversation: list, tools: list) -> tuple:
"""Send a message via Ollama-compatible API and handle tool calls."""
headers = {"Content-Type": "application/json"}
if OLLAMA_API_KEY:
headers["Authorization"] = f"Bearer {OLLAMA_API_KEY}"
ollama_tools = _format_tools_ollama(tools)
om = [{"role": "system", "content": SYSTEM_PROMPT}]
# Convert conversation to Ollama format
for msg in conversation:
if msg["role"] == "user":
text = ""
for c in msg.get("content", []):
if isinstance(c, dict) and "text" in c:
text += c["text"]
elif isinstance(c, str):
text = c
if text:
om.append({"role": "user", "content": text})
elif msg["role"] == "assistant":
text = ""
for c in msg.get("content", []):
if isinstance(c, dict) and "text" in c:
text += c["text"]
elif isinstance(c, str):
text = c
if text:
om.append({"role": "assistant", "content": text})
om.append({"role": "user", "content": user_message})
sl = StatusLine("THINKING", "Analyzing request...")
sl.start()
try:
r = httpx.post(
f"{OLLAMA_URL}/api/chat", headers=headers,
json={"model": OLLAMA_MODEL, "messages": om, "tools": ollama_tools,
"stream": False, "options": {"temperature": 0.1}},
timeout=600.0,
)
sl.stop()
if r.status_code != 200:
return f"Ollama error: {r.text}", conversation
result = r.json()
message = result.get("message", {})
except Exception as e:
sl.stop()
return f"Error: {e}", conversation
conversation.append({"role": "user", "content": [{"text": user_message}]})
tc = message.get("tool_calls", [])
while tc:
trt = []
for t in tc:
fn = t.get("function", {})
tn, ta = fn.get("name"), fn.get("arguments", {})
action_msg = get_action_message(tn, ta)
print_status("", action_msg)
sl = StatusLine("", action_msg)
sl.start()
rt = call_mcp_tool(tn, ta)
sl.stop()
print(f"{DIM}{rt[:3000]}{RESET}", flush=True)
trt.append(f"[{tn}]: {rt}")
om.append({"role": "assistant", "content": message.get("content", "")})
om.append({"role": "user", "content": "Tool results:\n" + "\n".join(trt)})
sl = StatusLine("ANALYZING", "Processing results...")
sl.start()
try:
r = httpx.post(
f"{OLLAMA_URL}/api/chat", headers=headers,
json={"model": OLLAMA_MODEL, "messages": om, "tools": ollama_tools,
"stream": False, "options": {"temperature": 0.1}},
timeout=600.0,
)
except Exception:
sl.stop()
return "Ollama timed out.", conversation
sl.stop()
result = r.json()
message = result.get("message", {})
tc = message.get("tool_calls", [])
ft = message.get("content", "")
conversation.append({"role": "assistant", "content": [{"text": ft}]})
return ft, conversation
# =============================================================================
# MAIN LOOP
# =============================================================================
def main():
parser = argparse.ArgumentParser(description="MCP Offensive Security Demo")
parser.add_argument("query", nargs="*", help="Single query to execute")
args = parser.parse_args()
# Check server
try:
r = httpx.get(f"{MCP_SERVER_URL}/health", timeout=5.0)
if r.status_code != 200:
print(f"{RED}Server not healthy at {MCP_SERVER_URL}{RESET}")
sys.exit(1)
except Exception:
print(f"{RED}Cannot connect to server at {MCP_SERVER_URL}{RESET}")
print(f"Start the server first: python server.py")
sys.exit(1)
tools = get_tools_from_server()
if not tools:
print(f"{RED}No tools loaded from server{RESET}")
sys.exit(1)
chat_fn = chat_bedrock if AI_BACKEND == "bedrock" else chat_ollama
conversation = []
print(f"\n{BOLD}ModTester{RESET}")
print(f"{DIM}Backend: {AI_BACKEND.upper()} | Tools: {len(tools)} | Server: {MCP_SERVER_URL}{RESET}")
print(f"{DIM}Type 'quit' to exit{RESET}\n")
# Single query mode
if args.query:
query = " ".join(args.query)
response, conversation = chat_fn(query, conversation, tools)
print(f"\n{response}")
return
# Interactive mode
while True:
try:
user_input = input(f"{GREEN}ModTester ▶{RESET} ").strip()
except (KeyboardInterrupt, EOFError):
print(f"\n{DIM}Goodbye.{RESET}")
break
if not user_input:
continue
if user_input.lower() in ("quit", "exit", "q"):
print(f"{DIM}Goodbye.{RESET}")
break
if user_input.lower() == "clear":
conversation = []
print(f"{DIM}Conversation cleared.{RESET}")
continue
response, conversation = chat_fn(user_input, conversation, tools)
print(f"\n{response}\n")
if __name__ == "__main__":
main()