class Rex::Post::Meterpreter::Ui::Console

This class provides a shell driven interface to the meterpreter client API.

Public Class Methods

new(client) click to toggle source

Initialize the meterpreter console.

Calls superclass method Rex::Ui::Text::DispatcherShell::new
# File lib/rex/post/meterpreter/ui/console.rb, line 27
def initialize(client)
  if (Rex::Compat.is_windows())
    super("meterpreter")
  else
    super("%undmeterpreter%clr")
  end

  # The meterpreter client context
  self.client = client

  # Queued commands array
  self.commands = []

  # Point the input/output handles elsewhere
  reset_ui

  enstack_dispatcher(Console::CommandDispatcher::Core)

  # Set up logging to whatever logsink 'core' is using
  if ! $dispatcher['meterpreter']
    $dispatcher['meterpreter'] = $dispatcher['core']
  end
end

Public Instance Methods

interact(&block) click to toggle source

Called when someone wants to interact with the meterpreter client. It’s assumed that init_ui has been called prior.

# File lib/rex/post/meterpreter/ui/console.rb, line 55
def interact(&block)
  init_tab_complete

  # Run queued commands
  commands.delete_if { |ent|
    run_single(ent)
    true
  }

  # Run the interactive loop
  run { |line|
    # Run the command
    run_single(line)

    # If a block was supplied, call it, otherwise return false
    if (block)
      block.call
    else
      false
    end
  }
end
interact_with_channel(channel) click to toggle source

Interacts with the supplied channel.

# File lib/rex/post/meterpreter/ui/console.rb, line 81
def interact_with_channel(channel)
  channel.extend(InteractiveChannel) unless (channel.kind_of?(InteractiveChannel) == true)
  channel.on_command_proc = self.on_command_proc if self.on_command_proc
  channel.on_print_proc   = self.on_print_proc if self.on_print_proc

  channel.interact(input, output)
  channel.reset_ui
end
log_error(msg) click to toggle source

Logs that an error occurred and persists the callstack.

# File lib/rex/post/meterpreter/ui/console.rb, line 120
def log_error(msg)
  print_error(msg)

  elog(msg, 'meterpreter')

  dlog("Call stack:\n#{$@.join("\n")}", 'meterpreter')
end
queue_cmd(cmd) click to toggle source

Queues a command to be run when the interactive loop is entered.

# File lib/rex/post/meterpreter/ui/console.rb, line 93
def queue_cmd(cmd)
  self.commands << cmd
end
run_command(dispatcher, method, arguments) click to toggle source

Runs the specified command wrapper in something to catch meterpreter exceptions.

# File lib/rex/post/meterpreter/ui/console.rb, line 101
def run_command(dispatcher, method, arguments)
  begin
    super
  rescue Timeout::Error
    log_error("Operation timed out.")
  rescue RequestError => info
    log_error(info.to_s)
  rescue Rex::AddressInUse => e
    log_error(e.message)
  rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError
    self.client.kill
  rescue  ::Exception => e
    log_error("Error running command #{method}: #{e.class} #{e}")
  end
end