2005-07-14 22:45:10 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The shell class provides a command-prompt style interface in a
|
|
|
|
# generic fashion.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module Shell
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This module is meant to be mixed into an input medium class instance as a
|
|
|
|
# means of extending it to display a prompt before each call to gets.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module InputShell
|
|
|
|
attr_accessor :prompt, :output
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
def pgets
|
2007-02-11 06:59:22 +00:00
|
|
|
output.print(prompt)
|
|
|
|
output.flush
|
2007-04-04 02:38:15 +00:00
|
|
|
|
|
|
|
output.prompting
|
|
|
|
buf = gets
|
|
|
|
output.prompting(false)
|
|
|
|
|
|
|
|
buf
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Initializes a shell that has a prompt and can be interacted with.
|
|
|
|
#
|
2005-07-14 22:45:10 +00:00
|
|
|
def initialize(prompt, prompt_char = '>')
|
|
|
|
# Set the stop flag to false
|
|
|
|
self.stop_flag = false
|
|
|
|
self.disable_output = false
|
|
|
|
|
|
|
|
# Initialize the prompt
|
|
|
|
self.init_prompt = prompt
|
|
|
|
self.prompt_char = prompt_char
|
2007-01-19 08:46:06 +00:00
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
# Initialize the user interface handles
|
|
|
|
init_ui(Input::Stdio.new, Output::Stdio.new)
|
|
|
|
end
|
2005-07-14 22:45:10 +00:00
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
# Initializes the user interface input/output classes.
|
|
|
|
#
|
|
|
|
def init_ui(in_input = nil, in_output = nil)
|
2007-01-19 08:46:06 +00:00
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
# Initialize the input and output methods
|
|
|
|
self.input = in_input
|
|
|
|
self.output = in_output
|
|
|
|
|
|
|
|
if (self.input)
|
|
|
|
begin
|
2005-11-28 16:36:06 +00:00
|
|
|
if (self.input.supports_readline)
|
|
|
|
self.input = Input::Readline.new(lambda { |str| tab_complete(str) })
|
|
|
|
end
|
2005-07-18 04:07:56 +00:00
|
|
|
rescue
|
|
|
|
end
|
|
|
|
|
|
|
|
# Extend the input medium as an input shell if the input medium
|
|
|
|
# isn't intrinsicly a shell.
|
|
|
|
if (self.input.intrinsic_shell? == false)
|
|
|
|
self.input.extend(InputShell)
|
|
|
|
end
|
|
|
|
|
|
|
|
self.input.output = self.output
|
|
|
|
end
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
update_prompt
|
|
|
|
end
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Resets the user interface handles.
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
def reset_ui
|
|
|
|
init_ui
|
|
|
|
end
|
|
|
|
|
2005-10-02 03:21:26 +00:00
|
|
|
#
|
|
|
|
# Sets the log source that should be used for logging input and output.
|
|
|
|
#
|
|
|
|
def set_log_source(log_source)
|
|
|
|
self.log_source = log_source
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Unsets the log source so that logging becomes disabled.
|
|
|
|
#
|
|
|
|
def unset_log_source
|
|
|
|
set_log_source(nil)
|
|
|
|
end
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Performs tab completion on the supplied string.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def tab_complete(str)
|
|
|
|
return tab_complete_proc(str) if (tab_complete_proc)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Run the command processing loop.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
2005-07-18 04:07:56 +00:00
|
|
|
def run(&block)
|
2005-07-14 22:45:10 +00:00
|
|
|
stop_flag = false
|
|
|
|
|
2008-11-18 19:41:13 +00:00
|
|
|
begin
|
|
|
|
|
|
|
|
while ((line = input.pgets))
|
|
|
|
log_output(input.prompt)
|
|
|
|
|
|
|
|
# If a block was passed in, pass the line to it. If it returns true,
|
|
|
|
# break out of the shell loop.
|
|
|
|
if (block)
|
|
|
|
break if (block.call(line))
|
|
|
|
# Otherwise, call what should be an overriden instance method to
|
|
|
|
# process the line.
|
|
|
|
else
|
|
|
|
run_single(line)
|
|
|
|
end
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2008-11-18 19:41:13 +00:00
|
|
|
# If the stop flag was set or we've hit EOF, break out
|
|
|
|
break if (input.eof? or self.stop_flag)
|
|
|
|
end
|
|
|
|
# Prevent accidental console quits
|
|
|
|
rescue ::Interrupt
|
|
|
|
output.print("Interrupt: use the 'exit' command to quit\n")
|
|
|
|
retry
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Stop processing user input.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def stop
|
|
|
|
self.stop_flag = true
|
|
|
|
end
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Checks to see if the shell has stopped.
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
def stopped?
|
|
|
|
self.stop_flag
|
|
|
|
end
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Change the input prompt.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def update_prompt(prompt = '', new_prompt_char = nil)
|
|
|
|
new_prompt = self.init_prompt + ' ' + prompt + prompt_char + ' '
|
|
|
|
|
|
|
|
# Substitute colors
|
|
|
|
new_prompt.gsub!(/%u/, colorize('underline'))
|
|
|
|
new_prompt.gsub!(/%b/, colorize('bold'))
|
2009-08-18 04:54:58 +00:00
|
|
|
new_prompt.gsub!(/%cya/, colorize('cyan'))
|
2005-07-14 22:45:10 +00:00
|
|
|
new_prompt.gsub!(/%c/, colorize('clear'))
|
|
|
|
new_prompt.gsub!(/%red/, colorize('red'))
|
|
|
|
new_prompt.gsub!(/%grn/, colorize('green'))
|
|
|
|
new_prompt.gsub!(/%blu/, colorize('blue'))
|
|
|
|
new_prompt.gsub!(/%yel/, colorize('yellow'))
|
|
|
|
new_prompt.gsub!(/%whi/, colorize('white'))
|
|
|
|
new_prompt.gsub!(/%mag/, colorize('magenta'))
|
|
|
|
new_prompt.gsub!(/%blk/, colorize('black'))
|
|
|
|
new_prompt.gsub!(/%dred/, colorize('dark', 'red'))
|
|
|
|
new_prompt.gsub!(/%dgrn/, colorize('dark', 'green'))
|
|
|
|
new_prompt.gsub!(/%dblu/, colorize('dark', 'blue'))
|
|
|
|
new_prompt.gsub!(/%dyel/, colorize('dark', 'yellow'))
|
|
|
|
new_prompt.gsub!(/%dcya/, colorize('dark', 'cyan'))
|
|
|
|
new_prompt.gsub!(/%dwhi/, colorize('dark', 'white'))
|
|
|
|
new_prompt.gsub!(/%dmag/, colorize('dark', 'magenta'))
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
self.input.prompt = new_prompt if (self.input)
|
2005-07-14 22:45:10 +00:00
|
|
|
self.prompt_char = new_prompt_char if (new_prompt_char)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Color checks
|
|
|
|
#
|
|
|
|
|
|
|
|
#
|
|
|
|
# Checks to see whether or not colors are supported on this shell
|
2005-11-15 05:22:13 +00:00
|
|
|
# console.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def supports_color?
|
2007-02-16 05:22:36 +00:00
|
|
|
# Color is disabled until we resolve some bugs
|
2007-02-11 19:33:01 +00:00
|
|
|
return false
|
2008-11-10 21:44:58 +00:00
|
|
|
|
|
|
|
term = Rex::Compat.getenv('TERM')
|
|
|
|
(term and term.match(/(?:vt10[03]|xterm(?:-color)?|linux|screen)/i) != nil)
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Resets coloring so that it's back to normal.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def reset_color
|
|
|
|
print(colorize('clear'))
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Returns colorized text if it's supported, otherwise an empty string.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def colorize(*color)
|
2005-10-02 03:21:26 +00:00
|
|
|
return do_colorize(*color)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2007-02-11 19:33:01 +00:00
|
|
|
# Colorize if this shell supports it
|
2005-10-02 03:21:26 +00:00
|
|
|
#
|
2007-02-11 19:33:01 +00:00
|
|
|
def do_colorize(*color)
|
|
|
|
supports_color?() ? Rex::Ui::Text::Color.ansi(*color) : ''
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Output shortcuts
|
|
|
|
#
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Prints an error message to the output handle.
|
|
|
|
#
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_error(msg='')
|
2006-08-29 00:48:33 +00:00
|
|
|
return if (output.nil?)
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
# Errors are not subject to disabled output
|
2005-10-02 03:21:26 +00:00
|
|
|
log_output(output.print_error(msg))
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Prints a status message to the output handle.
|
|
|
|
#
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_status(msg='')
|
2005-07-14 22:45:10 +00:00
|
|
|
return if (disable_output == true)
|
|
|
|
|
2005-10-02 03:21:26 +00:00
|
|
|
log_output(output.print_status(msg))
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Prints a line of text to the output handle.
|
|
|
|
#
|
2007-02-11 19:40:33 +00:00
|
|
|
def print_line(msg='')
|
2005-07-14 22:45:10 +00:00
|
|
|
return if (disable_output == true)
|
|
|
|
|
2005-10-02 03:21:26 +00:00
|
|
|
log_output(output.print_line(msg))
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Prints a raw message to the output handle.
|
|
|
|
#
|
2007-02-11 19:40:33 +00:00
|
|
|
def print(msg='')
|
2005-07-14 22:45:10 +00:00
|
|
|
return if (disable_output == true)
|
2005-10-02 03:21:26 +00:00
|
|
|
log_output(output.print(msg))
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Whether or not output has been disabled.
|
|
|
|
#
|
2005-07-14 22:45:10 +00:00
|
|
|
attr_accessor :disable_output
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# The input handle to read user input from.
|
|
|
|
#
|
|
|
|
attr_reader :input
|
|
|
|
#
|
|
|
|
# The output handle to write output to.
|
|
|
|
#
|
|
|
|
attr_reader :output
|
2005-07-14 22:45:10 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Parse a line into an array of arguments.
|
2005-07-14 22:45:10 +00:00
|
|
|
#
|
|
|
|
def parse_line(line)
|
2005-10-02 03:21:26 +00:00
|
|
|
log_input(line)
|
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
line.gsub!(/(\r|\n)/, '')
|
|
|
|
|
|
|
|
begin
|
|
|
|
return args = Rex::Parser::Arguments.from_s(line)
|
2006-10-29 17:41:19 +00:00
|
|
|
rescue ::ArgumentError
|
2005-07-14 22:45:10 +00:00
|
|
|
print_error("Parse error: #{$!}")
|
|
|
|
end
|
|
|
|
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
2005-10-02 03:21:26 +00:00
|
|
|
#
|
|
|
|
# Print the prompt, but do not log it.
|
|
|
|
#
|
|
|
|
def _print_prompt(prompt)
|
|
|
|
output.print(prompt)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes the supplied input to the log source if one has been registered.
|
|
|
|
#
|
|
|
|
def log_input(buf)
|
|
|
|
rlog(do_colorize("red") + buf + do_colorize("clear"), log_source) if (log_source)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes the supplied output to the log source if one has been registered.
|
|
|
|
#
|
|
|
|
def log_output(buf)
|
|
|
|
rlog(do_colorize("blue") + buf + do_colorize("clear"), log_source) if (log_source)
|
|
|
|
end
|
2005-07-14 22:45:10 +00:00
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_writer :input, :output # :nodoc:
|
|
|
|
attr_accessor :stop_flag, :init_prompt # :nodoc:
|
|
|
|
attr_accessor :prompt_char, :tab_complete_proc # :nodoc:
|
|
|
|
attr_accessor :log_source # :nodoc:
|
2005-07-14 22:45:10 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-12-08 15:37:10 +00:00
|
|
|
###
|
|
|
|
#
|
|
|
|
# Pseudo-shell interface that simply includes the Shell mixin.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class PseudoShell
|
|
|
|
include Shell
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-11-10 21:44:58 +00:00
|
|
|
end end end
|