From f43c07631311d9f11fc37af8a9fa522a552e9e44 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Tue, 12 Apr 2005 06:39:33 +0000 Subject: [PATCH] added eof git-svn-id: file:///home/svn/incoming/trunk@2373 4d416f70-5f16-0410-b530-b9f4589650da --- lib/rex/post/meterpreter/channel.rb | 6 ++++- lib/rex/post/meterpreter/channels/pool.rb | 20 ++++++++++++--- .../post/meterpreter/channels/pools/file.rb | 25 +++++++++++++++++++ .../meterpreter/extensions/stdapi/File.rb | 5 ++++ lib/rex/post/meterpreter/packet.rb | 12 ++++++++- 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/lib/rex/post/meterpreter/channel.rb b/lib/rex/post/meterpreter/channel.rb index 8dcfcb3955..cfa566b347 100644 --- a/lib/rex/post/meterpreter/channel.rb +++ b/lib/rex/post/meterpreter/channel.rb @@ -153,7 +153,11 @@ class Channel request.add_tlv(TLV_TYPE_LENGTH, length) request.add_tlvs(addends) - response = self.client.send_request(request) + begin + response = self.client.send_request(request) + rescue + return nil + end # If the channel is in synchronous mode, the response should contain # data that was read from the remote side of the channel diff --git a/lib/rex/post/meterpreter/channels/pool.rb b/lib/rex/post/meterpreter/channels/pool.rb index e6f853e5b2..80a48e0110 100644 --- a/lib/rex/post/meterpreter/channels/pool.rb +++ b/lib/rex/post/meterpreter/channels/pool.rb @@ -48,6 +48,20 @@ class Pool < Rex::Post::Meterpreter::Channel # ## + # Synonym for tell + def pos + return tell + end + + # Wraps the read operation to raise end-of-file as necessary + def read(length = nil) + if (self.eof) + raise EOFError + end + + return super(length) + end + # Stub for seeking to a different location on the remote half of the # channel def seek(offset, whence = SEEK_SET) @@ -60,10 +74,8 @@ class Pool < Rex::Post::Meterpreter::Channel raise NotImplementedError end - # Synonym for tell - def pos - return tell - end +protected + attr_accessor :_eof end diff --git a/lib/rex/post/meterpreter/channels/pools/file.rb b/lib/rex/post/meterpreter/channels/pools/file.rb index 7755e5d75c..3d6ff96770 100644 --- a/lib/rex/post/meterpreter/channels/pools/file.rb +++ b/lib/rex/post/meterpreter/channels/pools/file.rb @@ -34,6 +34,31 @@ TLV_TYPE_SEEK_POS = TLV_META_TYPE_UINT | (TLV_TEMP + 2) super(client, cid, type, flags) end + ## + # + # I/O operations + # + ## + + # Checks to see if the end-of-file has been reached + def eof + request = Packet.create_request('stdapi_fs_file_eof') + + request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) + + begin + response = self.client.send_request(request) + rescue + return true + end + + if (response.has_tlv?(TLV_TYPE_BOOL)) + return response.get_tlv_value(TLV_TYPE_BOOL) + end + + return false + end + # Seeks to a different location in the file def seek(offset, whence = SEEK_SET) sane = 0 diff --git a/lib/rex/post/meterpreter/extensions/stdapi/File.rb b/lib/rex/post/meterpreter/extensions/stdapi/File.rb index c031a924c0..6c9f44750f 100644 --- a/lib/rex/post/meterpreter/extensions/stdapi/File.rb +++ b/lib/rex/post/meterpreter/extensions/stdapi/File.rb @@ -42,6 +42,11 @@ class File < Rex::Post::Meterpreter::Extensions::Stdapi::IO # IO implementators # ## + + # Returns whether or not the file has reach EOF + def eof + return self.filed.eof + end # Returns the current position of the file pointer def pos diff --git a/lib/rex/post/meterpreter/packet.rb b/lib/rex/post/meterpreter/packet.rb index 683ec920bc..6665a0cbd4 100644 --- a/lib/rex/post/meterpreter/packet.rb +++ b/lib/rex/post/meterpreter/packet.rb @@ -128,7 +128,11 @@ class Tlv elsif (self.type & TLV_META_TYPE_UINT == TLV_META_TYPE_UINT) raw = [value].pack("N") elsif (self.type & TLV_META_TYPE_BOOL == TLV_META_TYPE_BOOL) - raw = [value].pack("c") + if (value == true) + raw = [1].pack("c") + else + raw = [0].pack("c") + end end return [raw.length + 8, self.type].pack("NN") + raw @@ -150,6 +154,12 @@ class Tlv self.value = raw.unpack("NNN")[2] elsif (self.type & TLV_META_TYPE_BOOL == TLV_META_TYPE_BOOL) self.value = raw.unpack("NNc")[2] + + if (self.value == 1) + self.value = true + else + self.value = false + end else self.value = raw[8..raw.length-1] end