Add #to_s

bug/bundler_fix
jvazquez-r7 2014-12-07 17:52:39 -06:00
commit d1d20ee614
No known key found for this signature in database
GPG Key ID: 38D99152B9352D83
28 changed files with 419 additions and 4 deletions

View File

@ -49,6 +49,17 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = '[ '
contents_data = contents.collect {|content| "#{print_content(content)}"}
str << contents_data.join(', ')
str << ' ]'
str
end
end
end
end

View File

@ -42,6 +42,16 @@ module Rex
self
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
contents_hex = []
contents.each_byte {|byte| contents_hex << "0x#{byte.to_s(16)}" }
"[ #{contents_hex.join(', ')} ]"
end
# Serializes the Java::Serialization::Model::BlockData
#
# @return [String]

View File

@ -53,6 +53,16 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
contents_hex = []
contents.each_byte {|byte| contents_hex << "0x#{byte.to_s(16)}" }
"[ #{contents_hex.join(', ')} ]"
end
end
end
end

View File

@ -48,6 +48,13 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
print_content(description)
end
end
end
end

View File

@ -98,6 +98,55 @@ module Rex
encoded << content.encode
encoded
end
# Creates a print-friendly string representation
#
# @param content [Rex::Java::Serialization::Model::Element] the content to print
# @return [String]
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} }"
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 NullReference
str << "#{print_class(content)}"
when Reset
str << "#{print_class(content)}"
when Reference
str << "#{print_class(content)} { #{content.to_s} }"
else
raise ::RuntimeError, 'Failed to serialize content'
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
end
end
end

View File

@ -28,6 +28,13 @@ module Rex
def encode
''
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
self.class.name.split('::').last
end
end
end
end

View File

@ -105,6 +105,20 @@ module Rex
false
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = "#{name} "
if is_primitive?
str << "(#{type})"
else
str << "(#{field_type})"
end
str
end
private
# Whether the type opcode is a valid one.

View File

@ -38,6 +38,7 @@ module Rex
encoded
end
end
end
end

View File

@ -8,7 +8,7 @@ module Rex
include Rex::Java::Serialization::Model::Contents
# @!attribute array_description
# @return [Java::Serialization::Model::ClassDescription] The description of the array
# @return [Java::Serialization::Model::ClassDesc] The description of the array
attr_accessor :array_description
# @!attribute type
# @return [String] The type of the array values
@ -66,6 +66,15 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = "#{type}, "
values_data = values.collect {|v| "#{v}"}
str << "#{values_data}"
end
private
# Deserializes the NewArray length

View File

@ -84,6 +84,27 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = "#{class_name}, [ "
fields_str = []
fields.each do |field|
fields_str << field.to_s
end
str << "#{fields_str.join(', ')} ]"
case super_class.description
when NewClassDesc
str << ", @super_class: #{super_class.description.class_name.to_s}"
when Reference
str << ", @super_class: #{super_class.description.to_s}"
end
str
end
private
# Deserializes a class serial version

View File

@ -11,7 +11,7 @@ module Rex
# @return [Java::Serialization::Model::ClassDescription] The description of the enum
attr_accessor :enum_description
# @!attribute constant_name
# @return [Array] The constant value in the Java Enum
# @return [Java::Serialization::Model::Utf] The constant value in the Java Enum
attr_accessor :constant_name
# @param stream [Rex::Java::Serialization::Model::Stream] the stream where it belongs to
@ -50,6 +50,13 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
constant_name.to_s
end
private
# Deserializes the NewEnum constant name

View File

@ -8,7 +8,7 @@ module Rex
include Rex::Java::Serialization::Model::Contents
# @!attribute class_desc
# @return [Java::Serialization::Model::ClassDescription] The description of the object
# @return [Rex::Java::Serialization::Model::ClassDesc] The description of the object
attr_accessor :class_desc
# @!attribute class_data
# @return [Array] The data of the object
@ -63,6 +63,23 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = ''
if class_desc.description.class == NewClassDesc
str << class_desc.description.class_name.to_s
elsif class_desc.description.class == Reference
str << (class_desc.description.handle - BASE_WIRE_HANDLE).to_s(16)
end
str << ' => { '
data = class_data.collect { |data| data.to_s }
str << data.join(', ')
str << ' }'
end
private
# Deserializes the class_data for a class_desc and its super classes

