MCPcopy Create free account
hub / github.com/GH05TCREW/MetasploitMCP / terminate_session

Function terminate_session

MetasploitMCP.py:1589–1632  ·  view source on GitHub ↗

Forcefully terminate a Metasploit session using the session.stop() method. Args: session_id: ID of the session to terminate. Returns: Dictionary with status and result message.

(session_id: int)

Source from the content-addressed store, hash-verified

1587
1588@mcp.tool()
1589async def terminate_session(session_id: int) -> Dict[str, Any]:
1590 """
1591 Forcefully terminate a Metasploit session using the session.stop() method.
1592
1593 Args:
1594 session_id: ID of the session to terminate.
1595
1596 Returns:
1597 Dictionary with status and result message.
1598 """
1599 client = get_msf_client()
1600 session_id_str = str(session_id)
1601 logger.info(f"Terminating session {session_id}")
1602
1603 try:
1604 # Check if session exists
1605 current_sessions = await asyncio.to_thread(lambda: client.sessions.list)
1606 if session_id_str not in current_sessions:
1607 logger.error(f"Session {session_id} not found.")
1608 return {"status": "error", "message": f"Session {session_id} not found."}
1609
1610 # Get a handle to the session
1611 session = await asyncio.to_thread(lambda: client.sessions.session(session_id_str))
1612
1613 # Stop the session
1614 await asyncio.to_thread(lambda: session.stop())
1615
1616 # Verify termination
1617 await asyncio.sleep(1.0) # Give MSF time to process termination
1618 current_sessions_after = await asyncio.to_thread(lambda: client.sessions.list)
1619
1620 if session_id_str not in current_sessions_after:
1621 logger.info(f"Successfully terminated session {session_id}")
1622 return {"status": "success", "message": f"Session {session_id} terminated successfully."}
1623 else:
1624 logger.warning(f"Session {session_id} still appears in the sessions list after termination attempt.")
1625 return {"status": "warning", "message": f"Session {session_id} may not have been terminated properly."}
1626
1627 except MsfRpcError as e:
1628 logger.error(f"MsfRpcError terminating session {session_id}: {e}")
1629 return {"status": "error", "message": f"Error terminating session {session_id}: {e}"}
1630 except Exception as e:
1631 logger.exception(f"Unexpected error terminating session {session_id}")
1632 return {"status": "error", "message": f"Unexpected error terminating session {session_id}: {e}"}
1633
1634# --- FastAPI Application Setup ---
1635

Callers 1

Calls 1

get_msf_clientFunction · 0.85

Tested by 1