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.



110
111
112
113
# File 'lib/msf/core/auxiliary/redis.rb', line 110

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

Instance Method Details

#data_at_counterObject



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

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

#parseObject



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

def parse
  @counter = 0
  parse_next
end

#parse_bulk_stringObject



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/msf/core/auxiliary/redis.rb', line 155

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



177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/msf/core/auxiliary/redis.rb', line 177

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/msf/core/auxiliary/redis.rb', line 124

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



146
147
148
149
150
151
152
153
# File 'lib/msf/core/auxiliary/redis.rb', line 146

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