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



109
110
111
# File 'lib/rex/encoder/alpha2/generic.rb', line 109

def Generic.add_terminator()
	'AA'
end

.default_accepted_charsObject



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

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

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



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rex/encoder/alpha2/generic.rb', line 94

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



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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rex/encoder/alpha2/generic.rb', line 38

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


	# Remove bad chars from the accepted_chars list.  Sadly 'A' must be
	# an accepted char or we'll certainly fail at this point.  This could
	# be fixed later maybe with some recalculation of the encoder stubs...
	# - Puss
	(badchars || '').unpack('C*').map { |c| accepted_chars.delete([c].pack('C')) }

	first    = 0
	second   = 1
	randbase = 0
	found    = nil


	gen_base_set(block).each do |randbase_|
		second   = gen_second(block, randbase_)
		next if second < 0
		if accepted_chars.include?([second].pack('C'))
			found    = second
			randbase = randbase_
			break
		end
	end

	if not found
		msg = "No valid base found for #{"0x%.2x" % block}"
		if not accepted_chars.include?([second].pack('C'))
			msg << ": BadChar to #{second}"
		elsif second < 1
			msg << ": Negative"
		end
		raise RuntimeError, msg
	end

	if (randbase > 0xa0)
		# first num must be 4
		first = (randbase/0x10) + 0x40
	elsif (randbase == 0x00) || (randbase == 0x10)
		# first num must be 5
		first = (randbase/0x10) + 0x50
	else
		# pick one at "random"
		first = (randbase/0x10)
		if (first % 2) > 0
			first += 0x40
		else
			first += 0x50
		end
	end

	# now add our new bytes :)
	[first.to_i, second].pack('CC')
end

.gen_base_set(ignored_max = 0x0f) ⇒ Object



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

def Generic.gen_base_set(ignored_max=0x0f)
	# 0xf is max for XOR encodings - non-unicode
	max = 0x0f
	Rex::Text.shuffle_a(
		[* ( (0..(max)).map { |i| i *= 0x10 } ) ]
	)
end

.gen_decoder(reg, offset) ⇒ Object



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

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

.gen_decoder_prefix(reg, offset) ⇒ Object



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

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



33
34
35
36
# File 'lib/rex/encoder/alpha2/generic.rb', line 33

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