2014-12-04 22:33:33 +00:00
|
|
|
require 'rex/java'
|
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
load Metasploit::Framework.root.join('tools/java_deserializer.rb').to_path
|
|
|
|
|
|
|
|
describe JavaDeserializer do
|
|
|
|
|
|
|
|
before(:all) do
|
|
|
|
@out = $stdout
|
|
|
|
@err = $stderr
|
|
|
|
|
|
|
|
$stdout = StringIO.new
|
|
|
|
$stderr = StringIO.new
|
|
|
|
end
|
|
|
|
|
|
|
|
after(:all) do
|
|
|
|
$stdout = @out
|
|
|
|
$stderr = @err
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:deserializer) do
|
|
|
|
described_class.new
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:valid_stream) do
|
|
|
|
"\xac\xed\x00\x05\x75\x72\x00\x02" +
|
|
|
|
"\x5b\x43\xb0\x26\x66\xb0\xe2\x5d" +
|
|
|
|
"\x84\xac\x02\x00\x00\x78\x70\x00" +
|
|
|
|
"\x00\x00\x02\x00\x61\x00\x62"
|
|
|
|
end
|
|
|
|
|
|
|
|
describe ".new" do
|
|
|
|
it "returns a JavaDeserializer instance" do
|
|
|
|
expect(deserializer).to be_a(JavaDeserializer)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "initializes file to nil" do
|
|
|
|
expect(deserializer.file).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#run" do
|
|
|
|
context "when file is nil" do
|
|
|
|
it "returns nil" do
|
|
|
|
expect(deserializer.run).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when file contains a valid stream" do
|
2014-12-08 05:45:31 +00:00
|
|
|
it "prints the stream contents" do
|
2014-12-04 22:33:33 +00:00
|
|
|
expect(File).to receive(:new) do
|
|
|
|
contents = valid_stream
|
|
|
|
StringIO.new(contents)
|
|
|
|
end
|
|
|
|
deserializer.file = 'sample'
|
2014-12-08 05:45:31 +00:00
|
|
|
deserializer.run
|
|
|
|
expect($stdout.string).to include('[7e0001] NewArray { char, ["97", "98"] }')
|
2014-12-04 22:33:33 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when file contains an invalid stream" do
|
2014-12-08 05:45:31 +00:00
|
|
|
it "prints the error while deserializing" do
|
2014-12-04 22:33:33 +00:00
|
|
|
expect(File).to receive(:new) do
|
|
|
|
contents = 'invalid_stream'
|
|
|
|
StringIO.new(contents)
|
|
|
|
end
|
|
|
|
deserializer.file = 'sample'
|
2014-12-08 05:45:31 +00:00
|
|
|
deserializer.run
|
|
|
|
expect($stdout.string).to include('[-] Failed to unserialize Stream')
|
2014-12-04 22:33:33 +00:00
|
|
|
end
|
|
|
|
end
|
2014-12-08 05:45:31 +00:00
|
|
|
|
2014-12-04 22:33:33 +00:00
|
|
|
end
|
|
|
|
end
|