Class: Rex::Proto::Http::WebSocket::Frame

Inherits:
BinData::Record
  • Object
show all
Defined in:
lib/rex/proto/http/web_socket.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_masking_key(data, mask) ⇒ Object


377
378
379
380
381
382
383
384
385
# File 'lib/rex/proto/http/web_socket.rb', line 377

def self.apply_masking_key(data, mask)
  mask = [mask].pack('N').each_byte.to_a
  xored = ''
  data.each_byte.each_with_index do |byte, index|
    xored << (byte ^ mask[index % 4]).chr
  end

  xored
end

.from_binary(value, last: true, mask: true) ⇒ Object


387
388
389
# File 'lib/rex/proto/http/web_socket.rb', line 387

def self.from_binary(value, last: true, mask: true)
  from_opcode(Opcode::BINARY, value, last: last, mask: mask)
end

.from_text(value, last: true, mask: true) ⇒ Object


391
392
393
# File 'lib/rex/proto/http/web_socket.rb', line 391

def self.from_text(value, last: true, mask: true)
  from_opcode(Opcode::TEXT, value, last: last, mask: mask)
end

Instance Method Details

#mask!(key = nil) ⇒ String

Update the frame instance in place to apply a masking key to the payload data as defined in RFC 6455 section 5.3.

Parameters:

  • key (nil, Integer) (defaults to: nil)

    either an explicit 32-bit masking key or nil to generate a random one

Returns:

  • (String)

    the masked payload data is returned


400
401
402
403
404
405
406
# File 'lib/rex/proto/http/web_socket.rb', line 400

def mask!(key = nil)
  header.masked.assign(1)
  key = rand(1..0xffffffff) if key.nil?
  header.masking_key.assign(key)
  payload_data.assign(self.class.apply_masking_key(payload_data, header.masking_key))
  payload_data.value
end

#payload_lenObject


418
419
420
421
422
423
424
425
426
427
# File 'lib/rex/proto/http/web_socket.rb', line 418

def payload_len
  case header.payload_len_sm
  when 127
    header.payload_len_lg
  when 126
    header.payload_len_md
  else
    header.payload_len_sm
  end
end

#payload_len=(value) ⇒ Object


429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/rex/proto/http/web_socket.rb', line 429

def payload_len=(value)
  if value < 126
    header.payload_len_sm.assign(value)
  elsif value < 0xffff
    header.payload_len_sm.assign(126)
    header.payload_len_md.assign(value)
  elsif value < 0x7fffffffffffffff
    header.payload_len_sm.assign(127)
    header.payload_len_lg.assign(value)
  else
    raise ArgumentError, 'payload length is outside the acceptable range'
  end
end

#unmask!String

Update the frame instance in place to apply a masking key to the payload data as defined in RFC 6455 section 5.3.

Returns:

  • (String)

    the unmasked payload data is returned


412
413
414
415
416
# File 'lib/rex/proto/http/web_socket.rb', line 412

def unmask!
  payload_data.assign(self.class.apply_masking_key(payload_data, header.masking_key))
  header.masked.assign(0)
  payload_data.value
end