View File

@ -45,6 +45,13 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
"0x#{handle.to_s(16)}"
end
end
end
end

View File

@ -66,6 +66,24 @@ module Rex
self.references.push(ref)
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
str = "@magic: 0x#{magic.to_s(16)}\n"
str << "@version: #{version}\n"
str << "@contents: [\n"
contents.each do |content|
str << " #{print_content(content)}\n"
end
str << "]\n"
str << "@references: [\n"
references.each do |ref|
str << " [#{(references.index(ref) + BASE_WIRE_HANDLE).to_s(16)}] #{print_content(ref)}\n"
end
str << "]\n"
end
private
# Deserializes the magic stream value

View File

@ -53,6 +53,13 @@ module Rex
encoded
end
# Creates a print-friendly string representation
#
# @return [String]
def to_s
contents
end
end
end
end

View File

@ -86,4 +86,17 @@ describe Rex::Java::Serialization::Model::Annotation do
end
end
describe "#to_s" do
it "prints an empty annotation" do
annotation.decode(empty_contents_io)
expect(annotation.to_s).to eq('[ EndBlockData ]')
end
it "prints an annotation with contents" do
annotation.decode(contents_io)
expect(annotation.to_s).to eq('[ BlockData { [ 0x1, 0x2, 0x3, 0x4, 0x5 ] }, BlockDataLong { [ 0x1, 0x2, 0x3, 0x4, 0x5 ] }, EndBlockData ]')
end
end
end

View File

@ -87,5 +87,18 @@ describe Rex::Java::Serialization::Model::BlockDataLong do
expect(block.contents).to eq("\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10")
end
end
describe "#to_s" do
it "prints a block with contents" do
block.decode(sample_block_io)
expect(block.to_s).to eq('[ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10 ]')
end
it "prints an empty string for an empty block" do
block.decode(empty_block_io)
expect(block.to_s).to eq('[ ]')
end
end
end
end

View File

@ -88,4 +88,16 @@ describe Rex::Java::Serialization::Model::BlockData do
end
end
end
describe "#to_s" do
it "prints a block with contents" do
block.decode(sample_block_io)
expect(block.to_s).to eq('[ 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0x10 ]')
end
it "prints an empty string for an empty block" do
block.decode(empty_block_io)
expect(block.to_s).to eq('[ ]')
end
end
end

View File

@ -68,4 +68,11 @@ describe Rex::Java::Serialization::Model::ClassDesc do
expect(class_desc.encode.unpack("C*")).to eq(sample.unpack("C*"))
end
end
describe "#to_s" do
it "prints a sample ClassDesc" do
class_desc.decode(sample_io)
expect(class_desc.to_s).to be_a(String)
end
end
end

View File

@ -90,4 +90,16 @@ describe Rex::Java::Serialization::Model::Field do
end
end
end
describe "#to_s" do
it "prints an stream containing a primitive field" do
field.decode(sample_primitive_io)
expect(field.to_s).to eq('number (int)')
end
it "prints an stream containing an object field" do
field.decode(sample_object_io)
expect(field.to_s).to eq('test_array ([LEmployee;)')
end
end
end

View File

@ -87,6 +87,18 @@ describe Rex::Java::Serialization::Model::LongUtf do
expect(long_utf.contents).to eq('java.lang.Number')
end
end
describe "#to_s" do
it "prints an stream containing a sample long utf" do
long_utf.decode(sample_utf_io)
expect(long_utf.to_s).to eq('java.lang.Number')
end
it "prints an stream containing an empty long utf" do
long_utf.decode(empty_utf_io)
expect(long_utf.to_s).to eq('')
end
end
end
end

View File

