Class: Rex::Poly::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/poly.rb

Overview

This class encapsulates the state of a single polymorphic block set generation. It tracks the current set of consumed registers, the linear list of blocks generated, the end-result buffer, and the phase of generation. The fields exposed by the State class are intended for use only by the polymorphic generation subsystem and should not be modified directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Initializes the polymorphic generation state.



22
23
24
25
# File 'lib/rex/poly.rb', line 22

def initialize
	@block_list = nil
	reset
end

Instance Attribute Details

#badcharsObject

Characters to avoid when selecting permutations, if any.



128
129
130
# File 'lib/rex/poly.rb', line 128

def badchars
  @badchars
end

#block_listObject

The linear list of blocks that is generated by calling the generate method on a LogicalBlock.



108
109
110
# File 'lib/rex/poly.rb', line 108

def block_list
  @block_list
end

#bufferObject

The buffer state for the current polymorphic generation. This stores the end-result of a call to generate on a LogicalBlock.



102
103
104
# File 'lib/rex/poly.rb', line 102

def buffer
  @buffer
end

#curr_offsetObject

The current offset into the polymorphic buffer that is being generated. This is updated as blocks are appended to the block_list.



114
115
116
# File 'lib/rex/poly.rb', line 114

def curr_offset
  @curr_offset
end

#first_phaseObject

A boolean field that is used by the LogicalBlock class to track whether or not it is in the first phase (generating the block list), or in the second phase (generating the polymorphic buffer). This phases are used to indicate whether or not the offset_of and regnum_of methods will return actual results.



123
124
125
# File 'lib/rex/poly.rb', line 123

def first_phase
  @first_phase
end

Instance Method Details

#consume_regnum(regnum) ⇒ Object

Consumes a register number, thus removing it from the pool that can be assigned. The consumed register number is returned to the caller.

Raises:



57
58
59
60
61
62
63
# File 'lib/rex/poly.rb', line 57

def consume_regnum(regnum)
	raise RuntimeError, "Register #{regnum} is already consumed." if (consumed_regnum?(regnum))

	@regnums[regnum] = true

	regnum
end

#consume_regnum_from_set(regnum_set) ⇒ Object

Acquires a register number that has not already been consumed from the supplied register number set and consumes it, returning the selected register number to the caller. The register number is selected from the set at random.

Raises:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rex/poly.rb', line 71

def consume_regnum_from_set(regnum_set)
	# Pick a random starting point within the supplied set.
	idx = rand(regnum_set.length)

	# Try each index in the set.
	regnum_set.length.times { |x|
		regnum = regnum_set[(idx + x) % regnum_set.length]

		next if (consumed_regnum?(regnum))

		return consume_regnum(regnum)
	}

	# If we get through the entire iteration without finding a register,
	# then we are out of registers to assign.
	raise RuntimeError, "No registers are available to consume from the set"
end

#consumed_regnum?(regnum) ⇒ Boolean

Returns true if the supplied register number is already consumed.

Returns:

  • (Boolean)


49
50
51
# File 'lib/rex/poly.rb', line 49

def consumed_regnum?(regnum)
	@regnums[regnum]
end

#defecate_regnum(regnum) ⇒ Object

Eliminates a register number from the consumed pool so that it can be used in the future. This happens after a block indicates that a register has been clobbered.



94
95
96
# File 'lib/rex/poly.rb', line 94

def defecate_regnum(regnum)
	@regnums.delete(regnum)
end

#resetObject

Resets the generation state to have a plain start by clearing all consumed registers, resetting the polymorphic buffer back to its beginning and destroying any block generation state.



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rex/poly.rb', line 32

def reset
	# Reset the generation flag on any blocks in the block list
	@block_list.each { |block|
		block[0].generated = false
	} if (@block_list)

	@regnums     = Hash.new
	@buffer      = ''
	@block_list  = []
	@curr_offset = 0
	@first_phase = true
	@badchars    = nil
end