Class: Msf::Auxiliary::Redis::RESPParser

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/core/auxiliary/redis.rb

Constant Summary collapse

LINE_BREAK =
"\r\n"

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ RESPParser

Returns a new instance of RESPParser.



116
117
118
119
# File 'lib/msf/core/auxiliary/redis.rb', line 116

def initialize(data)
  @raw_data = data
  @counter = 0
end

Instance Method Details

#data_at_counterObject



126
127
128
# File 'lib/msf/core/auxiliary/redis.rb', line 126

def data_at_counter
  @raw_data[@counter..-1]
end

#parseObject



121
122
123
124
# File 'lib/msf/core/auxiliary/redis.rb', line 121

def parse
  @counter = 0
  parse_next
end

#parse_bulk_stringObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/msf/core/auxiliary/redis.rb', line 161

def parse_bulk_string
  unless /\A\$(?<str_len>[-\d]+)(\r|$)/ =~ data_at_counter
    raise "RESP parsing error in bulk string"
  end

  @counter += (1 + str_len.length)
  str_len = str_len.to_i

  if data_at_counter.start_with?(LINE_BREAK)
    @counter += LINE_BREAK.length
  end

  result = nil
  if str_len != -1
    result = data_at_counter[0..str_len - 1]
    @counter += str_len
    @counter += 2 # Skip over next CLRF
  end
  result
end

#parse_nextObject



183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/msf/core/auxiliary/redis.rb', line 183

def parse_next
  case data_at_counter[0]
  when "*"
    parse_resp_array
  when "+"
    parse_simple_string
  when "$"
    parse_bulk_string
  else
    raise "RESP parsing error: " + data_at_counter
  end
end

#parse_resp_arrayObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/msf/core/auxiliary/redis.rb', line 130

def parse_resp_array
  # Read array length
  unless /\A\*(?<arr_len>\d+)(\r|$)/ =~ data_at_counter
    raise "RESP parsing error in array"
  end

  @counter += (1 + arr_len.length)

  if data_at_counter.start_with?(LINE_BREAK)
    @counter += LINE_BREAK.length
  end
    
  arr_len = arr_len.to_i
    
  result = []
  for index in 1..arr_len do
    element = parse_next
    result.append(element)
  end
  result
end

#parse_simple_stringObject



152
153
154
155
156
157
158
159
# File 'lib/msf/core/auxiliary/redis.rb', line 152

def parse_simple_string
  str_end = data_at_counter.index(LINE_BREAK)
  str_end = str_end.to_i
  result = data_at_counter[1..str_end - 1]
  @counter += str_end
  @counter += 2 # Skip over next CLRF
  result
end