From 2173ba8fc8a8cd76361d927d147e7c26f02f3291 Mon Sep 17 00:00:00 2001 From: Matt Miller Date: Tue, 26 Jul 2005 05:15:46 +0000 Subject: [PATCH] channel listing foo git-svn-id: file:///home/svn/incoming/trunk@2836 4d416f70-5f16-0410-b530-b9f4589650da --- lib/rex/post/meterpreter/channel.rb | 8 +- lib/rex/post/meterpreter/channel_container.rb | 13 ++- .../ui/console/command_dispatcher/core.rb | 108 +++++++++++++----- 3 files changed, 98 insertions(+), 31 deletions(-) diff --git a/lib/rex/post/meterpreter/channel.rb b/lib/rex/post/meterpreter/channel.rb index 24e69570c4..44a435c52b 100644 --- a/lib/rex/post/meterpreter/channel.rb +++ b/lib/rex/post/meterpreter/channel.rb @@ -9,9 +9,9 @@ module Meterpreter # # The various types of channels # -CHANNEL_CLASS_STREAM = 1 -CHANNEL_CLASS_DATAGRAM = 2 -CHANNEL_CLASS_POOL = 3 +CHANNEL_CLASS_STREAM = 'stream' +CHANNEL_CLASS_DATAGRAM = 'datagram' +CHANNEL_CLASS_POOL = 'pool' # # The various flags that can affect how the channel operates @@ -298,7 +298,7 @@ class Channel # Stub close handler def dio_close_handler(packet) - client.remove_channel(self) + client.remove_channel(self.cid) # Trap IOErrors as parts of the channel may have already been closed begin diff --git a/lib/rex/post/meterpreter/channel_container.rb b/lib/rex/post/meterpreter/channel_container.rb index d5529cb04b..4b407f25c3 100644 --- a/lib/rex/post/meterpreter/channel_container.rb +++ b/lib/rex/post/meterpreter/channel_container.rb @@ -14,28 +14,39 @@ module Meterpreter ### module ChannelContainer + # # Initializes the channel association hash + # def initialize_channels self.channels = {} end + # # Adds a channel to the container that is indexed by its channel identifier + # def add_channel(channel) self.channels[channel.cid] = channel end + # # Looks up a channel instance based on its channel identifier + # def find_channel(cid) return self.channels[cid] end + # # Removes a channel based on its channel identifier + # def remove_channel(cid) return self.channels.delete(cid) end + attr_reader :channels + protected - attr_accessor :channels + + attr_writer :channels end diff --git a/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb b/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb index 3a38a48873..0ae96b03d5 100644 --- a/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb +++ b/lib/rex/post/meterpreter/ui/console/command_dispatcher/core.rb @@ -25,7 +25,7 @@ class Console::CommandDispatcher::Core end @@use_opts = Rex::Parser::Arguments.new( - "-h" => [ false, "Help banner." ]) + "-h" => [ false, "Help menu." ]) # # List of supported commands @@ -33,6 +33,8 @@ class Console::CommandDispatcher::Core def commands { "?" => "Help menu", + "close" => "Closes a channel", + "channel" => "Displays information about active channels", "exit" => "Terminate the meterpreter session", "help" => "Help menu", "interact" => "Interacts with a channel", @@ -41,7 +43,6 @@ class Console::CommandDispatcher::Core "quit" => "Terminate the meterpreter session", "read" => "Reads data from a channel", "write" => "Writes data to a channel", - "close" => "Closes a channel", } end @@ -52,6 +53,85 @@ class Console::CommandDispatcher::Core "Core" end + # + # Displays information about active channels + # + @@channel_opts = Rex::Parser::Arguments.new( + "-l" => [ false, "List active channels." ], + "-h" => [ false, "Help menu." ]) + + def cmd_channel(*args) + if (args.length == 0) + args.unshift("-h") + end + + mode = nil + + # Parse options + @@channel_opts.parse(args) { |opt, idx, val| + case opt + when "-h" + print( + "Usage: channel [options]\n\n" + + "Displays information about active channels.\n" + + @@channel_opts.usage) + return true + when "-l" + mode = 'list' + end + } + + # No mode, no service. + if (mode == nil) + return true + elsif (mode == 'list') + tbl = Rex::Ui::Text::Table.new( + 'Indent' => 4, + 'Columns' => + [ + 'Id', + 'Class', + 'Type' + ]) + items = 0 + + client.channels.each_pair { |cid, channel| + tbl << [ cid, channel.class.cls, channel.type ] + items += 1 + } + + if (items == 0) + print_line("No active channels.") + else + print("\n" + tbl.to_s + "\n") + end + end + end + + # + # Closes a supplied channel. + # + def cmd_close(*args) + if (args.length == 0) + print_line( + "Usage: close channel_id\n\n" + + "Closes the supplied channel.") + return true + end + + cid = args[0].to_i + channel = client.find_channel(cid) + + if (!channel) + print_error("Invalid channel identifier specified.") + return true + else + channel.close + + print_status("Closed channel #{cid}.") + end + end + # # Terminates the meterpreter session. # @@ -271,30 +351,6 @@ class Console::CommandDispatcher::Core return true end - # - # Closes a supplied channel. - # - def cmd_close(*args) - if (args.length == 0) - print_line( - "Usage: close channel_id\n\n" + - "Closes the supplied channel.") - return true - end - - cid = args[0].to_i - channel = client.find_channel(cid) - - if (!channel) - print_error("Invalid channel identifier specified.") - return true - else - channel.close - - print_status("Closed channel #{cid}.") - end - end - protected attr_accessor :extensions, :ext_hash