2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2006-01-06 00:57:14 +00:00
|
|
|
|
|
|
|
require 'rex/text'
|
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Encoder
|
|
|
|
module Alpha2
|
|
|
|
|
|
|
|
class Generic
|
2009-11-04 05:31:54 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Note: 'A' is presumed to be accepted, but excluded from the accepted characters, because it serves as the terminator
|
|
|
|
def Generic.default_accepted_chars ; ('a' .. 'z').to_a + ('B' .. 'Z').to_a + ('0' .. '9').to_a ; end
|
2009-11-04 05:31:54 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def Generic.gen_decoder_prefix(reg, offset)
|
|
|
|
# Should never happen - have to pick a specifc
|
|
|
|
# encoding:
|
|
|
|
# alphamixed, alphaupper, unicodemixed, unicodeupper
|
|
|
|
''
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def Generic.gen_decoder(reg, offset)
|
|
|
|
# same as above
|
|
|
|
return ''
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def Generic.gen_second(block, base)
|
|
|
|
# XOR encoder for ascii - unicode uses additive
|
|
|
|
(block^base)
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def Generic.encode_byte(block, badchars)
|
|
|
|
accepted_chars = default_accepted_chars.dup
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
badchars.each_char {|c| accepted_chars.delete(c) } if badchars
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# No, not nipple.
|
|
|
|
nibble_chars = Array.new(0x10) {[]}
|
|
|
|
accepted_chars.each {|c| nibble_chars[c.unpack('C')[0] & 0x0F].push(c) }
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
poss_encodings = []
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
block_low_nibble = block & 0x0F
|
|
|
|
block_high_nibble = block >> 4
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Get list of chars suitable for expressing lower part of byte
|
|
|
|
first_chars = nibble_chars[block_low_nibble]
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Build a list of possible encodings
|
|
|
|
first_chars.each do |first_char|
|
|
|
|
first_high_nibble = first_char.unpack('C')[0] >> 4
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# In the decoding process, the low nibble of the second char gets combined
|
|
|
|
# (either ADDed or XORed depending on the encoder) with the high nibble of the first char,
|
|
|
|
# and we want the high nibble of our input byte to result
|
|
|
|
second_low_nibble = gen_second(block_high_nibble, first_high_nibble) & 0x0F
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Find valid second chars for this first char and add each combination to our possible encodings
|
|
|
|
second_chars = nibble_chars[second_low_nibble]
|
|
|
|
second_chars.each {|second_char| poss_encodings.push(second_char + first_char) }
|
|
|
|
end
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
if poss_encodings.empty?
|
|
|
|
raise RuntimeError, "No encoding of #{"0x%.2X" % block} possible with limited character set"
|
|
|
|
end
|
2013-03-08 00:16:57 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# Return a random encoding
|
|
|
|
poss_encodings[rand(poss_encodings.length)]
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
def Generic.encode(buf, reg, offset, badchars = '')
|
|
|
|
encoded = gen_decoder(reg, offset)
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
buf.each_byte {
|
|
|
|
|block|
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
encoded << encode_byte(block, badchars)
|
|
|
|
}
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
encoded << add_terminator()
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
return encoded
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2013-08-30 21:28:33 +00:00
|
|
|
# 'A' signifies the end of the encoded shellcode
|
|
|
|
def Generic.add_terminator()
|
|
|
|
'AA'
|
|
|
|
end
|
2006-01-06 00:57:14 +00:00
|
|
|
|
2009-11-04 05:31:54 +00:00
|
|
|
end end end end
|
|
|
|
|