Class: Rex::Post::Meterpreter::ExtensionMapper

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

Constant Summary collapse

@@klasses =
{}

Class Method Summary collapse

Class Method Details

.dump_extensionsObject



96
97
98
99
100
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 96

def self.dump_extensions
  self.get_extension_names.each { |n|
    STDERR.puts("EXTENSION_ID_#{n.upcase} = #{self.get_extension_id(n)}\n")
  }
end

.get_extension_id(name) ⇒ Integer?

Get the numeric ID for the specified extension name.

Parameters:

  • name (String)

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

Returns:

  • (Integer, nil)

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



26
27
28
29
30
31
32
33
34
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 26

def self.get_extension_id(name)
  begin
    k = self.get_extension_klass(name)
  rescue RuntimeError
    return nil
  end

  k.extension_id
end

.get_extension_klass(name) ⇒ Class

Get the class for the specified extension name.

Parameters:

  • name (String)

    The name of the extension to retrieve the class for. This parameter is case insensitive.

Returns:

  • (Class)

    The extension class.

Raises:

  • (RuntimeError)

    A RuntimeError is raised if the specified module can not be loaded.



81
82
83
84
85
86
87
88
89
90
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 81

def self.get_extension_klass(name)
  name.downcase!

  unless @@klasses[name]
    mod = self.get_extension_module(name)
    @@klasses[name] = mod.const_get(mod.name.split('::').last)
  end

  @@klasses[name]
end

.get_extension_klassesObject



92
93
94
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 92

def self.get_extension_klasses
  self.get_extension_names.map { |name| self.get_extension_module(name) }
end

.get_extension_module(name) ⇒ Module

Get the module for the specified extension name.

Parameters:

  • name (String)

    The name of the extension to retrieve the module for. This parameter is case insensitive.

Returns:

  • (Module)

    The extension module.

Raises:

  • (RuntimeError)

    A RuntimeError is raised if the specified module can not be loaded.



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 61

def self.get_extension_module(name)
  name.downcase!

  begin
    require("rex/post/meterpreter/extensions/#{name}/#{name}")
  rescue LoadError
    # the extension doesn't exist on disk
    raise RuntimeError, "Unable to load extension '#{name}' - module does not exist."
  end
  s = Rex::Post::Meterpreter::Extensions.constants.find { |c| name == c.to_s.downcase }
  Rex::Post::Meterpreter::Extensions.const_get(s)
end

.get_extension_name(id) ⇒ String?

Get the string extension name for the specified extension ID.

Parameters:

  • id (Integer)

    The ID of the extension to retrieve the name for.

Returns:

  • (String, nil)

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



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

def self.get_extension_name(id)
  id = id - (id % COMMAND_ID_RANGE)

  self.get_extension_names.find do |name|
    begin
      klass = self.get_extension_klass(name)
    rescue RuntimeError
      next
    end

    klass.extension_id == id
  end
end

.get_extension_namesArray<String>

Get the names of all of the extensions.

Returns:

  • (Array<String>)

    An array of all of the extension names.



14
15
16
17
18
19
# File 'lib/rex/post/meterpreter/extension_mapper.rb', line 14

def self.get_extension_names
  base = ::File.join(File.dirname(__dir__), 'meterpreter/extensions')
  ::Dir.entries(base).select do |e|
    ::File.directory?(::File.join(base, e)) && !['.', '..'].include?(e)
  end
end