Class: Rex::Post::Meterpreter::Client
- Inherits:
-
Object
- Object
- Rex::Post::Meterpreter::Client
- Includes:
- Channel::Container, PacketDispatcher, PivotContainer
- Defined in:
- lib/rex/post/meterpreter/client.rb
Overview
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.
Direct Known Subclasses
Constant Summary collapse
- @@ext_hash =
Extension name to class hash.
{}
- @@ssl_cached_cert =
Cached auto-generated SSL certificate
nil- @@ssl_mutex =
Mutex to synchronize class-wide operations
::Mutex.new
Constants included from PacketDispatcher
PacketDispatcher::PACKET_TIMEOUT, PacketDispatcher::PING_TIME
Instance Attribute Summary collapse
-
#alive ⇒ Object
Whether this session is alive.
-
#c2_profile ⇒ Object
Reference to the c2 profile instance associated with this connection, if any.
-
#capabilities ⇒ Object
The libraries available to this meterpreter server.
-
#comm_timeout ⇒ Object
The Communication Timeout.
-
#commands ⇒ Object
A list of the commands.
-
#conn_id ⇒ Object
The Connection ID.
-
#debug_build ⇒ Object
Whether or not to use a debug build for loaded extensions.
-
#encode_unicode ⇒ Object
Flag indicating whether to hex-encode UTF-8 file names and other strings.
-
#expiration ⇒ Object
The Session Expiration Timeout.
-
#ext ⇒ Object
The extension alias under which all extensions can be accessed by name.
-
#ext_aliases ⇒ Object
protected
:nodoc:.
-
#last_checkin ⇒ Object
The timestamp of the last received response.
-
#parser ⇒ Object
protected
:nodoc:.
-
#passive_dispatcher ⇒ Object
The Passive Dispatcher.
-
#pivot_session ⇒ Object
Reference to a session to pivot through.
-
#response_timeout ⇒ Object
The timeout value to use when waiting for responses.
-
#retry_total ⇒ Object
The total time for retrying connections.
-
#retry_wait ⇒ Object
The time to wait between retry attempts.
-
#send_keepalives ⇒ Object
Whether to send pings every so often to determine liveness.
-
#sock ⇒ Object
The socket the client is communicating over.
-
#ssl ⇒ Object
Use SSL (HTTPS).
-
#ssl_cert ⇒ Object
Use this SSL Certificate (unified PEM).
-
#target_id ⇒ Object
The unique target identifier for this payload.
-
#url ⇒ Object
The Connect URL.
Attributes included from PivotContainer
#pivot_listeners, #pivot_sessions
Attributes included from PacketDispatcher
#comm_mutex, #dispatcher_thread, #passive_service, #receiver_thread, #recv_queue, #send_queue, #session_guid, #tlv_enc_key, #tlv_log_file, #tlv_log_file_path, #tlv_log_output, #tlv_logging_error_occured, #waiters
Attributes included from Channel::Container
Class Method Summary collapse
-
.check_ext_hash(name) ⇒ Object
Checks the extension hash to see if a class has already been associated with the supplied extension name.
-
.default_timeout ⇒ Object
Returns the default timeout that request packets will use when waiting for a response.
-
.lookup_error(code) ⇒ Object
Lookup the error that occurred.
-
.set_ext_hash(name, klass) ⇒ Object
Stores the name to class association for the supplied extension name.
Instance Method Summary collapse
-
#add_extension(name, commands = []) ⇒ Object
Loads the client half of the supplied extension and initializes it as a registered extension that can be reached through client.ext..
-
#cleanup_meterpreter ⇒ Object
Cleans up the meterpreter instance, terminating the dispatcher thread.
-
#deregister_extension(name) ⇒ Object
Deregisters an extension alias of the supplied name.
-
#deregister_extension_alias(name) ⇒ Object
Deregisters a previously registered extension alias.
-
#dump_extension_tree ⇒ Object
Dumps the extension tree.
-
#each_extension(&block) ⇒ Object
Enumerates all of the loaded extensions.
- #generate_ssl_context ⇒ Object
-
#init_meterpreter(sock, opts = {}) ⇒ Object
Initializes the meterpreter client instance.
-
#initialize(sock, opts = {}) ⇒ Client
constructor
Initializes the client context with the supplied socket through which communication with the server will be performed.
-
#method_missing(symbol, *args) ⇒ Object
Translates unhandled methods into registered extension aliases if a matching extension alias exists for the supplied symbol.
-
#register_extension_alias(name, ext) ⇒ Object
Registers an aliased extension that can be referenced through client.name.
-
#register_extension_aliases(aliases) ⇒ Object
Registers zero or more aliases that are provided in an array.
- #swap_sock_plain_to_ssl ⇒ Object
- #swap_sock_ssl_to_plain ⇒ Object
-
#unicode_filter_decode(str) ⇒ Object
Decodes (or not) a UTF-8 string.
-
#unicode_filter_encode(str) ⇒ Object
Encodes (or not) a UTF-8 string.
-
#unwrap_packet(raw_bytes) ⇒ Object
Unwrap the given packet data from any prefixes and suffixes that are stored in the associated C2 profile client configuration (if it exists) and handle decoding of data.
-
#wrap_packet(raw_bytes) ⇒ Object
Wrap the given packet data with any prefixes and suffixes that are stored in the associated C2 profile server configuration (if it exists) and handle encoding of data.
Methods included from PivotContainer
#add_pivot_listener, #add_pivot_session, #find_pivot_listener, #find_pivot_session, #initialize_pivots, #remove_pivot_listener, #remove_pivot_session
Methods included from PacketDispatcher
#add_response_waiter, #decrypt_inbound_packet, #deregister_inbound_handler, #dispatch_inbound_packet, #initialize_inbound_handlers, #initialize_passive_dispatcher, #initialize_tlv_logging, #keepalive, #log_packet, #log_packet_to_console, #log_packet_to_file, #monitor_socket, #monitor_stop, #notify_response_waiter, #on_passive_request, #pivot_keepalive_start, #receive_packet, #register_inbound_handler, #remove_response_waiter, #send_packet, #send_packet_wait_response, #send_request, #shutdown_passive_dispatcher, #shutdown_tlv_logging
Methods included from Channel::Container
#add_channel, #find_channel, #initialize_channels, #remove_channel
Constructor Details
#initialize(sock, opts = {}) ⇒ Client
Initializes the client context with the supplied socket through which communication with the server will be performed.
83 84 85 |
# File 'lib/rex/post/meterpreter/client.rb', line 83 def initialize(sock, opts={}) init_meterpreter(sock, opts) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
Translates unhandled methods into registered extension aliases if a matching extension alias exists for the supplied symbol.
336 337 338 339 |
# File 'lib/rex/post/meterpreter/client.rb', line 336 def method_missing(symbol, *args) #$stdout.puts("method_missing: #{symbol}") self.ext_aliases.aliases[symbol.to_s] end |
Instance Attribute Details
#alive ⇒ Object
Whether this session is alive. If the socket is disconnected or broken, this will be false
469 470 471 |
# File 'lib/rex/post/meterpreter/client.rb', line 469 def alive @alive end |
#c2_profile ⇒ Object
Reference to the c2 profile instance associated with this connection, if any.
533 534 535 |
# File 'lib/rex/post/meterpreter/client.rb', line 533 def c2_profile @c2_profile end |
#capabilities ⇒ Object
The libraries available to this meterpreter server
477 478 479 |
# File 'lib/rex/post/meterpreter/client.rb', line 477 def capabilities @capabilities end |
#comm_timeout ⇒ Object
The Communication Timeout
501 502 503 |
# File 'lib/rex/post/meterpreter/client.rb', line 501 def comm_timeout @comm_timeout end |
#commands ⇒ Object
A list of the commands
525 526 527 |
# File 'lib/rex/post/meterpreter/client.rb', line 525 def commands @commands end |
#conn_id ⇒ Object
The Connection ID
481 482 483 |
# File 'lib/rex/post/meterpreter/client.rb', line 481 def conn_id @conn_id end |
#debug_build ⇒ Object
Whether or not to use a debug build for loaded extensions
537 538 539 |
# File 'lib/rex/post/meterpreter/client.rb', line 537 def debug_build @debug_build end |
#encode_unicode ⇒ Object
Flag indicating whether to hex-encode UTF-8 file names and other strings
521 522 523 |
# File 'lib/rex/post/meterpreter/client.rb', line 521 def encode_unicode @encode_unicode end |
#expiration ⇒ Object
The Session Expiration Timeout
497 498 499 |
# File 'lib/rex/post/meterpreter/client.rb', line 497 def expiration @expiration end |
#ext ⇒ Object
The extension alias under which all extensions can be accessed by name. For example:
client.ext.stdapi
452 453 454 |
# File 'lib/rex/post/meterpreter/client.rb', line 452 def ext @ext end |
#ext_aliases ⇒ Object (protected)
:nodoc:
540 541 542 |
# File 'lib/rex/post/meterpreter/client.rb', line 540 def ext_aliases @ext_aliases end |
#last_checkin ⇒ Object
The timestamp of the last received response
529 530 531 |
# File 'lib/rex/post/meterpreter/client.rb', line 529 def last_checkin @last_checkin end |
#parser ⇒ Object (protected)
:nodoc:
540 541 542 |
# File 'lib/rex/post/meterpreter/client.rb', line 540 def parser @parser end |
#passive_dispatcher ⇒ Object
The Passive Dispatcher
513 514 515 |
# File 'lib/rex/post/meterpreter/client.rb', line 513 def passive_dispatcher @passive_dispatcher end |
#pivot_session ⇒ Object
Reference to a session to pivot through
517 518 519 |
# File 'lib/rex/post/meterpreter/client.rb', line 517 def pivot_session @pivot_session end |
#response_timeout ⇒ Object
The timeout value to use when waiting for responses.
460 461 462 |
# File 'lib/rex/post/meterpreter/client.rb', line 460 def response_timeout @response_timeout end |
#retry_total ⇒ Object
The total time for retrying connections
505 506 507 |
# File 'lib/rex/post/meterpreter/client.rb', line 505 def retry_total @retry_total end |
#retry_wait ⇒ Object
The time to wait between retry attempts
509 510 511 |
# File 'lib/rex/post/meterpreter/client.rb', line 509 def retry_wait @retry_wait end |
#send_keepalives ⇒ Object
Whether to send pings every so often to determine liveness.
464 465 466 |
# File 'lib/rex/post/meterpreter/client.rb', line 464 def send_keepalives @send_keepalives end |
#sock ⇒ Object
The socket the client is communicating over.
456 457 458 |
# File 'lib/rex/post/meterpreter/client.rb', line 456 def sock @sock end |
#ssl ⇒ Object
Use SSL (HTTPS)
489 490 491 |
# File 'lib/rex/post/meterpreter/client.rb', line 489 def ssl @ssl end |
#ssl_cert ⇒ Object
Use this SSL Certificate (unified PEM)
493 494 495 |
# File 'lib/rex/post/meterpreter/client.rb', line 493 def ssl_cert @ssl_cert end |
#target_id ⇒ Object
The unique target identifier for this payload
473 474 475 |
# File 'lib/rex/post/meterpreter/client.rb', line 473 def target_id @target_id end |
#url ⇒ Object
The Connect URL
485 486 487 |
# File 'lib/rex/post/meterpreter/client.rb', line 485 def url @url end |
Class Method Details
.check_ext_hash(name) ⇒ Object
Checks the extension hash to see if a class has already been associated with the supplied extension name.
68 69 70 |
# File 'lib/rex/post/meterpreter/client.rb', line 68 def self.check_ext_hash(name) @@ext_hash[name] end |
.default_timeout ⇒ Object
Returns the default timeout that request packets will use when waiting for a response.
322 323 324 |
# File 'lib/rex/post/meterpreter/client.rb', line 322 def Client.default_timeout return 300 end |
.lookup_error(code) ⇒ Object
Lookup the error that occurred
60 61 62 |
# File 'lib/rex/post/meterpreter/client.rb', line 60 def self.lookup_error(code) code end |
.set_ext_hash(name, klass) ⇒ Object
Stores the name to class association for the supplied extension name.
75 76 77 |
# File 'lib/rex/post/meterpreter/client.rb', line 75 def self.set_ext_hash(name, klass) @@ext_hash[name] = klass end |
Instance Method Details
#add_extension(name, commands = []) ⇒ Object
Loads the client half of the supplied extension and initializes it as a registered extension that can be reached through client.ext..
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/rex/post/meterpreter/client.rb', line 351 def add_extension(name, commands=[]) self.commands.concat(commands) # Check to see if this extension has already been loaded. if ((klass = self.class.check_ext_hash(name.downcase)) == nil) klass = Rex::Post::Meterpreter::ExtensionMapper.get_extension_klass(name) # Save the module name to class association now that the code is # loaded. self.class.set_ext_hash(name.downcase, klass) end # Create a new instance of the extension inst = klass.new(self) self.ext.aliases[inst.name] = inst return true end |
#cleanup_meterpreter ⇒ Object
Cleans up the meterpreter instance, terminating the dispatcher thread.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rex/post/meterpreter/client.rb', line 90 def cleanup_meterpreter if self.pivot_session self.pivot_session.remove_pivot_session(self.session_guid) end self.pivot_sessions.keys.each do |k| pivot = self.pivot_sessions[k] pivot.pivoted_session.kill('Pivot closed') pivot.pivoted_session.shutdown_passive_dispatcher end unless self.skip_cleanup ext.aliases.each_value do | extension | extension.cleanup if extension.respond_to?( 'cleanup' ) end end dispatcher_thread.kill if dispatcher_thread unless self.skip_cleanup core.shutdown rescue nil end shutdown_passive_dispatcher shutdown_tlv_logging end |
#deregister_extension(name) ⇒ Object
Deregisters an extension alias of the supplied name.
373 374 375 |
# File 'lib/rex/post/meterpreter/client.rb', line 373 def deregister_extension(name) self.ext.aliases.delete(name) end |
#deregister_extension_alias(name) ⇒ Object
Deregisters a previously registered extension alias.
416 417 418 |
# File 'lib/rex/post/meterpreter/client.rb', line 416 def deregister_extension_alias(name) self.ext_aliases.aliases.delete(name) end |
#dump_extension_tree ⇒ Object
Dumps the extension tree.
423 424 425 426 427 428 429 |
# File 'lib/rex/post/meterpreter/client.rb', line 423 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 |
#each_extension(&block) ⇒ Object
Enumerates all of the loaded extensions.
380 381 382 |
# File 'lib/rex/post/meterpreter/client.rb', line 380 def each_extension(&block) self.ext.aliases.each(block) end |
#generate_ssl_context ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rex/post/meterpreter/client.rb', line 272 def generate_ssl_context ctx = nil ssl_cert_info = nil loop do # Load a custom SSL certificate if one has been specified if self.ssl_cert wlog("Loading custom SSL certificate for Meterpreter session") ssl_cert_info = Rex::Socket::SslTcpServer.ssl_parse_pem(self.ssl_cert) wlog("Loaded custom SSL certificate for Meterpreter session") break end # Generate a certificate if necessary and cache it if ! @@ssl_cached_cert @@ssl_mutex.synchronize do wlog("Generating SSL certificate for Meterpreter sessions") @@ssl_cached_cert = Rex::Socket::SslTcpServer.ssl_generate_certificate wlog("Generated SSL certificate for Meterpreter sessions") end end # Use the cached certificate ssl_cert_info = @@ssl_cached_cert break end # Create a new context for each session ctx = OpenSSL::SSL::SSLContext.new() ctx.key = ssl_cert_info[0] ctx.cert = ssl_cert_info[1] ctx.extra_chain_cert = ssl_cert_info[2] ctx. = 0 ctx.session_id_context = Rex::Text.rand_text(16) ctx end |
#init_meterpreter(sock, opts = {}) ⇒ Object
Initializes the meterpreter client instance
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/rex/post/meterpreter/client.rb', line 147 def init_meterpreter(sock,opts={}) self.sock = sock self.parser = PacketParser.new self.ext = ObjectAliases.new self.ext_aliases = ObjectAliases.new self.alive = true self.target_id = opts[:target_id] self.capabilities = opts[:capabilities] || {} self.commands = [] self.last_checkin = ::Time.now self.conn_id = opts[:conn_id] self.url = opts[:url] self.ssl = opts[:ssl] self.c2_profile = opts[:c2_profile] self.pivot_session = opts[:pivot_session] if self.pivot_session self.expiration = self.pivot_session.expiration self.comm_timeout = self.pivot_session.comm_timeout self.retry_total = self.pivot_session.retry_total self.retry_wait = self.pivot_session.retry_wait else self.expiration = opts[:expiration] self.comm_timeout = opts[:comm_timeout] self.retry_total = opts[:retry_total] self.retry_wait = opts[:retry_wait] self.passive_dispatcher = opts[:passive_dispatcher] end self.response_timeout = opts[:timeout] || self.class.default_timeout self.send_keepalives = true # TODO: Clarify why we don't allow unicode to be set in initial options # self.encode_unicode = opts.has_key?(:encode_unicode) ? opts[:encode_unicode] : true self.encode_unicode = false self.aes_key = nil self.session_guid = opts[:session_guid] || "\x00" * 16 # The SSL certificate is being passed down as a file path if opts[:ssl_cert] if ! ::File.exist? opts[:ssl_cert] elog("SSL certificate at #{opts[:ssl_cert]} does not exist and will be ignored") else # Load the certificate the same way that SslTcpServer does it self.ssl_cert = ::File.read(opts[:ssl_cert]) end end # Use the debug build if specified self.debug_build = opts[:debug_build] # Protocol specific dispatch mixins go here, this may be neater with explicit Client classes opts[:dispatch_ext].each {|dx| self.extend(dx)} if opts[:dispatch_ext] initialize_passive_dispatcher if opts[:passive_dispatcher] register_extension_alias('core', ClientCore.new(self)) initialize_inbound_handlers initialize_channels initialize_pivots # Register the channel and pivot inbound packet handlers register_inbound_handler(Rex::Post::Meterpreter::Channel) register_inbound_handler(Rex::Post::Meterpreter::Pivot) monitor_socket end |
#register_extension_alias(name, ext) ⇒ Object
Registers an aliased extension that can be referenced through client.name.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/rex/post/meterpreter/client.rb', line 388 def register_extension_alias(name, ext) self.ext_aliases.aliases[name] = ext # Whee! Syntactic sugar, where art thou? # # Create an instance method on this object called +name+ that returns # +ext+. We have to do it this way instead of simply # self.class.class_eval so that other meterpreter sessions don't get # extension methods when this one does (class << self; self; end).class_eval do define_method(name.to_sym) do ext end end ext end |
#register_extension_aliases(aliases) ⇒ Object
Registers zero or more aliases that are provided in an array.
407 408 409 410 411 |
# File 'lib/rex/post/meterpreter/client.rb', line 407 def register_extension_aliases(aliases) aliases.each { |a| register_extension_alias(a['name'], a['ext']) } end |
#swap_sock_plain_to_ssl ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/rex/post/meterpreter/client.rb', line 218 def swap_sock_plain_to_ssl # Create a new SSL session on the existing socket ctx = generate_ssl_context() ssl = OpenSSL::SSL::SSLSocket.new(sock, ctx) # Use non-blocking OpenSSL operations on Windows if !( ssl.respond_to?(:accept_nonblock) and Rex::Compat.is_windows ) ssl.accept else begin ssl.accept_nonblock # Ruby 1.8.7 and 1.9.0/1.9.1 uses a standard Errno rescue ::Errno::EAGAIN, ::Errno::EWOULDBLOCK IO::select(nil, nil, nil, 0.10) retry # Ruby 1.9.2+ uses IO::WaitReadable/IO::WaitWritable rescue ::Exception => e if ::IO.const_defined?('WaitReadable') and e.kind_of?(::IO::WaitReadable) IO::select( [ ssl ], nil, nil, 0.10 ) retry end if ::IO.const_defined?('WaitWritable') and e.kind_of?(::IO::WaitWritable) IO::select( nil, [ ssl ], nil, 0.10 ) retry end raise e end end self.sock.extend(Rex::Socket::SslTcp) self.sock.sslsock = ssl self.sock.sslctx = ctx self.sock.sslhash = Rex::Text.sha1_raw(ctx.cert.to_der) tag = self.sock.get_once(-1, 30) if(not tag or tag !~ /^GET \//) raise RuntimeError, "Could not read the HTTP hello token" end end |
#swap_sock_ssl_to_plain ⇒ Object
262 263 264 265 266 267 268 269 270 |
# File 'lib/rex/post/meterpreter/client.rb', line 262 def swap_sock_ssl_to_plain # Remove references to the SSLSocket and Context self.sock.sslsock.close self.sock.sslsock = nil self.sock.sslctx = nil self.sock.sslhash = nil self.sock = self.sock.fd self.sock.extend(::Rex::Socket::Tcp) end |
#unicode_filter_decode(str) ⇒ Object
Decodes (or not) a UTF-8 string
441 442 443 |
# File 'lib/rex/post/meterpreter/client.rb', line 441 def unicode_filter_decode(str) self.encode_unicode ? Rex::Text.unicode_filter_decode(str) : str end |
#unicode_filter_encode(str) ⇒ Object
Encodes (or not) a UTF-8 string
434 435 436 |
# File 'lib/rex/post/meterpreter/client.rb', line 434 def unicode_filter_encode(str) self.encode_unicode ? Rex::Text.unicode_filter_encode(str) : str end |
#unwrap_packet(raw_bytes) ⇒ Object
Unwrap the given packet data from any prefixes and suffixes that are stored in the associated C2 profile client configuration (if it exists) and handle decoding of data
136 137 138 139 140 141 142 |
# File 'lib/rex/post/meterpreter/client.rb', line 136 def unwrap_packet(raw_bytes) if self.c2_profile raw_bytes = self.c2_profile.unwrap_inbound_post(raw_bytes) end raw_bytes end |
#wrap_packet(raw_bytes) ⇒ Object
Wrap the given packet data with any prefixes and suffixes that are stored in the associated C2 profile server configuration (if it exists) and handle encoding of data
123 124 125 126 127 128 129 |
# File 'lib/rex/post/meterpreter/client.rb', line 123 def wrap_packet(raw_bytes) if self.c2_profile raw_bytes = self.c2_profile.wrap_outbound_get(raw_bytes) end raw_bytes end |