2005-04-10 01:17:19 +00:00
|
|
|
#!/usr/bin/ruby
|
|
|
|
|
|
|
|
require 'socket'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'rex/post/meterpreter/client_core'
|
|
|
|
require 'rex/post/meterpreter/channel'
|
|
|
|
require 'rex/post/meterpreter/channel_container'
|
|
|
|
require 'rex/post/meterpreter/dependencies'
|
|
|
|
require 'rex/post/meterpreter/object_aliases'
|
|
|
|
require 'rex/post/meterpreter/packet'
|
|
|
|
require 'rex/post/meterpreter/packet_parser'
|
|
|
|
require 'rex/post/meterpreter/packet_dispatcher'
|
2005-04-10 01:17:19 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
|
|
|
module Meterpreter
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Client
|
|
|
|
# ------
|
|
|
|
#
|
|
|
|
# The logical meterpreter client class. This class manages a single session
|
|
|
|
# with a meterpreter server instance.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Client
|
|
|
|
|
2005-04-10 02:05:43 +00:00
|
|
|
include Rex::Post::Meterpreter::PacketDispatcher
|
2005-04-12 05:37:11 +00:00
|
|
|
include Rex::Post::Meterpreter::ChannelContainer
|
2005-04-10 02:05:43 +00:00
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
# Initializes the client context with the supplied socket through
|
|
|
|
# which communication with the server will be performed
|
2005-07-08 00:28:52 +00:00
|
|
|
def initialize(sock, to = self.class.default_timeout)
|
2005-04-10 17:08:27 +00:00
|
|
|
self.sock = sock
|
|
|
|
self.parser = PacketParser.new
|
|
|
|
self.ext = ObjectAliases.new
|
|
|
|
self.ext_aliases = ObjectAliases.new
|
2005-04-10 08:09:25 +00:00
|
|
|
|
2005-07-08 00:28:52 +00:00
|
|
|
self.response_timeout = to
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
register_extension_alias('core', ClientCore.new(self))
|
|
|
|
|
|
|
|
initialize_inbound_handlers
|
|
|
|
initialize_channels
|
|
|
|
|
|
|
|
# Register the channel inbound packet handler
|
|
|
|
register_inbound_handler(Rex::Post::Meterpreter::Channel)
|
2005-04-10 01:17:19 +00:00
|
|
|
|
|
|
|
monitor_socket
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 17:08:27 +00:00
|
|
|
#
|
|
|
|
# Accessors
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
# Returns the default timeout that request packets will use when
|
|
|
|
# waiting for a response
|
2005-04-10 09:20:47 +00:00
|
|
|
def Client.default_timeout
|
|
|
|
return 30
|
2005-04-10 01:17:19 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 08:09:25 +00:00
|
|
|
#
|
2005-04-10 17:08:27 +00:00
|
|
|
# Alias processor
|
2005-04-10 08:09:25 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
# Translates unhandled methods into registered extension aliases
|
|
|
|
# if a matching extension alias exists for the supplied symbol
|
2005-04-10 08:09:25 +00:00
|
|
|
def method_missing(symbol, *args)
|
2005-04-10 17:08:27 +00:00
|
|
|
return self.ext_aliases.aliases[symbol.to_s];
|
2005-04-10 08:09:25 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-04-10 08:09:25 +00:00
|
|
|
#
|
|
|
|
# Extension registration
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
# Loads the client half of the supplied extension and initializes it as a
|
|
|
|
# registered extension that can be reached through client.ext.[extension].
|
2005-04-10 08:09:25 +00:00
|
|
|
def add_extension(name)
|
2005-07-08 00:28:52 +00:00
|
|
|
require("Rex/Post/Meterpreter/Extensions/#{name}/#{name}")
|
2005-04-10 08:09:25 +00:00
|
|
|
|
2005-06-02 02:03:37 +00:00
|
|
|
# XXX might want to be safer and catch the exception here?
|
|
|
|
# maybe not since we are just going to reraise right away...
|
|
|
|
ext = Rex::Post::Meterpreter::Extensions.const_get(name).const_get(name).new(self)
|
2005-04-10 08:09:25 +00:00
|
|
|
|
2005-04-10 17:08:27 +00:00
|
|
|
self.ext.aliases[ext.name] = ext
|
2005-04-10 16:21:53 +00:00
|
|
|
|
|
|
|
return true
|
2005-04-10 08:09:25 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
# Deregisters an extension alias of the supplied name
|
2005-04-10 08:09:25 +00:00
|
|
|
def deregister_extension(name)
|
2005-04-10 17:08:27 +00:00
|
|
|
self.ext.aliases.delete(name)
|
2005-04-10 08:09:25 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
# Enumerates all of the loaded extensions
|
2005-04-10 08:09:25 +00:00
|
|
|
def each_extension(&block)
|
2005-04-10 17:08:27 +00:00
|
|
|
self.ext.aliases.each(block)
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
# Registers an aliased extension that can be referenced through
|
|
|
|
# client.name
|
2005-04-10 17:08:27 +00:00
|
|
|
def register_extension_alias(name, ext)
|
|
|
|
self.ext_aliases.aliases[name] = ext
|
|
|
|
end
|
|
|
|
|
2005-04-15 06:23:59 +00:00
|
|
|
# Registers zero or more aliases that are provided in an array
|
|
|
|
def register_extension_aliases(aliases)
|
|
|
|
aliases.each { |a|
|
|
|
|
register_extension_alias(a['name'], a['ext'])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
# Deregisters a previously registered extension alias
|
2005-04-10 17:08:27 +00:00
|
|
|
def deregister_extension_alias(name)
|
|
|
|
self.ext_aliases.aliases.delete(name)
|
2005-04-10 08:09:25 +00:00
|
|
|
end
|
|
|
|
|
2005-04-15 06:23:59 +00:00
|
|
|
# Dumps the extension tree
|
|
|
|
def dump_extension_tree()
|
|
|
|
items = []
|
|
|
|
items.concat(self.ext.dump_alias_tree('client.ext'))
|
|
|
|
items.concat(self.ext_aliases.dump_alias_tree('client'))
|
|
|
|
|
|
|
|
return items.sort
|
|
|
|
end
|
|
|
|
|
2005-04-18 08:29:30 +00:00
|
|
|
attr_reader :ext, :sock
|
2005-07-08 00:28:52 +00:00
|
|
|
attr_accessor :response_timeout
|
2005-04-12 05:37:11 +00:00
|
|
|
protected
|
2005-04-18 08:29:30 +00:00
|
|
|
attr_accessor :parser, :ext_aliases
|
|
|
|
attr_writer :ext, :sock
|
2005-04-10 01:17:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end; end; end
|