2005-07-18 04:07:56 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
require 'rex/post/meterpreter'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Post
|
2005-07-18 05:13:21 +00:00
|
|
|
module Meterpreter
|
|
|
|
module Ui
|
2005-07-18 04:07:56 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Console
|
|
|
|
# -------
|
|
|
|
#
|
|
|
|
# This class provides a shell driven interface to the meterpreter client API.
|
|
|
|
#
|
|
|
|
###
|
2005-07-18 05:13:21 +00:00
|
|
|
class Console
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
include Rex::Ui::Text::DispatcherShell
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
# Dispatchers
|
2005-07-19 04:21:15 +00:00
|
|
|
require 'rex/post/meterpreter/ui/console/interactive_channel'
|
2005-07-18 05:59:27 +00:00
|
|
|
require 'rex/post/meterpreter/ui/console/command_dispatcher'
|
2005-07-18 07:46:54 +00:00
|
|
|
require 'rex/post/meterpreter/ui/console/command_dispatcher/core'
|
2005-07-18 04:07:56 +00:00
|
|
|
|
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
# Initialize the meterpreter console
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def initialize(client)
|
2005-07-18 07:46:54 +00:00
|
|
|
super("%umeterpreter%c")
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
# The meterpreter client context
|
|
|
|
self.client = client
|
|
|
|
|
|
|
|
# Point the input/output handles elsewhere
|
|
|
|
reset_ui
|
|
|
|
|
2005-07-18 07:46:54 +00:00
|
|
|
enstack_dispatcher(Console::CommandDispatcher::Core)
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called when someone wants to interact with the meterpreter client. It's
|
|
|
|
# assumed that init_ui has been called prior.
|
|
|
|
#
|
|
|
|
def interact(&block)
|
2005-07-18 05:13:21 +00:00
|
|
|
run { |line|
|
|
|
|
# Run the command
|
|
|
|
run_single(line)
|
2005-07-18 04:07:56 +00:00
|
|
|
|
|
|
|
# If a block was supplied, call it, otherwise return false
|
|
|
|
if (block)
|
|
|
|
block.call
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
# Interacts with the supplied channel
|
|
|
|
#
|
|
|
|
def interact_with_channel(channel)
|
|
|
|
channel.extend(InteractiveChannel) unless (channel.kind_of?(InteractiveChannel) == true)
|
|
|
|
|
|
|
|
channel.init_ui(input, output)
|
|
|
|
channel.interact
|
|
|
|
channel.reset_ui
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Runs the specified command wrapper in something to catch meterpreter
|
|
|
|
# exceptions.
|
|
|
|
#
|
|
|
|
def run_command(dispatcher, method, arguments)
|
|
|
|
begin
|
|
|
|
super
|
|
|
|
rescue TimeoutError
|
2005-07-23 17:52:29 +00:00
|
|
|
log_error("Operation timed out.")
|
2005-07-19 04:21:15 +00:00
|
|
|
rescue RequestError => info
|
2005-07-23 17:52:29 +00:00
|
|
|
log_error(info.to_s)
|
2005-07-23 05:13:27 +00:00
|
|
|
rescue
|
2005-07-23 17:52:29 +00:00
|
|
|
log_error("Error running command #{method}: #{$!}")
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-23 17:52:29 +00:00
|
|
|
def log_error(msg)
|
|
|
|
print_error(msg)
|
|
|
|
|
|
|
|
elog(msg, 'meterpreter')
|
|
|
|
|
|
|
|
dlog("Call stack:\n#{$@.join("\n")}", 'meterpreter')
|
|
|
|
end
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
attr_reader :client
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_writer :client
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
end
|