Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/rex/post/sql/ui/console.rb
Views: 11789
require 'rex/post/sql/ui/console/command_dispatcher'1require 'rex/post/sql/ui/console/interactive_sql_client'2require 'rex/post/session_compatible_modules'34module Rex5module Post6module Sql7module Ui89#10# Base console class for Generic SQL consoles11#12module Console1314include Rex::Ui::Text::DispatcherShell15include Rex::Post::SessionCompatibleModules1617# Called when someone wants to interact with an SQL client. It's18# assumed that init_ui has been called prior.19#20# @param [Proc] block21# @return [Integer]22def interact(&block)23# Run queued commands24commands.delete_if do |ent|25run_single(ent)26true27end2829# Run the interactive loop30run do |line|31# Run the command32run_single(line)3334# If a block was supplied, call it, otherwise return false35if block36block.call37else38false39end40end41end4243# Queues a command to be run when the interactive loop is entered.44#45# @param [Object] cmd46# @return [Object]47def queue_cmd(cmd)48self.commands << cmd49end5051# Runs the specified command wrapper in something to catch meterpreter52# exceptions.53#54# @param [Object] dispatcher55# @param [Object] method56# @param [Object] arguments57# @return [FalseClass]58def run_command(dispatcher, method, arguments)59begin60super61rescue ::Timeout::Error62log_error('Operation timed out.')63rescue ::Rex::InvalidDestination => e64log_error(e.message)65rescue ::Errno::EPIPE, ::OpenSSL::SSL::SSLError, ::IOError66self.session.kill67rescue ::StandardError => e68log_error("Error running command #{method}: #{e.class} #{e}")69elog(e)70end71end7273# @param [Hash] opts74# @return [String]75def help_to_s(opts = {})76super + format_session_compatible_modules77end7879#80# Notification to display when initially interacting with the client via the query_interactive command81#82# @return [String]83def interact_with_client_notification84print_status("Starting interactive SQL shell for #{sql_prompt}")85print_status('SQL commands ending with ; will be executed on the remote server. Use the %grnexit%clr command to exit.')86print_line87end8889#90# Create prompt via client and session data91#92# @return [String]93def sql_prompt94"#{session.type} @ #{client.peerinfo}#{current_database.blank? ? '' : " (#{current_database})"}"95end9697#98# Interacts with the supplied client.99#100def interact_with_client(client_dispatcher: nil)101return unless client_dispatcher102103interact_with_client_notification104client.extend(InteractiveSqlClient) unless client.is_a?(InteractiveSqlClient)105client.on_command_proc = self.on_command_proc if self.on_command_proc && client.respond_to?(:on_command_proc)106client.on_print_proc = self.on_print_proc if self.on_print_proc && client.respond_to?(:on_print_proc)107client.on_log_proc = method(:log_output) if self.respond_to?(:log_output, true) && client.respond_to?(:on_log_proc)108client.client_dispatcher = client_dispatcher109110client.interact(input, output)111client.reset_ui112end113114# @param [Object] val115# @return [String]116def format_prompt(val)117substitute_colors("%und#{sql_prompt}%clr > ", true)118end119120#121# Log that an error occurred.122#123def log_error(msg)124print_error(msg)125126elog(msg, session.type)127128dlog("Call stack:\n#{$ERROR_POSITION.join("\n")}", session.type)129end130131def current_database132client.current_database133end134end135end136end137end138end139140141