2011-05-25 04:35:54 +08:00
#!/usr/bin/env ruby
2012-06-29 13:18:28 +08:00
# -*- coding: binary -*-
2013-06-20 23:44:52 +08:00
2016-02-13 00:49:18 +08:00
class MsfVenomError < StandardError; end
2016-05-10 03:25:23 +08:00
class HelpError < StandardError; end
2016-02-13 00:49:18 +08:00
class UsageError < MsfVenomError; end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
require 'optparse'
2018-03-17 14:30:54 +08:00
require 'timeout'
2013-07-02 06:15:48 +08:00
2020-05-05 19:34:44 +08:00
# Silences warnings as they only serve to confuse end users
if defined?(Warning) && Warning.respond_to?(:[]=)
Warning[:deprecated] = false
end
2020-03-13 01:58:26 +08:00
msfbase = __FILE__
while File.symlink?(msfbase)
msfbase = File.expand_path(File.readlink(msfbase), File.dirname(msfbase))
end
$:.unshift(File.expand_path(File.join(File.dirname(msfbase), 'lib')))
2018-03-17 14:03:09 +08:00
2020-03-13 01:58:26 +08:00
require 'metasploit/framework/profiler'
Metasploit::Framework::Profiler.start
def require_deps
2018-03-17 14:03:09 +08:00
require 'msfenv'
$:.unshift(ENV['MSF_LOCAL_LIB']) if ENV['MSF_LOCAL_LIB']
require 'rex'
require 'msf/ui'
require 'msf/base'
require 'msf/core/payload_generator'
@framework_loaded = true
end
2016-02-13 00:49:18 +08:00
# Creates a new framework object.
#
# @note Ignores any previously cached value.
# @param (see ::Msf::Simple::Framework.create)
# @return [Msf::Framework]
def init_framework(create_opts={})
2018-03-17 14:03:09 +08:00
require_deps unless @framework_loaded
2016-02-13 00:49:18 +08:00
create_opts[:module_types] ||= [
::Msf::MODULE_PAYLOAD, ::Msf::MODULE_ENCODER, ::Msf::MODULE_NOP
]
2018-03-17 15:24:17 +08:00
create_opts[:module_types].map! do |type|
type = Msf.const_get("MODULE_#{type.upcase}")
end
2019-07-26 00:02:48 +08:00
@framework = ::Msf::Simple::Framework.create
2016-02-13 00:49:18 +08:00
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
# Cached framework object
#
# @return [Msf::Framework]
def framework
return @framework if @framework
2013-07-02 14:58:08 +08:00
2016-02-13 00:49:18 +08:00
init_framework
2013-07-02 14:58:08 +08:00
2016-02-13 00:49:18 +08:00
@framework
end
2013-07-02 14:58:08 +08:00
2016-02-13 00:49:18 +08:00
def parse_args(args)
opts = {}
datastore = {}
opt = OptionParser.new
banner = "MsfVenom - a Metasploit standalone payload generator.\n"
banner << "Also a replacement for msfpayload and msfencode.\n"
2018-04-11 00:14:14 +08:00
banner << "Usage: #{$0} [options] <var=val>\n"
banner << "Example: #{$0} -p windows/meterpreter/reverse_tcp LHOST=<IP> -f exe -o payload.exe"
2016-02-13 00:49:18 +08:00
opt.banner = banner
opt.separator('')
opt.separator('Options:')
2018-08-25 03:33:36 +08:00
opt.on('-l', '--list <type>', Array, 'List all modules for [type]. Types are: payloads, encoders, nops, platforms, archs, encrypt, formats, all') do |l|
2018-06-02 07:33:07 +08:00
if l.to_s.empty?
2018-05-15 21:33:41 +08:00
l = ["all"]
end
opts[:list] = l
end
2018-04-11 00:14:14 +08:00
opt.on('-p', '--payload <payload>', String,
2018-05-24 14:34:19 +08:00
"Payload to use (--list payloads to list, --list-options for arguments). Specify '-' or STDIN for custom") do |p|
2016-02-13 00:49:18 +08:00
if p == '-'
opts[:payload] = 'stdin'
else
opts[:payload] = p
2013-07-02 06:15:48 +08:00
end
2016-02-13 00:49:18 +08:00
end
2013-07-02 06:15:48 +08:00
2018-05-24 14:34:19 +08:00
opt.on('--list-options', "List --payload <value>'s standard, advanced and evasion options") do
2016-02-13 00:49:18 +08:00
opts[:list_options] = true
end
2015-05-19 05:39:52 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-f', '--format <format>', String, "Output format (use --list formats to list)") do |f|
2018-11-04 21:57:45 +08:00
opts[:format] = f.downcase
2016-02-13 00:49:18 +08:00
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-e', '--encoder <encoder>', String, 'The encoder to use (use --list encoders to list)') do |e|
opts[:encoder] = e
2016-02-13 00:49:18 +08:00
end
2015-05-19 05:39:52 +08:00
2019-02-14 04:53:56 +08:00
opt.on('--sec-name <value>', String, 'The new section name to use when generating large Windows binaries. Default: random 4-character alpha string') do |s|
2018-10-31 12:39:21 +08:00
opts[:secname] = s
end
2018-05-15 21:33:41 +08:00
opt.on('--smallest', 'Generate the smallest possible payload using all available encoders') do
opts[:smallest] = true
2018-04-11 00:14:14 +08:00
end
2018-05-15 21:33:41 +08:00
opt.on('--encrypt <value>', String, 'The type of encryption or encoding to apply to the shellcode (use --list encrypt to list)') do |e|
opts[:encryption_format] = e
2018-04-11 00:14:14 +08:00
end
2018-05-15 21:33:41 +08:00
opt.on('--encrypt-key <value>', String, 'A key to be used for --encrypt') do |e|
2019-07-20 07:06:59 +08:00
init_framework
opts[:encryption_key] = Rex::Text.dehex(e)
2018-04-11 00:14:14 +08:00
end
2018-05-15 21:33:41 +08:00
opt.on('--encrypt-iv <value>', String, 'An initialization vector for --encrypt') do |e|
2018-04-11 00:14:14 +08:00
opts[:encryption_iv] = e
end
2018-08-25 03:33:36 +08:00
opt.on('-a', '--arch <arch>', String, 'The architecture to use for --payload and --encoders (use --list archs to list)') do |a|
2016-02-13 00:49:18 +08:00
opts[:arch] = a
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
opt.on('--platform <platform>', String, 'The platform for --payload (use --list platforms to list)') do |l|
2016-02-13 00:49:18 +08:00
opts[:platform] = l
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-o', '--out <path>', 'Save the payload to a file') do |x|
opts[:out] = x
end
opt.on('-b', '--bad-chars <list>', String, 'Characters to avoid example: \'\x00\xff\'') do |b|
2019-07-20 07:06:59 +08:00
init_framework
opts[:badchars] = Rex::Text.dehex(b)
2018-05-15 21:33:41 +08:00
end
opt.on('-n', '--nopsled <length>', Integer, 'Prepend a nopsled of [length] size on to the payload') do |n|
opts[:nops] = n.to_i
2016-02-13 00:49:18 +08:00
end
2015-07-17 05:00:04 +08:00
2019-02-14 04:53:56 +08:00
opt.on('--pad-nops', 'Use nopsled size specified by -n <length> as the total payload size, auto-prepending a nopsled of quantity (nops minus payload length)') do
2018-11-21 13:26:03 +08:00
opts[:padnops] = true
end
2018-10-16 03:46:16 +08:00
2018-04-11 00:14:14 +08:00
opt.on('-s', '--space <length>', Integer, 'The maximum size of the resulting payload') do |s|
2016-02-13 00:49:18 +08:00
opts[:space] = s
end
2013-07-02 06:15:48 +08:00
2018-04-11 00:14:14 +08:00
opt.on('--encoder-space <length>', Integer, 'The maximum size of the encoded payload (defaults to the -s value)') do |s|
2016-02-13 00:49:18 +08:00
opts[:encoder_space] = s
end
2015-05-19 03:27:52 +08:00
2018-04-11 00:14:14 +08:00
opt.on('-i', '--iterations <count>', Integer, 'The number of times to encode the payload') do |i|
2016-02-13 00:49:18 +08:00
opts[:iterations] = i
end
2013-07-02 06:15:48 +08:00
2018-04-11 00:14:14 +08:00
opt.on('-c', '--add-code <path>', String, 'Specify an additional win32 shellcode file to include') do |x|
2016-02-13 00:49:18 +08:00
opts[:add_code] = x
end
2013-07-02 06:15:48 +08:00
2018-04-11 00:14:14 +08:00
opt.on('-x', '--template <path>', String, 'Specify a custom executable file to use as a template') do |x|
2016-02-13 00:49:18 +08:00
opts[:template] = x
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-k', '--keep', 'Preserve the --template behaviour and inject the payload as a new thread') do
2016-02-13 00:49:18 +08:00
opts[:keep] = true
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-v', '--var-name <value>', String, 'Specify a custom variable name to use for certain output formats') do |x|
2016-02-13 00:49:18 +08:00
opts[:var_name] = x
end
2014-12-16 13:59:34 +08:00
2018-05-15 21:33:41 +08:00
opt.on('-t', '--timeout <second>', Integer, "The number of seconds to wait when reading the payload from STDIN (default 30, 0 to disable)") do |x|
2018-04-13 19:31:34 +08:00
opts[:timeout] = x
2018-04-13 19:16:11 +08:00
end
2016-02-13 00:49:18 +08:00
opt.on_tail('-h', '--help', 'Show this message') do
2016-05-10 03:25:23 +08:00
raise HelpError, "#{opt}"
2016-02-13 00:49:18 +08:00
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
begin
opt.parse!(args)
rescue OptionParser::InvalidOption => e
raise UsageError, "Invalid option\n#{opt}"
rescue OptionParser::MissingArgument => e
raise UsageError, "Missing required argument for option\n#{opt}"
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
if opts.empty?
raise UsageError, "No options\n#{opt}"
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
if args
args.each do |x|
k,v = x.split('=', 2)
datastore[k.upcase] = v.to_s
2013-07-02 06:15:48 +08:00
end
2018-05-24 14:34:19 +08:00
if opts[:payload].to_s =~ /[\_\/]reverse/ && datastore['LHOST'].nil?
2019-07-20 07:06:59 +08:00
init_framework
2016-02-13 00:49:18 +08:00
datastore['LHOST'] = Rex::Socket.source_address
2013-07-02 06:15:48 +08:00
end
2016-02-13 00:49:18 +08:00
end
if opts[:payload].nil? # if no payload option is selected assume we are reading it from stdin
opts[:payload] = "stdin"
end
2013-07-02 06:15:48 +08:00
2018-05-24 14:34:19 +08:00
if opts[:payload].downcase == 'stdin' && !opts[:list]
2016-02-13 00:49:18 +08:00
$stderr.puts "Attempting to read payload from STDIN..."
begin
2018-04-13 19:31:34 +08:00
opts[:timeout] ||= 30
2018-04-13 19:16:11 +08:00
::Timeout.timeout(opts[:timeout]) do
2016-02-13 00:49:18 +08:00
opts[:stdin] = payload_stdin
2014-02-06 05:25:14 +08:00
end
2016-02-13 00:49:18 +08:00
rescue Timeout::Error
opts[:stdin] = ''
2013-07-02 06:15:48 +08:00
end
end
2016-02-13 00:49:18 +08:00
opts[:datastore] = datastore
2014-02-06 05:25:14 +08:00
2016-02-13 00:49:18 +08:00
opts
end
# Read a raw payload from stdin (or whatever IO object we're currently
# using as stdin, see {#initialize})
#
# @return [String]
def payload_stdin
@in = $stdin
@in.binmode
payload = @in.read
payload
end
2013-07-02 06:15:48 +08:00
2018-05-15 21:33:41 +08:00
def dump_platforms
init_framework(:module_types => [])
supported_platforms = []
2018-05-24 14:34:19 +08:00
Msf::Module::Platform.subclasses.each {|c| supported_platforms << c.realname.downcase}
2018-05-15 21:33:41 +08:00
tbl = Rex::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Platforms [--platform <value>]",
'Columns' =>
[
"Name",
])
supported_platforms.sort.each do |name|
2018-08-24 10:53:49 +08:00
tbl << [name]
end
"\n" + tbl.to_s + "\n"
end
2018-08-25 03:33:36 +08:00
def dump_archs
2018-08-24 10:53:49 +08:00
init_framework(:module_types => [])
2018-08-25 03:33:36 +08:00
supported_archs = ARCH_ALL.dup
2018-08-24 10:53:49 +08:00
tbl = Rex::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Architectures [--arch <value>]",
'Columns' =>
[
"Name",
])
2018-08-25 03:33:36 +08:00
supported_archs.sort.each do |name|
2018-08-24 10:53:49 +08:00
tbl << [name]
2018-05-15 21:33:41 +08:00
end
"\n" + tbl.to_s + "\n"
end
def dump_encrypt
init_framework(:module_types => [])
tbl = Rex::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Encryption Formats [--encrypt <value>]",
'Columns' =>
[
"Name",
])
::Msf::Simple::Buffer.encryption_formats.each do |name|
tbl << [ name]
end
"\n" + tbl.to_s + "\n"
end
def dump_formats
init_framework(:module_types => [])
tbl1 = Rex::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Executable Formats [--format <value>]",
'Columns' =>
[
"Name"
])
::Msf::Util::EXE.to_executable_fmt_formats.each do |name|
tbl1 << [ name ]
end
tbl2 = Rex::Text::Table.new(
'Indent' => 4,
'Header' => "Framework Transform Formats [--format <value>]",
'Columns' =>
[
"Name"
])
::Msf::Simple::Buffer.transform_formats.each do |name|
tbl2 << [ name ]
end
"\n" + tbl1.to_s + "\n" + tbl2.to_s + "\n"
end
2020-03-05 09:22:01 +08:00
def dump_payloads(platform = nil, arch = nil)
2018-03-17 15:24:17 +08:00
init_framework(:module_types => [ :payload ])
2016-08-11 02:30:09 +08:00
tbl = Rex::Text::Table.new(
2016-02-13 00:49:18 +08:00
'Indent' => 4,
2018-05-15 21:33:41 +08:00
'Header' => "Framework Payloads (#{framework.stats.num_payloads} total) [--payload <value>]",
2016-02-13 00:49:18 +08:00
'Columns' =>
[
"Name",
"Description"
])
2020-03-05 09:22:01 +08:00
framework.payloads.each_module(
'Platform' => platform ? Msf::Module::PlatformList.transform(platform.split(',')) : nil,
'Arch' => arch ? arch.split(',') : nil) do |name, mod|
tbl << [ name, mod.new.description.split.join(' ') ]
end
2016-02-13 00:49:18 +08:00
"\n" + tbl.to_s + "\n"
end
def dump_encoders(arch = nil)
2018-03-17 15:24:17 +08:00
init_framework(:module_types => [ :encoder ])
2016-08-11 02:30:09 +08:00
tbl = Rex::Text::Table.new(
2016-02-13 00:49:18 +08:00
'Indent' => 4,
2018-05-15 21:33:41 +08:00
'Header' => "Framework Encoders" + ((arch) ? " (architectures: #{arch})" : "") + " [--encoder <value>]",
2016-02-13 00:49:18 +08:00
'Columns' =>
[
"Name",
"Rank",
"Description"
])
cnt = 0
framework.encoders.each_module(
2020-03-05 09:22:01 +08:00
'Arch' => arch ? arch.split(',') : nil) do |name, mod|
2013-07-02 06:15:48 +08:00
tbl << [ name, mod.rank_to_s, mod.new.name ]
cnt += 1
2020-03-05 09:22:01 +08:00
end
2013-07-02 06:15:48 +08:00
(cnt > 0) ? "\n" + tbl.to_s + "\n" : "\nNo compatible encoders found.\n\n"
2016-02-13 00:49:18 +08:00
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
def dump_nops
2018-03-17 15:24:17 +08:00
init_framework(:module_types => [ :nop ])
2016-08-11 02:30:09 +08:00
tbl = Rex::Text::Table.new(
2016-02-13 00:49:18 +08:00
'Indent' => 4,
'Header' => "Framework NOPs (#{framework.stats.num_nops} total)",
'Columns' =>
[
"Name",
"Description"
])
2020-03-05 09:22:01 +08:00
framework.nops.each_module do |name, mod|
2016-02-13 00:49:18 +08:00
tbl << [ name, mod.new.description.split.join(' ') ]
2020-03-05 09:22:01 +08:00
end
2016-02-13 00:49:18 +08:00
"\n" + tbl.to_s + "\n"
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
begin
generator_opts = parse_args(ARGV)
2018-10-16 03:46:16 +08:00
2016-05-10 03:25:23 +08:00
rescue HelpError => e
$stderr.puts e.message
exit(1)
2018-03-17 14:24:54 +08:00
rescue MsfVenomError => e
2016-02-13 00:49:18 +08:00
$stderr.puts "Error: #{e.message}"
exit(1)
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
if generator_opts[:list]
generator_opts[:list].each do |mod|
case mod.downcase
when "payloads", "payload", "p"
2020-03-05 09:22:01 +08:00
$stdout.puts dump_payloads(generator_opts[:platform], generator_opts[:arch])
2016-02-13 00:49:18 +08:00
when "encoders", "encoder", "e"
$stdout.puts dump_encoders(generator_opts[:arch])
when "nops", "nop", "n"
$stdout.puts dump_nops
2018-05-15 21:33:41 +08:00
when "platforms", "dump_platform"
$stdout.puts dump_platforms
2018-08-25 03:33:36 +08:00
when "archs", "dump_arch"
$stdout.puts dump_archs
2018-05-15 21:33:41 +08:00
when "encrypts", "encrypt", "encryption"
$stdout.puts dump_encrypt
when "formats", "format", "f"
$stdout.puts dump_formats
when "all", "a"
2016-02-13 00:49:18 +08:00
# 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
2018-05-15 21:33:41 +08:00
$stdout.puts dump_platforms
2018-08-25 03:33:36 +08:00
$stdout.puts dump_archs
2018-05-15 21:33:41 +08:00
$stdout.puts dump_encrypt
$stdout.puts dump_formats
2016-02-13 00:49:18 +08:00
else
2018-08-25 03:33:36 +08:00
$stderr.puts "Invalid type (#{mod}). These are valid: payloads, encoders, nops, platforms, archs, encrypt, formats, all"
2016-02-13 00:49:18 +08:00
end
2014-02-06 05:25:14 +08:00
end
2016-02-13 00:49:18 +08:00
exit(0)
end
2013-07-02 06:15:48 +08:00
2016-02-13 00:49:18 +08:00
if generator_opts[:list_options]
payload_mod = framework.payloads.create(generator_opts[:payload])
if payload_mod.nil?
$stderr.puts "Invalid payload: #{generator_opts[:payload]}"
2018-05-15 21:33:41 +08:00
exit(1)
2013-07-02 06:15:48 +08:00
end
2011-05-25 22:41:29 +08:00
2018-05-15 21:33:41 +08:00
$stderr.puts "Options for #{payload_mod.fullname}:\n" + "="*25 + "\n\n"
2016-02-13 00:49:18 +08:00
$stdout.puts ::Msf::Serializer::ReadableText.dump_module(payload_mod, ' ')
2014-11-10 04:27:59 +08:00
2018-05-15 21:33:41 +08:00
$stderr.puts "\nAdvanced options for #{payload_mod.fullname}:\n" + "="*25 + "\n\n"
2016-02-13 00:49:18 +08:00
$stdout.puts ::Msf::Serializer::ReadableText.dump_advanced_options(payload_mod, ' ')
2014-11-10 04:27:59 +08:00
2018-05-15 21:33:41 +08:00
$stderr.puts "\nEvasion options for #{payload_mod.fullname}:\n" + "="*25 + "\n\n"
$stdout.puts ::Msf::Serializer::ReadableText.dump_evasion_options(payload_mod, ' ')
2016-02-13 00:49:18 +08:00
exit(0)
end
2015-03-23 09:24:26 +08:00
2016-02-13 00:49:18 +08:00
generator_opts[:framework] = framework
generator_opts[:cli] = true
2015-03-23 09:24:26 +08:00
2016-02-13 00:49:18 +08:00
begin
venom_generator = Msf::PayloadGenerator.new(generator_opts)
payload = venom_generator.generate_payload
2019-05-08 17:37:46 +08:00
rescue Msf::InvalidFormat => e
2018-11-04 22:25:37 +08:00
$stderr.puts "Error: #{e.message}"
$stderr.puts dump_formats
2016-02-13 00:49:18 +08:00
rescue ::Exception => e
elog("#{e.class} : #{e.message}\n#{e.backtrace * "\n"}")
$stderr.puts "Error: #{e.message}"
end
2014-02-14 00:19:26 +08:00
2016-02-13 00:49:18 +08:00
# No payload generated, no point to go on
exit(2) unless payload
2012-03-17 09:17:45 +08:00
2016-02-13 00:49:18 +08:00
if generator_opts[:out]
2013-07-02 06:15:48 +08:00
begin
2016-02-13 00:49:18 +08:00
::File.open(generator_opts[:out], 'wb') do |f|
f.write(payload)
end
$stderr.puts "Saved as: #{generator_opts[:out]}"
2014-02-06 05:25:14 +08:00
rescue ::Exception => e
2016-02-13 00:49:18 +08:00
# If I can't save it, then I can't save it. I don't think it matters what error.
2014-11-11 03:25:53 +08:00
elog("#{e.class} : #{e.message}\n#{e.backtrace * "\n"}")
2015-07-03 15:38:52 +08:00
$stderr.puts "Error: #{e.message}"
2013-07-02 06:15:48 +08:00
end
2016-02-13 00:49:18 +08:00
else
output_stream = $stdout
output_stream.binmode
output_stream.write payload
# trailing newline for pretty output
$stderr.puts unless payload =~ /\n$/
2013-11-21 07:57:08 +08:00
end