@ -414,7 +414,53 @@ describe Rex::Java::Serialization::Model::NewArray do
expect(new_array.encode.unpack("C*")).to eq(string_array.unpack("C*"))
end
end
describe "#to_s" do
it "prints a boolean array stream" do
new_array.decode(boolean_array_io)
expect(new_array.to_s).to eq('boolean, ["1", "0", "1", "1", "1", "1", "1", "1", "1", "0"]')
end
it "prints a byte array stream" do
new_array.decode(byte_array_io)
expect(new_array.to_s).to eq('byte, ["-20", "65"]')
end
it "prints a char array stream" do
new_array.decode(char_array_io)
expect(new_array.to_s).to eq('char, ["97", "98"]')
end
it "prints a short array stream" do
new_array.decode(short_array_io)
expect(new_array.to_s).to eq('short, ["-20", "65"]')
end
it "prints a double array stream" do
new_array.decode(double_array_io)
expect(new_array.to_s).to eq('double, ["0.25", "0.21"]')
end
it "prints a float array stream" do
new_array.decode(float_array_io)
expect(new_array.to_s).to eq('float, ["1.0", "2.0"]')
end
it "prints a int array stream" do
new_array.decode(int_array_io)
expect(new_array.to_s).to eq('int, ["-20", "65"]')
end
it "prints a long array stream" do
new_array.decode(long_array_io)
expect(new_array.to_s).to eq('long, ["-20", "65"]')
end
it "prints a string array stream" do
new_array.decode(string_array_io)
expect(new_array.to_s).to eq('java.lang.String;, ["msf"]')
end
end
end

View File

@ -130,4 +130,11 @@ describe Rex::Java::Serialization::Model::NewClassDesc do
expect(class_desc_new.encode.unpack("C*")).to eq(sample.unpack("C*"))
end
end
describe "#to_s" do
it "prints a sample NewClassDesc stream" do
class_desc_new.decode(sample_io)
expect(class_desc_new.to_s).to eq('java.lang.Byte, [ value (byte) ], @super_class: java.lang.Number')
end
end
end

View File

@ -71,4 +71,10 @@ describe Rex::Java::Serialization::Model::NewEnum do
end
end
describe "#to_s" do
it "prints a sample NewEnum stream" do
new_enum.decode(sample_enum_io)
expect(new_enum.to_s).to eq('SUNDAY')
end
end
end

View File

@ -70,4 +70,10 @@ describe Rex::Java::Serialization::Model::NewObject do
end
end
describe "#to_s" do
it "prints a sample Object stream" do
new_object.decode(easy_object_io)
expect(new_object.to_s).to eq('Easy => { ["int", 1094861636] }')
end
end
end

View File

