Skip to content

Commit

Permalink
rename shell to query_interactive for sql session types, add -i flag
Browse files Browse the repository at this point in the history
  • Loading branch information
zgoldman-r7 committed Feb 27, 2024
1 parent b91430c commit afbd18f
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/rex/post/mssql/ui/console/command_dispatcher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ def name
def cmd_query_help
print_line 'Usage: query'
print_line
print_line 'Run a raw SQL query on the target.'
print_line
print_line 'Run a single SQL query on the target.'
print_line @@query_opts.usage
print_line 'Examples:'
print_line
print_line ' query select @@version;'
Expand Down
4 changes: 2 additions & 2 deletions lib/rex/post/mysql/ui/console/command_dispatcher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def name
def cmd_query_help
print_line 'Usage: query'
print_line
print_line 'Run a raw SQL query on the target.'
print_line
print_line 'Run a single SQL query on the target.'
print_line @@query_opts.usage
print_line 'Examples:'
print_line
print_line ' query SHOW DATABASES;'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ def name
def cmd_query_help
print_line 'Usage: query'
print_line
print_line 'Run a raw SQL query on the target.'
print_line
print_line 'Run a single SQL query on the target.'
print_line @@query_opts.usage
print_line 'Examples:'
print_line
print_line ' query SELECT user;'
Expand Down
31 changes: 21 additions & 10 deletions lib/rex/post/sql/ui/console/command_dispatcher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ module Client

include Rex::Post::Sql::Ui::Console::CommandDispatcher

@@query_opts = Rex::Parser::Arguments.new(
['-h', '--help'] => [false, 'Help menu.'],
['-i', '--interact'] => [false, 'Enter an interactive prompt for running multiple SQL queries'],
)

#
# Initializes an instance of the core command set using the supplied console
# for interactivity.
Expand All @@ -32,8 +37,8 @@ def initialize(console)
#
def commands
cmds = {
'query' => 'Run a raw SQL query',
'shell' => 'Enter a raw shell where SQL queries can be executed',
'query' => 'Run a single SQL query',
'query_interactive' => 'Enter an interactive prompt for running multiple SQL queries',
}

reqs = {}
Expand All @@ -54,17 +59,17 @@ def help_args?(args)
args.include?('-h') || args.include?('--help')
end

def cmd_shell_help
print_line 'Usage: shell'
def cmd_query_interactive_help
print_line 'Usage: query_interactive'
print_line
print_line 'Go into a raw SQL shell where SQL queries can be executed.'
print_line 'Go into an interactive SQL shell where SQL queries can be executed.'
print_line "To exit, type 'exit', 'quit', 'end' or 'stop'."
print_line
end

def cmd_shell(*args)
def cmd_query_interactive(*args)
if help_args?(args)
cmd_shell_help
cmd_query_interactive_help
return
end

Expand Down Expand Up @@ -120,9 +125,15 @@ def run_query(query)
end

def cmd_query(*args)
if help_args?(args)
cmd_query_help
return
@@query_opts.parse(args) do |opt, idx, val|
case opt
when '-h', '--help'
cmd_query_help
return
when '-i', '--interact'
cmd_query_interactive
return
end
end

result = run_query(args.join(' '))
Expand Down
4 changes: 2 additions & 2 deletions lib/rex/post/sql/ui/console/interactive_sql_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def _multiline

if finished
self.interacting = false
print_status 'Exiting Shell mode.'
print_status 'Exiting Interactive mode.'
return { status: :exit, result: nil }
end

Expand All @@ -116,7 +116,7 @@ def _fallback

if stop_words.include? line.chomp.downcase
self.interacting = false
print_status 'Exiting Shell mode.'
print_status 'Exiting Interactive mode.'
return { status: :exit, result: nil }
end

Expand Down
4 changes: 3 additions & 1 deletion lib/rex/ui/text/dispatcher_shell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ def help_to_s(opts={})
# If this dispatcher has no commands, we can't do anything useful.
return "" if commands.nil? or commands.length == 0

# Set increase width of commands column if values longer than 12 characters exist, but cap at 20
command_width = [[12, commands.keys.max_by(&:length).length].max, 20].min
# Display the commands
tbl = Rex::Text::Table.new(
'Header' => "#{self.name} Commands",
Expand All @@ -250,7 +252,7 @@ def help_to_s(opts={})
{
'Command' =>
{
'Width' => 12
'Width' => command_width
}
})

Expand Down

0 comments on commit afbd18f

Please sign in to comment.