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



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

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



389
390
391
# File 'lib/rex/proto/http/web_socket.rb', line 389

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



393
394
395
# File 'lib/rex/proto/http/web_socket.rb', line 393

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



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

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



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

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



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

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



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

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