diff --git a/lib/rex/java/serialization.rb b/lib/rex/java/serialization.rb index 9638ce4f45..fb049b85aa 100644 --- a/lib/rex/java/serialization.rb +++ b/lib/rex/java/serialization.rb @@ -27,6 +27,24 @@ module Rex SC_SERIALIZABLE = 0x02 SC_EXTERNALIZABLE = 0x04 SC_ENUM = 0x10 + + PRIMITIVE_TYPE_CODES = { + 'B' => 'byte', + 'C' => 'char', + 'D' => 'double', + 'F' => 'float', + 'I' => 'int', + 'J' => 'long', + 'S' => 'short', + 'Z' => 'boolean' + } + + OBJECT_TYPE_CODES = { + '[' => 'array', + 'L' => 'object' + } + + TYPE_CODES = PRIMITIVE_TYPE_CODES.merge(OBJECT_TYPE_CODES) end end end diff --git a/lib/rex/java/serialization/model/annotation.rb b/lib/rex/java/serialization/model/annotation.rb index 0d1329d4f2..29e526a704 100644 --- a/lib/rex/java/serialization/model/annotation.rb +++ b/lib/rex/java/serialization/model/annotation.rb @@ -6,6 +6,8 @@ module Rex # annotations (classAnnotation) and object annotations (objectAnnotation). class Annotation < Element + include Rex::Java::Serialization + # @!attribute contents # @return [Array] The annotation contents attr_accessor :contents @@ -28,13 +30,13 @@ module Rex opcode = opcode.unpack('C')[0] case opcode - when Rex::Java::Serialization::TC_BLOCKDATA + when TC_BLOCKDATA block = BlockData.decode(io) self.contents << block - when Rex::Java::Serialization::TC_BLOCKDATALONG + when TC_BLOCKDATALONG block = BlockDataLong.decode(io) self.contents << block - when Rex::Java::Serialization::TC_ENDBLOCKDATA + when TC_ENDBLOCKDATA return self else #TODO: unsupported @@ -55,9 +57,9 @@ module Rex contents.each do |content| case content when Rex::Java::Serialization::Model::BlockData - encoded << [Rex::Java::Serialization::TC_BLOCKDATA].pack('C') + encoded << [TC_BLOCKDATA].pack('C') when Rex::Java::Serialization::Model::BlockDataLong - encoded << [Rex::Java::Serialization::TC_BLOCKDATALONG].pack('C') + encoded << [TC_BLOCKDATALONG].pack('C') else raise ::RuntimeError, 'Unsupported content' end @@ -65,7 +67,7 @@ module Rex encoded << encoded_content end - encoded << [Rex::Java::Serialization::TC_ENDBLOCKDATA].pack('C') + encoded << [TC_ENDBLOCKDATA].pack('C') encoded end diff --git a/lib/rex/java/serialization/model/class_description.rb b/lib/rex/java/serialization/model/class_description.rb index e4c63f844a..7b13b417cb 100644 --- a/lib/rex/java/serialization/model/class_description.rb +++ b/lib/rex/java/serialization/model/class_description.rb @@ -5,6 +5,8 @@ module Rex # This class provides a class description representation class ClassDescription < Element + include Rex::Java::Serialization + # @!attribute class_name # @return [Java::Serialization::Model::Utf] The name of the class attr_accessor :class_name @@ -73,10 +75,10 @@ module Rex case super_class when Rex::Java::Serialization::Model::ClassDescription - encoded << [Rex::Java::Serialization::TC_CLASSDESC].pack('C') + encoded << [TC_CLASSDESC].pack('C') encoded << super_class.encode when nil - encoded << [Rex::Java::Serialization::TC_NULL].pack('C') + encoded << [TC_NULL].pack('C') else #TODO: support other superclass types raise RuntimeError, 'Failed to serialize ClassDescription' @@ -142,9 +144,9 @@ module Rex class_desc = nil case super_opcode - when Rex::Java::Serialization::TC_NULL + when TC_NULL class_desc = nil - when Rex::Java::Serialization::TC_CLASSDESC + when TC_CLASSDESC class_desc = ClassDescription.decode(io) else #TODO: Support TC_PROXYCLASSDESC diff --git a/lib/rex/java/serialization/model/field.rb b/lib/rex/java/serialization/model/field.rb index 817b1d5848..2ca0e5afa0 100644 --- a/lib/rex/java/serialization/model/field.rb +++ b/lib/rex/java/serialization/model/field.rb @@ -6,23 +6,7 @@ module Rex # both primitive descriptions (primitiveDesc) and object descriptions (objectDesc). class Field < Element - PRIMITIVE_TYPE_CODES = { - 'B' => 'byte', - 'C' => 'char', - 'D' => 'double', - 'F' => 'float', - 'I' => 'integer', - 'J' => 'long', - 'S' => 'short', - 'Z' => 'boolean', - } - - OBJECT_TYPE_CODES = { - '[' => 'array', - 'L' => 'object' - } - - TYPE_CODES = PRIMITIVE_TYPE_CODES.merge(OBJECT_TYPE_CODES) + include Rex::Java::Serialization # @!attribute type # @return [String] The type of the field. @@ -133,7 +117,7 @@ module Rex # # @return [String] def encode_field_type - encoded = [Java::Serialization::TC_STRING].pack('C') + encoded = [TC_STRING].pack('C') encoded << field_type.encode encoded @@ -146,7 +130,7 @@ module Rex # @raise [RuntimeError] if unserialization isn't possible def decode_field_type(io) opcode = io.read(1) - unless opcode && opcode == [Java::Serialization::TC_STRING].pack('C') + unless opcode && opcode == [TC_STRING].pack('C') raise ::RuntimeError, 'Failed to unserialize Field' end type = Utf.decode(io) diff --git a/spec/lib/rex/java/serialization/model/field_spec.rb b/spec/lib/rex/java/serialization/model/field_spec.rb index c4dd3fb568..a20fb6dded 100644 --- a/spec/lib/rex/java/serialization/model/field_spec.rb +++ b/spec/lib/rex/java/serialization/model/field_spec.rb @@ -36,7 +36,7 @@ describe Rex::Java::Serialization::Model::Field do context "when primitive field" do it do - field.type = 'integer' + field.type = 'int' field.name = Rex::Java::Serialization::Model::Utf.new('number') expect(field.encode).to eq(sample_primitive) end @@ -60,7 +60,7 @@ describe Rex::Java::Serialization::Model::Field do it "decodes field type" do field.decode(sample_primitive_io) - expect(field.type).to eq('integer') + expect(field.type).to eq('int') end it "decodes field name as Utf" do