Module: Rex::Post::Sql::Ui::Console

Includes:
Rex::Post::SessionCompatibleModules, Ui::Text::DispatcherShell
Included in:
MSSQL::Ui::Console, MySQL::Ui::Console, PostgreSQL::Ui::Console
Defined in:
lib/rex/post/sql/ui/console.rb,
lib/rex/post/sql/ui/console/command_dispatcher.rb,
lib/rex/post/sql/ui/console/interactive_sql_client.rb,
lib/rex/post/sql/ui/console/command_dispatcher/core.rb,
lib/rex/post/sql/ui/console/command_dispatcher/client.rb

Overview

Base console class for Generic SQL consoles

Defined Under Namespace

Modules: CommandDispatcher, InteractiveSqlClient

Instance Attribute Summary

Attributes included from Ui::Text::DispatcherShell

#blocked, #busy, #dispatcher_stack

Attributes included from Ui::Text::Shell

#cont_flag, #cont_prompt, #disable_output, #framework, #hist_last_saved, #histfile, #history_manager, #input, #local_hostname, #local_username, #log_source, #name, #on_command_proc, #on_print_proc, #output, #prompt, #prompt_char, #stop_count, #stop_flag, #tab_complete_proc

Instance Method Summary collapse

Methods included from Rex::Post::SessionCompatibleModules

#format_session_compatible_modules, #session_compatible_modules

Methods included from Ui::Text::DispatcherShell

#append_dispatcher, #block_command, #blocked_command?, #current_dispatcher, #destack_dispatcher, #enstack_dispatcher, #initialize, #remove_dispatcher, #run_single, #shellsplitex, #tab_complete, #tab_complete_helper, #tab_complete_stub, #unblock_command, #unknown_command

Methods included from Ui::Text::Shell

#_print_prompt, #get_input_line, #init_tab_complete, #init_ui, #initialize, #log_input, #log_output, #parse_line, #print, #print_error, #print_good, #print_line, #print_status, #print_warning, #prompt_yesno, #reset_ui, #run, #set_log_source, #stop, #stopped?, #supports_color?, #tab_complete, #unset_log_source, #update_prompt, #with_history_manager_context

Methods included from Ui::Text::Resource

#load_resource

Instance Method Details

#current_databaseObject



132
133
134
# File 'lib/rex/post/sql/ui/console.rb', line 132

def current_database
  client.current_database
end

#format_prompt(val) ⇒ String

Parameters:

  • val (Object)

Returns:

  • (String)


117
118
119
# File 'lib/rex/post/sql/ui/console.rb', line 117

def format_prompt(val)
  substitute_colors("%und#{sql_prompt}%clr > ", true)
end

#help_to_s(opts = {}) ⇒ String

Parameters:

  • opts (Hash) (defaults to: {})

Returns:

  • (String)


76
77
78
# File 'lib/rex/post/sql/ui/console.rb', line 76

def help_to_s(opts = {})
  super + format_session_compatible_modules
end

#interact(&block) ⇒ Integer

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

Parameters:

  • block (Proc)

Returns:

  • (Integer)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rex/post/sql/ui/console.rb', line 23

def interact(&block)
  # Run queued commands
  commands.delete_if do |ent|
    run_single(ent)
    true
  end

  # Run the interactive loop
  run do |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
end

#interact_with_client(client_dispatcher: nil) ⇒ Object

Interacts with the supplied client.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rex/post/sql/ui/console.rb', line 101

def interact_with_client(client_dispatcher: nil)
  return unless client_dispatcher

  interact_with_client_notification
  client.extend(InteractiveSqlClient) unless client.is_a?(InteractiveSqlClient)
  client.on_command_proc = self.on_command_proc if self.on_command_proc && client.respond_to?(:on_command_proc)
  client.on_print_proc   = self.on_print_proc if self.on_print_proc && client.respond_to?(:on_print_proc)
  client.on_log_proc = method(:log_output) if self.respond_to?(:log_output, true) && client.respond_to?(:on_log_proc)
  client.client_dispatcher = client_dispatcher

  client.interact(input, output)
  client.reset_ui
end

#interact_with_client_notificationString

Notification to display when initially interacting with the client via the query_interactive command

Returns:

  • (String)


84
85
86
87
88
# File 'lib/rex/post/sql/ui/console.rb', line 84

def interact_with_client_notification
  print_status("Starting interactive SQL shell for #{sql_prompt}")
  print_status('SQL commands ending with ; will be executed on the remote server. Use the %grnexit%clr command to exit.')
  print_line
end

#log_error(msg) ⇒ Object

Log that an error occurred.



124
125
126
127
128
129
130
# File 'lib/rex/post/sql/ui/console.rb', line 124

def log_error(msg)
  print_error(msg)

  elog(msg, session.type)

  dlog("Call stack:\n#{$ERROR_POSITION.join("\n")}", session.type)
end

#queue_cmd(cmd) ⇒ Object

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

Parameters:

  • cmd (Object)

Returns:

  • (Object)


48
49
50
# File 'lib/rex/post/sql/ui/console.rb', line 48

def queue_cmd(cmd)
  self.commands << cmd
end

#run_command(dispatcher, method, arguments) ⇒ FalseClass

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

Parameters:

  • dispatcher (Object)
  • method (Object)
  • arguments (Object)

Returns:

  • (FalseClass)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rex/post/sql/ui/console.rb', line 59

def run_command(dispatcher, method, arguments)
  begin
    super
  rescue ::Timeout::Error
    log_error('Operation timed out.')
  rescue ::Rex::InvalidDestination => e
    log_error(e.message)
  rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError
    self.session.kill
  rescue ::StandardError => e
    log_error("Error running command #{method}: #{e.class} #{e}")
    elog(e)
  end
end

#sql_promptString

Create prompt via client and session data

Returns:

  • (String)


94
95
96
# File 'lib/rex/post/sql/ui/console.rb', line 94

def sql_prompt
  "#{session.type} @ #{client.peerinfo}#{current_database.blank? ? '' : " (#{current_database})"}"
end