Class: Msf::MCP::Metasploit::JsonRpcClient

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(host:, port:, endpoint: DEFAULT_ENDPOINT, token:, ssl: true) ⇒ JsonRpcClient

Initialize JSON-RPC client

Parameters:

  • host (String)

    Metasploit RPC host

  • port (Integer)

    Metasploit RPC port

  • endpoint (String) (defaults to: DEFAULT_ENDPOINT)

    API endpoint path (default: DEFAULT_ENDPOINT)

  • token (String)

    Bearer authentication token

  • ssl (Boolean) (defaults to: true)

    Use SSL (default: true)



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.

Parameters:

  • _user (String)

    Ignored

  • _password (String)

    Ignored

Returns:

  • (String)

    The existing token



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

Parameters:

  • method (String)

    API method name

  • args (Array) (defaults to: [])

    Arguments to pass to the method (must be an array)

Returns:

  • (Hash)

    API response

Raises:



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

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

  • (Hash)

    Response with ‘creds’ array



121
122
123
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 121

def db_creds(options = {})
  call_api('db.creds', [options])
end

#db_hosts(options = {}) ⇒ Hash

Get hosts from database

Parameters:

  • options (Hash) (defaults to: {})

    Query options (workspace, limit, offset, etc.)

Returns:

  • (Hash)

    Response with ‘hosts’ array



93
94
95
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 93

def db_hosts(options = {})
  call_api('db.hosts', [options])
end

#db_loot(options = {}) ⇒ Hash

Get loot from database

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

  • (Hash)

    Response with ‘loots’ array



128
129
130
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 128

def db_loot(options = {})
  call_api('db.loots', [options])
end

#db_notes(options = {}) ⇒ Hash

Get notes from database

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

  • (Hash)

    Response with ‘notes’ array



114
115
116
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 114

def db_notes(options = {})
  call_api('db.notes', [options])
end

#db_services(options = {}) ⇒ Hash

Get services from database

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

  • (Hash)

    Response with ‘services’ array



100
101
102
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 100

def db_services(options = {})
  call_api('db.services', [options])
end

#db_vulns(options = {}) ⇒ Hash

Get vulnerabilities from database

Parameters:

  • options (Hash) (defaults to: {})

    Query options

Returns:

  • (Hash)

    Response with ‘vulns’ array



107
108
109
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 107

def db_vulns(options = {})
  call_api('db.vulns', [options])
end

#module_check(type, name, options = {}) ⇒ Hash

Run the check method of a Metasploit module

Parameters:

  • type (String)

    Module type (‘exploit’ or ‘auxiliary’)

  • name (String)

    Module name

  • options (Hash) (defaults to: {})

    Module datastore options

Returns:

  • (Hash)

    Response containing ‘job_id’ and ‘uuid’ keys



146
147
148
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 146

def module_check(type, name, options = {})
  call_api('module.check', [type, name, options])
end

#module_execute(type, name, options = {}) ⇒ Hash

Execute a Metasploit module

Parameters:

  • type (String)

    Module type (‘exploit’, ‘auxiliary’, ‘post’, ‘payload’, ‘evasion’)

  • name (String)

    Module name (e.g. ‘multi/handler’)

  • options (Hash) (defaults to: {})

    Module datastore options

Returns:

  • (Hash)

    Response containing ‘job_id’ and ‘uuid’ keys



137
138
139
# File 'lib/msf/core/mcp/metasploit/jsonrpc_client.rb', line 137

def module_execute(type, name, options = {})
  call_api('module.execute', [type, name, options])
end

#module_info(type, name) ⇒ Hash

Get module information

Parameters:

  • type (String)

    Module type (‘exploit’, ‘auxiliary’, ‘post’, etc.)

  • name (String)

    Module name

Returns:

  • (Hash)

    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

Parameters:

  • uuid (String)

    Module run UUID returned by module_execute / module_check

Returns:

  • (Hash)

    Result hash with ‘status’ key (and ‘result’ / ‘error’ as applicable)



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_statsHash

Get currently running module stats (waiting / running / results)

Returns:

  • (Hash)

    Hash with ‘waiting’, ‘running’, ‘results’ arrays of UUIDs



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

Parameters:

  • query (String)

    Search query

Returns:

  • (Array<Hash>)

    Module metadata



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_listHash

List active sessions

Returns:

  • (Hash)

    Hash keyed by session ID with session info hashes as values



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)

Parameters:

  • session_id (Integer)

    Session ID

Returns:

  • (Hash)

    Response with ‘data’ key containing the buffered output



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

Parameters:

  • session_id (Integer)

    Session ID

Returns:

  • (Hash)

    Response with ‘result’ key set to ‘success’



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

Parameters:

  • session_id (Integer)

    Session ID

  • data (String)

    Data to send to the session

Returns:

  • (Hash)

    Response with ‘result’ key



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

#shutdownObject

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