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



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

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



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

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

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



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

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



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
69
# File 'lib/rex/encoder/alpha2/generic.rb', line 32

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



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

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

.gen_decoder_prefix(reg, offset) ⇒ Object



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

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



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

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