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.



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

def initialize
	@block_list = nil
	reset
end

Instance Attribute Details

#badcharsObject

Characters to avoid when selecting permutations, if any.



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

def badchars
  @badchars
end

#block_listObject

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



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

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.



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

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.



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

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.



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

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:



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

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:



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

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)


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

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.



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

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.



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

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