2005-12-17 06:46:23 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-11-28 21:38:48 +00:00
|
|
|
#
|
2010-05-03 17:13:09 +00:00
|
|
|
# $Id$
|
|
|
|
#
|
2005-11-28 21:38:48 +00:00
|
|
|
# This user interface allows users to interact with the framework through a
|
|
|
|
# command line interface (CLI) rather than having to use a prompting console
|
|
|
|
# or web-based interface.
|
|
|
|
#
|
2010-05-03 17:13:09 +00:00
|
|
|
# $Revision$
|
|
|
|
#
|
2005-10-02 05:47:52 +00:00
|
|
|
|
2009-01-30 06:27:10 +00:00
|
|
|
msfbase = __FILE__
|
|
|
|
while File.symlink?(msfbase)
|
|
|
|
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
|
|
|
|
end
|
|
|
|
|
2012-02-04 06:32:37 +00:00
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
|
2005-10-02 05:47:52 +00:00
|
|
|
require 'rex'
|
|
|
|
|
2010-05-03 17:13:09 +00:00
|
|
|
Indent = ' '
|
2005-10-02 05:47:52 +00:00
|
|
|
|
2013-07-24 19:40:46 +00:00
|
|
|
# Payload naming style is kind of inconsistent, so instead of
|
|
|
|
# finding the exact path name, we provide the most educated guess based
|
|
|
|
# on platform/stage type/session type/payload name suffix/etc.
|
|
|
|
def guess_payload_name(p)
|
|
|
|
matches = []
|
|
|
|
payload = p.split('/')
|
|
|
|
platform = payload[0]
|
|
|
|
suffix = payload[-1]
|
|
|
|
stage_types = ['singles', 'stagers', 'stages']
|
|
|
|
session_types = ['meterpreter', 'shell']
|
|
|
|
arch = ''
|
|
|
|
|
|
|
|
# Rule out some possibilities
|
|
|
|
if p =~ /meterpreter/
|
|
|
|
session_types.delete('shell')
|
|
|
|
stage_types.delete('singles')
|
|
|
|
end
|
|
|
|
if p =~ /shell\/.+$/
|
|
|
|
session_types.delete('meterpreter')
|
|
|
|
stage_types.delete('singles')
|
|
|
|
end
|
|
|
|
|
|
|
|
if p =~ /x64/
|
|
|
|
arch = 'x64'
|
|
|
|
elsif p =~ /x86/
|
|
|
|
arch = 'x86'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determine if the payload is staged. If it is, then
|
|
|
|
# we need to load that staged module too.
|
|
|
|
if session_types.include?('shell') and stage_types.include?('stages')
|
|
|
|
if arch == 'x64'
|
|
|
|
matches << /stages\/#{platform}\/x64\/shell/
|
|
|
|
elsif arch == 'x86'
|
|
|
|
matches << /stages\/#{platform}\/x86\/shell/
|
|
|
|
else
|
|
|
|
matches << /stages\/#{platform}\/shell/
|
|
|
|
end
|
|
|
|
elsif session_types.include?('meterpreter') and stage_types.include?('stages')
|
|
|
|
if arch == 'x64'
|
|
|
|
matches << /stages\/#{platform}\/x64\/meterpreter/
|
|
|
|
elsif arch == 'x86'
|
|
|
|
matches << /stages\/#{platform}\/x86\/meterpreter/
|
|
|
|
else
|
|
|
|
matches << /stages\/#{platform}\/meterpreter/
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Guess the second possible match
|
|
|
|
stage_types *= "|"
|
|
|
|
session_types *= "|"
|
|
|
|
|
|
|
|
if arch == 'x64'
|
|
|
|
matches << /payloads\/(#{stage_types})\/#{platform}\/x64\/.*(#{suffix})\.rb$/
|
|
|
|
elsif arch == 'x86'
|
|
|
|
matches << /payloads\/(#{stage_types})\/#{platform}\/x86\/.*(#{suffix})\.rb$/
|
|
|
|
else
|
|
|
|
matches << /payloads\/(#{stage_types})\/#{platform}\/.*(#{suffix})\.rb$/
|
|
|
|
end
|
|
|
|
|
|
|
|
matches
|
|
|
|
end
|
|
|
|
|
|
|
|
def guess_encoder_name(e)
|
|
|
|
[/#{e}/]
|
|
|
|
end
|
|
|
|
|
|
|
|
def guess_nop_name(n)
|
|
|
|
[/#{n}/]
|
|
|
|
end
|
2007-02-21 15:10:56 +00:00
|
|
|
|
2006-01-10 16:07:45 +00:00
|
|
|
def usage (str = nil, extra = nil)
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => "Usage: #{$0} <exploit_name> <option=value> [mode]",
|
|
|
|
'Indent' => 4,
|
|
|
|
'Columns' => ['Mode', 'Description']
|
|
|
|
)
|
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
tbl << ['(H)elp', "You're looking at it baby!"]
|
|
|
|
tbl << ['(S)ummary', 'Show information about this module']
|
|
|
|
tbl << ['(O)ptions', 'Show available options for this module']
|
|
|
|
tbl << ['(A)dvanced', 'Show available advanced options for this module']
|
|
|
|
tbl << ['(I)DS Evasion', 'Show available ids evasion options for this module']
|
|
|
|
tbl << ['(P)ayloads', 'Show available payloads for this module']
|
2007-02-15 07:33:40 +00:00
|
|
|
tbl << ['(T)argets', 'Show available targets for this exploit module']
|
|
|
|
tbl << ['(AC)tions', 'Show available actions for this auxiliary module']
|
2006-09-14 05:33:47 +00:00
|
|
|
tbl << ['(C)heck', 'Run the check routine of the selected module']
|
|
|
|
tbl << ['(E)xecute', 'Execute the selected module']
|
2006-01-10 16:07:45 +00:00
|
|
|
|
|
|
|
$stdout.puts "Error: #{str}\n\n" if str
|
|
|
|
$stdout.puts tbl.to_s + "\n"
|
|
|
|
$stdout.puts extra + "\n" if extra
|
|
|
|
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2009-03-07 18:04:05 +00:00
|
|
|
# Handle the help option before loading modules
|
|
|
|
exploit_name = ARGV.shift
|
|
|
|
exploit = nil
|
|
|
|
module_class = "exploit"
|
|
|
|
|
|
|
|
if(exploit_name == "-h")
|
|
|
|
usage()
|
2013-07-23 04:26:44 +00:00
|
|
|
else
|
|
|
|
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
|
|
|
|
require 'fastlib'
|
|
|
|
require 'msfenv'
|
|
|
|
require 'msf/ui'
|
|
|
|
require 'msf/base'
|
2013-07-24 19:44:07 +00:00
|
|
|
end
|
2009-03-07 18:04:05 +00:00
|
|
|
|
2009-03-07 23:15:53 +00:00
|
|
|
if (not exploit_name)
|
2013-07-24 19:40:46 +00:00
|
|
|
# This is what happens if the user doesn't specify a module name:
|
|
|
|
# msfcli will end up loading EVERYTHING to memory to show you a help
|
|
|
|
# menu plus a list of modules available. Really expensive if you ask me.
|
|
|
|
$stderr.puts "[*] Please wait while we load the module tree..."
|
|
|
|
$framework = Msf::Simple::Framework.create
|
2006-09-14 05:33:47 +00:00
|
|
|
ext = ''
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2005-12-15 05:07:14 +00:00
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'Exploits',
|
|
|
|
'Indent' => 4,
|
|
|
|
'Columns' => [ 'Name', 'Description' ])
|
|
|
|
|
|
|
|
$framework.exploits.each_module { |name, mod|
|
2006-09-14 05:33:47 +00:00
|
|
|
tbl << [ 'exploit/' + name, mod.new.name ]
|
2005-12-15 05:07:14 +00:00
|
|
|
}
|
2006-09-14 05:33:47 +00:00
|
|
|
ext << tbl.to_s + "\n"
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
|
|
|
'Header' => 'Auxiliary',
|
|
|
|
'Indent' => 4,
|
|
|
|
'Columns' => [ 'Name', 'Description' ])
|
2005-12-15 05:07:14 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
$framework.auxiliary.each_module { |name, mod|
|
|
|
|
tbl << [ 'auxiliary/' + name, mod.new.name ]
|
|
|
|
}
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
ext << tbl.to_s + "\n"
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
usage(nil, ext)
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
|
|
|
|
2008-10-04 20:46:54 +00:00
|
|
|
|
2007-02-18 12:27:17 +00:00
|
|
|
# Process special var/val pairs...
|
|
|
|
Msf::Ui::Common.process_cli_arguments($framework, ARGV)
|
|
|
|
|
2013-07-24 19:40:46 +00:00
|
|
|
# Add modules like exploit/aux, encoders, nops we want to load to the list
|
2013-07-24 22:15:56 +00:00
|
|
|
whitelist = []
|
|
|
|
whitelist << /#{exploit_name}/ # Add exploit
|
|
|
|
whitelist << /x86\/single_byte/ # Add default NOP module
|
|
|
|
whitelist << /x86\/shikata_ga_nai/ # Add default encoder
|
|
|
|
whitelist << /generic\/none/ # Add another default encoder
|
2013-07-24 19:40:46 +00:00
|
|
|
ARGV.each { |args|
|
|
|
|
var, val = args.split('=', 2)
|
|
|
|
next if var.nil? or val.nil?
|
2013-07-24 22:15:56 +00:00
|
|
|
whitelist.concat(guess_payload_name(val)) if var =~ /payload/
|
|
|
|
whitelist.concat(guess_encoder_name(val)) if var =~ /encoder/
|
|
|
|
whitelist.concat(guess_nops_name(val)) if var =~ /nops/
|
2013-07-24 19:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$stderr.puts "[*] Initializing modules..."
|
|
|
|
$framework = Msf::Simple::Framework.create({'DeferModuleLoads'=>true})
|
2013-07-24 22:15:56 +00:00
|
|
|
$framework.init_module_paths({:whitelist=>whitelist})
|
2013-07-24 19:40:46 +00:00
|
|
|
if ($framework.modules.module_load_error_by_path.length > 0)
|
|
|
|
print("Warning: The following modules could not be loaded!\n\n")
|
|
|
|
|
|
|
|
$framework.modules.module_load_error_by_path.each do |path, error|
|
|
|
|
print("\t#{path}: #{error}\n\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
# Determine what type of module it is
|
|
|
|
case exploit_name
|
|
|
|
when /exploit\/(.*)/
|
|
|
|
exploit = $framework.exploits.create($1)
|
|
|
|
module_class = 'exploit'
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
when /auxiliary\/(.*)/
|
|
|
|
exploit = $framework.auxiliary.create($1)
|
|
|
|
module_class = 'auxiliary'
|
2007-02-18 12:27:17 +00:00
|
|
|
|
|
|
|
else
|
|
|
|
exploit = $framework.exploits.create(exploit_name)
|
2008-12-20 04:20:42 +00:00
|
|
|
if exploit == nil
|
|
|
|
# Try falling back on aux modules
|
|
|
|
exploit = $framework.auxiliary.create(exploit_name)
|
|
|
|
module_class = 'auxiliary'
|
|
|
|
end
|
2007-02-18 12:27:17 +00:00
|
|
|
|
2006-09-14 05:33:47 +00:00
|
|
|
end
|
2005-10-02 05:47:52 +00:00
|
|
|
|
2008-10-04 20:46:54 +00:00
|
|
|
|
2005-10-02 05:47:52 +00:00
|
|
|
if (exploit == nil)
|
2006-09-14 05:33:47 +00:00
|
|
|
usage("Invalid module: #{exploit_name}")
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
|
|
|
|
2006-02-04 23:59:17 +00:00
|
|
|
exploit.init_ui(
|
2010-05-03 17:13:09 +00:00
|
|
|
Rex::Ui::Text::Input::Stdio.new,
|
2006-02-04 23:59:17 +00:00
|
|
|
Rex::Ui::Text::Output::Stdio.new
|
|
|
|
)
|
2010-05-03 17:13:09 +00:00
|
|
|
|
2006-01-05 22:20:28 +00:00
|
|
|
# Evalulate the command (default to "help")
|
|
|
|
mode = ARGV.pop || 'h'
|
2005-10-02 05:47:52 +00:00
|
|
|
|
|
|
|
# Import options
|
2013-07-11 11:27:57 +00:00
|
|
|
begin
|
|
|
|
exploit.datastore.import_options_from_s(ARGV.join('_|_'), '_|_')
|
2013-07-11 16:36:21 +00:00
|
|
|
rescue Rex::ArgumentParseError => e
|
|
|
|
puts "[!] Error: #{e.message}\n\n"
|
2013-07-11 11:27:57 +00:00
|
|
|
exit
|
|
|
|
end
|
2007-04-28 18:41:09 +00:00
|
|
|
|
|
|
|
# Initialize associated modules
|
|
|
|
payload = nil
|
|
|
|
encoder = nil
|
|
|
|
nop = nil
|
|
|
|
|
|
|
|
if (exploit.datastore['PAYLOAD'])
|
|
|
|
payload = $framework.payloads.create(exploit.datastore['PAYLOAD'])
|
2009-04-03 12:54:58 +00:00
|
|
|
if (payload != nil)
|
2013-07-11 11:27:57 +00:00
|
|
|
payload.datastore.import_options_from_s(ARGV.join('_|_'), '_|_')
|
2009-04-03 12:54:58 +00:00
|
|
|
end
|
2007-04-28 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (exploit.datastore['ENCODER'])
|
|
|
|
encoder = $framework.encoders.create(exploit.datastore['ENCODER'])
|
2009-04-03 12:54:58 +00:00
|
|
|
if (encoder != nil)
|
2013-07-11 11:27:57 +00:00
|
|
|
encoder.datastore.import_options_from_s(ARGV.join('_|_'), '_|_')
|
2009-04-03 12:54:58 +00:00
|
|
|
end
|
2007-04-28 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if (exploit.datastore['NOP'])
|
|
|
|
nop = $framework.nops.create(exploit.datastore['NOP'])
|
2009-04-03 12:54:58 +00:00
|
|
|
if (nop != nil)
|
2013-07-11 11:27:57 +00:00
|
|
|
nop.datastore.import_options_from_s(ARGV.join('_|_'), '_|_')
|
2009-04-03 12:54:58 +00:00
|
|
|
end
|
2007-04-28 18:41:09 +00:00
|
|
|
end
|
|
|
|
|
2013-07-24 19:40:46 +00:00
|
|
|
|
|
|
|
|
2005-10-02 05:47:52 +00:00
|
|
|
case mode.downcase
|
2006-01-10 16:07:45 +00:00
|
|
|
when 'h'
|
|
|
|
usage
|
2005-10-02 05:47:52 +00:00
|
|
|
when "s"
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_module(exploit, Indent))
|
2007-04-28 18:41:09 +00:00
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_module(payload, Indent)) if payload
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_module(encoder, Indent)) if encoder
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_module(nop, Indent)) if nop
|
2005-10-02 05:47:52 +00:00
|
|
|
when "o"
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_options(exploit, Indent))
|
2013-07-11 17:47:26 +00:00
|
|
|
$stdout.puts("\nPayload:\n\n" + Msf::Serializer::ReadableText.dump_options(payload, Indent)) if payload
|
|
|
|
$stdout.puts("\nEncoder:\n\n" + Msf::Serializer::ReadableText.dump_options(encoder, Indent)) if encoder
|
|
|
|
$stdout.puts("\nNOP\n\n" + Msf::Serializer::ReadableText.dump_options(nop, Indent)) if nop
|
2005-10-02 05:47:52 +00:00
|
|
|
when "a"
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_advanced_options(exploit, Indent))
|
2013-07-11 17:47:26 +00:00
|
|
|
$stdout.puts("\nPayload:\n\n" + Msf::Serializer::ReadableText.dump_advanced_options(payload, Indent)) if payload
|
|
|
|
$stdout.puts("\nEncoder:\n\n" + Msf::Serializer::ReadableText.dump_advanced_options(encoder, Indent)) if encoder
|
|
|
|
$stdout.puts("\nNOP:\n\n" + Msf::Serializer::ReadableText.dump_advanced_options(nop, Indent)) if nop
|
2006-01-05 03:57:12 +00:00
|
|
|
when "i"
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_evasion_options(exploit, Indent))
|
2013-07-11 17:47:26 +00:00
|
|
|
$stdout.puts("\nPayload:\n\n" + Msf::Serializer::ReadableText.dump_evasion_options(payload, Indent)) if payload
|
|
|
|
$stdout.puts("\nEncoder:\n\n" + Msf::Serializer::ReadableText.dump_evasion_options(encoder, Indent)) if encoder
|
|
|
|
$stdout.puts("\nNOP:\n\n" + Msf::Serializer::ReadableText.dump_evasion_options(nop, Indent)) if nop
|
2005-10-02 05:47:52 +00:00
|
|
|
when "p"
|
2006-09-14 05:33:47 +00:00
|
|
|
if (module_class == 'exploit')
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_compatible_payloads(exploit, Indent, "Compatible payloads"))
|
|
|
|
else
|
|
|
|
$stdout.puts("\nError: This type of module does not support payloads")
|
|
|
|
end
|
2005-10-02 05:47:52 +00:00
|
|
|
when "t"
|
2006-09-14 05:33:47 +00:00
|
|
|
if (module_class == 'exploit')
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_exploit_targets(exploit, Indent))
|
|
|
|
else
|
|
|
|
$stdout.puts("\nError: This type of module does not support targets")
|
2010-05-03 17:13:09 +00:00
|
|
|
end
|
2007-02-15 07:33:40 +00:00
|
|
|
when "ac"
|
2006-09-14 05:33:47 +00:00
|
|
|
if (module_class == 'auxiliary')
|
|
|
|
$stdout.puts("\n" + Msf::Serializer::ReadableText.dump_auxiliary_actions(exploit, Indent))
|
|
|
|
else
|
|
|
|
$stdout.puts("\nError: This type of module does not support actions")
|
2010-05-03 17:13:09 +00:00
|
|
|
end
|
2005-10-02 05:47:52 +00:00
|
|
|
when "c"
|
2006-09-14 05:33:47 +00:00
|
|
|
if (module_class == 'exploit')
|
|
|
|
begin
|
2007-03-17 19:39:30 +00:00
|
|
|
if (code = exploit.check_simple(
|
|
|
|
'LocalInput' => Rex::Ui::Text::Input::Stdio.new,
|
|
|
|
'LocalOutput' => Rex::Ui::Text::Output::Stdio.new))
|
2006-09-14 05:33:47 +00:00
|
|
|
stat = (code == Msf::Exploit::CheckCode::Vulnerable) ? '[+]' : '[*]'
|
|
|
|
|
|
|
|
$stdout.puts("#{stat} #{code[1]}")
|
|
|
|
else
|
|
|
|
$stderr.puts("Check failed: The state could not be determined.")
|
|
|
|
end
|
|
|
|
rescue
|
|
|
|
$stderr.puts("Check failed: #{$!}")
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
2006-09-14 05:33:47 +00:00
|
|
|
else
|
|
|
|
$stdout.puts("\nError: This type of module does not support the check feature")
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
|
|
|
when "e"
|
2010-10-17 05:16:57 +00:00
|
|
|
con = Msf::Ui::Console::Driver.new(
|
|
|
|
Msf::Ui::Console::Driver::DefaultPrompt,
|
|
|
|
Msf::Ui::Console::Driver::DefaultPromptChar,
|
2011-10-23 12:04:41 +00:00
|
|
|
{
|
2013-07-24 19:40:46 +00:00
|
|
|
'Framework' => $framework,
|
|
|
|
# When I use msfcli, chances are I want speed, so ASCII art fanciness
|
|
|
|
# probably isn't much of a big deal for me.
|
|
|
|
'DisableBanner' => true
|
2010-10-17 05:16:57 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
con.run_single("use #{module_class}/#{exploit.refname}")
|
2011-10-23 12:04:41 +00:00
|
|
|
|
2010-10-17 05:16:57 +00:00
|
|
|
ARGV.each do |arg|
|
|
|
|
k,v = arg.split("=", 2)
|
|
|
|
con.run_single("set #{k} #{v}")
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
2010-10-17 05:16:57 +00:00
|
|
|
|
2013-07-24 23:35:06 +00:00
|
|
|
con.run_single("exploit")
|
2011-10-23 12:04:41 +00:00
|
|
|
|
2010-10-17 05:16:57 +00:00
|
|
|
# If we have sessions or jobs, keep running
|
|
|
|
if $framework.sessions.length > 0 or $framework.jobs.length > 0
|
|
|
|
con.run
|
|
|
|
else
|
|
|
|
con.run_single("quit")
|
2010-05-03 17:13:09 +00:00
|
|
|
end
|
2010-10-17 05:16:57 +00:00
|
|
|
|
2006-01-10 16:07:45 +00:00
|
|
|
else
|
|
|
|
usage("Invalid mode #{mode}")
|
2005-10-02 05:47:52 +00:00
|
|
|
end
|
|
|
|
|
2013-07-24 19:40:46 +00:00
|
|
|
$stdout.puts
|