Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /SHAdd0WTAka_Zen-Ai-Pentest /mcp /metasploit /server.py
| """ | |
| Metasploit MCP – Remote Exploit Framework Connector. | |
| Verbindet Hermes mit Metasploit RPC (msfrpc). | |
| Ermöglicht: Exploit-Suche, Ausführung, Session-Management. | |
| """ | |
| import json | |
| import os | |
| import socket | |
| import ssl | |
| from typing import Any | |
| from mcp.server.fastmcp import FastMCP | |
| mcp = FastMCP("metasploit", instructions="Metasploit Framework RPC. Search/run exploits, manage sessions.") | |
| MSF_HOST = os.environ.get("MSF_HOST", "127.0.0.1") | |
| MSF_PORT = int(os.environ.get("MSF_PORT", "55553")) | |
| MSF_TOKEN = os.environ.get("MSF_TOKEN", "") | |
| class MsfRpc: | |
| def __init__(self, host: str = MSF_HOST, port: int = MSF_PORT, token: str = MSF_TOKEN): | |
| self.host = host | |
| self.port = port | |
| self.token = token | |
| self.sock = None | |
| self._auth() | |
| def _connect(self): | |
| self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| self.sock.settimeout(30) | |
| self.sock.connect((self.host, self.port)) | |
| def _send_recv(self, data: dict) -> dict: | |
| if not self.sock: | |
| self._connect() | |
| payload = json.dumps(data) + "\n" | |
| self.sock.sendall(payload.encode()) | |
| resp = b"" | |
| while True: | |
| chunk = self.sock.recv(65536) | |
| if not chunk: | |
| break | |
| resp += chunk | |
| if b"\n" in resp: | |
| line, _ = resp.split(b"\n", 1) | |
| return json.loads(line) | |
| return {} | |
| def _auth(self): | |
| self._connect() | |
| resp = self._send_recv( | |
| { | |
| "method": "auth.login", | |
| "params": [self.token, ""], | |
| "id": 1, | |
| } | |
| ) | |
| if resp.get("result") != "success": | |
| self.sock = None | |
| def call(self, method: str, params: list | None = None): | |
| return self._send_recv( | |
| { | |
| "method": f"msf.{method}", | |
| "params": params or [], | |
| "id": int.from_bytes(os.urandom(4), "big"), | |
| } | |
| ) | |
| def close(self): | |
| if self.sock: | |
| self.sock.close() | |
| self.sock = None | |
| def _get_msf() -> MsfRpc | None: | |
| try: | |
| return MsfRpc() | |
| except Exception: | |
| return None | |
| def msf_status() -> str: | |
| """Check Metasploit RPC connection status.""" | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Cannot connect to Metasploit RPC"}) | |
| try: | |
| version = msf.call("version") | |
| msf.close() | |
| return json.dumps({"status": "ok", **version}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def module_search( | |
| search_term: str, | |
| module_type: str = "", | |
| ) -> str: | |
| """Search Metasploit modules. | |
| Args: | |
| search_term: Search keyword (e.g. "apache", "eternalblue") | |
| module_type: Filter by type (exploit, auxiliary, payload, post) | |
| """ | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| params = [search_term] | |
| if module_type: | |
| result = msf.call("module.search", [module_type, search_term]) | |
| else: | |
| result = msf.call("module.search", params) | |
| msf.close() | |
| return json.dumps({"status": "ok", "results": result.get("data", []), "count": len(result.get("data", []))}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def module_info( | |
| module_type: str, | |
| module_name: str, | |
| ) -> str: | |
| """Get detailed info about a Metasploit module. | |
| Args: | |
| module_type: Module type (exploit, auxiliary, payload, post, encoder, nop) | |
| module_name: Full module path (e.g. "exploit/multi/http/apache_normalize_path) | |
| """ | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| result = msf.call("module.info", [module_type, module_name]) | |
| msf.close() | |
| return json.dumps({"status": "ok", "info": result}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def module_execute( | |
| module_type: str, | |
| module_name: str, | |
| options: dict[str, Any] | None = None, | |
| payload: str = "", | |
| ) -> str: | |
| """Execute a Metasploit module (exploit/auxiliary). | |
| Args: | |
| module_type: Module type (exploit, auxiliary) | |
| module_name: Full module path | |
| options: Module options dict (RHOSTS, RPORT, etc.) | |
| payload: Payload to use (for exploits) | |
| """ | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| opts = options or {} | |
| result = msf.call("module.execute", [module_type, module_name, opts]) | |
| msf.close() | |
| return json.dumps({"status": "ok", "result": result}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def session_list() -> str: | |
| """List active Metasploit sessions.""" | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| result = msf.call("session.list") | |
| msf.close() | |
| sessions = [] | |
| if isinstance(result, dict): | |
| for sid, info in result.items(): | |
| sessions.append( | |
| { | |
| "id": sid, | |
| "type": info.get("type", ""), | |
| "target": info.get("target_host", ""), | |
| "platform": info.get("platform", ""), | |
| "via_exploit": info.get("via_exploit", ""), | |
| } | |
| ) | |
| return json.dumps({"status": "ok", "sessions": sessions, "count": len(sessions)}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def session_shell( | |
| session_id: int, | |
| command: str, | |
| ) -> str: | |
| """Run a shell command in an active Meterpreter session. | |
| Args: | |
| session_id: Session ID | |
| command: Shell command to run | |
| """ | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| result = msf.call("session.shell_write", [session_id, command + "\n"]) | |
| import time | |
| time.sleep(1) | |
| output = msf.call("session.shell_read", [session_id]) | |
| msf.close() | |
| return json.dumps({"status": "ok", "session_id": session_id, "output": output}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def session_stop( | |
| session_id: int, | |
| ) -> str: | |
| """Stop/kill a Metasploit session. | |
| Args: | |
| session_id: Session ID to kill | |
| """ | |
| msf = _get_msf() | |
| if not msf: | |
| return json.dumps({"status": "error", "error": "Metasploit RPC unavailable"}) | |
| try: | |
| result = msf.call("session.stop", [session_id]) | |
| msf.close() | |
| return json.dumps({"status": "ok", "result": result}) | |
| except Exception as e: | |
| return json.dumps({"status": "error", "error": str(e)}) | |
| def main(): | |
| mcp.run() | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 7.3 kB
- Xet hash:
- 99376ab9e6d417d049c4359dcf05afb9b6146f27f079c982b0eb5486e894df3c
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.