Class: Msf::MCP::Metasploit::JsonRpcClient
- Inherits:
-
Object
- Object
- Msf::MCP::Metasploit::JsonRpcClient
- Defined in:
- lib/msf/core/mcp/metasploit/jsonrpc_client.rb
Overview
JSON-RPC 2.0 client for Metasploit Framework Implements bearer token authentication for the Metasploit JSON-RPC API Endpoint: /api/v1/json-rpc (default port 8081) See: lib/msf/core/rpc/json/ in Metasploit Framework repository
Constant Summary collapse
- DEFAULT_ENDPOINT =
'/api/v1/json-rpc'
Instance Method Summary collapse
-
#authenticate(_user, _password) ⇒ String
No-op for JSON-RPC: authentication uses a pre-configured bearer token.
-
#call_api(method, args = []) ⇒ Hash
Call Metasploit API method using JSON-RPC 2.0 format.
-
#db_creds(options = {}) ⇒ Hash
Get credentials from database.
-
#db_hosts(options = {}) ⇒ Hash
Get hosts from database.
-
#db_loot(options = {}) ⇒ Hash
Get loot from database.
-
#db_notes(options = {}) ⇒ Hash
Get notes from database.
-
#db_services(options = {}) ⇒ Hash
Get services from database.
-
#db_vulns(options = {}) ⇒ Hash
Get vulnerabilities from database.
-
#initialize(host:, port:, endpoint: DEFAULT_ENDPOINT, token:, ssl: true) ⇒ JsonRpcClient
constructor
Initialize JSON-RPC client.
-
#module_check(type, name, options = {}) ⇒ Hash
Run the check method of a Metasploit module.
-
#module_execute(type, name, options = {}) ⇒ Hash
Execute a Metasploit module.
-
#module_info(type, name) ⇒ Hash
Get module information.
-
#module_results(uuid) ⇒ Hash
Get module execution results by UUID.
-
#running_stats ⇒ Hash
Get currently running module stats (waiting / running / results).
-
#search_modules(query) ⇒ Array<Hash>
Search for Metasploit modules.
-
#session_list ⇒ Hash
List active sessions.
-
#session_read(session_id) ⇒ Hash
Read buffered output from an interactive session (meterpreter, DB, SMB).
-
#session_stop(session_id) ⇒ Hash
Stop (kill) an active session.
-
#session_write(session_id, data) ⇒ Hash
Send input to an interactive session.
-
#shutdown ⇒ Object
Shutdown client.
Constructor Details
#initialize(host:, port:, endpoint: DEFAULT_ENDPOINT, token:, ssl: true) ⇒ JsonRpcClient
Initialize JSON-RPC client
21 22 23 24 25 26 27 28 29 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 21 def initialize(host:, port:, endpoint: DEFAULT_ENDPOINT, token:, ssl: true) @host = host @port = port @endpoint = endpoint @token = token @request_id = 0 @http = nil @ssl = ssl end |
Instance Method Details
#authenticate(_user, _password) ⇒ String
No-op for JSON-RPC: authentication uses a pre-configured bearer token. This method exists so that JsonRpcClient satisfies the same interface as MessagePackClient, allowing the Client facade to delegate uniformly.
38 39 40 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 38 def authenticate(_user, _password) @token end |
#call_api(method, args = []) ⇒ Hash
Call Metasploit API method using JSON-RPC 2.0 format
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 50 def call_api(method, args = []) raise ArgumentError, "args must be an Array, got #{args.class}" unless args.is_a?(Array) @request_id += 1 # Build JSON-RPC 2.0 request as a hash request_body = { jsonrpc: '2.0', method: method, params: args, id: @request_id } # Send HTTP request response = send_request(request_body) # Check for JSON-RPC error if response['error'] error_msg = response['error']['message'] || 'Unknown error' raise APIError, error_msg end response['result'] end |
#db_creds(options = {}) ⇒ Hash
Get credentials from database
121 122 123 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 121 def db_creds( = {}) call_api('db.creds', []) end |
#db_hosts(options = {}) ⇒ Hash
Get hosts from database
93 94 95 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 93 def db_hosts( = {}) call_api('db.hosts', []) end |
#db_loot(options = {}) ⇒ Hash
Get loot from database
128 129 130 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 128 def db_loot( = {}) call_api('db.loots', []) end |
#db_notes(options = {}) ⇒ Hash
Get notes from database
114 115 116 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 114 def db_notes( = {}) call_api('db.notes', []) end |
#db_services(options = {}) ⇒ Hash
Get services from database
100 101 102 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 100 def db_services( = {}) call_api('db.services', []) end |
#db_vulns(options = {}) ⇒ Hash
Get vulnerabilities from database
107 108 109 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 107 def db_vulns( = {}) call_api('db.vulns', []) end |
#module_check(type, name, options = {}) ⇒ Hash
Run the check method of a Metasploit module
146 147 148 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 146 def module_check(type, name, = {}) call_api('module.check', [type, name, ]) end |
#module_execute(type, name, options = {}) ⇒ Hash
Execute a Metasploit module
137 138 139 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 137 def module_execute(type, name, = {}) call_api('module.execute', [type, name, ]) end |
#module_info(type, name) ⇒ Hash
Get module information
86 87 88 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 86 def module_info(type, name) call_api('module.info', [type, name]) end |
#module_results(uuid) ⇒ Hash
Get module execution results by UUID
153 154 155 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 153 def module_results(uuid) call_api('module.results', [uuid]) end |
#running_stats ⇒ Hash
Get currently running module stats (waiting / running / results)
159 160 161 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 159 def running_stats call_api('module.running_stats', []) end |
#search_modules(query) ⇒ Array<Hash>
Search for Metasploit modules
78 79 80 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 78 def search_modules(query) call_api('module.search', [query]) end |
#session_list ⇒ Hash
List active sessions
165 166 167 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 165 def session_list call_api('session.list', []) end |
#session_read(session_id) ⇒ Hash
Read buffered output from an interactive session (meterpreter, DB, SMB)
179 180 181 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 179 def session_read(session_id) call_api('session.interactive_read', [session_id]) end |
#session_stop(session_id) ⇒ Hash
Stop (kill) an active session
172 173 174 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 172 def session_stop(session_id) call_api('session.stop', [session_id]) end |
#session_write(session_id, data) ⇒ Hash
Send input to an interactive session
187 188 189 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 187 def session_write(session_id, data) call_api('session.interactive_write', [session_id, data]) end |
#shutdown ⇒ Object
Shutdown client
192 193 194 195 |
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 192 def shutdown @http&.finish if @http&.started? @http = nil end |