java_deserializer_spec.rb 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. require 'rex/java'
  2. require 'stringio'
  3. load Metasploit::Framework.root.join('tools/exploit/java_deserializer.rb').to_path
  4. RSpec.describe JavaDeserializer do
  5. before(:context) do
  6. @out = $stdout
  7. @err = $stderr
  8. $stdout = StringIO.new
  9. $stderr = StringIO.new
  10. end
  11. after(:context) do
  12. $stdout = @out
  13. $stderr = @err
  14. end
  15. subject(:deserializer) do
  16. described_class.new
  17. end
  18. let(:valid_stream) do
  19. "\xac\xed\x00\x05\x75\x72\x00\x02" +
  20. "\x5b\x43\xb0\x26\x66\xb0\xe2\x5d" +
  21. "\x84\xac\x02\x00\x00\x78\x70\x00" +
  22. "\x00\x00\x02\x00\x61\x00\x62"
  23. end
  24. describe ".new" do
  25. it "returns a JavaDeserializer instance" do
  26. expect(deserializer).to be_a(JavaDeserializer)
  27. end
  28. it "initializes file to nil" do
  29. expect(deserializer.file).to be_nil
  30. end
  31. end
  32. describe "#run" do
  33. context "when file is nil" do
  34. it "returns nil" do
  35. expect(deserializer.run).to be_nil
  36. end
  37. end
  38. context "when file contains a valid stream" do
  39. before(:example) do
  40. $stdout.string = ''
  41. end
  42. context "when no options" do
  43. it "prints the stream contents" do
  44. expect(File).to receive(:new) do
  45. contents = valid_stream
  46. StringIO.new(contents)
  47. end
  48. deserializer.file = 'sample'
  49. deserializer.run
  50. expect($stdout.string).to include('[7e0001] NewArray { char, ["97", "98"] }')
  51. end
  52. end
  53. context "when :array in options" do
  54. it "prints the array contents" do
  55. expect(File).to receive(:new) do
  56. contents = valid_stream
  57. StringIO.new(contents)
  58. end
  59. deserializer.file = 'sample'
  60. deserializer.run({:array => '0'})
  61. expect($stdout.string).to include('Array Type: char')
  62. end
  63. end
  64. end
  65. context "when file contains an invalid stream" do
  66. it "prints the error while deserializing" do
  67. expect(File).to receive(:new) do
  68. contents = 'invalid_stream'
  69. StringIO.new(contents)
  70. end
  71. deserializer.file = 'sample'
  72. deserializer.run
  73. expect($stdout.string).to include('[-] Failed to unserialize Stream')
  74. end
  75. end
  76. end
  77. end