2005-07-14 22:45:10 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2009-11-10 15:33:14 +00:00
|
|
|
# The shell class provides a command-prompt style interface in a
|
2005-07-14 22:45:10 +00:00
|
|
|
# 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
|
|
|
|
|
2011-07-21 18:19:43 +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.
|
|
|
|
#
|
2011-07-21 06:14:25 +00:00
|
|
|
def initialize(prompt, prompt_char = '>', histfile = nil, framework = nil)
|
2005-07-14 22:45:10 +00:00
|
|
|
# Set the stop flag to false
|
|
|
|
self.stop_flag = false
|
|
|
|
self.disable_output = false
|
2009-11-09 00:33:40 +00:00
|
|
|
self.stop_count = 0
|
2005-07-14 22:45:10 +00:00
|
|
|
|
|
|
|
# Initialize the prompt
|
|
|
|
self.init_prompt = prompt
|
|
|
|
self.prompt_char = prompt_char
|
2009-11-10 15:33:14 +00:00
|
|
|
|
2009-11-26 07:13:21 +00:00
|
|
|
self.histfile = histfile
|
2010-08-23 21:19:01 +00:00
|
|
|
self.hist_last_saved = 0
|
2011-07-21 15:35:28 +00:00
|
|
|
|
2011-07-21 06:14:25 +00:00
|
|
|
self.framework = framework
|
2005-07-18 04:07:56 +00:00
|
|
|
end
|
2005-07-14 22:45:10 +00:00
|
|
|
|
2010-05-20 23:47:49 +00:00
|
|
|
def init_tab_complete
|
|
|
|
if (self.input and self.input.supports_readline)
|
|
|
|
self.input = Input::Readline.new(lambda { |str| tab_complete(str) })
|
2010-08-23 20:56:50 +00:00
|
|
|
if Readline::HISTORY.length == 0 and histfile and File.exists?(histfile)
|
2010-08-23 21:19:01 +00:00
|
|
|
File.readlines(histfile).each { |e|
|
|
|
|
Readline::HISTORY << e.chomp
|
|
|
|
}
|
|
|
|
self.hist_last_saved = Readline::HISTORY.length
|
2010-05-20 23:47:49 +00:00
|
|
|
end
|
|
|
|
self.input.output = self.output
|
|
|
|
update_prompt(input.prompt)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
#
|
|
|
|
# Initializes the user interface input/output classes.
|
|
|
|
#
|
|
|
|
def init_ui(in_input = nil, in_output = nil)
|
|
|
|
# Initialize the input and output methods
|
|
|
|
self.input = in_input
|
|
|
|
self.output = in_output
|
|
|
|
|
|
|
|
if (self.input)
|
|
|
|
# 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
|
2009-11-10 15:33:14 +00:00
|
|
|
|
2005-07-18 04:07:56 +00:00
|
|
|
self.input.output = self.output
|
|
|
|
end
|
2011-06-25 20:33:15 +00:00
|
|
|
update_prompt('')
|
2005-07-14 22:45:10 +00:00
|
|
|
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
|
|
|
|
2008-11-18 19:41:13 +00:00
|
|
|
begin
|
2009-11-10 15:33:14 +00:00
|
|
|
|
2009-11-09 00:33:40 +00:00
|
|
|
while true
|
|
|
|
# If the stop flag was set or we've hit EOF, break out
|
|
|
|
break if (self.stop_flag or self.stop_count > 1)
|
|
|
|
|
2010-05-20 23:47:49 +00:00
|
|
|
init_tab_complete
|
2011-07-21 18:19:43 +00:00
|
|
|
|
|
|
|
if framework
|
|
|
|
if input.prompt.include?("%T")
|
|
|
|
t = Time.now
|
|
|
|
if framework.datastore['PromptTimeFormat']
|
|
|
|
t = t.strftime(framework.datastore['PromptTimeFormat'])
|
|
|
|
end
|
|
|
|
input.prompt.gsub!(/%T/, t.to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
if input.prompt.include?("%H")
|
|
|
|
hostname = ENV['HOSTNAME']
|
|
|
|
if hostname.nil?
|
|
|
|
hostname = `hostname`.split('.')[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if hostname is still nil
|
|
|
|
if hostname.nil?
|
|
|
|
hostname = ENV['COMPUTERNAME']
|
|
|
|
end
|
|
|
|
|
|
|
|
if hostname.nil?
|
|
|
|
hostname = 'unknown'
|
|
|
|
end
|
|
|
|
|
|
|
|
input.prompt.gsub!(/%H/, hostname.chomp)
|
|
|
|
end
|
|
|
|
|
|
|
|
if input.prompt.include?("%U")
|
|
|
|
user = ENV['USER']
|
|
|
|
if user.nil?
|
|
|
|
user = `whoami`
|
|
|
|
end
|
|
|
|
|
|
|
|
# check if username is still nil
|
|
|
|
if user.nil?
|
|
|
|
user = ENV['USERNAME']
|
|
|
|
end
|
|
|
|
|
|
|
|
if user.nil?
|
|
|
|
user = 'unknown'
|
|
|
|
end
|
|
|
|
|
|
|
|
input.prompt.gsub!(/%U/, user.chomp)
|
|
|
|
end
|
|
|
|
|
2011-07-26 23:36:30 +00:00
|
|
|
input.prompt.gsub!(/%S/, framework.sessions.length.to_s)
|
|
|
|
input.prompt.gsub!(/%J/, framework.jobs.length.to_s)
|
2011-07-21 18:19:43 +00:00
|
|
|
input.prompt.gsub!(/%L/, Rex::Socket.source_address("50.50.50.50"))
|
|
|
|
input.prompt.gsub!(/%D/, ::Dir.getwd)
|
2011-07-24 18:41:23 +00:00
|
|
|
self.init_prompt = input.prompt
|
2011-07-21 18:19:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
line = input.pgets()
|
2008-11-18 19:41:13 +00:00
|
|
|
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)
|
2009-11-09 00:33:40 +00:00
|
|
|
break if (line == nil or block.call(line))
|
|
|
|
elsif(input.eof? or line == nil)
|
|
|
|
# If you have sessions active, this will give you a shot to exit gravefully
|
|
|
|
# If you really are ambitious, 2 eofs will kick this out
|
|
|
|
self.stop_count += 1
|
|
|
|
next if(self.stop_count > 1)
|
|
|
|
run_single("quit")
|
|
|
|
else
|
2008-11-18 19:41:13 +00:00
|
|
|
# Otherwise, call what should be an overriden instance method to
|
|
|
|
# process the line.
|
2009-11-26 02:18:02 +00:00
|
|
|
ret = run_single(line)
|
|
|
|
# don't bother saving lines that couldn't be found as a
|
2009-11-26 07:13:21 +00:00
|
|
|
# command, create the file if it doesn't exist
|
|
|
|
if ret and self.histfile
|
2010-08-23 21:19:01 +00:00
|
|
|
File.open(self.histfile, "a+") { |f|
|
2009-11-26 02:18:02 +00:00
|
|
|
f.puts(line)
|
2010-08-23 21:19:01 +00:00
|
|
|
}
|
2009-11-26 02:18:02 +00:00
|
|
|
end
|
2009-11-09 00:33:40 +00:00
|
|
|
self.stop_count = 0
|
2008-11-18 19:41:13 +00:00
|
|
|
end
|
2005-07-18 04:07:56 +00:00
|
|
|
|
2008-11-18 19:41:13 +00:00
|
|
|
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
|
|
|
#
|
2011-07-21 06:14:25 +00:00
|
|
|
# prompt - the actual prompt
|
|
|
|
# new_prompt_char the char to append to the prompt
|
|
|
|
# mode - append or not to append - false = append true = make a new prompt
|
|
|
|
def update_prompt(prompt = nil, new_prompt_char = nil, mode = false)
|
2009-11-10 06:58:01 +00:00
|
|
|
if (self.input)
|
2011-07-21 06:14:25 +00:00
|
|
|
if prompt
|
2009-11-10 06:58:01 +00:00
|
|
|
new_prompt = self.init_prompt + ' ' + prompt + prompt_char + ' '
|
|
|
|
else
|
|
|
|
new_prompt = self.prompt || ''
|
|
|
|
end
|
2011-07-21 15:35:28 +00:00
|
|
|
|
2011-07-21 06:14:25 +00:00
|
|
|
if mode
|
|
|
|
new_prompt = prompt + (new_prompt_char || prompt_char) + ' '
|
2011-07-22 01:49:29 +00:00
|
|
|
end
|
2005-07-14 22:45:10 +00:00
|
|
|
|
2009-11-10 06:58:01 +00:00
|
|
|
# Save the prompt before any substitutions
|
|
|
|
self.prompt = new_prompt
|
2005-07-14 22:45:10 +00:00
|
|
|
|
2009-11-10 06:58:01 +00:00
|
|
|
# Set the actual prompt to the saved prompt with any substitutions
|
|
|
|
# or updates from our output driver, be they color or whatever
|
|
|
|
self.input.prompt = self.output.update_prompt(new_prompt)
|
|
|
|
self.prompt_char = new_prompt_char if (new_prompt_char)
|
|
|
|
end
|
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?)
|
|
|
|
|
2011-02-15 05:45:01 +00:00
|
|
|
self.on_print_proc.call(msg) if self.on_print_proc
|
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)
|
|
|
|
|
2011-02-15 05:45:01 +00:00
|
|
|
self.on_print_proc.call(msg) if self.on_print_proc
|
2005-10-02 03:21:26 +00:00
|
|
|
log_output(output.print_status(msg))
|
2005-07-14 22:45:10 +00:00
|
|
|
end
|
|
|
|
|
2010-04-19 22:09:55 +00:00
|
|
|
#
|
|
|
|
# Prints a good message to the output handle.
|
|
|
|
#
|
|
|
|
def print_good(msg='')
|
|
|
|
return if (disable_output == true)
|
|
|
|
|
2011-02-15 05:45:01 +00:00
|
|
|
self.on_print_proc.call(msg) if self.on_print_proc
|
2010-04-19 22:09:55 +00:00
|
|
|
log_output(output.print_good(msg))
|
|
|
|
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)
|
|
|
|
|
2011-02-15 05:45:01 +00:00
|
|
|
self.on_print_proc.call(msg) if self.on_print_proc
|
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)
|
2011-02-15 05:45:01 +00:00
|
|
|
self.on_print_proc.call(msg) if self.on_print_proc
|
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
|
|
|
|
2011-02-15 05:45:01 +00:00
|
|
|
attr_accessor :on_command_proc
|
|
|
|
attr_accessor :on_print_proc
|
2011-07-21 15:35:28 +00:00
|
|
|
attr_accessor :framework
|
2011-02-15 05:45:01 +00:00
|
|
|
|
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)/, '')
|
2009-11-10 15:33:14 +00:00
|
|
|
|
2005-07-14 22:45:10 +00:00
|
|
|
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)
|
2009-11-10 15:55:42 +00:00
|
|
|
rlog(buf, log_source) if (log_source)
|
2005-10-02 03:21:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Writes the supplied output to the log source if one has been registered.
|
|
|
|
#
|
|
|
|
def log_output(buf)
|
2009-11-10 15:55:42 +00:00
|
|
|
rlog(buf, log_source) if (log_source)
|
2005-10-02 03:21:26 +00:00
|
|
|
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:
|
2009-11-10 06:58:01 +00:00
|
|
|
attr_accessor :prompt # :nodoc:
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :prompt_char, :tab_complete_proc # :nodoc:
|
2009-11-26 02:18:02 +00:00
|
|
|
attr_accessor :histfile # :nodoc:
|
2010-08-23 21:19:01 +00:00
|
|
|
attr_accessor :hist_last_saved # the number of history lines when last saved/loaded
|
2009-11-09 00:33:40 +00:00
|
|
|
attr_accessor :log_source, :stop_count # :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
|
2009-11-10 15:33:14 +00:00
|
|
|
|