Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/spec/lib/rex/exploit/view_state_spec.rb
Views: 11783
require 'spec_helper'1require 'rex/version'23require 'rex/text'45# rubocop:disable Lint/DeprecatedGemVersion6RSpec.describe Rex::Exploit::ViewState do7let(:data) { Random.new.bytes(rand(10..100)) }8let(:key) { Random.new.bytes(20) }910context 'when the algorithm is SHA-1' do11let(:algo) { 'sha1' }1213describe '.decode_viewstate' do14let(:encoded) { described_class.generate_viewstate(data, algo: algo, key: key) }1516it 'returns the data and HMAC' do17decoded = described_class.decode_viewstate(encoded, algo: algo)18expect(decoded).to be_a Hash19expect(decoded[:data]).to eq data20expect(decoded[:hmac]).to eq described_class.generate_viewstate_hmac(data, algo: algo, key: key)21end22end2324describe '.generate_viewstate' do25it 'generates the HMAC signature' do26expect(described_class).to receive(:generate_viewstate_hmac).with(data, algo: algo, key: key).and_call_original27described_class.generate_viewstate(data, algo: algo, key: key)28end2930it 'generates a Base64 encoded blob' do31viewstate = described_class.generate_viewstate(data, algo: algo, key: key)32debase64ed = Rex::Text.decode_base64(viewstate)33expect(debase64ed).to eq data + described_class.generate_viewstate_hmac(data, algo: algo, key: key)34end35end3637describe '.generate_viewstate_hmac' do38it 'delegates to OpenSSL::HMAC' do39expect(OpenSSL::HMAC).to receive(:digest).with(algo, key,data)40described_class.generate_viewstate_hmac(data, algo: algo, key: key)41end4243it 'generates a 20 byte HMAC' do44hmac = described_class.generate_viewstate_hmac(data, algo: algo, key: key)45expect(hmac.bytesize).to eq 2046end47end4849describe '.is_viewstate_valid?' do50let(:encoded) { described_class.generate_viewstate(data, algo: algo, key: key) }5152it 'raises an Error when it can not be decoded' do53# use key.length / 2 to guarantee there is not enough data for the key to be found54expect { described_class.is_viewstate_valid?(Rex::Text.encode_base64('A' * (key.length / 2))) }.to raise_error(described_class::Error)55end5657it 'returns true for the correct key' do58expect(described_class.is_viewstate_valid?(encoded, algo: algo, key: key)).to be_truthy59end6061it 'returns false for the incorrect key' do62expect(described_class.is_viewstate_valid?(encoded, algo: algo, key: key + '#')).to be_falsey63end64end65end66end676869