Skip to content

Commit

Permalink
Code clean-up for PostgreSQL session type
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanusz-r7 committed Jan 4, 2024
1 parent 7f9d4fd commit 6230fab
Showing 1 changed file with 8 additions and 83 deletions.
91 changes: 8 additions & 83 deletions lib/rex/post/postgresql/ui/console/command_dispatcher/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,11 @@ def initialize(console)
@db_search_results = []
end

@@db_opts = Rex::Parser::Arguments.new(
["-h", "--help"] => [false, 'Help menu' ],
["-l", "--list"] => [ false, "List all databases"],
["-t", "--tables"] => [ false, "List all tables in the current database"],
["-i", "--interact"] => [ true, "Interact with the supplied database", "database"],
)

#
# List of supported commands.
#
def commands
cmds = {
'db' => 'View the available databases and interact with one',
'tables' => 'View the available tables in the currently selected DB',
'sql' => 'Run a raw SQL query',
'query' => 'Run a raw SQL query',
'shell' => 'Enter a raw shell where SQL queries can be executed',
}
Expand Down Expand Up @@ -93,10 +83,7 @@ def cmd_shell_help
end

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

use_history = true
prompt_proc_before = ::Reline.prompt_proc
Expand All @@ -120,17 +107,16 @@ def cmd_shell(*args)
end

if finished
print_status "Exiting Shell mode."
print_status 'Exiting Shell mode.'
::Reline.prompt_proc = prompt_proc_before
return
end

formatted_query = raw_query.split.map { |word| word.chomp('\\') }.reject(&:empty?).compact.join(' ')

print_status "Running SQL Command: '#{formatted_query}'"
self.cmd_query(formatted_query)
cmd_query(formatted_query)
end

::Reline.prompt_proc = prompt_proc_before
end

def cmd_query_help
Expand Down Expand Up @@ -162,77 +148,16 @@ def format_result(result, mapped_columns = {})
end

def cmd_query(*args)
help_out = args.include?('-h') || args.include?('--help')
self.cmd_query_help && return if help_out
cmd_query_help && return if help_args?(args)

result = self.client.query(args.join(' ').to_s)
table = self.format_result(result)
result = client.query(args.join(' ').to_s)
table = format_result(result)

print_line table.to_s
print_line(table.to_s)
end

alias cmd_sql cmd_query
alias cmd_sql_help cmd_query_help

#
# Open the Pry debugger on the current session
#
def cmd_db(*args)
if args.include?('-h') || args.include?('--help')
cmd_db_help
return
end

method = :list

# Parse options
@@db_opts.parse(args) do |opt, idx, val|
case opt
when '-l', '--list'
method = :list
when '-t', '--tables'
method = :tables
when '-i', '--interact'
method = :interact
end
end

# Perform action
case method
when :tables
# TODO: Print all tables in the current DB
when :list
result = self.client.query('SELECT datname, datdba, encoding, datcollate, datctype, datistemplate FROM pg_database;')
print_line self.format_result(result).to_s
when :interact

end
end

def cmd_db_tabs(_str, words)
return [] if words.length > 1

@@db_opts.option_keys
end

def cmd_db_help
print_line 'Usage: db'
print_line
print_line 'View the databases available on the remote target.'
print_line
end

def cmd_tables
tables = self.client.query('SELECT * FROM information_schema.tables;')
print_line format_result(tables).to_s
end

protected

def print_no_db_selected
print_error("No active database selected")
nil
end
end
end
end
Expand Down

0 comments on commit 6230fab

Please sign in to comment.