Class: Msf::MCP::Metasploit::ResponseTransformer

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/mcp/metasploit/response_transformer.rb

Overview

Transforms Metasploit RPC responses into MCP-compatible format Adds metadata, converts field names, and formats timestamps

Class Method Summary collapse

Class Method Details

.transform_creds(response) ⇒ Array<Hash>

Transform credentials response

Parameters:

  • response (Hash)

    Raw response with ‘creds’ array

Returns:

  • (Array<Hash>)

    Transformed credentials



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 152

def self.transform_creds(response)
  return [] unless response.is_a?(Hash) && response['creds'].is_a?(Array)

  response['creds'].map do |cred|
    {
      host: cred['host'],
      port: cred['port'],
      protocol: cred['proto'],
      service_name: cred['sname'],
      user: cred['user'],
      secret: cred['pass'],
      type: cred['type'],
      updated_at: format_timestamp(cred['updated_at']),
      realm_key: cred['realm_key'],
      realm_value: cred['realm_value']
    }.compact
  end
end

.transform_hosts(response) ⇒ Array<Hash>

Transform hosts response

Parameters:

  • response (Hash)

    Raw response with ‘hosts’ array

Returns:

  • (Array<Hash>)

    Transformed hosts with MCP metadata



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 64

def self.transform_hosts(response)
  return [] unless response.is_a?(Hash) && response['hosts'].is_a?(Array)

  response['hosts'].map do |host|
    {
      created_at: format_timestamp(host['created_at']),
      address: host['address'],
      mac_address: host['mac'],
      hostname: host['name'],
      state: host['state'],
      os_name: host['os_name'],
      os_flavor: host['os_flavor'],
      os_service_pack: host['os_sp'],
      os_language: host['os_lang'],
      updated_at: format_timestamp(host['updated_at']),
      purpose: host['purpose'],
      info: host['info'],
      comments: host['comments']
    }.compact
  end
end

.transform_loot(response) ⇒ Array<Hash>

Transform loot response

Parameters:

  • response (Hash)

    Raw response with ‘loots’ array

Returns:

  • (Array<Hash>)

    Transformed loot



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 174

def self.transform_loot(response)
  return [] unless response.is_a?(Hash) && response['loots'].is_a?(Array)

  response['loots'].map do |loot|
    {
      host: loot['host'],
      service_name_or_port: loot['service'],
      loot_type: loot['ltype'],
      content_type: loot['ctype'],
      name: loot['name'],
      info: loot['info'],
      data: loot['data'],
      created_at: format_timestamp(loot['created_at']),
      updated_at: format_timestamp(loot['updated_at'])
    }.compact
  end
end

.transform_module_info(info) ⇒ Hash

Transform module info response

Parameters:

  • info (Hash)

    Raw module info from Metasploit

Returns:

  • (Hash)

    Transformed info with MCP metadata



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 30

def self.transform_module_info(info)
  return {} unless info.is_a?(Hash)

  {
    type: info['type'],
    name: info['name'],
    fullname: info['fullname'],
    rank: info['rank'],
    disclosure_date: info['disclosuredate'],
    description: info['description'],
    license: info['license'],
    filepath: info['filepath']&.sub(/^.*modules\//, 'modules/'), # Dont expose the install path
    architectures: info['arch'],
    platforms: info['platform'],
    authors: info['authors'],
    privileged: info['privileged'],
    has_check_method: info['check'],
    # TODO: write transformer for default_options
    default_options: info['default_options'],
    references: transform_references(info['references']),
    targets: info['targets'],
    default_target: info['default_target'],
    stance: info['stance'],
    actions: info['actions'],
    default_action: info['default_action'],
    notes: info['notes'],
    # TODO: write transformer for options
    options: info['options']
  }.compact
end

.transform_modules(modules) ⇒ Array<Hash>

Transform module search results

Parameters:

  • modules (Array<Hash>)

    Raw module data from Metasploit

Returns:

  • (Array<Hash>)

    Transformed modules with MCP metadata



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 13

def self.transform_modules(modules)
  return [] unless modules.is_a?(Array)

  modules.map do |mod|
    {
      name: mod['name'] || mod['fullname'],
      type: mod['type'],
      fullname: mod['fullname'],
      rank: mod['rank'],
      disclosure_date: mod['disclosuredate']
    }.compact
  end
end

.transform_notes(response) ⇒ Array<Hash>

Transform notes response

Parameters:

  • response (Hash)

    Raw response with ‘notes’ array

Returns:

  • (Array<Hash>)

    Transformed notes



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 135

def self.transform_notes(response)
  return [] unless response.is_a?(Hash) && response['notes'].is_a?(Array)

  response['notes'].map do |note|
    {
      host: note['host'],
      service_name_or_port: note['service'],
      note_type: note['type'] || note['ntype'],
      data: note['data'],
      created_at: format_timestamp(note['time'])
    }.compact
  end
end

.transform_service(service) ⇒ Hash

Transform a single service, recursively transforming its parent services.

Parameters:

  • service (Hash)

    Raw service data

Returns:

  • (Hash)

    Transformed service



98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 98

def self.transform_service(service)
  {
    host_address: service['host'],
    created_at: format_timestamp(service['created_at']),
    updated_at: format_timestamp(service['updated_at']),
    port: service['port'],
    protocol: service['proto'],
    state: service['state'],
    name: service['name'],
    info: service['info'],
    resource: service['resource'],
    parents: (service['parents'] || []).map { |parent| transform_service(parent) }
  }.compact
end

.transform_services(response) ⇒ Array<Hash>

Transform services response

Parameters:

  • response (Hash)

    Raw response with ‘services’ array

Returns:

  • (Array<Hash>)

    Transformed services



89
90
91
92
93
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 89

def self.transform_services(response)
  return [] unless response.is_a?(Hash) && response['services'].is_a?(Array)

  response['services'].map { |service| transform_service(service) }
end

.transform_vulns(response) ⇒ Array<Hash>

Transform vulnerabilities response

Parameters:

  • response (Hash)

    Raw response with ‘vulns’ array

Returns:

  • (Array<Hash>)

    Transformed vulnerabilities



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/msf/core/mcp/metasploit/response_transformer.rb', line 116

def self.transform_vulns(response)
  return [] unless response.is_a?(Hash) && response['vulns'].is_a?(Array)

  response['vulns'].map do |vuln|
    {
      host: vuln['host'],
      port: vuln['port'],
      protocol: vuln['proto'],
      name: vuln['name'],
      references: parse_refs(vuln['refs']),
      created_at: format_timestamp(vuln['time']),
      resource: vuln['resource']
    }.compact
  end
end