diff --git a/lib/rex/post/meterpreter/channel.rb b/lib/rex/post/meterpreter/channel.rb index 5cdcce2844..9f65c31bd0 100644 --- a/lib/rex/post/meterpreter/channel.rb +++ b/lib/rex/post/meterpreter/channel.rb @@ -141,6 +141,11 @@ class Channel if (cid and client) client.add_channel(self) end + ObjectSpace.define_finalizer( self, self.class.finalize(self.client, self.cid) ) + end + + def self.finalize(client,cid) + proc { self._close(client,cid) } end ## @@ -262,27 +267,29 @@ class Channel # # Closes the channel. # - def _close(addends = nil) - if (self.cid == nil) + def self._close(client, cid, addends=nil) + if (cid == nil) raise IOError, "Channel has been closed.", caller end request = Packet.create_request('core_channel_close') # Populate the request - request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) + request.add_tlv(TLV_TYPE_CHANNEL_ID, cid) request.add_tlvs(addends) - self.client.send_request(request) + client.send_request(request) # Disassociate this channel instance - self.client.remove_channel(self.cid) - - self.cid = nil + client.remove_channel(cid) return true end - + + def _close(addends = nil) + self.class._close(self.client, self.cid, addends) + self.cid = nil + end # # Enables or disables interactive mode. # diff --git a/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log.rb b/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log.rb index a05e969b1c..b209934757 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/sys/event_log.rb @@ -22,7 +22,7 @@ module Sys ### class EventLog - class < Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessSubsystem::Memory.new(self), 'thread' => Rex::Post::Meterpreter::Extensions::Stdapi::Sys::ProcessSubsystem::Thread.new(self), }) + + ObjectSpace.define_finalizer( self, self.class.finalize(self.client, self.handle) ) end + def self.finalize(client,handle) + proc { self.close(client,handle) } + end + # # Returns the executable name of the process. # @@ -303,20 +309,23 @@ class Process < Rex::Post::Process # # Closes the handle to the process that was opened. # - def close + def self.close(client,handle) request = Packet.create_request('stdapi_sys_process_close') - request.add_tlv(TLV_TYPE_HANDLE, handle) - response = client.send_request(request) - handle = nil; - return true end + + # + # Instance method + # + def close(handle) + self.class.close(self.client, handle) + end # - # Block untill this process terminates on the remote side. + # Block until this process terminates on the remote side. # By default we choose not to allow a packet responce timeout to # occur as we may be waiting indefinatly for the process to terminate. # diff --git a/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/image.rb b/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/image.rb index e0023b735b..cd5b68b5da 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/image.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/sys/process_subsystem/image.rb @@ -126,4 +126,4 @@ protected end -end; end; end; end; end; end; end \ No newline at end of file +end; end; end; end; end; end; end diff --git a/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb b/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb index 1c09a72e1a..1080568079 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/sys/registry_subsystem/registry_key.rb @@ -29,6 +29,12 @@ class RegistryKey self.base_key = base_key self.perm = perm self.hkey = hkey + + ObjectSpace.define_finalizer( self, self.class.finalize(self.client, self.hkey) ) + end + + def self.finalize(client,hkey) + proc { self.close(client,hkey) } end ## @@ -99,12 +105,17 @@ class RegistryKey # Closes the open key. This must be called if the registry # key was opened. # - def close() - if (self.hkey != nil) - return self.client.sys.registry.close_key(hkey) + def self.close(client, hkey) + if hkey != nil + return client.sys.registry.close_key(hkey) end - return false + return false + end + + # Instance method for the same + def close() + self.class.close(self.client, self.hkey) end ## diff --git a/lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb b/lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb index f1760e0944..d97643aed6 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/sys/thread.rb @@ -34,6 +34,11 @@ class Thread < Rex::Post::Thread self.process = process self.handle = handle self.tid = tid + ObjectSpace.define_finalizer( self, self.class.finalize(self.process.client, self.handle) ) + end + + def self.finalize(client,handle) + proc { self.close(client,handle) } end ## @@ -153,17 +158,18 @@ class Thread < Rex::Post::Thread # # Closes the thread handle. # - def close + def self.close(client, handle) request = Packet.create_request('stdapi_sys_process_thread_close') - request.add_tlv(TLV_TYPE_THREAD_HANDLE, handle) - - process.client.send_request(request) - + client.send_request(request) handle = nil - return true end + + # Instance method + def self.close + self.class.close(self.process.client, self.handle) + end attr_reader :process, :handle, :tid # :nodoc: protected @@ -171,4 +177,4 @@ protected end -end; end; end; end; end; end \ No newline at end of file +end; end; end; end; end; end