Method: Rex::Exploitation::Powershell::Parser#block_extract

Defined in:
lib/rex/exploitation/powershell/parser.rb

#block_extract(idx) ⇒ String

Extract block of code inside brackets/parenthesis

Attempts to match the bracket at idx, handling nesting manually Once the balanced matching bracket is found, all script content between idx and the index of the matching bracket is returned

Parameters:

  • idx (Integer)

    index of opening bracket

Returns:

  • (String)

    content between matching brackets



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rex/exploitation/powershell/parser.rb', line 135

def block_extract(idx)
  fail ArgumentError unless idx

  if idx < 0 || idx >= code.length
    fail ArgumentError, 'Invalid index'
  end

  start = code[idx]
  stop = match_start(start)
  delims = scan_with_index(/#{Regexp.escape(start)}|#{Regexp.escape(stop)}/, code[idx + 1..-1])
  delims.map { |x| x[1] = x[1] + idx + 1 }
  c = 1
  sidx = nil
  # Go through delims till we balance, get idx
  while (c != 0) && (x = delims.shift)
    sidx = x[1]
    x[0] == stop ? c -= 1 : c += 1
  end

  code[idx..sidx]
end