Repository navigation
Expand file tree
/
Copy pathkali_api_server.py
More file actions
executable file
·705 lines (601 loc) · 24.5 KB
/
Copy pathkali_api_server.py
File metadata and controls
executable file
·705 lines (601 loc) · 24.5 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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#!/usr/bin/env python3
"""
Kali Linux Tools API Server
This Flask application provides API endpoints for executing various Kali Linux
security tools. It serves as the backend execution engine for the MCP server.
Usage:
python3 kali_api_server.py [--debug]
Requirements:
- Python 3.8+
- Flask
- Subprocess module
"""
import argparse
import json
import logging
import os
import subprocess
import sys
import traceback
from typing import Dict, Any
from flask import Flask, request, jsonify
from debug_utils import configure_debug_mode, debug_endpoint, log_system_info
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.StreamHandler(sys.stdout)
]
)
logger = logging.getLogger(__name__)
# Configuration
API_PORT = int(os.environ.get("API_PORT", 5000))
DEBUG_MODE = os.environ.get("DEBUG_MODE", "0").lower() in ("1", "true", "yes", "y")
app = Flask(__name__)
def execute_command(command: str) -> Dict[str, Any]:
"""
Execute a shell command and return the result
Args:
command: The command to execute
Returns:
A dictionary containing the stdout, stderr, and return code
"""
logger.info(f"Executing command: {command}")
try:
process = subprocess.Popen(
command,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
stdout, stderr = process.communicate(timeout=300)
return_code = process.returncode
# Debug output for command execution
if DEBUG_MODE:
logger.debug(f"Command output - stdout: {stdout}")
logger.debug(f"Command output - stderr: {stderr}")
logger.debug(f"Command output - return code: {return_code}")
return {
"stdout": stdout,
"stderr": stderr,
"return_code": return_code,
"success": return_code == 0
}
except subprocess.TimeoutExpired:
logger.error("Command execution timed out after 300 seconds")
return {
"stdout": "",
"stderr": "Command execution timed out after 300 seconds",
"return_code": -1,
"success": False
}
except Exception as e:
logger.error(f"Error executing command: {str(e)}")
logger.error(traceback.format_exc())
return {
"stdout": "",
"stderr": f"Error executing command: {str(e)}",
"return_code": -1,
"success": False
}
@app.route("/api/tools/nmap", methods=["POST"])
@debug_endpoint
def nmap():
"""Execute nmap scan with the provided parameters."""
try:
params = request.json
target = params.get("target", "")
scan_type = params.get("scan_type", "-sV")
ports = params.get("ports", "")
additional_args = params.get("additional_args", "")
if not target:
logger.warning("Nmap called without target parameter")
return jsonify({
"error": "Target parameter is required"
}), 400
# Validate inputs to prevent command injection
if not all(c.isalnum() or c in ".-_" for c in target):
logger.warning(f"Potential command injection attempt in nmap target: {target}")
return jsonify({
"error": "Invalid target parameter"
}), 400
command = f"nmap {scan_type}"
if ports:
if not all(c.isdigit() or c in ",-" for c in ports):
logger.warning(f"Potential command injection attempt in nmap ports: {ports}")
return jsonify({
"error": "Invalid ports parameter"
}), 400
command += f" -p {ports}"
if additional_args:
# Basic validation for additional args - more sophisticated validation would be better
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in nmap additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
command += f" {target}"
logger.debug(f"Executing nmap command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in nmap endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/gobuster", methods=["POST"])
@debug_endpoint
def gobuster():
"""Execute gobuster with the provided parameters."""
try:
params = request.json
url = params.get("url", "")
mode = params.get("mode", "dir")
wordlist = params.get("wordlist", "/usr/share/wordlists/dirb/common.txt")
additional_args = params.get("additional_args", "")
if not url:
logger.warning("Gobuster called without URL parameter")
return jsonify({
"error": "URL parameter is required"
}), 400
# Validate mode
if mode not in ["dir", "dns", "fuzz", "vhost"]:
logger.warning(f"Invalid gobuster mode: {mode}")
return jsonify({
"error": f"Invalid mode: {mode}. Must be one of: dir, dns, fuzz, vhost"
}), 400
command = f"gobuster {mode} -u {url} -w {wordlist}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in gobuster additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
logger.debug(f"Executing gobuster command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in gobuster endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/dirb", methods=["POST"])
@debug_endpoint
def dirb():
"""Execute dirb with the provided parameters."""
try:
params = request.json
url = params.get("url", "")
wordlist = params.get("wordlist", "/usr/share/wordlists/dirb/common.txt")
additional_args = params.get("additional_args", "")
if not url:
logger.warning("Dirb called without URL parameter")
return jsonify({
"error": "URL parameter is required"
}), 400
command = f"dirb {url} {wordlist}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in dirb additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
logger.debug(f"Executing dirb command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in dirb endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/nikto", methods=["POST"])
@debug_endpoint
def nikto():
"""Execute nikto with the provided parameters."""
try:
params = request.json
target = params.get("target", "")
additional_args = params.get("additional_args", "")
if not target:
logger.warning("Nikto called without target parameter")
return jsonify({
"error": "Target parameter is required"
}), 400
command = f"nikto -h {target}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in nikto additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
logger.debug(f"Executing nikto command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in nikto endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/sqlmap", methods=["POST"])
@debug_endpoint
def sqlmap():
"""Execute sqlmap with the provided parameters."""
try:
params = request.json
url = params.get("url", "")
data = params.get("data", "")
additional_args = params.get("additional_args", "")
if not url:
logger.warning("SQLMap called without URL parameter")
return jsonify({
"error": "URL parameter is required"
}), 400
command = f"sqlmap -u {url}"
if data:
command += f" --data=\"{data}\""
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in sqlmap additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
logger.debug(f"Executing sqlmap command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in sqlmap endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/metasploit", methods=["POST"])
@debug_endpoint
def metasploit():
"""Execute metasploit module with the provided parameters."""
try:
params = request.json
module = params.get("module", "")
options = params.get("options", {})
if not module:
logger.warning("Metasploit called without module parameter")
return jsonify({
"error": "Module parameter is required"
}), 400
# Validate module name to prevent command injection
if not all(c.isalnum() or c in "/._-" for c in module):
logger.warning(f"Potential command injection attempt in metasploit module: {module}")
return jsonify({
"error": "Invalid module parameter"
}), 400
# Format options for Metasploit
options_str = ""
for key, value in options.items():
options_str += f" {key}={value}"
# Create an MSF resource script
resource_content = f"use {module}\n"
for key, value in options.items():
resource_content += f"set {key} {value}\n"
resource_content += "exploit\n"
# Save resource script to a temporary file
resource_file = "/tmp/mcp_msf_resource.rc"
with open(resource_file, "w") as f:
f.write(resource_content)
if DEBUG_MODE:
logger.debug(f"Created MSF resource script at {resource_file}: {resource_content}")
command = f"msfconsole -q -r {resource_file}"
logger.debug(f"Executing metasploit command: {command}")
result = execute_command(command)
# Clean up the temporary file
try:
os.remove(resource_file)
except Exception as e:
logger.warning(f"Error removing temporary resource file: {str(e)}")
return jsonify(result)
except Exception as e:
logger.error(f"Error in metasploit endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/hydra", methods=["POST"])
@debug_endpoint
def hydra():
"""Execute hydra with the provided parameters."""
try:
params = request.json
target = params.get("target", "")
service = params.get("service", "")
username = params.get("username", "")
username_file = params.get("username_file", "")
password = params.get("password", "")
password_file = params.get("password_file", "")
additional_args = params.get("additional_args", "")
if not target or not service:
logger.warning("Hydra called without target or service parameter")
return jsonify({
"error": "Target and service parameters are required"
}), 400
if not (username or username_file) or not (password or password_file):
logger.warning("Hydra called without username/password parameters")
return jsonify({
"error": "Username/username_file and password/password_file are required"
}), 400
# Basic target validation
if not all(c.isalnum() or c in ".-_:" for c in target):
logger.warning(f"Potential command injection attempt in hydra target: {target}")
return jsonify({
"error": "Invalid target parameter"
}), 400
# Basic service validation
if not all(c.isalnum() or c in "._-" for c in service):
logger.warning(f"Potential command injection attempt in hydra service: {service}")
return jsonify({
"error": "Invalid service parameter"
}), 400
command = f"hydra -t 4"
if username:
command += f" -l {username}"
elif username_file:
command += f" -L {username_file}"
if password:
command += f" -p {password}"
elif password_file:
command += f" -P {password_file}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in hydra additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
command += f" {target} {service}"
logger.debug(f"Executing hydra command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in hydra endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/john", methods=["POST"])
@debug_endpoint
def john():
"""Execute john with the provided parameters."""
try:
params = request.json
hash_file = params.get("hash_file", "")
wordlist = params.get("wordlist", "")
format_type = params.get("format", "")
additional_args = params.get("additional_args", "")
if not hash_file:
logger.warning("John called without hash_file parameter")
return jsonify({
"error": "Hash file parameter is required"
}), 400
command = f"john"
if format_type:
# Validate format type
if not all(c.isalnum() or c in "-_" for c in format_type):
logger.warning(f"Potential command injection in john format: {format_type}")
return jsonify({
"error": "Invalid format parameter"
}), 400
command += f" --format={format_type}"
if wordlist:
command += f" --wordlist={wordlist}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in john additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
command += f" {hash_file}"
logger.debug(f"Executing john command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in john endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/wpscan", methods=["POST"])
@debug_endpoint
def wpscan():
"""Execute wpscan with the provided parameters."""
try:
params = request.json
url = params.get("url", "")
additional_args = params.get("additional_args", "")
if not url:
logger.warning("WPScan called without URL parameter")
return jsonify({
"error": "URL parameter is required"
}), 400
command = f"wpscan --url {url}"
if additional_args:
# Basic validation for additional args
if ";" in additional_args or "&" in additional_args or "|" in additional_args:
logger.warning(f"Potential command injection in wpscan additional args: {additional_args}")
return jsonify({
"error": "Invalid additional_args parameter"
}), 400
command += f" {additional_args}"
logger.debug(f"Executing wpscan command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in wpscan endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/api/tools/enum4linux", methods=["POST"])
@debug_endpoint
def enum4linux():
"""Execute enum4linux with the provided parameters."""
try:
params = request.json
target = params.get("target", "")
additional_args = params.get("additional_args", "-a")
if not target:
logger.warning("Enum4linux called without target parameter")
return jsonify({
"error": "Target parameter is required"
}), 400
# Basic target validation
if not all(c.isalnum() or c in ".-_:" for c in target):
logger.warning(f"Potential command injection attempt in enum4linux target: {target}")
return jsonify({
"error": "Invalid target parameter"
}), 400
command = f"enum4linux {additional_args} {target}"
logger.debug(f"Executing enum4linux command: {command}")
result = execute_command(command)
return jsonify(result)
except Exception as e:
logger.error(f"Error in enum4linux endpoint: {str(e)}")
logger.error(traceback.format_exc())
return jsonify({
"error": f"Server error: {str(e)}"
}), 500
@app.route("/debug/tool-test", methods=["POST"])
def debug_tool_test():
"""Test if a tool is installed and working."""
if not DEBUG_MODE:
return jsonify({
"status": "error",
"message": "Debug mode is not enabled"
}), 403
tool = request.json.get("tool", "")
if not tool:
return jsonify({
"status": "error",
"message": "Tool parameter is required"
}), 400
# Simple version check for the tool
command = f"{tool} --version"
result = execute_command(command)
if result["success"]:
return jsonify({
"status": "success",
"tool": tool,
"installed": True,
"version": result["stdout"].strip(),
"command_output": result
})
else:
# Try help option if version fails
command = f"{tool} --help"
result = execute_command(command)
return jsonify({
"status": "warning",
"tool": tool,
"installed": result["success"],
"help_output": result["stdout"] if result["success"] else None,
"error": result["stderr"] if not result["success"] else None,
"command_output": result
})
@app.route("/debug/command", methods=["POST"])
def debug_command():
"""Execute a command for debugging purposes."""
if not DEBUG_MODE:
return jsonify({
"status": "error",
"message": "Debug mode is not enabled"
}), 403
command = request.json.get("command", "")
if not command:
return jsonify({
"status": "error",
"message": "Command parameter is required"
}), 400
# Restrict commands to those that are safe
if any(unsafe in command for unsafe in [";", "|", "&&", "||", ">", "<"]):
return jsonify({
"status": "error",
"message": "Unsafe command rejected"
}), 400
# Only allow certain commands
allowed_commands = ["ls", "ps", "id", "whoami", "which", "whereis", "cat", "head", "tail"]
if not any(command.startswith(cmd) for cmd in allowed_commands):
return jsonify({
"status": "error",
"message": f"Command not allowed. Allowed commands: {', '.join(allowed_commands)}"
}), 400
result = execute_command(command)
return jsonify({
"status": "success",
"command": command,
"output": result
})
# Health check endpoint
@app.route("/health", methods=["GET"])
def health_check():
"""Health check endpoint."""
# Check if essential tools are installed
essential_tools = ["nmap", "gobuster", "dirb", "nikto"]
tools_status = {}
for tool in essential_tools:
try:
result = execute_command(f"which {tool}")
tools_status[tool] = result["success"]
except:
tools_status[tool] = False
all_essential_tools_available = all(tools_status.values())
return jsonify({
"status": "healthy",
"message": "Kali Linux Tools API Server is running",
"tools_status": tools_status,
"all_essential_tools_available": all_essential_tools_available
})
@app.route("/mcp/capabilities", methods=["GET"])
def get_capabilities():
# Return tool capabilities similar to our existing MCP server
pass
@app.route("/mcp/tools/kali_tools/<tool_name>", methods=["POST"])
def execute_tool(tool_name):
# Direct tool execution without going through the API server
pass
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(description="Run the Kali Linux API Server")
parser.add_argument("--debug", action="store_true", help="Enable debug mode")
parser.add_argument("--port", type=int, default=API_PORT, help=f"Port for the API server (default: {API_PORT})")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
# Set configuration from command line arguments
if args.debug:
DEBUG_MODE = True
os.environ["DEBUG_MODE"] = "1"
os.environ["DEBUG_LEVEL"] = "DEBUG"
logger.setLevel(logging.DEBUG)
if args.port != API_PORT:
API_PORT = args.port
# Configure debug mode if enabled
if DEBUG_MODE:
log_system_info()
app = configure_debug_mode(app, is_mcp_server=False)
logger.info("Debug mode enabled - additional endpoints available at /debug/*")
logger.info(f"Starting Kali Linux Tools API Server on port {API_PORT}")
app.run(host="0.0.0.0", port=API_PORT, debug=DEBUG_MODE)