2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2011-05-12 20:03:55 +00:00
|
|
|
require 'metasm'
|
2005-07-09 00:24:02 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class represents the base class for a logical payload. The framework
|
|
|
|
# automatically generates payload combinations at runtime which are all
|
|
|
|
# extended for this Payload as a base class.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Payload < Msf::Module
|
|
|
|
|
2011-05-12 14:59:44 +00:00
|
|
|
require 'rex/payloads'
|
2006-10-16 21:40:38 +00:00
|
|
|
|
2011-05-12 20:03:55 +00:00
|
|
|
require 'msf/core/payload/single'
|
|
|
|
require 'msf/core/payload/generic'
|
|
|
|
require 'msf/core/payload/stager'
|
2005-07-09 00:24:02 +00:00
|
|
|
|
2005-07-09 01:03:11 +00:00
|
|
|
# Platform specific includes
|
2011-05-12 20:03:55 +00:00
|
|
|
require 'msf/core/payload/aix'
|
|
|
|
require 'msf/core/payload/bsd'
|
|
|
|
require 'msf/core/payload/linux'
|
|
|
|
require 'msf/core/payload/osx'
|
|
|
|
require 'msf/core/payload/solaris'
|
|
|
|
require 'msf/core/payload/windows'
|
|
|
|
require 'msf/core/payload/netware'
|
|
|
|
require 'msf/core/payload/java'
|
2007-07-25 03:24:51 +00:00
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
##
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Payload types
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
##
|
2005-07-09 00:24:02 +00:00
|
|
|
module Type
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Single payload type. These types of payloads are self contained and
|
|
|
|
# do not go through any staging.
|
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
Single = (1 << 0)
|
2007-07-25 03:24:51 +00:00
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# The stager half of a staged payload. Its responsibility in life is to
|
|
|
|
# read in the stage and execute it.
|
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
Stager = (1 << 1)
|
2005-10-19 03:20:20 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# The stage half of a staged payload. This payload performs whatever
|
|
|
|
# arbitrary task it's designed to do, possibly making use of the same
|
|
|
|
# connection that the stager used to read the stage in on, if
|
|
|
|
# applicable.
|
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
Stage = (1 << 2)
|
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Creates an instance of a payload module using the supplied information.
|
|
|
|
#
|
2005-07-11 22:51:25 +00:00
|
|
|
def initialize(info = {})
|
|
|
|
super
|
2005-10-19 01:48:10 +00:00
|
|
|
|
2007-06-16 05:04:03 +00:00
|
|
|
# If this is a staged payload but there is no stage information,
|
|
|
|
# then this is actually a stager + single combination. Set up the
|
|
|
|
# information hash accordingly.
|
|
|
|
if self.class.include?(Msf::Payload::Single) and
|
|
|
|
self.class.include?(Msf::Payload::Stager)
|
|
|
|
self.module_info['Stage'] = {}
|
|
|
|
|
|
|
|
if self.module_info['Payload']
|
2007-08-11 00:37:50 +00:00
|
|
|
self.module_info['Stage']['Payload'] = self.module_info['Payload']['Payload'] || ""
|
|
|
|
self.module_info['Stage']['Assembly'] = self.module_info['Payload']['Assembly'] || ""
|
|
|
|
self.module_info['Stage']['Offsets'] = self.module_info['Payload']['Offsets'] || {}
|
2007-06-16 05:04:03 +00:00
|
|
|
else
|
2007-08-11 00:37:50 +00:00
|
|
|
self.module_info['Stage']['Payload'] = ""
|
|
|
|
self.module_info['Stage']['Assembly'] = ""
|
|
|
|
self.module_info['Stage']['Offsets'] = {}
|
2007-06-16 05:04:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@staged = true
|
2009-06-25 17:59:51 +00:00
|
|
|
else
|
|
|
|
@staged = false
|
2007-06-16 05:04:03 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 01:48:10 +00:00
|
|
|
# Update the module info hash with the connection type
|
|
|
|
# that is derived from the handler for this payload. This is
|
|
|
|
# used for compatibility filtering purposes.
|
|
|
|
self.module_info['ConnectionType'] = connection_type
|
2005-07-11 22:51:25 +00:00
|
|
|
end
|
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Accessors
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
# Returns MODULE_PAYLOAD to indicate that this is a payload module.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-14 20:05:41 +00:00
|
|
|
def self.type
|
|
|
|
return MODULE_PAYLOAD
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns MODULE_PAYLOAD to indicate that this is a payload module.
|
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def type
|
|
|
|
return MODULE_PAYLOAD
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Returns the string of bad characters for this payload, if any.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def badchars
|
|
|
|
return self.module_info['BadChars']
|
|
|
|
end
|
|
|
|
|
2005-07-18 02:01:36 +00:00
|
|
|
#
|
|
|
|
# The list of registers that should be saved by any NOP generators or
|
|
|
|
# encoders, if possible.
|
|
|
|
#
|
|
|
|
def save_registers
|
|
|
|
return self.module_info['SaveRegisters']
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Returns the type of payload, either single or staged. Stage is
|
|
|
|
# the default because singles and stagers are encouraged to include
|
|
|
|
# the Single and Stager mixin which override the payload_type.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def payload_type
|
|
|
|
return Type::Stage
|
|
|
|
end
|
|
|
|
|
2007-06-16 05:04:03 +00:00
|
|
|
#
|
|
|
|
# Returns the string version of the payload type
|
|
|
|
#
|
|
|
|
def payload_type_s
|
|
|
|
case payload_type
|
|
|
|
when Type::Stage
|
|
|
|
return "stage"
|
|
|
|
when Type::Stager
|
|
|
|
return "stager"
|
|
|
|
when Type::Single
|
|
|
|
return "single"
|
|
|
|
else
|
|
|
|
return "unknown"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-11-12 05:11:56 +00:00
|
|
|
#
|
|
|
|
# This method returns whether or not this payload uses staging.
|
|
|
|
#
|
|
|
|
def staged?
|
2007-06-16 05:04:03 +00:00
|
|
|
(@staged or payload_type == Type::Stager or payload_type == Type::Stage)
|
2005-11-12 05:11:56 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Returns the payload's size. If the payload is staged, the size of the
|
|
|
|
# first stage is returned.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def size
|
2010-08-20 20:35:48 +00:00
|
|
|
pl = nil
|
2010-08-19 00:10:06 +00:00
|
|
|
begin
|
|
|
|
pl = generate()
|
|
|
|
rescue NoCompatiblePayloadError
|
|
|
|
end
|
2010-08-20 20:35:48 +00:00
|
|
|
pl ||= ''
|
2010-08-19 00:10:06 +00:00
|
|
|
pl.length
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Returns the raw payload that has not had variable substitution occur.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def payload
|
2007-08-11 00:37:50 +00:00
|
|
|
return module_info['Payload'] ? module_info['Payload']['Payload'] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the assembly string that describes the payload if one exists.
|
|
|
|
#
|
|
|
|
def assembly
|
|
|
|
return module_info['Payload'] ? module_info['Payload']['Assembly'] : nil
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2012-12-01 20:39:23 +00:00
|
|
|
#
|
|
|
|
# Sets the assembly string that describes the payload
|
|
|
|
# If this method is used to define the payload, a payload with no offsets will be created
|
|
|
|
#
|
|
|
|
def assembly=(asm)
|
|
|
|
module_info['Payload'] ||= {'Offsets' => {} }
|
|
|
|
module_info['Payload']['Assembly'] = asm
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Returns the offsets to variables that must be substitute, if any.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def offsets
|
2007-08-11 00:37:50 +00:00
|
|
|
return module_info['Payload'] ? module_info['Payload']['Offsets'] : nil
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2005-10-01 06:09:46 +00:00
|
|
|
#
|
|
|
|
# Returns the staging convention that the payload uses, if any. This is
|
|
|
|
# used to make sure that only compatible stagers and stages are built
|
|
|
|
# (where assumptions are made about register/environment initialization
|
|
|
|
# state and hand-off).
|
|
|
|
#
|
|
|
|
def convention
|
|
|
|
module_info['Convention']
|
|
|
|
end
|
|
|
|
|
2005-10-19 01:48:10 +00:00
|
|
|
#
|
|
|
|
# Returns the module's connection type, such as reverse, bind, noconn,
|
|
|
|
# or whatever else the case may be.
|
|
|
|
#
|
|
|
|
def connection_type
|
|
|
|
handler_klass.general_handler_type
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
# Returns the method used by the payload to resolve symbols for the purpose
|
|
|
|
# of calling functions, such as ws2ord.
|
2005-10-19 01:48:10 +00:00
|
|
|
#
|
|
|
|
def symbol_lookup
|
|
|
|
module_info['SymbolLookup']
|
|
|
|
end
|
|
|
|
|
2005-10-01 06:09:46 +00:00
|
|
|
#
|
|
|
|
# Checks to see if the supplied convention is compatible with this
|
|
|
|
# payload's convention.
|
|
|
|
#
|
|
|
|
def compatible_convention?(conv)
|
|
|
|
# If we ourself don't have a convention or our convention is equal to
|
|
|
|
# the one supplied, then we know we are compatible.
|
|
|
|
if ((self.convention == nil) or
|
|
|
|
(self.convention == conv))
|
|
|
|
true
|
|
|
|
# On the flip side, if we are a stager and the supplied convention is
|
|
|
|
# nil, then we know it's compatible.
|
|
|
|
elsif ((payload_type == Type::Stager) and
|
|
|
|
(conv == nil))
|
|
|
|
true
|
|
|
|
# Otherwise, the conventions don't match in some way or another, and as
|
|
|
|
# such we deem ourself as not being compatible with the supplied
|
|
|
|
# convention.
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Return the connection associated with this payload, or none if there
|
|
|
|
# isn't one.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-10-19 01:48:10 +00:00
|
|
|
def handler_klass
|
2005-07-17 06:01:11 +00:00
|
|
|
return module_info['Handler'] || Msf::Handler::None
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2005-07-16 07:32:11 +00:00
|
|
|
#
|
|
|
|
# Returns the session class that is associated with this payload and will
|
|
|
|
# be used to create a session as necessary.
|
|
|
|
#
|
|
|
|
def session
|
|
|
|
return module_info['Session']
|
|
|
|
end
|
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Generation & variable substitution
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
# Generates the payload and returns the raw buffer to the caller.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def generate
|
2007-06-16 05:04:03 +00:00
|
|
|
internal_generate
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Substitutes variables with values from the module's datastore in the
|
|
|
|
# supplied raw buffer for a given set of named offsets. For instance,
|
|
|
|
# RHOST is substituted with the RHOST value from the datastore which will
|
|
|
|
# have been populated by the framework.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2006-10-16 18:51:34 +00:00
|
|
|
# Supprted packing types:
|
|
|
|
#
|
2008-08-22 06:34:57 +00:00
|
|
|
# - ADDR (foo.com, 1.2.3.4)
|
|
|
|
# - ADDR6 (foo.com, fe80::1234:5678:8910:1234)
|
2008-10-19 20:38:07 +00:00
|
|
|
# - ADDR16MSB, ADD16LSB, ADDR22MSB, ADD22LSB (foo.com, 1.2.3.4)
|
2008-10-19 20:32:14 +00:00
|
|
|
# Advanced packing types for 16/16 and 22/10 bits substitution. The 16
|
|
|
|
# bits types uses two offsets indicating where the 16 bits pair will be
|
|
|
|
# substituted, while the 22 bits types uses two offsets indicating the
|
|
|
|
# instructions where the 22/10 bits pair will be substituted. Normally
|
|
|
|
# these are offsets to "sethi" and "or" instructions on SPARC architecture.
|
2008-08-22 06:34:57 +00:00
|
|
|
# - HEX (0x12345678, "\x41\x42\x43\x44")
|
|
|
|
# - RAW (raw bytes)
|
2006-10-16 18:51:34 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def substitute_vars(raw, offsets)
|
|
|
|
offsets.each_pair { |name, info|
|
|
|
|
offset, pack = info
|
|
|
|
|
|
|
|
# Give the derived class a chance to substitute this variable
|
2005-07-09 01:03:11 +00:00
|
|
|
next if (replace_var(raw, name, offset, pack) == true)
|
2005-07-09 00:24:02 +00:00
|
|
|
|
|
|
|
# Now it's our turn...
|
|
|
|
if ((val = datastore[name]))
|
|
|
|
if (pack == 'ADDR')
|
2005-07-10 19:21:40 +00:00
|
|
|
val = Rex::Socket.resolv_nbo(val)
|
2010-02-08 22:41:36 +00:00
|
|
|
|
2008-08-22 06:34:57 +00:00
|
|
|
# Someone gave us a funky address (ipv6?)
|
2008-08-22 22:06:14 +00:00
|
|
|
if(val.length == 16)
|
2008-10-19 20:32:14 +00:00
|
|
|
raise RuntimeError, "IPv6 address specified for IPv4 payload."
|
2008-08-22 06:34:57 +00:00
|
|
|
end
|
|
|
|
elsif (pack == 'ADDR6')
|
|
|
|
val = Rex::Socket.resolv_nbo(val)
|
2010-02-08 22:41:36 +00:00
|
|
|
|
2008-08-22 06:34:57 +00:00
|
|
|
# Convert v4 to the v6ish address
|
|
|
|
if(val.length == 4)
|
|
|
|
nip = "fe80::5efe:" + val.unpack("C*").join(".")
|
2008-09-05 16:23:34 +00:00
|
|
|
val = Rex::Socket.resolv_nbo(nip)
|
2010-02-08 22:41:36 +00:00
|
|
|
end
|
2008-10-19 20:32:14 +00:00
|
|
|
elsif (['ADDR16MSB', 'ADDR16LSB', 'ADDR22MSB', 'ADDR22LSB'].include?(pack))
|
|
|
|
val = Rex::Socket.resolv_nbo(val)
|
2010-02-08 22:41:36 +00:00
|
|
|
|
2008-10-19 20:32:14 +00:00
|
|
|
# Someone gave us a funky address (ipv6?)
|
|
|
|
if(val.length == 16)
|
|
|
|
raise RuntimeError, "IPv6 address specified for IPv4 payload."
|
|
|
|
end
|
2005-07-09 00:24:02 +00:00
|
|
|
elsif (pack == 'RAW')
|
|
|
|
# Just use the raw value...
|
|
|
|
else
|
2006-10-16 19:04:39 +00:00
|
|
|
# Check to see if the value is a hex string. If so, convert
|
|
|
|
# it.
|
|
|
|
if val.kind_of?(String)
|
|
|
|
if val =~ /^\\x/
|
|
|
|
val = [ val.gsub(/\\x/, '') ].pack("H*").unpack(pack)[0]
|
|
|
|
elsif val =~ /^0x/
|
|
|
|
val = val.hex
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-07-10 19:21:40 +00:00
|
|
|
# NOTE:
|
|
|
|
# Packing assumes integer format at this point, should fix...
|
2007-07-25 03:24:51 +00:00
|
|
|
val = [ val.to_i ].pack(pack)
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Substitute it
|
2008-10-19 20:32:14 +00:00
|
|
|
if (['ADDR16MSB', 'ADDR16LSB'].include?(pack))
|
|
|
|
if (offset.length != 2)
|
|
|
|
raise RuntimeError, "Missing value for payload offset, there must be two offsets."
|
|
|
|
end
|
|
|
|
|
|
|
|
if (pack == 'ADDR16LSB')
|
|
|
|
val = val.unpack('N').pack('V')
|
|
|
|
end
|
|
|
|
|
|
|
|
raw[offset[0], 2] = val[0, 2]
|
|
|
|
raw[offset[1], 2] = val[2, 2]
|
|
|
|
|
|
|
|
elsif (['ADDR22MSB', 'ADDR22LSB'].include?(pack))
|
|
|
|
if (offset.length != 2)
|
|
|
|
raise RuntimeError, "Missing value for payload offset, there must be two offsets."
|
|
|
|
end
|
|
|
|
|
|
|
|
if (pack == 'ADDR22LSB')
|
|
|
|
val = val.unpack('N').pack('V')
|
|
|
|
end
|
|
|
|
|
|
|
|
hi = (0xfffffc00 & val) >> 10
|
|
|
|
lo = 0x3ff & val
|
|
|
|
|
|
|
|
ins = raw[offset[0], 4]
|
|
|
|
raw[offset[0], 4] = ins | hi
|
|
|
|
|
|
|
|
ins = raw[offset[1], 4]
|
|
|
|
raw[offset[1], 4] = ins | lo
|
|
|
|
|
|
|
|
else
|
|
|
|
raw[offset, val.length] = val
|
|
|
|
|
|
|
|
end
|
2005-07-09 00:24:02 +00:00
|
|
|
else
|
2007-07-25 03:24:51 +00:00
|
|
|
wlog("Missing value for payload offset #{name}, skipping.",
|
2005-07-17 06:01:11 +00:00
|
|
|
'core', LEV_3)
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
# Replaces an individual variable in the supplied buffer at an offset
|
|
|
|
# using the given pack type. This is here to allow derived payloads
|
|
|
|
# the opportunity to replace advanced variables.
|
2005-07-13 18:06:12 +00:00
|
|
|
#
|
2005-07-09 00:24:02 +00:00
|
|
|
def replace_var(raw, name, offset, pack)
|
2005-07-09 01:03:11 +00:00
|
|
|
return false
|
2005-07-09 00:24:02 +00:00
|
|
|
end
|
|
|
|
|
2005-07-13 18:06:12 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Shortcut methods for filtering compatible encoders
|
|
|
|
# and NOP sleds
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the array of compatible encoders for this payload instance.
|
|
|
|
#
|
|
|
|
def compatible_encoders
|
|
|
|
encoders = []
|
|
|
|
|
|
|
|
framework.encoders.each_module_ranked(
|
2005-07-13 21:09:07 +00:00
|
|
|
'Arch' => self.arch) { |name, mod|
|
2005-07-14 14:46:18 +00:00
|
|
|
encoders << [ name, mod ]
|
2005-07-13 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return encoders
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the array of compatible nops for this payload instance.
|
|
|
|
#
|
|
|
|
def compatible_nops
|
|
|
|
nops = []
|
|
|
|
|
|
|
|
framework.nops.each_module_ranked(
|
2005-07-13 21:09:07 +00:00
|
|
|
'Arch' => self.arch) { |name, mod|
|
2005-07-14 14:46:18 +00:00
|
|
|
nops << [ name, mod ]
|
2005-07-13 18:06:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nops
|
|
|
|
end
|
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Event notifications.
|
|
|
|
#
|
|
|
|
##
|
2007-07-25 03:24:51 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
#
|
2007-02-11 03:16:08 +00:00
|
|
|
# Once an exploit completes and a session has been created on behalf of the
|
2005-11-19 15:09:41 +00:00
|
|
|
# payload, the framework will call the payload's on_session notification
|
|
|
|
# routine to allow it to manipulate the session prior to handing off
|
|
|
|
# control to the user.
|
|
|
|
#
|
|
|
|
def on_session(session)
|
2007-07-25 03:24:51 +00:00
|
|
|
|
|
|
|
|
2005-11-27 18:42:44 +00:00
|
|
|
# If this payload is associated with an exploit, inform the exploit
|
2007-07-25 03:24:51 +00:00
|
|
|
# that a session has been created and potentially shut down any
|
2005-11-27 18:42:44 +00:00
|
|
|
# open sockets. This allows active exploits to continue hammering
|
|
|
|
# on a service until a session is created.
|
2005-11-28 20:41:11 +00:00
|
|
|
if (assoc_exploit)
|
|
|
|
|
2005-11-27 18:42:44 +00:00
|
|
|
# Signal that a new session is created by calling the exploit's
|
|
|
|
# on_new_session handler. The default behavior is to set an
|
|
|
|
# instance variable, which the exploit will have to check.
|
2010-09-21 03:05:55 +00:00
|
|
|
begin
|
|
|
|
assoc_exploit.on_new_session(session)
|
|
|
|
rescue ::Exception => e
|
2011-11-20 01:10:08 +00:00
|
|
|
dlog("#{assoc_exploit.refname}: on_new_session handler triggered exception: #{e.class} #{e} #{e.backtrace}", 'core', LEV_1) rescue nil
|
2010-09-21 03:05:55 +00:00
|
|
|
end
|
2007-07-25 03:24:51 +00:00
|
|
|
|
2005-11-27 18:42:44 +00:00
|
|
|
# Set the abort sockets flag only if the exploit is not passive
|
|
|
|
# and the connection type is not 'find'
|
|
|
|
if (
|
|
|
|
(assoc_exploit.exploit_type == Exploit::Type::Remote) and
|
|
|
|
(assoc_exploit.passive? == false) and
|
|
|
|
(connection_type != 'find')
|
|
|
|
)
|
|
|
|
assoc_exploit.abort_sockets
|
|
|
|
end
|
|
|
|
|
2005-11-24 18:50:33 +00:00
|
|
|
end
|
2005-11-27 18:42:44 +00:00
|
|
|
|
2005-11-19 15:09:41 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# This attribute holds the string that should be prepended to the buffer
|
|
|
|
# when it's generated.
|
|
|
|
#
|
|
|
|
attr_accessor :prepend
|
|
|
|
#
|
|
|
|
# This attribute holds the string that should be appended to the buffer
|
|
|
|
# when it's generated.
|
|
|
|
#
|
|
|
|
attr_accessor :append
|
|
|
|
#
|
|
|
|
# This attribute holds the string that should be prepended to the encoded
|
|
|
|
# version of the payload (in front of the encoder as well).
|
|
|
|
#
|
|
|
|
attr_accessor :prepend_encoder
|
2005-07-09 00:24:02 +00:00
|
|
|
|
2005-11-15 21:25:23 +00:00
|
|
|
#
|
|
|
|
# If this payload is associated with an exploit, the assoc_exploit
|
|
|
|
# attribute will point to that exploit instance.
|
|
|
|
#
|
|
|
|
attr_accessor :assoc_exploit
|
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
protected
|
|
|
|
|
2007-08-11 00:37:50 +00:00
|
|
|
#
|
|
|
|
# If the payload has assembly that needs to be compiled, do so now.
|
2013-05-01 21:54:08 +00:00
|
|
|
#
|
|
|
|
# Blobs will be cached in the framework's PayloadSet
|
|
|
|
#
|
|
|
|
# @see PayloadSet#check_blob_cache
|
|
|
|
# @param asm [String] Assembly code to be assembled into a raw payload
|
|
|
|
# @return [String] The final, assembled payload
|
|
|
|
# @raise ArgumentError if +asm+ is blank
|
|
|
|
def build(asm, off={})
|
2009-10-26 17:44:43 +00:00
|
|
|
if(asm.nil? or asm.empty?)
|
2013-05-01 21:54:08 +00:00
|
|
|
raise ArgumentError, "Assembly must not be empty"
|
2009-10-26 17:44:43 +00:00
|
|
|
end
|
2007-08-11 00:37:50 +00:00
|
|
|
|
2013-05-01 21:54:08 +00:00
|
|
|
# Use the refname so blobs can be flushed when the module gets
|
|
|
|
# reloaded and use the hash value to ensure that we're actually
|
|
|
|
# getting the right blob for the given assembly.
|
|
|
|
cache_key = refname + asm.hash.to_s
|
2007-08-11 00:37:50 +00:00
|
|
|
cache_entry = framework.payloads.check_blob_cache(cache_key)
|
|
|
|
|
2008-03-18 16:01:15 +00:00
|
|
|
off.each_pair { |option, val|
|
|
|
|
if (val[1] == 'RAW')
|
|
|
|
asm = asm.gsub(/#{option}/){ datastore[option] }
|
|
|
|
off.delete(option)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2007-08-11 00:37:50 +00:00
|
|
|
# If there is a valid cache entry, then we don't need to worry about
|
|
|
|
# rebuilding the assembly
|
|
|
|
if cache_entry
|
|
|
|
# Update the local offsets from the cache
|
|
|
|
off.each_key { |option|
|
|
|
|
off[option] = cache_entry[1][option]
|
|
|
|
}
|
|
|
|
|
|
|
|
# Return the cached payload blob
|
|
|
|
return cache_entry[0].dup
|
|
|
|
end
|
|
|
|
|
|
|
|
# Assemble the payload from the assembly
|
2012-03-30 23:52:32 +00:00
|
|
|
a = self.arch
|
|
|
|
if a.kind_of? Array
|
|
|
|
a = self.arch.first
|
|
|
|
end
|
|
|
|
cpu = case a
|
|
|
|
when ARCH_X86 then Metasm::Ia32.new
|
|
|
|
when ARCH_X86_64 then Metasm::X86_64.new
|
|
|
|
when ARCH_X64 then Metasm::X86_64.new
|
|
|
|
when ARCH_PPC then Metasm::PowerPC.new
|
|
|
|
when ARCH_ARMLE then Metasm::ARM.new
|
|
|
|
else
|
|
|
|
elog("Broken payload #{refname} has arch unsupported with assembly: #{module_info["Arch"].inspect}")
|
|
|
|
elog("Call stack:\n#{caller.join("\n")}")
|
|
|
|
return ""
|
|
|
|
end
|
2012-03-14 21:39:56 +00:00
|
|
|
sc = Metasm::Shellcode.assemble(cpu, asm).encoded
|
2010-02-08 22:41:36 +00:00
|
|
|
|
2007-08-11 00:37:50 +00:00
|
|
|
# Calculate the actual offsets now that it's been built
|
|
|
|
off.each_pair { |option, val|
|
2012-03-14 21:39:56 +00:00
|
|
|
off[option] = [ sc.offset_of_reloc(option) || val[0], val[1] ]
|
2007-08-11 00:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
# Cache the payload blob
|
|
|
|
framework.payloads.add_blob_cache(cache_key, sc.data, off)
|
|
|
|
|
|
|
|
# Return a duplicated copy of the assembled payload
|
|
|
|
sc.data.dup
|
|
|
|
end
|
|
|
|
|
2007-06-16 05:04:03 +00:00
|
|
|
#
|
|
|
|
# Generate the payload using our local payload blob and offsets
|
|
|
|
#
|
|
|
|
def internal_generate
|
2007-08-11 00:37:50 +00:00
|
|
|
# Build the payload, either by using the raw payload blob defined in the
|
|
|
|
# module or by actually assembling it
|
2013-05-01 21:54:08 +00:00
|
|
|
if assembly and !assembly.empty?
|
|
|
|
raw = build(assembly, offsets)
|
|
|
|
else
|
|
|
|
raw = payload.dup
|
|
|
|
end
|
2007-06-16 05:04:03 +00:00
|
|
|
|
|
|
|
# If the payload is generated and there are offsets to substitute,
|
|
|
|
# do that now.
|
|
|
|
if (raw and offsets)
|
|
|
|
substitute_vars(raw, offsets)
|
|
|
|
end
|
|
|
|
|
|
|
|
return raw
|
|
|
|
end
|
|
|
|
|
2005-07-09 00:24:02 +00:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Custom merge operations for payloads
|
|
|
|
#
|
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Merge the name to prefix the existing one and separate them
|
|
|
|
# with a comma
|
|
|
|
#
|
|
|
|
def merge_name(info, val)
|
|
|
|
if (info['Name'])
|
|
|
|
info['Name'] = val + ',' + info['Name']
|
|
|
|
else
|
|
|
|
info['Name'] = val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2009-06-23 03:49:25 +00:00
|
|
|
end
|