2011-05-24 20:35:54 +00:00
|
|
|
#!/usr/bin/env ruby
|
2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2013-06-20 15:44:52 +00:00
|
|
|
|
2016-02-01 15:26:32 +00:00
|
|
|
if __FILE__ == $0
|
|
|
|
|
|
|
|
msfbase = __FILE__
|
|
|
|
while File.symlink?(msfbase)
|
|
|
|
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
|
|
|
|
end
|
2011-05-24 20:35:54 +00:00
|
|
|
|
2016-02-01 15:26:32 +00:00
|
|
|
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
|
|
|
|
require 'msfenv'
|
2011-11-21 20:18:57 +00:00
|
|
|
|
2016-02-01 15:26:32 +00:00
|
|
|
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
|
2011-05-24 20:35:54 +00:00
|
|
|
|
2016-02-01 15:26:32 +00:00
|
|
|
require 'rex'
|
|
|
|
require 'msf/ui'
|
|
|
|
require 'msf/base'
|
|
|
|
require 'msf/core/payload_generator'
|
2014-02-05 21:25:14 +00:00
|
|
|
|
2012-06-18 06:20:07 +00:00
|
|
|
|
2013-07-01 22:15:48 +00:00
|
|
|
class MsfVenomError < StandardError; end
|
|
|
|
class UsageError < MsfVenomError; end
|
|
|
|
class NoTemplateError < MsfVenomError; end
|
2013-07-02 06:58:08 +00:00
|
|
|
class IncompatibleError < MsfVenomError; end
|
2013-07-01 22:15:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
require 'optparse'
|
|
|
|
|
2013-07-02 06:58:08 +00:00
|
|
|
|
2013-07-05 20:18:17 +00:00
|
|
|
# Creates a new framework object.
|
|
|
|
#
|
2015-03-30 17:08:52 +00:00
|
|
|
# @note Ignores any previously cached value.
|
2013-07-05 20:18:17 +00:00
|
|
|
# @param (see ::Msf::Simple::Framework.create)
|
|
|
|
# @return [Msf::Framework]
|
2013-07-01 22:15:48 +00:00
|
|
|
def init_framework(create_opts={})
|
|
|
|
create_opts[:module_types] ||= [
|
|
|
|
::Msf::MODULE_PAYLOAD, ::Msf::MODULE_ENCODER, ::Msf::MODULE_NOP
|
|
|
|
]
|
|
|
|
@framework = ::Msf::Simple::Framework.create(create_opts.merge('DisableDatabase' => true))
|
|
|
|
end
|
|
|
|
|
2013-07-05 20:18:17 +00:00
|
|
|
# Cached framework object
|
|
|
|
#
|
|
|
|
# @return [Msf::Framework]
|
2013-07-02 06:58:08 +00:00
|
|
|
def framework
|
|
|
|
return @framework if @framework
|
|
|
|
|
|
|
|
init_framework
|
|
|
|
|
|
|
|
@framework
|
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
|
2013-07-01 22:15:48 +00:00
|
|
|
def parse_args(args)
|
2014-02-05 21:25:14 +00:00
|
|
|
opts = {}
|
|
|
|
datastore = {}
|
2013-07-01 22:15:48 +00:00
|
|
|
opt = OptionParser.new
|
2016-02-01 15:25:34 +00:00
|
|
|
banner = "MsfVenom - a Metasploit standalone payload generator.\n"
|
2015-05-29 15:34:12 +00:00
|
|
|
banner << "Also a replacement for msfpayload and msfencode.\n"
|
|
|
|
banner << "Usage: #{$0} [options] <var=val>"
|
|
|
|
opt.banner = banner
|
2013-07-01 22:15:48 +00:00
|
|
|
opt.separator('')
|
|
|
|
opt.separator('Options:')
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-p', '--payload <payload>', String,
|
|
|
|
'Payload to use. Specify a \'-\' or stdin to use custom payloads') do |p|
|
2013-07-01 22:15:48 +00:00
|
|
|
if p == '-'
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:payload] = 'stdin'
|
2013-07-01 22:15:48 +00:00
|
|
|
else
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:payload] = p
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('--payload-options', "List the payload's standard options") do
|
|
|
|
opts[:list_options] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
opt.on('-l', '--list [type]', Array, 'List a module type. Options are: payloads, encoders, nops, all') do |l|
|
2013-07-01 22:15:48 +00:00
|
|
|
if l.nil? or l.empty?
|
|
|
|
l = ["all"]
|
|
|
|
end
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:list] = l
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-n', '--nopsled <length>', Integer, 'Prepend a nopsled of [length] size on to the payload') do |n|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:nops] = n.to_i
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-f', '--format <format>', String, "Output format (use --help-formats for a list)") do |f|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:format] = f
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('--help-formats', String, "List available formats") do
|
|
|
|
init_framework(:module_types => [])
|
|
|
|
msg = "Executable formats\n" +
|
|
|
|
"\t" + ::Msf::Util::EXE.to_executable_fmt_formats.join(", ") + "\n" +
|
|
|
|
"Transform formats\n" +
|
|
|
|
"\t" + ::Msf::Simple::Buffer.transform_formats.join(", ")
|
|
|
|
raise UsageError, msg
|
|
|
|
end
|
|
|
|
|
|
|
|
opt.on('-e', '--encoder <encoder>', String, 'The encoder to use') do |e|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:encoder] = e
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-a', '--arch <arch>', String, 'The architecture to use') do |a|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:arch] = a
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('--platform <platform>', String, 'The platform of the payload') do |l|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:platform] = l
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-07-16 21:00:04 +00:00
|
|
|
opt.on('--help-platforms', String, 'List available platforms') do
|
|
|
|
init_framework(:module_types => [])
|
|
|
|
supported_platforms = []
|
|
|
|
Msf::Module::Platform.subclasses.each {|c| supported_platforms << "#{c.realname.downcase}"}
|
|
|
|
msg = "Platforms\n" +
|
|
|
|
"\t" + supported_platforms * ", "
|
|
|
|
raise UsageError, msg
|
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-s', '--space <length>', Integer, 'The maximum size of the resulting payload') do |s|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:space] = s
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('--encoder-space <length>', Integer, 'The maximum size of the encoded payload (defaults to the -s value)') do |s|
|
2015-05-18 19:27:52 +00:00
|
|
|
opts[:encoder_space] = s
|
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-b', '--bad-chars <list>', String, 'The list of characters to avoid example: \'\x00\xff\'') do |b|
|
2014-09-20 22:48:03 +00:00
|
|
|
opts[:badchars] = Rex::Text.hex_to_raw(b)
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-i', '--iterations <count>', Integer, 'The number of times to encode the payload') do |i|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:iterations] = i
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-c', '--add-code <path>', String, 'Specify an additional win32 shellcode file to include') do |x|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:add_code] = x
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-x', '--template <path>', String, 'Specify a custom executable file to use as a template') do |x|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:template] = x
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
opt.on('-k', '--keep', 'Preserve the template behavior and inject the payload as a new thread') do
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:keep] = true
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-o', '--out <path>', 'Save the payload') do |x|
|
2014-12-19 22:14:43 +00:00
|
|
|
opts[:out] = x
|
|
|
|
end
|
|
|
|
|
2015-05-18 21:39:52 +00:00
|
|
|
opt.on('-v', '--var-name <name>', String, 'Specify a custom variable name to use for certain output formats') do |x|
|
2014-12-16 05:59:34 +00:00
|
|
|
opts[:var_name] = x
|
|
|
|
end
|
|
|
|
|
2015-05-18 20:24:59 +00:00
|
|
|
opt.on('--smallest', 'Generate the smallest possible payload') do
|
|
|
|
opts[:smallest] = true
|
|
|
|
end
|
|
|
|
|
2013-07-01 22:15:48 +00:00
|
|
|
opt.on_tail('-h', '--help', 'Show this message') do
|
|
|
|
raise UsageError, "#{opt}"
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
2013-07-02 06:58:08 +00:00
|
|
|
opt.parse!(args)
|
2013-07-01 22:15:48 +00:00
|
|
|
rescue OptionParser::InvalidOption => e
|
|
|
|
raise UsageError, "Invalid option\n#{opt}"
|
|
|
|
rescue OptionParser::MissingArgument => e
|
2013-07-02 06:58:08 +00:00
|
|
|
raise UsageError, "Missing required argument for option\n#{opt}"
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
if opts.empty?
|
2013-07-01 22:15:48 +00:00
|
|
|
raise UsageError, "No options\n#{opt}"
|
|
|
|
end
|
|
|
|
|
|
|
|
if args
|
|
|
|
args.each do |x|
|
|
|
|
k,v = x.split('=', 2)
|
2014-02-05 21:25:14 +00:00
|
|
|
datastore[k.upcase] = v.to_s
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
2014-02-24 21:37:01 +00:00
|
|
|
if opts[:payload].to_s =~ /[\_\/]reverse/ and datastore['LHOST'].nil?
|
2014-02-24 21:24:57 +00:00
|
|
|
datastore['LHOST'] = Rex::Socket.source_address
|
2014-02-22 03:51:07 +00:00
|
|
|
end
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
if opts[:payload].nil? # if no payload option is selected assume we are reading it from stdin
|
|
|
|
opts[:payload] = "stdin"
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
if opts[:payload] == 'stdin' and not opts[:list]
|
|
|
|
$stderr.puts "Attempting to read payload from STDIN..."
|
|
|
|
begin
|
|
|
|
::Timeout.timeout(30) do
|
|
|
|
opts[:stdin] = payload_stdin
|
|
|
|
end
|
|
|
|
rescue Timeout::Error
|
|
|
|
opts[:stdin] = ''
|
|
|
|
end
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
opts[:datastore] = datastore
|
|
|
|
|
|
|
|
opts
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
|
2013-07-05 20:18:17 +00:00
|
|
|
# Read a raw payload from stdin (or whatever IO object we're currently
|
|
|
|
# using as stdin, see {#initialize})
|
|
|
|
#
|
|
|
|
# @return [String]
|
2013-07-01 22:15:48 +00:00
|
|
|
def payload_stdin
|
2014-02-05 21:25:14 +00:00
|
|
|
@in = $stdin
|
2013-07-02 06:58:08 +00:00
|
|
|
@in.binmode
|
|
|
|
payload = @in.read
|
2013-07-01 22:15:48 +00:00
|
|
|
payload
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump_payloads
|
|
|
|
init_framework(:module_types => [ ::Msf::MODULE_PAYLOAD ])
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
2016-02-01 15:25:34 +00:00
|
|
|
'Indent' => 4,
|
|
|
|
'Header' => "Framework Payloads (#{framework.stats.num_payloads} total)",
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
"Name",
|
|
|
|
"Description"
|
|
|
|
])
|
2013-07-01 22:15:48 +00:00
|
|
|
|
2013-07-02 06:58:08 +00:00
|
|
|
framework.payloads.each_module { |name, mod|
|
2015-01-29 01:42:06 +00:00
|
|
|
tbl << [ name, mod.new.description.split.join(' ') ]
|
2013-07-01 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"\n" + tbl.to_s + "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump_encoders(arch = nil)
|
|
|
|
init_framework(:module_types => [ ::Msf::MODULE_ENCODER ])
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
2016-02-01 15:25:34 +00:00
|
|
|
'Indent' => 4,
|
|
|
|
'Header' => "Framework Encoders" + ((arch) ? " (architectures: #{arch})" : ""),
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
"Name",
|
|
|
|
"Rank",
|
|
|
|
"Description"
|
|
|
|
])
|
2013-07-01 22:15:48 +00:00
|
|
|
cnt = 0
|
|
|
|
|
2013-07-02 06:58:08 +00:00
|
|
|
framework.encoders.each_module(
|
2013-07-01 22:15:48 +00:00
|
|
|
'Arch' => arch ? arch.split(',') : nil) { |name, mod|
|
|
|
|
tbl << [ name, mod.rank_to_s, mod.new.name ]
|
|
|
|
|
|
|
|
cnt += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
(cnt > 0) ? "\n" + tbl.to_s + "\n" : "\nNo compatible encoders found.\n\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
def dump_nops
|
|
|
|
init_framework(:module_types => [ ::Msf::MODULE_NOP ])
|
|
|
|
tbl = Rex::Ui::Text::Table.new(
|
2016-02-01 15:25:34 +00:00
|
|
|
'Indent' => 4,
|
|
|
|
'Header' => "Framework NOPs (#{framework.stats.num_nops} total)",
|
|
|
|
'Columns' =>
|
|
|
|
[
|
|
|
|
"Name",
|
|
|
|
"Description"
|
|
|
|
])
|
2013-07-01 22:15:48 +00:00
|
|
|
|
2013-07-02 06:58:08 +00:00
|
|
|
framework.nops.each_module { |name, mod|
|
2015-01-29 01:42:06 +00:00
|
|
|
tbl << [ name, mod.new.description.split.join(' ') ]
|
2013-07-01 22:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
"\n" + tbl.to_s + "\n"
|
|
|
|
end
|
|
|
|
|
2013-07-05 20:18:17 +00:00
|
|
|
|
2013-07-01 22:15:48 +00:00
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
begin
|
|
|
|
generator_opts = parse_args(ARGV)
|
|
|
|
rescue MsfVenomError, Msf::OptionValidateError => e
|
2015-07-03 07:38:52 +00:00
|
|
|
$stderr.puts "Error: #{e.message}"
|
2015-08-19 21:16:58 +00:00
|
|
|
exit(1)
|
2014-02-05 21:25:14 +00:00
|
|
|
end
|
2013-07-01 22:15:48 +00:00
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
if generator_opts[:list]
|
|
|
|
generator_opts[:list].each do |mod|
|
|
|
|
case mod.downcase
|
2016-02-04 15:56:07 +00:00
|
|
|
when "payloads", "payload", "p"
|
2016-02-01 15:25:34 +00:00
|
|
|
$stdout.puts dump_payloads
|
2016-02-04 15:56:07 +00:00
|
|
|
when "encoders", "encoder", "e"
|
2016-02-01 15:25:34 +00:00
|
|
|
$stdout.puts dump_encoders(generator_opts[:arch])
|
2016-02-04 15:56:07 +00:00
|
|
|
when "nops", "nop", "n"
|
2016-02-01 15:25:34 +00:00
|
|
|
$stdout.puts dump_nops
|
|
|
|
when "all"
|
|
|
|
# Init here so #dump_payloads doesn't create a framework with
|
|
|
|
# only payloads, etc.
|
|
|
|
init_framework
|
|
|
|
$stdout.puts dump_payloads
|
|
|
|
$stdout.puts dump_encoders
|
|
|
|
$stdout.puts dump_nops
|
|
|
|
else
|
|
|
|
$stderr.puts "Invalid module type. These are valid: payloads, encoders, nops, all"
|
2013-07-05 20:39:25 +00:00
|
|
|
end
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
2014-02-05 21:25:14 +00:00
|
|
|
exit(0)
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
2011-05-25 14:41:29 +00:00
|
|
|
|
2014-02-13 16:19:26 +00:00
|
|
|
if generator_opts[:list_options]
|
|
|
|
payload_mod = framework.payloads.create(generator_opts[:payload])
|
2014-11-09 20:27:59 +00:00
|
|
|
|
|
|
|
if payload_mod.nil?
|
|
|
|
$stderr.puts "Invalid payload: #{generator_opts[:payload]}"
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
|
2015-03-23 01:24:26 +00:00
|
|
|
$stderr.puts "Options for #{payload_mod.fullname}:\n\n"
|
2015-03-23 05:30:18 +00:00
|
|
|
$stdout.puts ::Msf::Serializer::ReadableText.dump_module(payload_mod, ' ')
|
2015-03-23 01:24:26 +00:00
|
|
|
|
|
|
|
$stderr.puts "Advanced options for #{payload_mod.fullname}:\n\n"
|
2015-03-23 05:30:18 +00:00
|
|
|
$stdout.puts ::Msf::Serializer::ReadableText.dump_advanced_options(payload_mod, ' ')
|
2015-03-23 01:24:26 +00:00
|
|
|
|
|
|
|
$stderr.puts "Evasion options for #{payload_mod.fullname}:\n\n"
|
2015-03-23 05:30:18 +00:00
|
|
|
$stdout.puts ::Msf::Serializer::ReadableText.dump_evasion_options(payload_mod, ' ')
|
2014-02-13 16:19:26 +00:00
|
|
|
exit(0)
|
|
|
|
end
|
|
|
|
|
2014-02-05 21:25:14 +00:00
|
|
|
generator_opts[:framework] = framework
|
|
|
|
generator_opts[:cli] = true
|
2012-03-17 01:17:45 +00:00
|
|
|
|
2013-07-01 22:15:48 +00:00
|
|
|
begin
|
2016-02-01 15:25:34 +00:00
|
|
|
venom_generator = Msf::PayloadGenerator.new(generator_opts)
|
2014-02-05 21:25:14 +00:00
|
|
|
payload = venom_generator.generate_payload
|
|
|
|
rescue ::Exception => e
|
2014-11-10 19:25:53 +00:00
|
|
|
elog("#{e.class} : #{e.message}\n#{e.backtrace * "\n"}")
|
2015-07-03 07:38:52 +00:00
|
|
|
$stderr.puts "Error: #{e.message}"
|
2013-07-01 22:15:48 +00:00
|
|
|
end
|
2014-02-05 21:25:14 +00:00
|
|
|
|
2014-12-19 22:35:17 +00:00
|
|
|
# No payload generated, no point to go on
|
2015-08-19 21:16:58 +00:00
|
|
|
exit(2) unless payload
|
2014-12-19 22:35:17 +00:00
|
|
|
|
2014-12-19 22:14:43 +00:00
|
|
|
if generator_opts[:out]
|
|
|
|
begin
|
|
|
|
::File.open(generator_opts[:out], 'wb') do |f|
|
|
|
|
f.write(payload)
|
|
|
|
end
|
|
|
|
$stderr.puts "Saved as: #{generator_opts[:out]}"
|
|
|
|
rescue ::Exception => e
|
|
|
|
# If I can't save it, then I can't save it. I don't think it matters what error.
|
|
|
|
elog("#{e.class} : #{e.message}\n#{e.backtrace * "\n"}")
|
2015-07-03 07:38:52 +00:00
|
|
|
$stderr.puts "Error: #{e.message}"
|
2014-12-19 22:14:43 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
output_stream = $stdout
|
|
|
|
output_stream.binmode
|
|
|
|
output_stream.write payload
|
2015-02-24 16:53:33 +00:00
|
|
|
# trailing newline for pretty output
|
|
|
|
$stderr.puts unless payload =~ /\n$/
|
2014-12-19 22:14:43 +00:00
|
|
|
end
|
2014-02-05 21:25:14 +00:00
|
|
|
|
2013-11-20 23:57:08 +00:00
|
|
|
end
|