Class: Rex::Encoder::Alpha2::Generic

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/encoder/alpha2/generic.rb

Direct Known Subclasses

AlphaMixed, AlphaUpper, UnicodeMixed, UnicodeUpper

Class Method Summary collapse

Class Method Details

.add_terminatorObject

'A' signifies the end of the encoded shellcode


85
86
87
# File 'lib/rex/encoder/alpha2/generic.rb', line 85

def Generic.add_terminator()
  'AA'
end

.default_accepted_charsObject

Note: 'A' is presumed to be accepted, but excluded from the accepted characters, because it serves as the terminator


12
# File 'lib/rex/encoder/alpha2/generic.rb', line 12

def Generic.default_accepted_chars ; ('a' .. 'z').to_a + ('B' .. 'Z').to_a + ('0' .. '9').to_a ; end

.encode(buf, reg, offset, badchars = '') ⇒ Object


70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rex/encoder/alpha2/generic.rb', line 70

def Generic.encode(buf, reg, offset, badchars = '')
  encoded = gen_decoder(reg, offset)

  buf.each_byte {
    |block|

    encoded << encode_byte(block, badchars)
  }

  encoded << add_terminator()

  return encoded
end

.encode_byte(block, badchars) ⇒ Object


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rex/encoder/alpha2/generic.rb', line 31

def Generic.encode_byte(block, badchars)
  accepted_chars = default_accepted_chars.dup

  badchars.each_char {|c| accepted_chars.delete(c) } if badchars

  # No, not nipple.
  nibble_chars = Array.new(0x10) {[]}
  accepted_chars.each {|c| nibble_chars[c.unpack('C')[0] & 0x0F].push(c) }

  poss_encodings = []

  block_low_nibble = block & 0x0F
  block_high_nibble = block >> 4

  # Get list of chars suitable for expressing lower part of byte
  first_chars = nibble_chars[block_low_nibble]

  # Build a list of possible encodings
  first_chars.each do |first_char|
    first_high_nibble = first_char.unpack('C')[0] >> 4

    # 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

    # 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

  if poss_encodings.empty?
    raise RuntimeError, "No encoding of #{"0x%.2X" % block} possible with limited character set"
  end

  # Return a random encoding
  poss_encodings[rand(poss_encodings.length)]
end

.gen_decoder(reg, offset) ⇒ Object


21
22
23
24
# File 'lib/rex/encoder/alpha2/generic.rb', line 21

def Generic.gen_decoder(reg, offset)
  # same as above
  return ''
end

.gen_decoder_prefix(reg, offset) ⇒ Object


14
15
16
17
18
19
# File 'lib/rex/encoder/alpha2/generic.rb', line 14

def Generic.gen_decoder_prefix(reg, offset)
  # Should never happen - have to pick a specifc
  # encoding:
  # alphamixed, alphaupper, unicodemixed, unicodeupper
  ''
end

.gen_second(block, base) ⇒ Object


26
27
28
29
# File 'lib/rex/encoder/alpha2/generic.rb', line 26

def Generic.gen_second(block, base)
  # XOR encoder for ascii - unicode uses additive
  (block^base)
end