-
Notifications
You must be signed in to change notification settings - Fork 14.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Land #18899, update ysoserial viewstate tool
- Loading branch information
Showing
5 changed files
with
260 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# -*- coding: binary -*- | ||
|
||
module Rex | ||
module Exploit | ||
class ViewState | ||
class Error < Rex::RuntimeError | ||
end | ||
|
||
def self.decode_viewstate(encoded_viewstate, algo: 'sha1') | ||
viewstate = Rex::Text.decode_base64(encoded_viewstate) | ||
|
||
unless Rex::Text.encode_base64(viewstate) == encoded_viewstate | ||
raise Error.new('Could not decode ViewState') | ||
end | ||
|
||
hmac_len = OpenSSL::Digest.new(algo).digest_length | ||
|
||
if (data = viewstate[0...-hmac_len]).empty? | ||
data = nil | ||
end | ||
|
||
hmac = viewstate[-hmac_len..-1] | ||
unless hmac&.length == hmac_len | ||
raise Error.new('Could not decode ViewState') | ||
end | ||
|
||
{ data: data, hmac: hmac } | ||
end | ||
|
||
def self.generate_viewstate(data, extra: '', algo: 'sha1', key: '') | ||
# Generate ViewState HMAC from known values and validation key | ||
hmac = generate_viewstate_hmac(data + extra, algo: algo, key: key) | ||
|
||
# Append HMAC to provided data and Base64-encode the whole shebang | ||
Rex::Text.encode_base64(data + hmac) | ||
end | ||
|
||
def self.generate_viewstate_hmac(data, algo: 'sha1', key: '') | ||
OpenSSL::HMAC.digest(algo, key, data) | ||
end | ||
|
||
def self.is_viewstate_valid?(encoded_viewstate, extra: '', algo: 'sha1', key: '') | ||
viewstate = decode_viewstate(encoded_viewstate) | ||
|
||
unless viewstate[:data] | ||
raise Error.new('Could not retrieve ViewState data') | ||
end | ||
|
||
unless (their_hmac = viewstate[:hmac]) | ||
raise Error.new('Could not retrieve ViewState HMAC') | ||
end | ||
|
||
our_hmac = generate_viewstate_hmac( | ||
viewstate[:data] + extra, | ||
algo: algo, | ||
key: key | ||
) | ||
|
||
# Do we have what it takes? | ||
our_hmac == their_hmac | ||
end | ||
|
||
class << self | ||
alias_method :can_sign_viewstate?, :is_viewstate_valid? | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'spec_helper' | ||
require 'rex/version' | ||
|
||
require 'rex/text' | ||
|
||
# rubocop:disable Lint/DeprecatedGemVersion | ||
RSpec.describe Rex::Exploit::ViewState do | ||
let(:data) { Random.new.bytes(rand(10..100)) } | ||
let(:key) { Random.new.bytes(20) } | ||
|
||
context 'when the algorithm is SHA-1' do | ||
let(:algo) { 'sha1' } | ||
|
||
describe '.decode_viewstate' do | ||
let(:encoded) { described_class.generate_viewstate(data, algo: algo, key: key) } | ||
|
||
it 'returns the data and HMAC' do | ||
decoded = described_class.decode_viewstate(encoded, algo: algo) | ||
expect(decoded).to be_a Hash | ||
expect(decoded[:data]).to eq data | ||
expect(decoded[:hmac]).to eq described_class.generate_viewstate_hmac(data, algo: algo, key: key) | ||
end | ||
end | ||
|
||
describe '.generate_viewstate' do | ||
it 'generates the HMAC signature' do | ||
expect(described_class).to receive(:generate_viewstate_hmac).with(data, algo: algo, key: key).and_call_original | ||
described_class.generate_viewstate(data, algo: algo, key: key) | ||
end | ||
|
||
it 'generates a Base64 encoded blob' do | ||
viewstate = described_class.generate_viewstate(data, algo: algo, key: key) | ||
debase64ed = Rex::Text.decode_base64(viewstate) | ||
expect(debase64ed).to eq data + described_class.generate_viewstate_hmac(data, algo: algo, key: key) | ||
end | ||
end | ||
|
||
describe '.generate_viewstate_hmac' do | ||
it 'delegates to OpenSSL::HMAC' do | ||
expect(OpenSSL::HMAC).to receive(:digest).with(algo, key,data) | ||
described_class.generate_viewstate_hmac(data, algo: algo, key: key) | ||
end | ||
|
||
it 'generates a 20 byte HMAC' do | ||
hmac = described_class.generate_viewstate_hmac(data, algo: algo, key: key) | ||
expect(hmac.bytesize).to eq 20 | ||
end | ||
end | ||
|
||
describe '.is_viewstate_valid?' do | ||
let(:encoded) { described_class.generate_viewstate(data, algo: algo, key: key) } | ||
|
||
it 'raises an Error when it can not be decoded' do | ||
# use key.length / 2 to guarantee there is not enough data for the key to be found | ||
expect { described_class.is_viewstate_valid?(Rex::Text.encode_base64('A' * (key.length / 2))) }.to raise_error(described_class::Error) | ||
end | ||
|
||
it 'returns true for the correct key' do | ||
expect(described_class.is_viewstate_valid?(encoded, algo: algo, key: key)).to be_truthy | ||
end | ||
|
||
it 'returns false for the incorrect key' do | ||
expect(described_class.is_viewstate_valid?(encoded, algo: algo, key: key + '#')).to be_falsey | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.