Files
metasploit-gs/lib/msf/ui/console/command_dispatcher/payload.rb
T

212 lines
7.5 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2010-09-21 00:13:30 +00:00
require 'rex/parser/arguments'
2005-07-10 10:08:10 +00:00
2005-07-10 07:15:20 +00:00
module Msf
2016-12-06 16:37:22 -06:00
module Ui
module Console
module CommandDispatcher
2017-01-02 15:39:48 -06:00
###
# Payload module command dispatcher.
###
class Payload
include Msf::Ui::Console::ModuleCommandDispatcher
# Load supported formats
@@supported_formats = \
2017-01-02 15:39:48 -06:00
Msf::Simple::Buffer.transform_formats + \
Msf::Util::EXE.to_executable_fmt_formats
@@generate_opts = Rex::Parser::Arguments.new(
"-p" => [ true, "The platform of the payload" ],
"-n" => [ true, "Prepend a nopsled of [length] size on to the payload" ],
"-f" => [ true, "Output format: #{@@supported_formats.join(',')}" ],
"-E" => [ false, "Force encoding" ],
"-e" => [ true, "The encoder to use" ],
2019-08-14 21:04:06 -05:00
"-P" => [ true, "Total desired payload size, auto-produce appropriate NOP sled length"],
"-S" => [ true, "The new section name to use when generating (large) Windows binaries"],
"-b" => [ true, "The list of characters to avoid example: '\\x00\\xff'" ],
"-i" => [ true, "The number of times to encode the payload" ],
"-x" => [ true, "Specify a custom executable file to use as a template" ],
"-k" => [ false, "Preserve the template behavior and inject the payload as a new thread" ],
"-o" => [ true, "The output file name (otherwise stdout)" ],
2018-05-23 13:37:53 -05:00
"-O" => [ true, "Deprecated: alias for the '-o' option" ],
"-v" => [ false, "Verbose output (display stage in addition to stager)" ],
"-h" => [ false, "Show this message" ],
2017-01-02 15:39:48 -06:00
)
#
# Returns the hash of commands specific to payload modules.
#
def commands
super.update(
"generate" => "Generates a payload",
"to_handler" => "Creates a handler with the specified payload"
)
end
2016-12-28 20:03:40 +00:00
2017-01-02 15:39:48 -06:00
def cmd_to_handler(*_args)
handler = framework.modules.create('exploit/multi/handler')
2016-12-28 20:03:40 +00:00
2017-01-02 15:39:48 -06:00
handler_opts = {
'Payload' => mod.refname,
'LocalInput' => driver.input,
'LocalOutput' => driver.output,
'ExitOnSession' => false,
'RunAsJob' => true
}
2016-12-28 20:03:40 +00:00
2017-01-02 15:39:48 -06:00
handler.datastore.merge!(mod.datastore)
handler.exploit_simple(handler_opts)
job_id = handler.job_id
2016-12-06 16:37:22 -06:00
2017-01-02 15:39:48 -06:00
print_status "Payload Handler Started as Job #{job_id}"
end
2016-12-06 16:37:22 -06:00
2017-01-02 15:39:48 -06:00
#
# Returns the command dispatcher name.
#
def name
"Payload"
end
2016-12-06 16:37:22 -06:00
def cmd_generate_help
print_line "Usage: generate [options]"
print_line
2019-08-14 21:28:13 -05:00
print_line "Generates a payload. Datastore options may be supplied after normal options."
print_line
print_line "Example: generate -f python LHOST=127.0.0.1"
print @@generate_opts.usage
end
2017-01-02 15:39:48 -06:00
#
# Generates a payload.
#
def cmd_generate(*args)
# Parse the arguments
encoder_name = nil
sled_size = nil
pad_nops = nil
sec_name = nil
2017-01-02 15:39:48 -06:00
option_str = nil
badchars = nil
format = "ruby"
2017-01-02 15:39:48 -06:00
ofile = nil
iter = 1
force = nil
template = nil
plat = nil
keep = false
verbose = false
2017-01-02 15:39:48 -06:00
@@generate_opts.parse(args) do |opt, _idx, val|
case opt
2016-12-06 16:37:22 -06:00
when '-b'
badchars = Rex::Text.dehex(val)
2016-12-06 16:37:22 -06:00
when '-e'
encoder_name = val
when '-E'
force = true
when '-n'
2016-12-06 16:37:22 -06:00
sled_size = val.to_i
when '-P'
pad_nops = val.to_i
when '-S'
sec_name = val
2016-12-06 16:37:22 -06:00
when '-f'
format = val
when '-o'
if val.include?('=')
2019-08-14 20:31:15 -05:00
print_error("The -o parameter of 'generate' is now preferred to indicate the output file, like with msfvenom\n")
option_str = val
2018-05-23 13:37:53 -05:00
else
ofile = val
end
2018-05-23 13:37:53 -05:00
when '-O'
print("Usage of the '-O' parameter is deprecated, prefer '-o' to indicate the output file")
2016-12-06 16:37:22 -06:00
ofile = val
when '-i'
iter = val
when '-k'
keep = true
when '-p'
plat = val
when '-x'
template = val
when '-v'
verbose = true
2016-12-06 16:37:22 -06:00
when '-h'
cmd_generate_help
return false
else
unless val.include?('=')
cmd_generate_help
return false
end
2019-08-14 20:31:15 -05:00
mod.datastore.import_options_from_s(val)
2017-01-02 15:39:48 -06:00
end
end
if encoder_name.nil? && mod.datastore['ENCODER']
encoder_name = mod.datastore['ENCODER']
2016-12-06 16:37:22 -06:00
end
2017-01-02 15:39:48 -06:00
# Generate the payload
begin
buf = mod.generate_simple(
'BadChars' => badchars,
'Encoder' => encoder_name,
'Format' => format,
2017-01-02 15:39:48 -06:00
'NopSledSize' => sled_size,
'PadNops' => pad_nops,
'SecName' => sec_name,
2017-01-02 15:39:48 -06:00
'OptionStr' => option_str,
'ForceEncode' => force,
'Template' => template,
'Platform' => plat,
'KeepTemplateWorking' => keep,
'Iterations' => iter,
'Verbose' => verbose
2017-01-02 15:39:48 -06:00
)
rescue
log_error("Payload generation failed: #{$ERROR_INFO}")
return false
end
2016-12-06 16:37:22 -06:00
2017-01-02 15:39:48 -06:00
if !ofile
# Display generated payload
2020-01-11 19:43:04 +08:00
puts(buf)
2017-01-02 15:39:48 -06:00
else
print_status("Writing #{buf.length} bytes to #{ofile}...")
fd = File.open(ofile, "wb")
fd.write(buf)
fd.close
end
true
2016-12-06 16:37:22 -06:00
end
def cmd_generate_tabs(str, words)
2017-11-01 20:38:45 -04:00
fmt = {
'-b' => [ true ],
'-E' => [ nil ],
2017-11-01 20:38:45 -04:00
'-e' => [ framework.encoders.map { |refname, mod| refname } ],
'-h' => [ nil ],
'-o' => [ true ],
'-P' => [ true ],
'-S' => [ true ],
'-f' => [ :file ],
'-t' => [ @@supported_formats ],
'-p' => [ true ],
'-k' => [ nil ],
'-x' => [ :file ],
'-i' => [ true ],
'-v' => [ nil ]
2017-11-01 20:38:45 -04:00
}
2017-11-11 16:47:11 -05:00
tab_complete_generic(fmt, str, words)
end
2016-12-06 16:37:22 -06:00
end
end
2013-08-30 16:28:33 -05:00
end
end
2005-07-10 07:15:20 +00:00
end