2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core'
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
module Msf
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class is used to track the state of a single encoding operation
|
|
|
|
# from start to finish.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class EncoderState
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Initializes a new encoder state, optionally with a key.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def initialize(key = nil)
|
2009-11-13 22:11:52 +00:00
|
|
|
@orig_buf = nil
|
|
|
|
@buf = nil
|
2005-05-21 17:57:00 +00:00
|
|
|
reset(key)
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Reset the encoder state by initializing the encoded buffer to an empty
|
|
|
|
# string.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def reset(key = nil)
|
|
|
|
init_key(key)
|
|
|
|
|
|
|
|
self.encoded = ''
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
2005-11-15 15:11:43 +00:00
|
|
|
# Set the initial encoding key.
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def init_key(key)
|
|
|
|
self.key = key
|
|
|
|
self.orig_key = key
|
|
|
|
end
|
|
|
|
|
2005-12-31 18:47:53 +00:00
|
|
|
#
|
|
|
|
# Set the raw buffer and the original buffer if one has not been set.
|
|
|
|
#
|
|
|
|
def buf=(buf)
|
|
|
|
@orig_buf = buf if (@orig_buf == nil or @buf == nil)
|
|
|
|
@buf = buf
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
attr_accessor :key # :nodoc:
|
|
|
|
attr_accessor :orig_key # :nodoc:
|
2005-12-31 18:47:53 +00:00
|
|
|
attr_reader :buf # :nodoc:
|
|
|
|
attr_reader :orig_buf # :nodoc:
|
2005-10-19 03:20:20 +00:00
|
|
|
attr_accessor :encoded # :nodoc:
|
|
|
|
attr_accessor :context # :nodoc:
|
|
|
|
attr_accessor :badchars # :nodoc:
|
2005-07-10 20:49:13 +00:00
|
|
|
|
2007-08-31 04:01:30 +00:00
|
|
|
# A boolean that indicates whether context encoding is enabled
|
|
|
|
attr_accessor :context_encoding
|
|
|
|
# The address that contains they key on the target machine
|
|
|
|
attr_accessor :context_address
|
|
|
|
|
2005-07-10 20:49:13 +00:00
|
|
|
# Decoder settings
|
2005-10-19 03:20:20 +00:00
|
|
|
attr_accessor :decoder_key_offset, :decoder_key_size, :decoder_key_pack # :nodoc:
|
2005-12-30 01:14:56 +00:00
|
|
|
attr_accessor :decoder_stub # :nodoc:
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
###
|
|
|
|
#
|
|
|
|
# This class is the base class that all encoders inherit from.
|
|
|
|
#
|
|
|
|
###
|
|
|
|
class Encoder < Module
|
|
|
|
|
2006-01-06 02:25:47 +00:00
|
|
|
#
|
|
|
|
# The type set that encoders can fall within. This classifies the type of
|
|
|
|
# output generated by the encoder in terms of the general character set
|
|
|
|
# that is used as well as other potential attributes.
|
|
|
|
#
|
|
|
|
module Type
|
|
|
|
#
|
|
|
|
# 'A' - 'Z', '0' - '9'
|
|
|
|
#
|
|
|
|
AlphanumUpper = "alpha_upper"
|
|
|
|
#
|
|
|
|
# 'a' - 'z', 'A' - 'Z', '0' - '9'
|
|
|
|
#
|
|
|
|
AlphanumMixed = "alpha_mixed"
|
|
|
|
#
|
|
|
|
# Unicode-safe 'A' - 'Z', '0' - '9'
|
|
|
|
#
|
|
|
|
AlphanumUnicodeUpper = "alpha_unicode_upper"
|
|
|
|
#
|
|
|
|
# Unicode-safe 'a' - 'z', 'A' - 'Z', '0' - '9'
|
|
|
|
#
|
|
|
|
AlphanumUnicodeMixed = "alpha_unicode_mixed"
|
2009-10-16 18:27:18 +00:00
|
|
|
#
|
2009-11-16 15:16:08 +00:00
|
|
|
# toupper/tolower safe ascii - not 'a' - 'z', 'A' - 'Z'
|
2009-10-16 18:27:18 +00:00
|
|
|
#
|
|
|
|
NonAlpha = "non_alpha"
|
|
|
|
#
|
|
|
|
# tolower safe ascii - not 'A' - 'Z' (more flexable than nonalpha)
|
|
|
|
#
|
|
|
|
NonUpper = "non_upper"
|
2006-08-14 08:55:37 +00:00
|
|
|
#
|
|
|
|
# tolower safe ascii UTF8-safe (<= 0x7f only)
|
|
|
|
#
|
|
|
|
NonUpperUtf8Safe = "non_upper_utf8_safe"
|
2007-04-01 21:00:51 +00:00
|
|
|
#
|
2012-06-14 22:29:52 +00:00
|
|
|
# tolower safe underscore safe for CVE-2012-2329 - PHP CGI apache_request_headers bof
|
|
|
|
#
|
|
|
|
NonUpperUnderscoreSafe = "non_upper_underscore"
|
|
|
|
#
|
2007-04-01 21:00:51 +00:00
|
|
|
# May result in the generation of any characters
|
|
|
|
#
|
|
|
|
Unspecified = "unspecified"
|
2009-11-16 15:16:08 +00:00
|
|
|
#
|
2007-04-01 21:00:51 +00:00
|
|
|
# The raw payload passed to the encoder will be the same as the encoded
|
|
|
|
# payload
|
2006-01-06 02:25:47 +00:00
|
|
|
#
|
2007-04-01 21:00:51 +00:00
|
|
|
Raw = "raw"
|
2009-12-28 20:39:43 +00:00
|
|
|
#
|
|
|
|
# Special Single Static Bit encoder
|
|
|
|
#
|
|
|
|
SingleStaticBit = "single_static_bit"
|
2010-06-24 22:36:51 +00:00
|
|
|
#
|
2010-10-18 15:41:13 +00:00
|
|
|
# Special printf(1) via PHP magic_quotes Command Encoder
|
2010-06-24 22:36:51 +00:00
|
|
|
#
|
2010-10-18 15:41:13 +00:00
|
|
|
PrintfPHPMagicQuotes = "printf_php_mq"
|
2006-01-06 02:25:47 +00:00
|
|
|
end
|
|
|
|
|
2005-11-15 15:11:43 +00:00
|
|
|
#
|
|
|
|
# Initializes an encoder module instance using the supplied information
|
|
|
|
# hash.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def initialize(info)
|
2005-11-24 20:41:56 +00:00
|
|
|
super({
|
|
|
|
'Platform' => '' # All platforms by default
|
|
|
|
}.update(info))
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# Encoder information accessors that can be overriden
|
|
|
|
# by derived classes
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-07-14 20:05:41 +00:00
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns MODULE_ENCODER to indicate that this is an encoder module.
|
|
|
|
#
|
2005-07-14 20:05:41 +00:00
|
|
|
def self.type
|
|
|
|
return MODULE_ENCODER
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns MODULE_ENCODER to indicate that this is an encoder module.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def type
|
|
|
|
return MODULE_ENCODER
|
|
|
|
end
|
|
|
|
|
2006-01-06 02:25:47 +00:00
|
|
|
#
|
|
|
|
# Returns the type or types of encoders that this specific module
|
|
|
|
# classifies as. If there is more than one type, the values should be
|
|
|
|
# separated by whitespace.
|
|
|
|
#
|
|
|
|
def encoder_type
|
2007-04-01 21:00:51 +00:00
|
|
|
module_info['EncoderType'] || Type::Unspecified
|
2006-01-06 02:25:47 +00:00
|
|
|
end
|
|
|
|
|
2005-07-10 20:49:13 +00:00
|
|
|
#
|
2005-11-28 12:40:13 +00:00
|
|
|
# Returns the decoder stub to use based on the supplied state.
|
2005-07-10 20:49:13 +00:00
|
|
|
#
|
|
|
|
def decoder_stub(state)
|
2005-11-01 00:02:51 +00:00
|
|
|
return decoder_hash['Stub'] || ''
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the offset to the key associated with the decoder stub.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def decoder_key_offset
|
2005-11-01 00:02:51 +00:00
|
|
|
return decoder_hash['KeyOffset']
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the size of the key, in bytes.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def decoder_key_size
|
2005-11-01 00:02:51 +00:00
|
|
|
return decoder_hash['KeySize']
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the size of each logical encoding block, in bytes. This
|
|
|
|
# is typically the same as decoder_key_size.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def decoder_block_size
|
2005-11-01 00:02:51 +00:00
|
|
|
return decoder_hash['BlockSize']
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the byte-packing character that should be used to encode
|
|
|
|
# the key.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def decoder_key_pack
|
2005-11-01 00:02:51 +00:00
|
|
|
return decoder_hash['KeyPack'] || 'V'
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns the module's decoder hash or an empty hash.
|
|
|
|
#
|
|
|
|
def decoder_hash
|
|
|
|
module_info['Decoder'] || {}
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# Encoding
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# This method generates an encoded version of the supplied buffer in buf
|
|
|
|
# using the bad characters as guides. On success, an encoded and
|
|
|
|
# functional version of the supplied buffer will be returned. Otherwise,
|
|
|
|
# an exception will be thrown if an error is encountered during the
|
|
|
|
# encoding process.
|
|
|
|
#
|
2009-12-13 19:03:35 +00:00
|
|
|
def encode(buf, badchars = nil, state = nil, platform = nil)
|
|
|
|
|
|
|
|
# Configure platform hints if necessary
|
|
|
|
init_platform(platform) if platform
|
|
|
|
|
2005-07-10 19:35:46 +00:00
|
|
|
# Initialize an empty set of bad characters
|
|
|
|
badchars = '' if (!badchars)
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Initialize the encoding state and key as necessary
|
|
|
|
if (state == nil)
|
|
|
|
state = EncoderState.new
|
|
|
|
end
|
|
|
|
|
|
|
|
# Prepend data to the buffer as necessary
|
|
|
|
buf = prepend_buf + buf
|
|
|
|
|
2005-12-30 01:14:56 +00:00
|
|
|
init_state(state)
|
|
|
|
|
|
|
|
# Save the buffer in the encoding state
|
2007-06-29 00:29:29 +00:00
|
|
|
state.badchars = badchars || ''
|
2005-12-30 01:14:56 +00:00
|
|
|
state.buf = buf
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# If this encoder is key-based and we don't already have a key, find one
|
|
|
|
if ((decoder_key_size) and
|
|
|
|
(state.key == nil))
|
|
|
|
# Find a key that doesn't contain and wont generate any bad
|
|
|
|
# characters
|
2007-08-31 04:01:30 +00:00
|
|
|
state.init_key(obtain_key(buf, badchars, state))
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
if (state.key == nil)
|
2005-07-10 19:35:46 +00:00
|
|
|
raise NoKeyError, "A key could not be found for the #{self.name} encoder.", caller
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-12-30 01:14:56 +00:00
|
|
|
# Reset the encoded buffer at this point since it may have been changed
|
|
|
|
# while finding a key.
|
|
|
|
state.encoded = ''
|
2005-07-10 20:49:13 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Call encode_begin to do any encoder specific pre-processing
|
|
|
|
encode_begin(state)
|
|
|
|
|
|
|
|
# Perform the actual encoding operation with the determined state
|
2005-12-30 01:14:56 +00:00
|
|
|
do_encode(state)
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
# Call encoded_end to do any encoder specific post-processing
|
|
|
|
encode_end(state)
|
|
|
|
|
|
|
|
# Return the encoded buffer to the caller
|
|
|
|
return state.encoded
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Performs the actual encoding operation after the encoder state has been
|
|
|
|
# initialized and is ready to go.
|
|
|
|
#
|
2005-12-30 01:14:56 +00:00
|
|
|
def do_encode(state)
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Copy the decoder stub since we may need to modify it
|
2005-07-10 20:49:13 +00:00
|
|
|
stub = decoder_stub(state).dup
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-11-01 00:30:20 +00:00
|
|
|
if (state.key != nil and state.decoder_key_offset)
|
2005-05-21 17:57:00 +00:00
|
|
|
# Substitute the decoder key in the copy of the decoder stub with the
|
|
|
|
# one that we found
|
2007-08-31 04:01:30 +00:00
|
|
|
real_key = state.key
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2007-08-31 04:01:30 +00:00
|
|
|
# If we're using context encoding, the actual value we use for
|
|
|
|
# substitution is the context address, not the key we use for
|
|
|
|
# encoding
|
|
|
|
real_key = state.context_address if (state.context_encoding)
|
|
|
|
|
|
|
|
stub[state.decoder_key_offset,state.decoder_key_size] = [ real_key.to_i ].pack(state.decoder_key_pack)
|
2005-11-01 00:02:51 +00:00
|
|
|
else
|
|
|
|
stub = encode_finalize_stub(state, stub)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Walk the buffer encoding each block along the way
|
|
|
|
offset = 0
|
|
|
|
|
2005-11-01 00:02:51 +00:00
|
|
|
if (decoder_block_size)
|
2005-12-30 01:14:56 +00:00
|
|
|
while (offset < state.buf.length)
|
|
|
|
block = state.buf[offset, decoder_block_size]
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2013-01-14 03:07:39 +00:00
|
|
|
# Append here (String#<<) instead of creating a new string with
|
|
|
|
# String#+ because the allocations kill performance with large
|
|
|
|
# buffers. This isn't usually noticeable on most shellcode, but
|
|
|
|
# when doing stage encoding on meterpreter (~750k bytes) the
|
|
|
|
# difference is 2 orders of magnitude.
|
|
|
|
state.encoded << encode_block(state,
|
2005-11-01 00:02:51 +00:00
|
|
|
block + ("\x00" * (decoder_block_size - block.length)))
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2005-11-01 00:02:51 +00:00
|
|
|
offset += decoder_block_size
|
|
|
|
end
|
|
|
|
else
|
2005-12-30 01:14:56 +00:00
|
|
|
state.encoded = encode_block(state, state.buf)
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
2005-12-30 01:14:56 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Prefix the decoder stub to the encoded buffer
|
|
|
|
state.encoded = stub + state.encoded
|
|
|
|
|
|
|
|
# Last but not least, do one last badchar pass to see if the stub +
|
|
|
|
# encoded payload leads to any bad char issues...
|
2005-12-30 01:14:56 +00:00
|
|
|
if ((badchar_idx = has_badchars?(state.encoded, state.badchars)) != nil)
|
2009-11-16 15:16:08 +00:00
|
|
|
raise BadcharError.new(state.encoded, badchar_idx, stub.length, state.encoded[badchar_idx]),
|
|
|
|
"The #{self.name} encoder failed to encode without bad characters.",
|
2005-05-21 17:57:00 +00:00
|
|
|
caller
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# Buffer management
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
|
|
|
|
|
|
|
#
|
|
|
|
# Returns a string that should be prepended to the encoded version of the
|
|
|
|
# buffer before returning it to callers.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def prepend_buf
|
|
|
|
return ''
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
#
|
|
|
|
# Pre-processing, post-processing, and block encoding stubs
|
|
|
|
#
|
2005-10-19 03:20:20 +00:00
|
|
|
##
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Called when encoding is about to start immediately after the encoding
|
|
|
|
# state has been initialized.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def encode_begin(state)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-11-01 00:02:51 +00:00
|
|
|
#
|
|
|
|
# This callback allows a derived class to finalize a stub after a key have
|
|
|
|
# been selected. The finalized stub should be returned.
|
|
|
|
#
|
|
|
|
def encode_finalize_stub(state, stub)
|
|
|
|
stub
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Called after encoding has completed.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def encode_end(state)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Called once for each block being encoded based on the attributes of the
|
|
|
|
# decoder.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def encode_block(state, block)
|
|
|
|
return block
|
|
|
|
end
|
|
|
|
|
2006-01-06 15:35:06 +00:00
|
|
|
#
|
|
|
|
# Provides the encoder with an opportunity to return the native format (as
|
|
|
|
# in the format the code will be in when it executes on the target). In
|
|
|
|
# general, the same buffer is returned to the caller. However, for things
|
|
|
|
# like unicode, the buffer is unicod encoded and then returned.
|
|
|
|
#
|
|
|
|
def to_native(buf)
|
|
|
|
buf
|
|
|
|
end
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
protected
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Initializes the encoding state supplied as an argument to the attributes
|
|
|
|
# that have been defined for this decoder stub, such as key offset, size,
|
|
|
|
# and pack.
|
|
|
|
#
|
2005-07-13 21:09:07 +00:00
|
|
|
def init_state(state)
|
|
|
|
# Update the state with default decoder information
|
|
|
|
state.decoder_key_offset = decoder_key_offset
|
|
|
|
state.decoder_key_size = decoder_key_size
|
|
|
|
state.decoder_key_pack = decoder_key_pack
|
2005-12-30 01:14:56 +00:00
|
|
|
state.decoder_stub = nil
|
2005-12-31 18:47:53 +00:00
|
|
|
|
|
|
|
# Restore the original buffer in case it was modified.
|
|
|
|
state.buf = state.orig_buf
|
2005-07-13 21:09:07 +00:00
|
|
|
end
|
|
|
|
|
2009-12-13 19:03:35 +00:00
|
|
|
#
|
|
|
|
# This provides a hook method for platform specific
|
|
|
|
# processing prior to the rest of encode() running
|
|
|
|
#
|
|
|
|
def init_platform(platform)
|
|
|
|
|
|
|
|
end
|
2009-11-16 15:16:08 +00:00
|
|
|
#
|
2007-08-31 04:01:30 +00:00
|
|
|
# Obtains the key to use during encoding. If context encoding is enabled,
|
|
|
|
# special steps are taken. Otherwise, the derived class is given an
|
|
|
|
# opportunity to find the key.
|
|
|
|
#
|
|
|
|
def obtain_key(buf, badchars, state)
|
|
|
|
if datastore['EnableContextEncoding']
|
|
|
|
return find_context_key(buf, badchars, state)
|
|
|
|
else
|
|
|
|
return find_key(buf, badchars, state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# This method finds a compatible key for the supplied buffer based also on
|
|
|
|
# the supplied bad characters list. This is meant to make encoders more
|
|
|
|
# reliable and less prone to bad character failure by doing a fairly
|
|
|
|
# complete key search before giving up on an encoder.
|
|
|
|
#
|
2007-08-31 04:01:30 +00:00
|
|
|
def find_key(buf, badchars, state)
|
|
|
|
# Otherwise, we use the traditional method
|
2005-05-21 17:57:00 +00:00
|
|
|
key_bytes = [ ]
|
|
|
|
cur_key = [ ]
|
|
|
|
bad_keys = find_bad_keys(buf, badchars)
|
|
|
|
found = false
|
2006-04-26 05:14:55 +00:00
|
|
|
allset = [*(0..255)]
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
# Keep chugging until we find something...right
|
|
|
|
while (!found)
|
|
|
|
# Scan each byte position
|
|
|
|
0.upto(decoder_key_size - 1) { |index|
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2006-04-26 16:51:05 +00:00
|
|
|
# Subtract the bad and leave the good
|
2009-09-22 06:49:52 +00:00
|
|
|
good_keys = allset - bad_keys[index].keys
|
2006-04-26 05:14:55 +00:00
|
|
|
|
2006-04-26 16:51:05 +00:00
|
|
|
# Was there anything left for this index?
|
|
|
|
if (good_keys.length == 0)
|
|
|
|
# Not much we can do about this :(
|
|
|
|
return nil
|
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
|
2006-04-26 16:51:05 +00:00
|
|
|
# Set the appropriate key byte
|
|
|
|
key_bytes[index] = good_keys[ rand(good_keys.length) ]
|
2005-05-21 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
2009-11-16 15:16:08 +00:00
|
|
|
# Assume that we're going to rock this...
|
2005-05-21 17:57:00 +00:00
|
|
|
found = true
|
|
|
|
|
|
|
|
# Scan each byte and see what we've got going on to make sure
|
|
|
|
# no funny business is happening
|
|
|
|
key_bytes.each { |byte|
|
2009-06-02 23:36:58 +00:00
|
|
|
if (badchars.index(byte.chr) != nil)
|
2005-05-21 17:57:00 +00:00
|
|
|
found = false
|
|
|
|
end
|
|
|
|
}
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2006-04-26 05:14:55 +00:00
|
|
|
found = find_key_verify(buf, key_bytes, badchars) if found
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Do we have all the key bytes accounted for?
|
|
|
|
if (key_bytes.length != decoder_key_size)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
|
|
|
return key_bytes_to_integer(key_bytes)
|
|
|
|
end
|
|
|
|
|
2007-08-31 04:01:30 +00:00
|
|
|
#
|
|
|
|
# Parses a context information file in an effort to find a compatible key
|
|
|
|
#
|
|
|
|
def find_context_key(buf, badchars, state)
|
|
|
|
# Make sure our context information file is sane
|
|
|
|
if File.exists?(datastore['ContextInformationFile']) == false
|
|
|
|
raise NoKeyError, "A context information file must specified when using context encoding", caller
|
|
|
|
end
|
|
|
|
|
|
|
|
# Holds the address and key that we ultimately find
|
|
|
|
address = nil
|
|
|
|
key = nil
|
|
|
|
|
|
|
|
# Now, parse records from the information file searching for entries
|
|
|
|
# that are compatible with our bad character set
|
|
|
|
File.open(datastore['ContextInformationFile']) { |f|
|
|
|
|
begin
|
|
|
|
# Keep looping until we hit an EOF error or we find
|
|
|
|
# a compatible key
|
|
|
|
while key.nil?
|
|
|
|
# Read in the header
|
2007-10-23 20:07:55 +00:00
|
|
|
type, chunk_base_address, size = f.read(9).unpack('CNN')
|
|
|
|
offset = 0
|
2007-08-31 04:01:30 +00:00
|
|
|
|
|
|
|
# Read in the blob of data that will act as our key state
|
|
|
|
data = f.read(size)
|
|
|
|
|
|
|
|
# If the address doesn't contain bad characters, check to see
|
|
|
|
# the data itself will result in bad characters being generated
|
|
|
|
while data.length > decoder_key_size
|
|
|
|
# Extract the current set of key bytes
|
|
|
|
key_bytes = []
|
|
|
|
|
|
|
|
# My ruby is rusty
|
|
|
|
data[0, decoder_key_size].each_byte { |b|
|
|
|
|
key_bytes << b
|
|
|
|
}
|
|
|
|
|
2007-10-23 20:07:55 +00:00
|
|
|
# If the key verifies correctly, we need to check it's address
|
2007-08-31 04:01:30 +00:00
|
|
|
if find_key_verify(buf, key_bytes, badchars)
|
2007-10-23 20:07:55 +00:00
|
|
|
address = chunk_base_address + offset
|
|
|
|
|
|
|
|
# Pack it to byte form so that we can check each byte for
|
|
|
|
# bad characters
|
|
|
|
address_bytes = integer_to_key_bytes(address)
|
2009-11-16 15:16:08 +00:00
|
|
|
|
2007-10-23 20:07:55 +00:00
|
|
|
# Scan each byte and see what we've got going on to make sure
|
|
|
|
# no funny business is happening with the address
|
|
|
|
invalid_key = false
|
|
|
|
address_bytes.each { |byte|
|
2009-06-02 23:36:58 +00:00
|
|
|
if badchars.index(byte.chr)
|
2007-10-23 20:07:55 +00:00
|
|
|
invalid_key = true
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
if invalid_key == false
|
|
|
|
key = key_bytes_to_integer(key_bytes)
|
|
|
|
break
|
|
|
|
end
|
2007-08-31 04:01:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# If it didn't verify, then we need to proceed
|
|
|
|
data = data[1, data.length - 1]
|
2007-10-23 20:07:55 +00:00
|
|
|
offset += 1
|
2007-08-31 04:01:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue EOFError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
# If the key is nil after all is said and done, then we failed to locate
|
|
|
|
# a compatible context-sensitive key
|
|
|
|
if key.nil?
|
|
|
|
raise NoKeyError, "No context key could be located in #{datastore['ContextInformationFile']}", caller
|
|
|
|
# Otherwise, we successfully determined the key, now we need to update
|
|
|
|
# the encoding state with our context address and set context encoding
|
|
|
|
# to true so that the encoders know to use it
|
|
|
|
else
|
|
|
|
ilog("#{refname}: Successfully found context address @ #{"%.8x" % address} using key #{"%.8x" % key}")
|
|
|
|
|
|
|
|
state.context_address = address
|
|
|
|
state.context_encoding = true
|
|
|
|
end
|
|
|
|
|
|
|
|
return key
|
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the list of bad keys associated with this encoder.
|
|
|
|
#
|
2006-08-14 08:55:37 +00:00
|
|
|
def find_bad_keys(buf, badchars)
|
2009-09-22 06:49:52 +00:00
|
|
|
return Array.new(decoder_key_size) { Hash.new }
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-10-19 03:20:20 +00:00
|
|
|
#
|
|
|
|
# Returns the index of any bad characters found in the supplied buffer.
|
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def has_badchars?(buf, badchars)
|
|
|
|
badchars.each_byte { |badchar|
|
2009-06-02 23:36:58 +00:00
|
|
|
idx = buf.index(badchar.chr)
|
2005-05-21 17:57:00 +00:00
|
|
|
|
|
|
|
if (idx != nil)
|
|
|
|
return idx
|
2009-11-16 15:16:08 +00:00
|
|
|
end
|
2005-05-21 17:57:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2005-07-12 14:32:44 +00:00
|
|
|
#
|
2009-11-16 15:16:08 +00:00
|
|
|
# Convert individual key bytes into a single integer based on the
|
2005-05-21 17:57:00 +00:00
|
|
|
# decoder's key size and packing requirements
|
2005-07-12 14:32:44 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def key_bytes_to_integer(key_bytes)
|
2006-04-26 05:14:55 +00:00
|
|
|
return key_bytes_to_buffer(key_bytes).unpack(decoder_key_pack)[0]
|
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# Convert individual key bytes into a byte buffer
|
|
|
|
#
|
|
|
|
def key_bytes_to_buffer(key_bytes)
|
2009-06-02 23:36:58 +00:00
|
|
|
return key_bytes.pack('C*')[0, decoder_key_size]
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-07-12 14:32:44 +00:00
|
|
|
#
|
2009-11-16 15:16:08 +00:00
|
|
|
# Convert an integer into the individual key bytes based on the
|
2005-05-21 17:57:00 +00:00
|
|
|
# decoder's key size and packing requirements
|
2005-07-12 14:32:44 +00:00
|
|
|
#
|
2005-05-21 17:57:00 +00:00
|
|
|
def integer_to_key_bytes(integer)
|
2009-06-02 23:36:58 +00:00
|
|
|
return [ integer.to_i ].pack(decoder_key_pack).unpack('C*')[0, decoder_key_size]
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
2005-12-30 06:05:23 +00:00
|
|
|
#
|
|
|
|
# Determines if the key selected by find_key is usable
|
|
|
|
#
|
2006-04-26 05:14:55 +00:00
|
|
|
def find_key_verify(buf, key_bytes, badchars)
|
2005-12-30 06:05:23 +00:00
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2005-05-21 17:57:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2005-07-09 21:18:49 +00:00
|
|
|
require 'msf/core/encoder/xor'
|
|
|
|
require 'msf/core/encoder/xor_additive_feedback'
|
2006-01-06 02:25:47 +00:00
|
|
|
require 'msf/core/encoder/alphanum'
|
2006-04-20 21:11:33 +00:00
|
|
|
require 'msf/core/encoder/nonalpha'
|
2009-06-02 23:36:58 +00:00
|
|
|
require 'msf/core/encoder/nonupper'
|
2009-11-16 15:16:08 +00:00
|
|
|
|