Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map Meterpreter command IDs to their names when raising a RequestError #14562

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 34 additions & 15 deletions lib/rex/post/meterpreter/extension_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,55 @@ class ExtensionMapper

@@klasses = {}

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

def self.get_extension_id(name)
k = self.get_extension_klass(name)
k.extension_id
end

def self.get_extension_name(id)
self.get_extension_names.each do |name|
begin
klass = self.get_extension_klass(name)
rescue RuntimeError
next
end
return name if klass.extension_id == id
end
end

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

def self.get_extension_klass(name)
name.downcase!

unless @@klasses[name]
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.select { |c|
name == c.to_s.downcase
}[0]
@@klasses[name] = Rex::Post::Meterpreter::Extensions.const_get(s).const_get(s)
mod = self.get_extension_module(name)
@@klasses[name] = mod.const_get(mod.name.split('::').last)
end

@@klasses[name]
end

def self.dump_extensions
base = ::File.join(File.dirname(__dir__), 'meterpreter/extensions')
names = ::Dir.entries(base).select { |e|
::File.directory?(::File.join(base, e)) && !['.', '..'].include?(e)
}
names.each { |n|
self.get_extension_names.each { |n|
STDERR.puts("EXTENSION_ID_#{n.upcase} = #{self.get_extension_id(n)}\n")
}
end
Expand Down
17 changes: 15 additions & 2 deletions lib/rex/post/meterpreter/packet_dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@ module Meterpreter
#
###
class RequestError < ArgumentError
def initialize(method, einfo, ecode=nil)
@method = method
def initialize(command_id, einfo, ecode=nil)
extension_id = command_id - (command_id % 1000)
if extension_id == 0 # this is the meterpreter core
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

if mod
command_name = mod.constants.select { |c| c.start_with?('COMMAND_ID_') }.find { |c| command_id == mod.const_get(c) }
command_name = command_name.to_s.delete_prefix('COMMAND_ID_').downcase if command_name
end

@method = command_name || "##{command_id}"
@result = einfo
@code = ecode || einfo
end
Expand Down