@ -15,6 +15,19 @@ describe Rex::Java::Serialization::Model::Stream do
"\x42\x43\x44"
end
let(:easy_object_stream_io) { StringIO.new(easy_object_stream) }
let(:easy_object_stream_to_s) {
<<-EOS
@magic: 0xaced
@version: 5
@contents: [
NewObject { Easy => { ["int", 1094861636] } }
]
@references: [
[7e0000] NewClassDesc { Easy, [ SSN (int) ] }
[7e0001] NewObject { Easy => { ["int", 1094861636] } }
]
EOS
}
let(:char_array_stream) do
"\xac\xed\x00\x05\x75\x72\x00\x02" +
@ -23,6 +36,19 @@ describe Rex::Java::Serialization::Model::Stream do
"\x00\x00\x02\x00\x61\x00\x62"
end
let(:char_array_stream_io) { StringIO.new(char_array_stream) }
let(:char_array_stream_to_s) {
<<-EOS
@magic: 0xaced
@version: 5
@contents: [
NewArray { char, ["97", "98"] }
]
@references: [
[7e0000] NewClassDesc { [C, [ ] }
[7e0001] NewArray { char, ["97", "98"] }
]
EOS
}
let(:complex_stream) do
"\xac\xed\x00\x05\x77\x22\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00" +
@ -56,6 +82,36 @@ describe Rex::Java::Serialization::Model::Stream do
"\xc1\xc0"
end
let(:complex_stream_io) { StringIO.new(complex_stream) }
let(:complex_stream_to_s) {
<<-EOS
@magic: 0xaced
@version: 5
@contents: [
BlockData { [ 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1, 0xf6, 0xb6, 0x89, 0x8d, 0x8b, 0xf2, 0x86, 0x43 ] }
NewArray { java.rmi.server.ObjID;, ["java.rmi.server.ObjID => { [\\"long\\", 991106561224880050], java.rmi.server.UID => { [\\"short\\", -32746], [\\"long\\", 1416095896184], [\\"int\\", -766517433] } }"] }
BlockData { [ 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1 ] }
NewObject { java.rmi.dgc.Lease => { ["long", 600000], java.rmi.dgc.VMID => { byte, ["107", "2", "-57", "114", "96", "28", "-57", "-107"], 5 => { ["short", -32767], ["long", 1416097169642], ["int", -379403840] } } } }
]
@references: [
[7e0000] NewClassDesc { [Ljava.rmi.server.ObjID;, [ ] }
[7e0001] NewArray { java.rmi.server.ObjID;, ["java.rmi.server.ObjID => { [\\"long\\", 991106561224880050], java.rmi.server.UID => { [\\"short\\", -32746], [\\"long\\", 1416095896184], [\\"int\\", -766517433] } }"] }
[7e0002] NewClassDesc { java.rmi.server.ObjID, [ objNum (long), space (Ljava/rmi/server/UID;) ] }
[7e0003] Utf { Ljava/rmi/server/UID; }
[7e0004] NewObject { java.rmi.server.ObjID => { ["long", 991106561224880050], java.rmi.server.UID => { ["short", -32746], ["long", 1416095896184], ["int", -766517433] } } }
[7e0005] NewClassDesc { java.rmi.server.UID, [ count (short), time (long), unique (int) ] }
[7e0006] NewObject { java.rmi.server.UID => { ["short", -32746], ["long", 1416095896184], ["int", -766517433] } }
[7e0007] NewClassDesc { java.rmi.dgc.Lease, [ value (long), vmid (Ljava/rmi/dgc/VMID;) ] }
[7e0008] Utf { Ljava/rmi/dgc/VMID; }
[7e0009] NewObject { java.rmi.dgc.Lease => { ["long", 600000], java.rmi.dgc.VMID => { byte, ["107", "2", "-57", "114", "96", "28", "-57", "-107"], 5 => { ["short", -32767], ["long", 1416097169642], ["int", -379403840] } } } }
[7e000a] NewClassDesc { java.rmi.dgc.VMID, [ addr ([B), uid (0x7e0003) ] }
[7e000b] Utf { [B }
[7e000c] NewObject { java.rmi.dgc.VMID => { byte, ["107", "2", "-57", "114", "96", "28", "-57", "-107"], 5 => { ["short", -32767], ["long", 1416097169642], ["int", -379403840] } } }
[7e000d] NewClassDesc { [B, [ ] }
[7e000e] NewArray { byte, ["107", "2", "-57", "114", "96", "28", "-57", "-107"] }
[7e000f] NewObject { 5 => { ["short", -32767], ["long", 1416097169642], ["int", -379403840] } }
]
EOS
}
describe ".new" do
it "Rex::Java::Serialization::Model::Stream" do
@ -129,6 +185,23 @@ describe Rex::Java::Serialization::Model::Stream do
end
end
describe "#to_s" do
it "prints a simple Object stream" do
stream.decode(easy_object_stream_io)
expect(stream.to_s).to eq(easy_object_stream_to_s)
end
it "prints a char array stream" do
stream.decode(char_array_stream_io)
expect(stream.to_s).to eq(char_array_stream_to_s)
end
it "prints a complex stream with references" do
stream.decode(complex_stream_io)
expect(stream.to_s).to eq(complex_stream_to_s)
end
end
describe "#encode" do
context "when serializing a simple Object stream" do
it "serializes the Stream" do

View File

@ -88,4 +88,17 @@ describe Rex::Java::Serialization::Model::Utf do
end
end
end
describe "#to_s" do
it "prints an stream containing a sample utf" do
utf.decode(sample_utf_io)
expect(utf.to_s).to eq('java.lang.Number')
end
it "prints an stream containing an empty utf" do
utf.decode(empty_utf_io)
expect(utf.to_s).to eq('')
end
end
end

View File

@ -49,7 +49,7 @@ class JavaDeserializer
return
end
pp(stream)
puts stream
end
private