From ca422916061fd984b9b1e9edf0c1d84963ef81c0 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Wed, 13 Apr 2005 07:31:11 +0000 Subject: [PATCH] refactored the pool interface to make the code more generic git-svn-id: file:///home/svn/incoming/trunk@2375 4d416f70-5f16-0410-b530-b9f4589650da --- lib/rex/post/meterpreter/channels/pool.rb | 82 ++++++++++++++++--- .../post/meterpreter/channels/pools/file.rb | 81 ------------------ lib/rex/post/meterpreter/packet.rb | 6 +- 3 files changed, 76 insertions(+), 93 deletions(-) diff --git a/lib/rex/post/meterpreter/channels/pool.rb b/lib/rex/post/meterpreter/channels/pool.rb index 8c1adfa858..68e6bb52b1 100644 --- a/lib/rex/post/meterpreter/channels/pool.rb +++ b/lib/rex/post/meterpreter/channels/pool.rb @@ -48,9 +48,23 @@ class Pool < Rex::Post::Meterpreter::Channel # ## - # Synonym for tell - def pos - return tell + # Checks eof + def eof + request = Packet.create_request('core_channel_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 # Wraps the read operation to raise end-of-file as necessary @@ -60,24 +74,70 @@ class Pool < Rex::Post::Meterpreter::Channel rescue data = nil end - - if ((data == nil) && (self.eof)) + + if (((data == nil) || (data.length == 0)) && + (self.eof)) raise EOFError end return data end - # Stub for seeking to a different location on the remote half of the - # channel + # Seeks to a different location in the pool def seek(offset, whence = SEEK_SET) - raise NotImplementedError + sane = 0 + + # Just in case... + case whence + when ::IO::SEEK_SET + sane = 0 + when ::IO::SEEK_CUR + sane = 1 + when ::IO::SEEK_END + sane = 2 + else + raise RuntimeError, "Invalid seek whence #{whence}.", caller + end + + request = Packet.create_request('core_channel_seek') + + request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) + request.add_tlv(TLV_TYPE_SEEK_OFFSET, offset) + request.add_tlv(TLV_TYPE_SEEK_WHENCE, sane) + + begin + response = self.client.send_request(request) + rescue + return -1 + end + + return tell end - # Stub for getting the current position on the remote half of the - # channel + # Synonym for tell + def pos + return tell + end + + # Gets the current position in the pool def tell - raise NotImplementedError + request = Packet.create_request('core_channel_tell') + pos = -1 + + request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) + + begin + response = self.client.send_request(request) + rescue + return pos + end + + # Set the return value to the position that we're at + if (response.has_tlv?(TLV_TYPE_SEEK_POS)) + pos = response.get_tlv_value(TLV_TYPE_SEEK_POS) + end + + return pos end protected diff --git a/lib/rex/post/meterpreter/channels/pools/file.rb b/lib/rex/post/meterpreter/channels/pools/file.rb index 3d6ff96770..09bac83a81 100644 --- a/lib/rex/post/meterpreter/channels/pools/file.rb +++ b/lib/rex/post/meterpreter/channels/pools/file.rb @@ -19,10 +19,6 @@ module Pools ### class File < Rex::Post::Meterpreter::Channels::Pool -TLV_TYPE_SEEK_OFFSET = TLV_META_TYPE_UINT | (TLV_TEMP + 0) -TLV_TYPE_SEEK_WHENCE = TLV_META_TYPE_UINT | (TLV_TEMP + 1) -TLV_TYPE_SEEK_POS = TLV_META_TYPE_UINT | (TLV_TEMP + 2) - ## # # Constructor @@ -34,83 +30,6 @@ 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 - - # Just in case... - case whence - when ::IO::SEEK_SET - sane = 0 - when ::IO::SEEK_CUR - sane = 1 - when ::IO::SEEK_END - sane = 2 - else - raise RuntimeError, "Invalid seek whence #{whence}.", caller - end - - request = Packet.create_request('stdapi_fs_file_seek') - - request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) - request.add_tlv(TLV_TYPE_SEEK_OFFSET, offset) - request.add_tlv(TLV_TYPE_SEEK_WHENCE, sane) - - begin - response = self.client.send_request(request) - rescue - return -1 - end - - return tell - end - - # Gets the current position of the file pointer - def tell - request = Packet.create_request('stdapi_fs_file_tell') - pos = -1 - - request.add_tlv(TLV_TYPE_CHANNEL_ID, self.cid) - - begin - response = self.client.send_request(request) - rescue - return pos - end - - # Set the return value to the position that we're at - if (response.has_tlv?(TLV_TYPE_SEEK_POS)) - pos = response.get_tlv_value(TLV_TYPE_SEEK_POS) - end - - return pos - end - end end; end; end; end; end diff --git a/lib/rex/post/meterpreter/packet.rb b/lib/rex/post/meterpreter/packet.rb index 6665a0cbd4..7f87351805 100644 --- a/lib/rex/post/meterpreter/packet.rb +++ b/lib/rex/post/meterpreter/packet.rb @@ -54,6 +54,10 @@ TLV_TYPE_CHANNEL_DATA = TLV_META_TYPE_RAW | 52 TLV_TYPE_CHANNEL_DATA_GROUP = TLV_META_TYPE_GROUP | 53 TLV_TYPE_CHANNEL_CLASS = TLV_META_TYPE_UINT | 54 +TLV_TYPE_SEEK_WHENCE = TLV_META_TYPE_UINT | 70 +TLV_TYPE_SEEK_OFFSET = TLV_META_TYPE_UINT | 71 +TLV_TYPE_SEEK_POS = TLV_META_TYPE_UINT | 72 + TLV_TYPE_EXCEPTION_CODE = TLV_META_TYPE_UINT | 300 TLV_TYPE_EXCEPTION_STRING = TLV_META_TYPE_STRING | 301 @@ -161,7 +165,7 @@ class Tlv self.value = false end else - self.value = raw[8..raw.length-1] + self.value = raw[8..-1] end return length;