2005-07-13 18:06:12 +00:00
|
|
|
require 'msf/core'
|
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class wrappers an encoded payload buffer and the means used to create
|
|
|
|
# one.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class EncodedPayload
|
|
|
|
|
|
|
|
include Framework::Offspring
|
|
|
|
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
# This method creates an encoded payload instance and returns it to the
|
|
|
|
# caller.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2008-05-26 07:59:03 +00:00
|
|
|
def self.create(pinst, reqs = {})
|
2005-07-13 18:06:12 +00:00
|
|
|
# Create the encoded payload instance
|
2005-07-13 21:09:07 +00:00
|
|
|
p = EncodedPayload.new(pinst.framework, pinst, reqs)
|
2005-07-13 18:06:12 +00:00
|
|
|
|
2008-05-26 07:59:03 +00:00
|
|
|
p.generate(reqs['Raw'])
|
2005-07-13 18:06:12 +00:00
|
|
|
|
|
|
|
return p
|
|
|
|
end
|
|
|
|
|
2005-11-02 16:49:45 +00:00
|
|
|
#
|
|
|
|
# Creates an instance of an EncodedPayload.
|
|
|
|
#
|
2005-07-13 18:06:12 +00:00
|
|
|
def initialize(framework, pinst, reqs)
|
|
|
|
self.framework = framework
|
|
|
|
self.pinst = pinst
|
|
|
|
self.reqs = reqs
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-11-02 16:49:45 +00:00
|
|
|
# This method enerates the full encoded payload and returns the encoded
|
|
|
|
# payload buffer.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2008-05-26 07:59:03 +00:00
|
|
|
def generate(raw = nil)
|
|
|
|
self.raw = raw
|
2005-07-13 21:09:07 +00:00
|
|
|
self.encoded = nil
|
|
|
|
self.nop_sled_size = 0
|
|
|
|
self.nop_sled = nil
|
|
|
|
self.encoder = nil
|
|
|
|
self.nop = nil
|
2005-07-13 18:06:12 +00:00
|
|
|
|
2006-07-30 18:45:36 +00:00
|
|
|
# Increase thread priority as necessary. This is done
|
|
|
|
# to ensure that the encoding and sled generation get
|
|
|
|
# enough time slices from the ruby thread scheduler.
|
|
|
|
priority = Thread.current.priority
|
|
|
|
|
|
|
|
if (priority == 0)
|
|
|
|
Thread.current.priority = 1
|
|
|
|
end
|
|
|
|
|
|
|
|
begin
|
|
|
|
# First, validate
|
|
|
|
pinst.validate()
|
|
|
|
|
|
|
|
# Generate the raw version of the payload first
|
2008-05-26 07:59:03 +00:00
|
|
|
generate_raw() if self.raw.nil?
|
2006-07-30 18:45:36 +00:00
|
|
|
|
|
|
|
# Encode the payload
|
|
|
|
encode()
|
|
|
|
|
|
|
|
# Build the NOP sled
|
|
|
|
generate_sled()
|
|
|
|
|
|
|
|
# Finally, set the complete payload definition
|
|
|
|
self.encoded = (self.nop_sled || '') + self.encoded
|
|
|
|
ensure
|
|
|
|
# Restore the thread priority
|
|
|
|
Thread.current.priority = priority
|
|
|
|
end
|
2005-07-13 18:06:12 +00:00
|
|
|
|
|
|
|
# Return the complete payload
|
|
|
|
return encoded
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Generates the raw payload from the payload instance. This populates the
|
|
|
|
# raw attribute.
|
|
|
|
#
|
|
|
|
def generate_raw
|
2005-07-13 21:09:07 +00:00
|
|
|
self.raw = (reqs['Prepend'] || '') + pinst.generate + (reqs['Append'] || '')
|
2006-10-16 23:59:14 +00:00
|
|
|
|
|
|
|
# If an encapsulation routine was supplied, then we should call it so
|
|
|
|
# that we can get the real raw payload.
|
|
|
|
if reqs['EncapsulationRoutine']
|
|
|
|
self.raw = reqs['EncapsulationRoutine'].call(reqs, raw)
|
|
|
|
end
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Scans for a compatible encoder using ranked precedence and populates the
|
|
|
|
# encoded attribute.
|
|
|
|
#
|
|
|
|
def encode
|
|
|
|
# If the exploit has bad characters, we need to run the list of encoders
|
|
|
|
# in ranked precedence and try to encode without them.
|
2008-05-26 07:59:03 +00:00
|
|
|
if reqs['BadChars'] or reqs['Encoder'] or reqs['ForceEncode']
|
2005-07-13 21:09:07 +00:00
|
|
|
encoders = pinst.compatible_encoders
|
|
|
|
|
|
|
|
# If the caller had a preferred encoder, try to find it and prefix it
|
|
|
|
if ((reqs['Encoder']) and
|
|
|
|
(preferred = framework.encoders[reqs['Encoder']]))
|
2005-07-14 14:46:18 +00:00
|
|
|
encoders.unshift([reqs['Encoder'], preferred ])
|
2005-07-13 21:09:07 +00:00
|
|
|
elsif (reqs['Encoder'])
|
|
|
|
wlog("#{pinst.refname}: Failed to find preferred encoder #{reqs['Encoder']}")
|
|
|
|
end
|
|
|
|
|
2007-10-02 03:36:45 +00:00
|
|
|
# If we don't want to allow encoder fall through and we have a
|
|
|
|
# non-zero number of encoders to choose from, then just choose the
|
|
|
|
# first one because it must succeed or we fail. Normally, the
|
|
|
|
# encoder don't fall through option will only be used when a
|
|
|
|
# preferred encoder is explicitly defined. Using don't fall through
|
|
|
|
# without selecting an encoder is ill-advised.
|
|
|
|
if reqs['EncoderDontFallThrough'] and encoders.length > 0
|
|
|
|
encoders = [encoders[0]]
|
|
|
|
end
|
|
|
|
|
2005-07-14 14:46:18 +00:00
|
|
|
encoders.each { |encname, encmod|
|
2005-07-13 21:09:07 +00:00
|
|
|
self.encoder = encmod.new
|
2005-09-24 19:17:07 +00:00
|
|
|
self.encoded = nil
|
2006-01-06 02:25:47 +00:00
|
|
|
|
|
|
|
# If there is an encoder type restriction, check to see if this
|
|
|
|
# encoder matches with what we're searching for.
|
|
|
|
if ((reqs['EncoderType']) and
|
|
|
|
(self.encoder.encoder_type.split(/\s+/).include?(reqs['EncoderType']) == false))
|
|
|
|
wlog("#{pinst.refname}: Encoder #{encoder.refname} is not a compatible encoder type: #{reqs['EncoderType']} != #{self.encoder.encoder_type}",
|
|
|
|
'core', LEV_1)
|
2007-10-02 03:36:45 +00:00
|
|
|
|
2006-01-06 02:25:47 +00:00
|
|
|
next
|
|
|
|
end
|
2006-09-12 05:34:58 +00:00
|
|
|
|
|
|
|
# If the exploit did not explicitly request a kind of encoder and
|
|
|
|
# the current encoder has a manual ranking, then it should not be
|
|
|
|
# considered as a valid encoder. A manual ranking tells the
|
|
|
|
# framework that an encoder must be explicitly defined as the
|
|
|
|
# encoder of choice for an exploit.
|
|
|
|
if ((reqs['EncoderType'].nil?) and
|
2006-12-28 05:19:31 +00:00
|
|
|
(reqs['Encoder'].nil?) and
|
2006-09-12 05:34:58 +00:00
|
|
|
(self.encoder.rank == ManualRanking))
|
|
|
|
wlog("#{pinst.refname}: Encoder #{encoder.refname} is manual ranked and was not defined as a preferred encoder.",
|
|
|
|
'core', LEV_1)
|
2007-08-31 04:01:30 +00:00
|
|
|
|
2006-09-12 05:34:58 +00:00
|
|
|
next
|
|
|
|
end
|
2006-01-06 02:25:47 +00:00
|
|
|
|
|
|
|
# If we have any encoder options, import them into the datastore
|
|
|
|
# of the encoder.
|
|
|
|
if (reqs['EncoderOptions'])
|
|
|
|
self.encoder.datastore.import_options_from_hash(reqs['EncoderOptions'])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Validate the encoder to make sure it's properly initialized.
|
|
|
|
begin
|
|
|
|
self.encoder.validate
|
|
|
|
rescue ::Exception
|
|
|
|
wlog("#{pinst.refname}: Failed to validate encoder #{encoder.refname}: #{$!}",
|
|
|
|
'core', LEV_1)
|
2007-08-31 04:01:30 +00:00
|
|
|
|
2006-01-06 02:25:47 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
# Try encoding with the current encoder
|
|
|
|
begin
|
2005-07-13 21:09:07 +00:00
|
|
|
self.encoded = self.encoder.encode(self.raw, reqs['BadChars'])
|
2006-01-06 02:25:47 +00:00
|
|
|
rescue ::Exception
|
2005-07-13 18:06:12 +00:00
|
|
|
wlog("#{pinst.refname}: Failed to encode payload with encoder #{encoder.refname}: #{$!}",
|
2005-11-02 00:27:59 +00:00
|
|
|
'core', LEV_1)
|
2006-01-06 02:25:47 +00:00
|
|
|
|
|
|
|
dlog("#{pinst.refname}: Call stack\n#{$@.join("\n")}", 'core', LEV_3)
|
2007-08-31 04:01:30 +00:00
|
|
|
|
2005-07-13 21:09:07 +00:00
|
|
|
next
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
2006-01-06 02:25:47 +00:00
|
|
|
dlog("#{pinst.refname}: Successfully encoded with encoder #{encoder.refname} (size is #{self.encoded.length})",
|
|
|
|
'core', LEV_2)
|
|
|
|
|
2005-07-18 00:03:29 +00:00
|
|
|
# Get the minimum number of nops to use
|
|
|
|
min = (reqs['MinNops'] || 0).to_i
|
2006-01-02 01:12:36 +00:00
|
|
|
min = 0 if reqs['DisableNops']
|
|
|
|
|
2005-07-18 00:03:29 +00:00
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
# Check to see if we have enough room for the minimum requirements
|
|
|
|
if ((reqs['Space']) and
|
2005-07-13 21:09:07 +00:00
|
|
|
(reqs['Space'] < self.encoded.length + min))
|
2005-07-13 18:06:12 +00:00
|
|
|
wlog("#{pinst.refname}: Encoded payload version is too large with encoder #{encoder.refname}",
|
|
|
|
'core', LEV_1)
|
2007-10-02 03:36:45 +00:00
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
next
|
|
|
|
end
|
2005-07-13 21:09:07 +00:00
|
|
|
|
|
|
|
break
|
2005-07-13 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# If the encoded payload is nil, raise an exception saying that we
|
|
|
|
# suck at life.
|
2005-07-13 21:09:07 +00:00
|
|
|
if (self.encoded == nil)
|
|
|
|
encoder = nil
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
raise NoEncodersSucceededError,
|
|
|
|
"#{pinst.refname}: All encoders failed to encode.",
|
|
|
|
caller
|
|
|
|
end
|
|
|
|
# If there are no bad characters, then the raw is the same as the
|
|
|
|
# encoded
|
|
|
|
else
|
2005-07-13 21:09:07 +00:00
|
|
|
self.encoded = raw
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Prefix the prepend encoder value
|
2005-07-13 21:09:07 +00:00
|
|
|
self.encoded = (reqs['PrependEncoder'] || '') + self.encoded
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Construct a NOP sled if necessary
|
|
|
|
#
|
|
|
|
def generate_sled
|
|
|
|
min = reqs['MinNops'] || 0
|
|
|
|
space = reqs['Space']
|
|
|
|
|
2005-07-13 21:09:07 +00:00
|
|
|
self.nop_sled_size = min
|
2005-07-13 18:06:12 +00:00
|
|
|
|
|
|
|
# Calculate the number of NOPs to pad out the buffer with based on the
|
|
|
|
# requirements. If there was a space requirement, check to see if
|
|
|
|
# there's any room at all left for a sled.
|
|
|
|
if ((space) and
|
|
|
|
(space > encoded.length))
|
2005-07-13 21:09:07 +00:00
|
|
|
self.nop_sled_size = reqs['Space'] - self.encoded.length
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If the maximum number of NOPs has been exceeded, wrap it back down.
|
|
|
|
if ((reqs['MaxNops']) and
|
2005-09-24 19:17:07 +00:00
|
|
|
(reqs['MaxNops'] < self.nop_sled_size))
|
2005-07-13 21:09:07 +00:00
|
|
|
self.nop_sled_size = reqs['MaxNops']
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
2006-01-02 01:12:36 +00:00
|
|
|
# Check for the DisableNops setting
|
|
|
|
self.nop_sled_size = 0 if reqs['DisableNops']
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
# Now construct the actual sled
|
2005-07-13 21:09:07 +00:00
|
|
|
if (self.nop_sled_size > 0)
|
2006-01-17 01:11:26 +00:00
|
|
|
nops = pinst.compatible_nops
|
|
|
|
|
|
|
|
# If the caller had a preferred nop, try to find it and prefix it
|
|
|
|
if ((reqs['Nop']) and
|
2006-01-17 04:09:40 +00:00
|
|
|
(preferred = framework.nops[reqs['Nop']]))
|
|
|
|
nops.unshift([reqs['Nop'], preferred ])
|
2006-01-17 01:11:26 +00:00
|
|
|
elsif (reqs['Nop'])
|
|
|
|
wlog("#{pinst.refname}: Failed to find preferred nop #{reqs['Nop']}")
|
|
|
|
end
|
|
|
|
|
|
|
|
nops.each { |nopname, nopmod|
|
2005-07-13 18:06:12 +00:00
|
|
|
# Create an instance of the nop module
|
2005-07-13 21:09:07 +00:00
|
|
|
self.nop = nopmod.new
|
2005-07-18 02:01:36 +00:00
|
|
|
|
|
|
|
# The list of save registers
|
|
|
|
save_regs = (reqs['SaveRegisters'] || []) + (pinst.save_registers || [])
|
|
|
|
|
|
|
|
if (save_regs.empty? == true)
|
|
|
|
save_regs = nil
|
|
|
|
end
|
2005-11-11 01:22:03 +00:00
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
begin
|
2005-12-30 02:38:18 +00:00
|
|
|
nop.copy_ui(pinst)
|
|
|
|
|
2005-07-15 22:30:04 +00:00
|
|
|
self.nop_sled = nop.generate_sled(self.nop_sled_size,
|
2005-07-13 18:06:12 +00:00
|
|
|
'BadChars' => reqs['BadChars'],
|
2005-07-18 02:01:36 +00:00
|
|
|
'SaveRegisters' => save_regs)
|
2005-07-13 18:06:12 +00:00
|
|
|
rescue
|
|
|
|
dlog("#{pinst.refname}: Nop generator #{nop.refname} failed to generate sled for payload: #{$!}",
|
|
|
|
'core', LEV_1)
|
2005-11-11 01:22:03 +00:00
|
|
|
|
|
|
|
self.nop = nil
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
2005-07-13 21:09:07 +00:00
|
|
|
|
|
|
|
break
|
2005-07-13 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
2005-07-13 21:09:07 +00:00
|
|
|
if (self.nop_sled == nil)
|
2005-07-13 18:06:12 +00:00
|
|
|
raise NoNopsSucceededError,
|
|
|
|
"#{pinst.refname}: All NOP generators failed to construct sled for.",
|
|
|
|
caller
|
|
|
|
end
|
|
|
|
else
|
2005-07-13 21:09:07 +00:00
|
|
|
self.nop_sled = ''
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 21:09:07 +00:00
|
|
|
return self.nop_sled
|
2005-07-13 18:06:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# The raw version of the payload
|
|
|
|
#
|
|
|
|
attr_reader :raw
|
|
|
|
#
|
|
|
|
# The encoded version of the raw payload plus the NOP sled
|
|
|
|
# if one was generated.
|
|
|
|
#
|
|
|
|
attr_reader :encoded
|
|
|
|
#
|
|
|
|
# The size of the NOP sled
|
|
|
|
#
|
|
|
|
attr_reader :nop_sled_size
|
|
|
|
#
|
|
|
|
# The NOP sled itself
|
|
|
|
#
|
|
|
|
attr_reader :nop_sled
|
2005-07-13 21:09:07 +00:00
|
|
|
#
|
|
|
|
# The encoder that was used
|
|
|
|
#
|
|
|
|
attr_reader :encoder
|
|
|
|
#
|
|
|
|
# The NOP generator that was used
|
|
|
|
#
|
|
|
|
attr_reader :nop
|
2005-07-13 18:06:12 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
attr_writer :raw # :nodoc:
|
|
|
|
attr_writer :encoded # :nodoc:
|
|
|
|
attr_writer :nop_sled_size # :nodoc:
|
|
|
|
attr_writer :nop_sled # :nodoc:
|
|
|
|
attr_writer :payload # :nodoc:
|
|
|
|
attr_writer :encoder # :nodoc:
|
|
|
|
attr_writer :nop # :nodoc:
|
2005-07-13 18:06:12 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# The payload instance used to generate the payload
|
|
|
|
#
|
|
|
|
attr_accessor :pinst
|
|
|
|
#
|
|
|
|
# The requirements used for generation
|
|
|
|
#
|
|
|
|
attr_accessor :reqs
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|