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.


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

def initialize
  @block_list = nil
  reset
end

Instance Attribute Details

#badcharsObject

Characters to avoid when selecting permutations, if any.


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

def badchars
  @badchars
end

#block_listObject

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


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

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.


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

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.


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

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.


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

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:


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

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:


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

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.


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

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.


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

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.


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

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