Class: Rex::Post::Meterpreter::CommandMapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/post/meterpreter/command_mapper.rb

Class Method Summary collapse

Class Method Details

.get_command_id(name) ⇒ Integer?

Get the numeric command ID for the specified command name.

Parameters:

  • name (String)

    The name of the command to retrieve the ID for. This parameter is case insensitive.

Returns:

  • (Integer, nil)

    The command ID or nil if the name does not exist.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rex/post/meterpreter/command_mapper.rb', line 16

def self.get_command_id(name)
  name = name.downcase

  return nil unless name.include?('_')

  mod_name, cmd_name = name.split('_', 2)
  if mod_name == 'core'
    mod = Rex::Post::Meterpreter
  else
    mod = Rex::Post::Meterpreter::ExtensionMapper.get_extension_module(mod_name)
  end

  return nil unless mod

  const_name = "COMMAND_ID_#{mod_name.upcase}_#{cmd_name.upcase}"
  return nil unless mod.const_defined?(const_name)

  mod.const_get(const_name)
end

.get_command_name(id) ⇒ String?

Get the string command name for the specified command ID.

Parameters:

  • id (Integer)

    The ID of the command to retrieve the name for.

Returns:

  • (String, nil)

    The command name or nil if the ID does not exist.


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/rex/post/meterpreter/command_mapper.rb', line 40

def self.get_command_name(id)
  extension_id = id - (id % COMMAND_ID_RANGE)
  if extension_id == Rex::Post::Meterpreter::ClientCore.extension_id  # this is the meterpreter core which is not exactly an extension.
    mod = Rex::Post::Meterpreter
  else
    mod_name = Rex::Post::Meterpreter::ExtensionMapper.get_extension_name(extension_id)
    mod = Rex::Post::Meterpreter::ExtensionMapper.get_extension_module(mod_name)
  end

  return nil unless mod

  command_name = mod.constants.select { |c| c.to_s.start_with?('COMMAND_ID_') }.find { |c| id == mod.const_get(c) }

  return nil unless command_name

  command_name.to_s.delete_prefix('COMMAND_ID_').downcase
end

.get_command_names(*extensions) ⇒ Array<String>

Get all of the string command names for the specified extensions.

Parameters:

  • extensions (Array<String>)

    The names of the extensions to retrieve all of the command names for. The extension names are case insensitive. If no extensions are specified, all extensions will be enumerated.

Returns:

  • (Array<String>)

    An array of all of the enumerated command names.


64
65
66
# File 'lib/rex/post/meterpreter/command_mapper.rb', line 64

def self.get_command_names(*extensions)
  self.get_commands(*extensions).keys
end

.get_commands(*extensions) ⇒ Hash<String, Integer>

Get a hash of all command name strings mapped to their numeric IDs.

Parameters:

  • extensions (Array<String>)

    The names of the extensions to retrieve all of the commands for. The extension names are case insensitive. If no extensions are specified, all extensions will be enumerated.

Returns:

  • (Hash<String, Integer>)

    An hash of all of the enumerated commands.


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rex/post/meterpreter/command_mapper.rb', line 74

def self.get_commands(*extensions)
  extensions = ['core'] + Rex::Post::Meterpreter::ExtensionMapper.get_extension_names if extensions.empty?

  commands = {}
  extensions.each do |mod_name|
    mod_name = mod_name.downcase

    if mod_name == 'core'
      mod = Rex::Post::Meterpreter
    else
      begin
        mod = Rex::Post::Meterpreter::ExtensionMapper.get_extension_module(mod_name)
      rescue RuntimeError
        next
      end
    end

    constants = mod.constants.select { |name| name.to_s.start_with?("COMMAND_ID_#{mod_name.upcase}") }
    commands.merge!(constants.map { |name| [name.to_s.delete_prefix('COMMAND_ID_').downcase, mod.const_get(name)] }.to_h)
  end

  commands
end