2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2005-04-10 01:17:19 +00:00
|
|
|
|
|
|
|
require 'socket'
|
2009-07-22 18:37:51 +00:00
|
|
|
require 'openssl'
|
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
require 'rex/script'
|
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
|
|
|
|
|
2005-07-18 07:46:54 +00:00
|
|
|
#
|
|
|
|
# Just to get it in there...
|
|
|
|
#
|
|
|
|
module Extensions
|
|
|
|
end
|
|
|
|
|
2005-04-10 01:17:19 +00:00
|
|
|
###
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# This class represents a logical meterpreter client class. This class
|
|
|
|
# provides an interface that is compatible with the Rex post-exploitation
|
|
|
|
# interface in terms of the feature set that it attempts to expose. This
|
|
|
|
# class is meant to drive a single meterpreter client session.
|
2005-04-10 01:17:19 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
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-11-19 15:09:41 +00:00
|
|
|
#
|
|
|
|
# Extension name to class hash.
|
|
|
|
#
|
|
|
|
@@ext_hash = {}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Checks the extension hash to see if a class has already been associated
|
|
|
|
# with the supplied extension name.
|
|
|
|
#
|
|
|
|
def self.check_ext_hash(name)
|
|
|
|
@@ext_hash[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Stores the name to class association for the supplied extension name.
|
|
|
|
#
|
|
|
|
def self.set_ext_hash(name, klass)
|
|
|
|
@@ext_hash[name] = klass
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Initializes the client context with the supplied socket through
|
2005-11-15 05:22:13 +00:00
|
|
|
# which communication with the server will be performed.
|
|
|
|
#
|
2010-04-03 05:21:15 +00:00
|
|
|
def initialize(sock,opts={})
|
|
|
|
init_meterpreter(sock, opts)
|
2005-07-18 05:59:27 +00:00
|
|
|
end
|
|
|
|
|
2006-09-17 23:12:46 +00:00
|
|
|
#
|
|
|
|
# Cleans up the meterpreter instance, terminating the dispatcher thread.
|
|
|
|
#
|
|
|
|
def cleanup_meterpreter
|
|
|
|
dispatcher_thread.kill if dispatcher_thread
|
|
|
|
end
|
|
|
|
|
2005-07-18 05:59:27 +00:00
|
|
|
#
|
|
|
|
# Initializes the meterpreter client instance
|
|
|
|
#
|
2010-04-03 05:21:15 +00:00
|
|
|
def init_meterpreter(sock,opts={})
|
2005-04-10 17:08:27 +00:00
|
|
|
self.sock = sock
|
|
|
|
self.parser = PacketParser.new
|
|
|
|
self.ext = ObjectAliases.new
|
|
|
|
self.ext_aliases = ObjectAliases.new
|
2010-01-28 23:53:15 +00:00
|
|
|
self.alive = true
|
2010-04-03 05:21:15 +00:00
|
|
|
self.target_id = opts[:target_id]
|
|
|
|
|
|
|
|
self.response_timeout = opts[:timeout] || self.class.default_timeout
|
|
|
|
self.send_keepalives = true
|
|
|
|
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2010-04-03 05:21:15 +00:00
|
|
|
# Switch the socket to SSL mode and receive the hello if needed
|
|
|
|
if not opts[:skip_ssl]
|
|
|
|
swap_sock_plain_to_ssl()
|
|
|
|
end
|
2009-07-09 03:22:10 +00:00
|
|
|
|
|
|
|
register_extension_alias('core', ClientCore.new(self))
|
|
|
|
|
|
|
|
initialize_inbound_handlers
|
|
|
|
initialize_channels
|
2005-04-10 08:09:25 +00:00
|
|
|
|
2009-07-09 03:22:10 +00:00
|
|
|
# Register the channel inbound packet handler
|
|
|
|
register_inbound_handler(Rex::Post::Meterpreter::Channel)
|
|
|
|
|
|
|
|
monitor_socket
|
|
|
|
end
|
|
|
|
|
|
|
|
def swap_sock_plain_to_ssl
|
2009-07-09 22:44:33 +00:00
|
|
|
# Create a new SSL session on the existing socket
|
|
|
|
ctx = generate_ssl_context()
|
|
|
|
ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx)
|
2009-07-16 16:02:24 +00:00
|
|
|
|
2009-07-09 22:44:33 +00:00
|
|
|
ssl.accept
|
|
|
|
|
2010-01-29 18:52:44 +00:00
|
|
|
self.sock.extend(Rex::Socket::SslTcp)
|
|
|
|
self.sock.sslsock = ssl
|
|
|
|
self.sock.sslctx = ctx
|
2009-07-16 16:02:24 +00:00
|
|
|
|
2010-04-03 05:21:15 +00:00
|
|
|
tag = self.sock.get_once(-1, 30)
|
|
|
|
if(not tag or tag !~ /^GET \//)
|
|
|
|
raise RuntimeError, "Could not read the HTTP hello token"
|
2009-06-26 23:18:53 +00:00
|
|
|
end
|
2005-04-10 01:17:19 +00:00
|
|
|
end
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2009-07-09 03:22:10 +00:00
|
|
|
def swap_sock_ssl_to_plain
|
2009-07-09 22:44:33 +00:00
|
|
|
# Remove references to the SSLSocket and Context
|
2010-01-29 18:52:44 +00:00
|
|
|
self.sock.sslsock.close
|
2009-07-09 03:22:10 +00:00
|
|
|
self.sock.sslsock = nil
|
|
|
|
self.sock.sslctx = nil
|
2010-01-29 18:52:44 +00:00
|
|
|
self.sock = self.sock.fd
|
2009-07-09 22:44:33 +00:00
|
|
|
self.sock.extend(::Rex::Socket::Tcp)
|
2009-07-09 03:22:10 +00:00
|
|
|
end
|
2009-06-26 23:18:53 +00:00
|
|
|
|
|
|
|
def generate_ssl_context
|
2009-07-09 03:22:10 +00:00
|
|
|
key = OpenSSL::PKey::RSA.new(1024){ }
|
2009-06-26 23:18:53 +00:00
|
|
|
cert = OpenSSL::X509::Certificate.new
|
|
|
|
cert.version = 2
|
2009-07-09 03:22:10 +00:00
|
|
|
cert.serial = rand(0xFFFFFFFF)
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2009-06-26 23:18:53 +00:00
|
|
|
subject = OpenSSL::X509::Name.new([
|
2009-10-31 20:44:23 +00:00
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
2009-06-26 23:18:53 +00:00
|
|
|
["L", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["O", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
2009-10-31 20:44:23 +00:00
|
|
|
["CN", self.sock.getsockname[1] || Rex::Text.rand_hostname],
|
2009-06-26 23:18:53 +00:00
|
|
|
])
|
|
|
|
issuer = OpenSSL::X509::Name.new([
|
2009-10-31 20:44:23 +00:00
|
|
|
["C","US"],
|
|
|
|
['ST', Rex::Text.rand_state()],
|
2009-06-26 23:18:53 +00:00
|
|
|
["L", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
|
|
|
["O", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
2009-10-31 20:44:23 +00:00
|
|
|
["CN", Rex::Text.rand_text_alpha(rand(20) + 10)],
|
2009-06-26 23:18:53 +00:00
|
|
|
])
|
|
|
|
|
|
|
|
cert.subject = subject
|
|
|
|
cert.issuer = issuer
|
2009-07-09 22:44:33 +00:00
|
|
|
cert.not_before = Time.now - (3600 * 365) + rand(3600 * 14)
|
|
|
|
cert.not_after = Time.now + (3600 * 365) + rand(3600 * 14)
|
2009-06-26 23:18:53 +00:00
|
|
|
cert.public_key = key.public_key
|
|
|
|
ef = OpenSSL::X509::ExtensionFactory.new(nil,cert)
|
|
|
|
cert.extensions = [
|
|
|
|
ef.create_extension("basicConstraints","CA:FALSE"),
|
|
|
|
ef.create_extension("subjectKeyIdentifier","hash"),
|
|
|
|
ef.create_extension("extendedKeyUsage","serverAuth"),
|
|
|
|
ef.create_extension("keyUsage","keyEncipherment,dataEncipherment,digitalSignature")
|
|
|
|
]
|
|
|
|
ef.issuer_certificate = cert
|
|
|
|
cert.add_extension ef.create_extension("authorityKeyIdentifier", "keyid:always,issuer:always")
|
|
|
|
cert.sign(key, OpenSSL::Digest::SHA1.new)
|
2009-10-31 20:44:23 +00:00
|
|
|
|
|
|
|
ctx = OpenSSL::SSL::SSLContext.new(:SSLv3)
|
2009-06-26 23:18:53 +00:00
|
|
|
ctx.key = key
|
|
|
|
ctx.cert = cert
|
|
|
|
|
2009-10-31 20:44:23 +00:00
|
|
|
ctx.session_id_context = Rex::Text.rand_text(16)
|
2009-06-26 23:18:53 +00:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
end
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
2010-02-23 17:59:35 +00:00
|
|
|
# Runs the meterpreter script in the context of a script container
|
2006-09-19 03:15:25 +00:00
|
|
|
#
|
2010-02-23 17:59:35 +00:00
|
|
|
def execute_file(file, args)
|
|
|
|
o = Rex::Script::Meterpreter.new(self, file)
|
|
|
|
o.run(args)
|
2006-09-19 03:15:25 +00:00
|
|
|
end
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2009-10-31 20:44:23 +00:00
|
|
|
#
|
2005-04-10 17:08:27 +00:00
|
|
|
# Accessors
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Returns the default timeout that request packets will use when
|
2005-11-15 05:22:13 +00:00
|
|
|
# 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
|
|
|
##
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Translates unhandled methods into registered extension aliases
|
2005-11-15 05:22:13 +00:00
|
|
|
# if a matching extension alias exists for the supplied symbol.
|
|
|
|
#
|
2005-04-10 08:09:25 +00:00
|
|
|
def method_missing(symbol, *args)
|
2005-12-13 05:59:59 +00:00
|
|
|
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
|
|
|
##
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
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-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-10 08:09:25 +00:00
|
|
|
def add_extension(name)
|
2005-11-19 15:09:41 +00:00
|
|
|
# Check to see if this extension has already been loaded.
|
|
|
|
if ((klass = self.class.check_ext_hash(name.downcase)) == nil)
|
|
|
|
old = Rex::Post::Meterpreter::Extensions.constants
|
|
|
|
require("rex/post/meterpreter/extensions/#{name.downcase}/#{name.downcase}")
|
|
|
|
new = Rex::Post::Meterpreter::Extensions.constants
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
# No new constants added?
|
|
|
|
if ((diff = new - old).empty?)
|
|
|
|
return false
|
|
|
|
end
|
2009-10-31 20:44:23 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
klass = Rex::Post::Meterpreter::Extensions.const_get(diff[0]).const_get(diff[0])
|
|
|
|
|
|
|
|
# Save the module name to class association now that the code is
|
|
|
|
# loaded.
|
|
|
|
self.class.set_ext_hash(name.downcase, klass)
|
2005-07-18 07:46:54 +00:00
|
|
|
end
|
2005-04-10 08:09:25 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
# Create a new instance of the extension
|
2005-12-13 05:59:59 +00:00
|
|
|
inst = klass.new(self)
|
|
|
|
|
|
|
|
self.ext.aliases[inst.name] = inst
|
2005-04-10 16:21:53 +00:00
|
|
|
|
|
|
|
return true
|
2005-04-10 08:09:25 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +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-11-15 05:22:13 +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-11-15 05:22:13 +00:00
|
|
|
#
|
2005-04-12 05:37:11 +00:00
|
|
|
# Registers an aliased extension that can be referenced through
|
2005-11-15 05:22:13 +00:00
|
|
|
# client.name.
|
|
|
|
#
|
2005-04-10 17:08:27 +00:00
|
|
|
def register_extension_alias(name, ext)
|
|
|
|
self.ext_aliases.aliases[name] = ext
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Registers zero or more aliases that are provided in an array.
|
|
|
|
#
|
2005-04-15 06:23:59 +00:00
|
|
|
def register_extension_aliases(aliases)
|
|
|
|
aliases.each { |a|
|
|
|
|
register_extension_alias(a['name'], a['ext'])
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +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-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Dumps the extension tree.
|
|
|
|
#
|
2005-04-15 06:23:59 +00:00
|
|
|
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-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The extension alias under which all extensions can be accessed by name.
|
|
|
|
# For example:
|
|
|
|
#
|
|
|
|
# client.ext.stdapi
|
|
|
|
#
|
|
|
|
#
|
|
|
|
attr_reader :ext
|
|
|
|
#
|
|
|
|
# The socket the client is communicating over.
|
|
|
|
#
|
|
|
|
attr_reader :sock
|
|
|
|
#
|
|
|
|
# The timeout value to use when waiting for responses.
|
|
|
|
#
|
2005-07-08 00:28:52 +00:00
|
|
|
attr_accessor :response_timeout
|
2010-01-28 23:53:15 +00:00
|
|
|
#
|
|
|
|
# Whether to send pings every so often to determine liveness.
|
|
|
|
#
|
|
|
|
attr_accessor :send_keepalives
|
|
|
|
#
|
|
|
|
# Whether this session is alive. If the socket is disconnected or broken,
|
|
|
|
# this will be false
|
|
|
|
#
|
|
|
|
attr_accessor :alive
|
2010-04-03 05:21:15 +00:00
|
|
|
#
|
|
|
|
# The unique target identifier for this payload
|
|
|
|
#
|
|
|
|
attr_accessor :target_id
|
|
|
|
|
2005-04-12 05:37:11 +00:00
|
|
|
protected
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :parser, :ext_aliases # :nodoc:
|
|
|
|
attr_writer :ext, :sock # :nodoc:
|
2005-04-10 01:17:19 +00:00
|
|
|
end
|
|
|
|
|
2009-06-26 23:18:53 +00:00
|
|
|
end; end; end
|
2009-10-31 20:44:23 +00:00
|
|
|
|