mcp: wait for msfrpcd readiness and log msfrpcd output for debugging

This commit is contained in:
giveen
2026-01-14 14:07:41 -07:00
parent bf283ab586
commit 83681d5f81
2 changed files with 18 additions and 5 deletions

View File

@@ -124,8 +124,18 @@ class MetasploitAdapter:
# Start reader to capture msfrpcd logs
loop = asyncio.get_running_loop()
loop.create_task(self._capture_msfrpcd_output())
# Give msfrpcd a moment to start
await asyncio.sleep(0.5)
# Poll the msfrpcd TCP socket until it's accepting connections or timeout
import socket
deadline = asyncio.get_event_loop().time() + 10.0
while asyncio.get_event_loop().time() < deadline:
try:
with socket.create_connection((msf_server, msf_port), timeout=1):
return
except Exception:
await asyncio.sleep(0.5)
# If we fallthrough, msfrpcd didn't become ready in time
return
except Exception:
return

View File

@@ -151,12 +151,15 @@ if [ "${LAUNCH_METASPLOIT_MCP,,}" = "true" ] && [ -n "${MSF_PASSWORD:-}" ]; then
# interface and does not require root privileges on modern systems for ephemeral ports.
msfrpcd_cmd=$(command -v msfrpcd || true)
if [ -n "$msfrpcd_cmd" ]; then
LOG_DIR="loot/artifacts"
mkdir -p "$LOG_DIR"
MSF_LOG="$LOG_DIR/metasploit_msfrpcd.log"
if [ "${MSF_SSL,,}" = "true" ] || [ "${MSF_SSL}" = "1" ]; then
"$msfrpcd_cmd" -U "$MSF_USER" -P "$MSF_PASSWORD" -a "$MSF_SERVER" -p "$MSF_PORT" -S &>/dev/null &
"$msfrpcd_cmd" -U "$MSF_USER" -P "$MSF_PASSWORD" -a "$MSF_SERVER" -p "$MSF_PORT" -S >"$MSF_LOG" 2>&1 &
else
"$msfrpcd_cmd" -U "$MSF_USER" -P "$MSF_PASSWORD" -a "$MSF_SERVER" -p "$MSF_PORT" &>/dev/null &
"$msfrpcd_cmd" -U "$MSF_USER" -P "$MSF_PASSWORD" -a "$MSF_SERVER" -p "$MSF_PORT" >"$MSF_LOG" 2>&1 &
fi
echo "msfrpcd started (check with: ss -ltn | grep $MSF_PORT)"
echo "msfrpcd started (logs: $MSF_LOG)"
else
echo "msfrpcd not found; please install Metasploit Framework to enable Metasploit RPC."
fi