Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/meterpreter/extension_mapper.rb
Views: 11784
# -*- coding: binary -*-12module Rex3module Post4module Meterpreter56class ExtensionMapper78@@klasses = {}910# Get the names of all of the extensions.11#12# @return [Array<String>] An array of all of the extension names.13def self.get_extension_names14base = ::File.join(File.dirname(__dir__), 'meterpreter/extensions')15::Dir.entries(base).select do |e|16::File.directory?(::File.join(base, e)) && !['.', '..'].include?(e)17end18end1920# Get the numeric ID for the specified extension name.21#22# @param [String] name The name of the extension to retrieve the ID for. This23# parameter is case insensitive.24# @return [Integer, nil] The extension ID or nil if the name does not exist.25def self.get_extension_id(name)26begin27k = self.get_extension_klass(name)28rescue RuntimeError29return nil30end3132k.extension_id33end3435# Get the string extension name for the specified extension ID.36#37# @param [Integer] id The ID of the extension to retrieve the name for.38# @return [String, nil] The extension name or nil if the ID does not exist.39def self.get_extension_name(id)40id = id - (id % COMMAND_ID_RANGE)4142self.get_extension_names.find do |name|43begin44klass = self.get_extension_klass(name)45rescue RuntimeError46next47end4849klass.extension_id == id50end51end5253# Get the module for the specified extension name.54#55# @param [String] name The name of the extension to retrieve the module for.56# This parameter is case insensitive.57# @raise [RuntimeError] A RuntimeError is raised if the specified module can58# not be loaded.59# @return [Module] The extension module.60def self.get_extension_module(name)61name.downcase!6263begin64require("rex/post/meterpreter/extensions/#{name}/#{name}")65rescue LoadError66# the extension doesn't exist on disk67raise RuntimeError, "Unable to load extension '#{name}' - module does not exist."68end69s = Rex::Post::Meterpreter::Extensions.constants.find { |c| name == c.to_s.downcase }70Rex::Post::Meterpreter::Extensions.const_get(s)71end7273# Get the class for the specified extension name.74#75# @param [String] name The name of the extension to retrieve the class for.76# This parameter is case insensitive.77# @raise [RuntimeError] A RuntimeError is raised if the specified module can78# not be loaded.79# @return [Class] The extension class.80def self.get_extension_klass(name)81name.downcase!8283unless @@klasses[name]84mod = self.get_extension_module(name)85@@klasses[name] = mod.const_get(mod.name.split('::').last)86end8788@@klasses[name]89end9091def self.get_extension_klasses92self.get_extension_names.map { |name| self.get_extension_module(name) }93end9495def self.dump_extensions96self.get_extension_names.each { |n|97STDERR.puts("EXTENSION_ID_#{n.upcase} = #{self.get_extension_id(n)}\n")98}99end100101end102103end104end105end106107108