Files
metasploit-gs/lib/msf/base/simple/payload.rb
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

146 lines
4.0 KiB
Ruby
Raw Normal View History

# -*- coding: binary -*-
2010-09-21 00:13:30 +00:00
2005-07-10 09:42:49 +00:00
module Msf
module Simple
###
#
# Simple payload wrapper class for performing generation.
#
###
module Payload
include Module
2013-08-30 16:28:33 -05:00
2005-07-10 09:42:49 +00:00
#
# Generate a payload with the mad skillz. The payload can be generated in
# a number of ways.
#
# opts can have:
#
2005-11-25 01:59:54 +00:00
# Encoder => A encoder module name.
# BadChars => A string of bad characters.
# Format => The format to represent the data as: ruby, perl, c, raw
# Options => A hash of options to set.
# OptionStr => A string of options in VAR=VAL form separated by
# whitespace.
# NoComment => Disables prepention of a comment
# NopSledSize => The number of NOPs to use
2005-11-25 01:59:54 +00:00
# MaxSize => The maximum size of the payload.
# Iterations => Number of times to encode.
2023-09-20 14:30:05 -06:00
# ForceEncode => Force encoding.
2005-07-10 09:42:49 +00:00
#
# raises:
#
# BadcharError => If the supplied encoder fails to encode the payload
# NoKeyError => No valid encoder key could be found
# ArgumentParseError => Options were supplied improperly
#
def self.generate_simple(payload, opts, &block)
2013-08-30 16:28:33 -05:00
# Clone the module to prevent changes to the original instance
payload = payload.replicant
Msf::Simple::Framework.simplify_module(payload)
yield(payload) if block_given?
2013-08-30 16:28:33 -05:00
# Import any options we may need
payload._import_extra_options(opts)
framework = payload.framework
2013-08-30 16:28:33 -05:00
2005-07-10 09:42:49 +00:00
# Generate the payload
e = EncodedPayload.create(payload,
'BadChars' => opts['BadChars'],
'MinNops' => opts['NopSledSize'],
'PadNops' => opts['PadNops'],
'Encoder' => opts['Encoder'],
'Iterations' => opts['Iterations'],
'ForceEncode' => opts['ForceEncode'],
'DisableNops' => opts['DisableNops'],
'Space' => opts['MaxSize'])
2013-08-30 16:28:33 -05:00
2005-07-10 19:21:40 +00:00
fmt = opts['Format'] || 'raw'
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
exeopts = {
:inject => opts['KeepTemplateWorking'],
:template => opts['Template'],
:template_path => opts['ExeDir'],
:secname => opts['SecName']
2010-09-21 00:13:30 +00:00
}
2013-08-30 16:28:33 -05:00
arch = payload.arch
2010-09-21 00:13:30 +00:00
plat = opts['Platform'] || payload.platform
2013-08-30 16:28:33 -05:00
2005-07-11 05:25:50 +00:00
# Save off the original payload length
len = e.encoded.length
2013-08-30 16:28:33 -05:00
if arch.index(ARCH_JAVA) and fmt == 'war'
return e.encoded_war.pack
end
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
output = Msf::Util::EXE.to_executable_fmt(framework, arch, plat, e.encoded, fmt, exeopts)
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
if not output
# Generate jar if necessary
if fmt == 'jar'
return e.encoded_jar.pack
end
2013-08-30 16:28:33 -05:00
# Serialize the generated payload to some sort of format
2010-09-21 00:13:30 +00:00
fmt ||= "ruby"
output = Buffer.transform(e.encoded, fmt)
2013-08-30 16:28:33 -05:00
# Prepend a comment
if (fmt != 'raw' and opts['NoComment'] != true)
((ou = payload.options.options_used_to_s(payload.datastore)) and ou.length > 0) ? ou += "\n" : ou = ''
2010-09-21 00:13:30 +00:00
output =
Buffer.comment(
"#{payload.refname} - #{len} bytes#{payload.staged? ? " (stage 1)" : ""}\n" +
"https://metasploit.com/\n" +
2010-09-21 00:13:30 +00:00
((e.encoder) ? "Encoder: #{e.encoder.refname}\n" : '') +
((e.nop) ? "NOP gen: #{e.nop.refname}\n" : '') +
"#{ou}",
fmt) +
output
2013-08-30 16:28:33 -05:00
# If verbose was requested and it's multistage, include the second stage too
if opts['Verbose'] && payload.staged?
stage = payload.generate_stage
2013-08-30 16:28:33 -05:00
# If a stage was generated, then display it
if stage and stage.length > 0
2010-09-21 00:13:30 +00:00
output +=
"\n" +
Buffer.comment(
2010-09-21 00:13:30 +00:00
"#{payload.refname} - #{stage.length} bytes (stage 2)\n" +
"https://metasploit.com/\n",
2010-09-21 00:13:30 +00:00
fmt) +
Buffer.transform(stage, fmt)
end
end
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
end
2013-08-30 16:28:33 -05:00
2005-07-10 19:21:40 +00:00
end
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
# How to warn?
#if exeopts[:fellback]
# $stderr.puts(OutError + "Warning: Falling back to default template: #{exeopts[:fellback]}")
#end
2013-08-30 16:28:33 -05:00
2010-09-21 00:13:30 +00:00
return output
2005-07-10 09:42:49 +00:00
end
2013-08-30 16:28:33 -05:00
#
2005-11-15 15:11:43 +00:00
# Calls the class method.
#
2012-06-15 14:25:47 -05:00
def generate_simple(opts, &block)
Msf::Simple::Payload.generate_simple(self, opts, &block)
end
2005-07-10 09:42:49 +00:00
end
end
end