Files
metasploit-gs/lib/rex/java/serialization/model/contents.rb
T

163 lines
6.0 KiB
Ruby
Raw Normal View History

2014-12-10 09:52:23 -06:00
# -*- coding: binary -*-
2014-12-04 18:28:25 -06:00
module Rex
module Java
module Serialization
module Model
module Contents
include Rex::Java::Serialization
# Deserializes a content
#
# @param io [IO] the io to read from
# @return [Rex::Java::Serialization::Model::Element] if deserialization succeeds
2015-04-05 18:43:03 -05:00
# @raise [Rex::Java::Serialization::DecodeError] if deserialization doesn't succeed or unsupported content
2014-12-05 17:14:37 -06:00
def decode_content(io, stream)
2014-12-04 18:28:25 -06:00
opcode = io.read(1)
2015-04-05 18:43:03 -05:00
raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize content' if opcode.nil?
2014-12-04 18:28:25 -06:00
opcode = opcode.unpack('C')[0]
content = nil
case opcode
when TC_BLOCKDATA
2014-12-05 17:14:37 -06:00
content = BlockData.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_BLOCKDATALONG
2014-12-05 17:14:37 -06:00
content = BlockDataLong.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_ENDBLOCKDATA
2014-12-05 17:14:37 -06:00
content = EndBlockData.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_OBJECT
2014-12-05 17:14:37 -06:00
content = NewObject.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_CLASS
2015-09-30 12:30:52 -05:00
content = NewClass.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_ARRAY
2014-12-05 17:14:37 -06:00
content = NewArray.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_STRING
2014-12-05 17:14:37 -06:00
content = Utf.decode(io, stream)
2014-12-05 19:12:05 -06:00
stream.add_reference(content) unless stream.nil?
2014-12-04 18:28:25 -06:00
when TC_LONGSTRING
2014-12-05 17:14:37 -06:00
content = LongUtf.decode(io, stream)
2014-12-05 19:12:05 -06:00
stream.add_reference(content) unless stream.nil?
2014-12-04 18:28:25 -06:00
when TC_ENUM
2014-12-05 17:14:37 -06:00
content = NewEnum.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_CLASSDESC
2014-12-05 17:14:37 -06:00
content = NewClassDesc.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_PROXYCLASSDESC
content = ProxyClassDesc.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_REFERENCE
2014-12-05 17:14:37 -06:00
content = Reference.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_NULL
2014-12-05 17:14:37 -06:00
content = NullReference.decode(io, stream)
2014-12-04 18:28:25 -06:00
when TC_EXCEPTION
2015-04-05 18:43:03 -05:00
raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize unsupported TC_EXCEPTION content'
2014-12-04 18:28:25 -06:00
when TC_RESET
2014-12-05 17:14:37 -06:00
content = Reset.decode(io, stream)
2014-12-04 18:28:25 -06:00
else
2015-04-05 18:43:03 -05:00
raise Rex::Java::Serialization::DecodeError, 'Failed to unserialize content'
2014-12-04 18:28:25 -06:00
end
content
end
# Serializes a content
#
# @param content [Rex::Java::Serialization::Model::Element] the content to serialize
# @return [String] if serialization succeeds
2015-04-05 18:43:03 -05:00
# @raise [Rex::Java::Serialization::EncodeError] if serialization doesn't succeed
2014-12-04 18:28:25 -06:00
def encode_content(content)
encoded = ''
case content
2014-12-05 20:16:50 -06:00
when BlockData
2014-12-04 18:28:25 -06:00
encoded << [TC_BLOCKDATA].pack('C')
2014-12-05 20:16:50 -06:00
when BlockDataLong
2014-12-04 18:28:25 -06:00
encoded << [TC_BLOCKDATALONG].pack('C')
2014-12-05 20:16:50 -06:00
when EndBlockData
2014-12-04 18:28:25 -06:00
encoded << [TC_ENDBLOCKDATA].pack('C')
2014-12-05 20:16:50 -06:00
when NewObject
2014-12-04 18:28:25 -06:00
encoded << [TC_OBJECT].pack('C')
2015-09-30 12:30:52 -05:00
when NewClass
2014-12-04 18:28:25 -06:00
encoded << [TC_CLASS].pack('C')
2014-12-05 20:16:50 -06:00
when NewArray
2014-12-04 18:28:25 -06:00
encoded << [TC_ARRAY].pack('C')
2014-12-05 20:16:50 -06:00
when Utf
2014-12-04 18:28:25 -06:00
encoded << [TC_STRING].pack('C')
2014-12-05 20:16:50 -06:00
when LongUtf
2014-12-04 18:28:25 -06:00
encoded << [TC_LONGSTRING].pack('C')
2014-12-05 20:16:50 -06:00
when NewEnum
2014-12-04 18:28:25 -06:00
encoded << [TC_ENUM].pack('C')
2014-12-05 20:16:50 -06:00
when NewClassDesc
2014-12-04 18:28:25 -06:00
encoded << [TC_CLASSDESC].pack('C')
when ProxyClassDesc
content = [TC_PROXYCLASSDESC].pack('C')
2014-12-05 20:16:50 -06:00
when NullReference
2014-12-04 18:28:25 -06:00
encoded << [TC_NULL].pack('C')
2014-12-05 20:16:50 -06:00
when Reset
2014-12-04 18:28:25 -06:00
encoded << [TC_RESET].pack('C')
2014-12-05 20:16:50 -06:00
when Reference
2014-12-05 19:55:52 -06:00
encoded << [TC_REFERENCE].pack('C')
2014-12-04 18:28:25 -06:00
else
2015-04-05 18:43:03 -05:00
raise Rex::Java::Serialization::EncodeError, 'Failed to serialize content'
2014-12-04 18:28:25 -06:00
end
encoded << content.encode
encoded
end
2014-12-07 17:52:09 -06:00
# Creates a print-friendly string representation
#
# @param content [Rex::Java::Serialization::Model::Element] the content to print
# @return [String]
2015-04-05 18:43:03 -05:00
# @raise [Rex::Java::Serialization::EncodeError] if the content is unknown
2014-12-07 17:52:09 -06:00
def print_content(content)
str = ''
case content
when BlockData
str << "#{print_class(content)} { #{content.to_s} }"
when BlockDataLong
str << "#{print_class(content)} { #{content.to_s} }"
when EndBlockData
str << "#{print_class(content)}"
when NewObject
str << "#{print_class(content)} { #{content.to_s} }"
when ClassDesc
str << "#{print_class(content)} { #{content.to_s} }"
2015-09-30 12:30:52 -05:00
when NewClass
str << "#{print_class(content)} { #{content.to_s} }"
2014-12-07 17:52:09 -06:00
when NewArray
str << "#{print_class(content)} { #{content.to_s} }"
when Utf
str << "#{print_class(content)} { #{content.to_s} }"
when LongUtf
str << "#{print_class(content)} { #{content.to_s} } "
when NewEnum
str << "#{print_class(content)} { #{content.to_s} }"
when NewClassDesc
str << "#{print_class(content)} { #{content.to_s} }"
when ProxyClassDesc
str << "#{print_class(content)} { #{content.to_s} }"
2014-12-07 17:52:09 -06:00
when NullReference
str << "#{print_class(content)}"
when Reset
str << "#{print_class(content)}"
when Reference
str << "#{print_class(content)} { #{content.to_s} }"
else
2015-04-05 18:43:03 -05:00
raise Rex::Java::Serialization::EncodeError, 'Failed to serialize content'
2014-12-07 17:52:09 -06:00
end
str
end
# Creates a print-friendly string representation of the content class
#
# @param content [Rex::Java::Serialization::Model::Element] the content
# @return [String]
def print_class(content)
content.class.name.split('::').last
end
2014-12-04 18:28:25 -06:00
end
end
end
end
end