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


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

def parse_bulk_string
  unless /\A\$(?<str_len>\d+)\r/ =~ data_at_counter
    raise "RESP parsing error in bulk string"
  end
  str_len = str_len.to_i
  @counter += data_at_counter.index(LINE_BREAK) + 2
  result = data_at_counter[0..str_len - 1]
  @counter += str_len
  @counter += 2 # Skip over next CLRF
  result
end

#parse_nextObject


163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/msf/core/auxiliary/redis.rb', line 163

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"
  end
end

#parse_resp_arrayObject


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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 += data_at_counter.index(LINE_BREAK) + 2
    
  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


141
142
143
144
145
146
147
148
# File 'lib/msf/core/auxiliary/redis.rb', line 141

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