From 3aa45638df1360af9e2f6409533d2b6fcef46fbb Mon Sep 17 00:00:00 2001 From: HD Moore Date: Sun, 2 Apr 2006 16:28:02 +0000 Subject: [PATCH] Added -c, -r, -v options to msfconsole git-svn-id: file:///home/svn/incoming/trunk@3584 4d416f70-5f16-0410-b530-b9f4589650da --- lib/msf/ui/console/driver.rb | 36 ++++++++++++++++++----- msfconsole | 55 +++++++++++++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 8 deletions(-) diff --git a/lib/msf/ui/console/driver.rb b/lib/msf/ui/console/driver.rb index f9626c202f..537131698d 100644 --- a/lib/msf/ui/console/driver.rb +++ b/lib/msf/ui/console/driver.rb @@ -75,7 +75,7 @@ class Driver < Msf::Ui::Driver register_event_handlers # Load console-specific configuration - load_config + load_config(opts['Config']) # Re-enable output self.disable_output = false @@ -84,7 +84,7 @@ class Driver < Msf::Ui::Driver on_startup # Process the resource script - process_rc_file + load_resource(opts['Resource']) # Whether or not command passthru should be allowed self.command_passthru = (opts['AllowCommandPassthru'] == false) ? false : true @@ -112,9 +112,9 @@ class Driver < Msf::Ui::Driver # # Loads configuration for the console. # - def load_config + def load_config(path=nil) begin - conf = Msf::Config.load + conf = Msf::Config.load(path) rescue wlog("Failed to load configuration: #{$!}") return @@ -151,14 +151,36 @@ class Driver < Msf::Ui::Driver end end - # - # TODO: # # Processes the resource script file for the console. # - def process_rc_file + def load_resource(path=nil) + path ||= File.join(Msf::Config.config_directory, 'msfconsole.rc') + return if not File.readable?(path) + + rcfd = File.open(path, 'r') + rcfd.each_line do |line| + print_line("resource> #{line.strip}") + run_single(line.strip) + end + rcfd.close end + # + # Creates the resource script file for the console. + # + def save_resource(data, path=nil) + path ||= File.join(Msf::Config.config_directory, 'msfconsole.rc') + + begin + rcfd = File.open(path, 'w') + rcfd.write(data) + rcfd.close + rescue ::Exception => e + # + end + end + # # Called before things actually get rolling such that banners can be # displayed, scripts can be processed, and other fun can be had. diff --git a/msfconsole b/msfconsole index f5378c680b..dacfc7a904 100755 --- a/msfconsole +++ b/msfconsole @@ -8,8 +8,61 @@ $:.unshift(File.join(File.dirname(__FILE__), 'lib')) require 'rex' require 'msf/ui' +require 'optparse' + +class OptsConsole + # + # Return a hash describing the options. + # + def self.parse(args) + options = {} + + opts = OptionParser.new do |opts| + opts.banner = "Usage: msfconsole [options]" + + opts.separator "" + opts.separator "Specific options:" + + + opts.on("-r", "-r ", "Execute the specified resource file") do |r| + options['Resource'] = r + end + + opts.on("-c", "-c ", "Load the specified configuration file") do |c| + options['Config'] = c + end + + # Boolean switch. + opts.on("-v", "--version", "Show version") do |v| + options['Version'] = true + end + + opts.separator "" + opts.separator "Common options:" + + opts.on_tail("-h", "--help", "Show this message") do + puts opts + exit + end + end + + opts.parse!(args) + + options + end +end + +options = OptsConsole.parse(ARGV) +if (options['Version']) + $stderr.puts 'Framework Version: ' + Msf::Framework::Version + exit +end begin - Msf::Ui::Console::Driver.new.run + Msf::Ui::Console::Driver.new( + Msf::Ui::Console::Driver::DefaultPrompt, + Msf::Ui::Console::Driver::DefaultPromptChar, + options + ).run rescue Interrupt end