2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
|
|
|
require 'msf/base'
|
|
|
|
require 'msf/ui'
|
|
|
|
require 'msf/ui/console/command_dispatcher'
|
2005-07-14 22:58:09 +00:00
|
|
|
require 'msf/ui/console/shell'
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/ui/console/table'
|
2005-07-12 05:39:44 +00:00
|
|
|
require 'find'
|
2005-07-07 06:14:58 +00:00
|
|
|
|
2005-05-22 19:39:21 +00:00
|
|
|
module Msf
|
|
|
|
module Ui
|
|
|
|
module Console
|
|
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# Driver
|
|
|
|
# ------
|
|
|
|
#
|
|
|
|
# This class implements a user interface driver on a console interface.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Driver < Msf::Ui::Driver
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
ConfigGroup = "framework/ui/console"
|
|
|
|
|
2005-05-22 19:39:21 +00:00
|
|
|
include Msf::Ui::Console::Shell
|
|
|
|
|
2005-07-12 05:39:44 +00:00
|
|
|
def initialize(prompt = "%umsf", prompt_char = ">%c")
|
2005-05-22 19:39:21 +00:00
|
|
|
# Initialize attributes
|
2005-07-14 06:34:58 +00:00
|
|
|
self.framework = Msf::Simple::Framework.create
|
2005-05-22 19:39:21 +00:00
|
|
|
self.dispatcher_stack = []
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
# Initialize config
|
|
|
|
Msf::Config.init
|
|
|
|
|
2005-05-22 19:39:21 +00:00
|
|
|
# Add the core command dispatcher as the root of the dispatcher
|
|
|
|
# stack
|
|
|
|
enstack_dispatcher(CommandDispatcher::Core)
|
|
|
|
|
|
|
|
# Initialize the super
|
2005-07-10 08:33:29 +00:00
|
|
|
super
|
2005-07-14 07:13:01 +00:00
|
|
|
|
|
|
|
# Process things before we actually display the prompt and get rocking
|
|
|
|
on_startup
|
2005-07-15 22:30:04 +00:00
|
|
|
|
|
|
|
# Load console-specific configuration
|
|
|
|
load_config
|
|
|
|
|
|
|
|
# Process the resource script
|
|
|
|
process_rc_file
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Loads configuration for the console
|
|
|
|
#
|
|
|
|
def load_config
|
|
|
|
begin
|
|
|
|
conf = Msf::Config.load
|
|
|
|
rescue
|
|
|
|
wlog("Failed to load configuration: #{$!}")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
# If we have configuration, process it
|
|
|
|
if (conf.group?(ConfigGroup))
|
|
|
|
conf[ConfigGroup].each_pair { |k, v|
|
|
|
|
case k
|
|
|
|
when "ActiveModule"
|
|
|
|
run_single("use #{v}")
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Saves configuration for the console
|
|
|
|
#
|
|
|
|
def save_config
|
|
|
|
# Build out the console config group
|
|
|
|
group = {}
|
|
|
|
|
|
|
|
if (active_module)
|
|
|
|
group['ActiveModule'] = active_module.fullname
|
|
|
|
end
|
|
|
|
|
|
|
|
# Save it
|
|
|
|
begin
|
|
|
|
Msf::Config.save(
|
|
|
|
ConfigGroup => group)
|
|
|
|
rescue
|
|
|
|
log_error("Failed to save console config: #{$!}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
#
|
|
|
|
# Processes the resource script file for the console
|
|
|
|
#
|
|
|
|
def process_rc_file
|
2005-07-14 07:13:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Called before things actually get rolling such that banners can be
|
|
|
|
# displayed, scripts can be processed, and other fun can be had.
|
|
|
|
#
|
|
|
|
def on_startup
|
|
|
|
# Prevent output from being displayed for now
|
|
|
|
self.disable_output = true
|
|
|
|
|
|
|
|
# Run a few commands to start things off
|
|
|
|
run_single("search #{File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'modules')}")
|
|
|
|
|
|
|
|
# Re-enable output
|
|
|
|
self.disable_output = false
|
|
|
|
|
|
|
|
# Build the banner message
|
|
|
|
run_single("banner")
|
2005-05-22 19:39:21 +00:00
|
|
|
end
|
|
|
|
|
2005-07-12 05:39:44 +00:00
|
|
|
#
|
|
|
|
# Performs tab completion on shell input if supported
|
|
|
|
#
|
|
|
|
def tab_complete(str)
|
|
|
|
items = []
|
|
|
|
|
|
|
|
# Next, try to match internal command or value completion
|
|
|
|
# Enumerate each entry in the dispatcher stack
|
|
|
|
dispatcher_stack.each { |dispatcher|
|
|
|
|
# If it supports commands, query them all
|
|
|
|
if (dispatcher.respond_to?('commands'))
|
|
|
|
items.concat(dispatcher.commands.to_a.map { |x| x[0] })
|
|
|
|
end
|
|
|
|
|
|
|
|
# If the dispatcher has custom tab completion items, use them
|
|
|
|
items.concat(dispatcher.tab_complete_items || [])
|
|
|
|
}
|
|
|
|
|
|
|
|
items.find_all { |e|
|
|
|
|
e =~ /^#{str}/
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-05-22 19:39:21 +00:00
|
|
|
# Run a single command line
|
|
|
|
def run_single(line)
|
|
|
|
arguments = parse_line(line)
|
|
|
|
method = arguments.shift
|
|
|
|
found = false
|
|
|
|
|
2005-07-10 08:33:29 +00:00
|
|
|
reset_color if (supports_color?)
|
|
|
|
|
2005-05-22 19:39:21 +00:00
|
|
|
if (method)
|
|
|
|
entries = dispatcher_stack.length
|
|
|
|
|
|
|
|
dispatcher_stack.each { |dispatcher|
|
|
|
|
begin
|
2005-07-10 19:21:40 +00:00
|
|
|
if (dispatcher.respond_to?('cmd_' + method))
|
|
|
|
eval("
|
2005-07-14 06:34:58 +00:00
|
|
|
dispatcher.#{'cmd_' + method}(*arguments)
|
2005-07-10 19:21:40 +00:00
|
|
|
found = true")
|
|
|
|
end
|
2005-05-22 19:39:21 +00:00
|
|
|
rescue
|
2005-07-10 23:41:35 +00:00
|
|
|
output.print_error("Error while running command #{method}: #{$!}\n#{$@.join("\n")}\n.")
|
2005-05-22 19:39:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If the dispatcher stack changed as a result of this command,
|
|
|
|
# break out
|
|
|
|
break if (dispatcher_stack.length != entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
output.print_error("Unknown command: #{method}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return found
|
|
|
|
end
|
|
|
|
|
|
|
|
# Push a dispatcher to the front of the stack
|
|
|
|
def enstack_dispatcher(dispatcher)
|
|
|
|
self.dispatcher_stack.unshift(dispatcher.new(self))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pop a dispatcher from the front of the stacker
|
|
|
|
def destack_dispatcher
|
|
|
|
self.dispatcher_stack.shift
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :dispatcher_stack, :framework
|
2005-07-14 06:34:58 +00:00
|
|
|
attr_accessor :active_module
|
2005-05-22 19:39:21 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
attr_writer :dispatcher_stack, :framework
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|