2005-07-18 05:13:21 +00:00
|
|
|
require 'rex/ui'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Ui
|
|
|
|
module Text
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# The dispatcher shell class is designed to provide a generic means
|
|
|
|
# of processing various shell commands that may be located in
|
|
|
|
# different modules or chunks of codes. These chunks are referred
|
|
|
|
# to as command dispatchers. The only requirement for command dispatchers is
|
|
|
|
# that they prefix every method that they wish to be mirrored as a command
|
|
|
|
# with the cmd_ prefix.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
module DispatcherShell
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Empty template base class for command dispatchers.
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
###
|
|
|
|
module CommandDispatcher
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Initializes the command dispatcher mixin.
|
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def initialize(shell)
|
|
|
|
self.shell = shell
|
|
|
|
self.tab_complete_items = []
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Returns nil for an empty set of commands.
|
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def commands
|
|
|
|
end
|
2005-11-15 05:22:13 +00:00
|
|
|
|
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# Wraps shell.print_error
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def print_error(msg = '')
|
|
|
|
shell.print_error(msg)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# Wraps shell.print_status
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def print_status(msg = '')
|
|
|
|
shell.print_status(msg)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# Wraps shell.print_line
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def print_line(msg = '')
|
|
|
|
shell.print_line(msg)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2009-02-21 19:22:06 +00:00
|
|
|
# Wraps shell.print
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def print(msg = '')
|
|
|
|
shell.print(msg)
|
|
|
|
end
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
#
|
|
|
|
# Wraps shell.update_prompt
|
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def update_prompt(prompt)
|
|
|
|
shell.update_prompt(prompt)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# No tab completion items by default
|
|
|
|
#
|
|
|
|
attr_accessor :shell, :tab_complete_items
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# DispatcherShell derives from shell.
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
include Shell
|
|
|
|
|
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Initialize the dispatcher shell.
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
def initialize(prompt, prompt_char = '>')
|
|
|
|
super
|
|
|
|
|
2005-11-25 04:11:22 +00:00
|
|
|
# Initialze the dispatcher array
|
2005-07-18 05:13:21 +00:00
|
|
|
self.dispatcher_stack = []
|
2005-11-25 04:11:22 +00:00
|
|
|
|
|
|
|
# Initialize the tab completion array
|
|
|
|
self.tab_words = []
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-25 04:11:22 +00:00
|
|
|
# This method accepts the entire line of text from the Readline
|
|
|
|
# routine, stores all completed words, and passes the partial
|
|
|
|
# word to the real tab completion function. This works around
|
|
|
|
# a design problem in the Readline module and depends on the
|
|
|
|
# Readline.basic_word_break_characters variable being set to \x00
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
def tab_complete(str)
|
2005-11-25 04:11:22 +00:00
|
|
|
# Check trailing whitespace so we can tell 'x' from 'x '
|
|
|
|
str_match = str.match(/\s+$/)
|
|
|
|
str_trail = (str_match.nil?) ? '' : str_match[0]
|
|
|
|
|
|
|
|
# Split the line up by whitespace into words
|
|
|
|
str_words = str.split(/[\s\t\n]+/)
|
|
|
|
|
|
|
|
# Append an empty word if we had trailing whitespace
|
|
|
|
str_words << '' if str_trail.length > 0
|
|
|
|
|
|
|
|
# Place the word list into an instance variable
|
|
|
|
self.tab_words = str_words
|
|
|
|
|
|
|
|
# Pop the last word and pass it to the real method
|
|
|
|
tab_complete_stub(self.tab_words.pop)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Performs tab completion of a command, if supported
|
|
|
|
# Current words can be found in self.tab_words
|
|
|
|
#
|
|
|
|
def tab_complete_stub(str)
|
2005-07-18 05:13:21 +00:00
|
|
|
items = []
|
2005-12-27 03:07:46 +00:00
|
|
|
|
|
|
|
return nil if not str
|
2005-11-23 22:12:49 +00:00
|
|
|
|
|
|
|
# puts "Words(#{tab_words.join(", ")}) Partial='#{str}'"
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
# Next, try to match internal command or value completion
|
|
|
|
# Enumerate each entry in the dispatcher stack
|
|
|
|
dispatcher_stack.each { |dispatcher|
|
2005-11-23 22:12:49 +00:00
|
|
|
|
|
|
|
# If no command is set and it supports commands, add them all
|
|
|
|
if (tab_words.empty? and dispatcher.respond_to?('commands'))
|
2005-07-18 05:13:21 +00:00
|
|
|
items.concat(dispatcher.commands.to_a.map { |x| x[0] })
|
|
|
|
end
|
|
|
|
|
2005-11-23 22:12:49 +00:00
|
|
|
# If the dispatcher exports a tab completion function, use it
|
|
|
|
if(dispatcher.respond_to?('tab_complete_helper'))
|
|
|
|
res = dispatcher.tab_complete_helper(str, tab_words)
|
|
|
|
|
|
|
|
if (res.nil?)
|
|
|
|
# A nil response indicates no optional arguments
|
|
|
|
return [''] if items.empty?
|
|
|
|
else
|
|
|
|
# Otherwise we add the completion items to the list
|
|
|
|
items.concat(res)
|
|
|
|
end
|
|
|
|
end
|
2005-07-18 05:13:21 +00:00
|
|
|
}
|
|
|
|
|
2005-12-25 22:47:38 +00:00
|
|
|
# Verify that our search string is a valid regex
|
|
|
|
begin
|
|
|
|
Regexp.compile(str)
|
|
|
|
rescue RegexpError => e
|
|
|
|
str = Regexp.escape(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
# XXX - This still doesn't fix some Regexp warnings:
|
|
|
|
# ./lib/rex/ui/text/dispatcher_shell.rb:171: warning: regexp has `]' without escape
|
|
|
|
|
2005-11-23 22:12:49 +00:00
|
|
|
# Match based on the partial word
|
2005-07-18 05:13:21 +00:00
|
|
|
items.find_all { |e|
|
|
|
|
e =~ /^#{str}/
|
2005-11-23 22:12:49 +00:00
|
|
|
# Prepend the rest of the command (or it gets replaced!)
|
|
|
|
}.map { |e|
|
|
|
|
tab_words.dup.push(e).join(' ')
|
2005-07-18 05:13:21 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Run a single command line.
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def run_single(line)
|
|
|
|
arguments = parse_line(line)
|
|
|
|
method = arguments.shift
|
|
|
|
found = false
|
2005-11-28 22:06:28 +00:00
|
|
|
error = false
|
2005-07-18 05:13:21 +00:00
|
|
|
|
|
|
|
reset_color if (supports_color?)
|
|
|
|
|
|
|
|
if (method)
|
|
|
|
entries = dispatcher_stack.length
|
|
|
|
|
|
|
|
dispatcher_stack.each { |dispatcher|
|
2006-01-24 03:59:44 +00:00
|
|
|
next if not dispatcher.respond_to?('commands')
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
begin
|
2006-01-24 03:59:44 +00:00
|
|
|
if (dispatcher.commands.has_key?(method))
|
2005-07-19 04:21:15 +00:00
|
|
|
run_command(dispatcher, method, arguments)
|
2005-07-18 07:46:54 +00:00
|
|
|
found = true
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
2005-11-28 22:06:28 +00:00
|
|
|
rescue
|
|
|
|
error = true
|
|
|
|
|
2006-08-29 00:48:33 +00:00
|
|
|
print_error(
|
2005-07-18 07:46:54 +00:00
|
|
|
"Error while running command #{method}: #{$!}" +
|
|
|
|
"\n\nCall stack:\n#{$@.join("\n")}")
|
2005-11-28 22:06:28 +00:00
|
|
|
rescue ::Exception
|
|
|
|
error = true
|
|
|
|
|
2006-08-29 00:48:33 +00:00
|
|
|
print_error(
|
2005-11-28 22:06:28 +00:00
|
|
|
"Error while running command #{method}: #{$!}")
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If the dispatcher stack changed as a result of this command,
|
|
|
|
# break out
|
|
|
|
break if (dispatcher_stack.length != entries)
|
|
|
|
}
|
|
|
|
|
2005-11-28 22:06:28 +00:00
|
|
|
if (found == false and error == false)
|
2005-07-18 05:13:21 +00:00
|
|
|
unknown_command(method, line)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return found
|
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
# Runs the supplied command on the given dispatcher.
|
|
|
|
#
|
|
|
|
def run_command(dispatcher, method, arguments)
|
2007-02-18 04:25:46 +00:00
|
|
|
self.busy = true
|
2007-09-22 20:20:10 +00:00
|
|
|
|
|
|
|
if(blocked_command?(method))
|
|
|
|
print_error("The #{method} command has been disabled.")
|
|
|
|
else
|
|
|
|
dispatcher.send('cmd_' + method, *arguments)
|
|
|
|
end
|
2007-02-18 04:25:46 +00:00
|
|
|
self.busy = false
|
2005-07-19 04:21:15 +00:00
|
|
|
end
|
|
|
|
|
2005-07-18 05:13:21 +00:00
|
|
|
#
|
|
|
|
# If the command is unknown...
|
|
|
|
#
|
|
|
|
def unknown_command(method, line)
|
2006-08-29 00:48:33 +00:00
|
|
|
print_error("Unknown command: #{method}.")
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Push a dispatcher to the front of the stack.
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def enstack_dispatcher(dispatcher)
|
2005-11-23 14:34:11 +00:00
|
|
|
self.dispatcher_stack.unshift(inst = dispatcher.new(self))
|
|
|
|
|
|
|
|
inst
|
2005-07-18 05:13:21 +00:00
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-11-15 05:22:13 +00:00
|
|
|
# Pop a dispatcher from the front of the stacker.
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
2005-07-18 05:13:21 +00:00
|
|
|
def destack_dispatcher
|
|
|
|
self.dispatcher_stack.shift
|
|
|
|
end
|
|
|
|
|
2005-11-23 14:34:11 +00:00
|
|
|
#
|
|
|
|
# Adds the supplied dispatcher to the end of the dispatcher stack so that
|
|
|
|
# it doesn't affect any enstack'd dispatchers.
|
|
|
|
#
|
|
|
|
def append_dispatcher(dispatcher)
|
|
|
|
self.dispatcher_stack.push(inst = dispatcher.new(self))
|
|
|
|
|
|
|
|
inst
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Removes the supplied dispatcher instance.
|
|
|
|
#
|
|
|
|
def remove_dispatcher(name)
|
|
|
|
self.dispatcher_stack.delete_if { |inst|
|
|
|
|
(inst.name == name)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the current active dispatcher
|
|
|
|
#
|
|
|
|
def current_dispatcher
|
|
|
|
self.dispatcher_stack[0]
|
|
|
|
end
|
|
|
|
|
2005-07-19 04:21:15 +00:00
|
|
|
#
|
|
|
|
# Return a readable version of a help banner for all of the enstacked
|
|
|
|
# dispatchers.
|
|
|
|
#
|
|
|
|
def help_to_s(opts = {})
|
|
|
|
str = ''
|
|
|
|
|
|
|
|
dispatcher_stack.reverse.each { |dispatcher|
|
|
|
|
# No commands? Suckage.
|
|
|
|
next if ((dispatcher.respond_to?('commands') == false) or
|
2005-10-17 00:25:07 +00:00
|
|
|
(dispatcher.commands == nil) or
|
2005-07-19 04:21:15 +00:00
|
|
|
(dispatcher.commands.length == 0))
|
|
|
|
|
|
|
|
# Display the commands
|
|
|
|
tbl = Table.new(
|
|
|
|
'Header' => "#{dispatcher.name} Commands",
|
|
|
|
'Indent' => opts['Indent'] || 4,
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
'Command',
|
|
|
|
'Description'
|
|
|
|
],
|
|
|
|
'ColProps' =>
|
|
|
|
{
|
|
|
|
'Command' =>
|
|
|
|
{
|
|
|
|
'MaxWidth' => 12
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
dispatcher.commands.sort.each { |c|
|
|
|
|
tbl << c
|
|
|
|
}
|
|
|
|
|
2009-03-08 07:55:47 +00:00
|
|
|
str << "\n" + tbl.to_s + "\n"
|
2005-07-19 04:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return str
|
|
|
|
end
|
2005-07-18 05:13:21 +00:00
|
|
|
|
|
|
|
|
2007-09-22 20:20:10 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Returns nil for an empty set of blocked commands.
|
|
|
|
#
|
|
|
|
def blocked_command?(cmd)
|
|
|
|
return false if not self.blocked
|
|
|
|
self.blocked.has_key?(cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Block a specific command
|
|
|
|
#
|
|
|
|
def block_command(cmd)
|
|
|
|
self.blocked ||= {}
|
|
|
|
self.blocked[cmd] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Unblock a specific command
|
|
|
|
#
|
|
|
|
def unblock_command(cmd)
|
|
|
|
self.blocked || return
|
|
|
|
self.blocked.delete(cmd)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2005-11-15 05:22:13 +00:00
|
|
|
attr_accessor :dispatcher_stack # :nodoc:
|
2005-11-25 04:11:22 +00:00
|
|
|
attr_accessor :tab_words # :nodoc:
|
2007-02-18 04:25:46 +00:00
|
|
|
attr_accessor :busy # :nodoc:
|
2007-09-22 20:20:10 +00:00
|
|
|
attr_accessor :blocked # :nodoc:
|
2005-07-18 05:13:21 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2009-02-21 19:22:06 +00:00
|
|
|
end
|