Module: Rex::Java::Serialization::Model::Contents

Includes:
Rex::Java::Serialization
Included in:
Annotation, ClassDesc, Field, NewArray, NewEnum, NewObject, Stream
Defined in:
lib/rex/java/serialization/model/contents.rb

Constant Summary

Constants included from Rex::Java::Serialization

BASE_WIRE_HANDLE, OBJECT_TYPE_CODES, PRIMITIVE_TYPE_CODES, SC_BLOCK_DATA, SC_ENUM, SC_EXTERNALIZABLE, SC_SERIALIZABLE, SC_WRITE_METHOD, STREAM_MAGIC, STREAM_VERSION, TC_ARRAY, TC_BLOCKDATA, TC_BLOCKDATALONG, TC_CLASS, TC_CLASSDESC, TC_ENDBLOCKDATA, TC_ENUM, TC_EXCEPTION, TC_LONGSTRING, TC_NULL, TC_OBJECT, TC_PROXYCLASSDESC, TC_REFERENCE, TC_RESET, TC_STRING, TYPE_CODES

Instance Method Summary collapse

Instance Method Details

#decode_content(io, stream) ⇒ Rex::Java::Serialization::Model::Element

Deserializes a content

Parameters:

  • io (IO)

    the io to read from

Returns:

Raises:

  • (RuntimeError)

    if deserialization doesn't succeed or unsupported content



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rex/java/serialization/model/contents.rb', line 15

def decode_content(io, stream)
  opcode = io.read(1)
  raise ::RuntimeError, 'Failed to unserialize content' if opcode.nil?
  opcode = opcode.unpack('C')[0]
  content = nil

  case opcode
  when TC_BLOCKDATA
    content = BlockData.decode(io, stream)
  when TC_BLOCKDATALONG
    content = BlockDataLong.decode(io, stream)
  when TC_ENDBLOCKDATA
    content = EndBlockData.decode(io, stream)
  when TC_OBJECT
    content = NewObject.decode(io, stream)
  when TC_CLASS
    content = ClassDesc.decode(io, stream)
  when TC_ARRAY
    content = NewArray.decode(io, stream)
  when TC_STRING
    content = Utf.decode(io, stream)
    stream.add_reference(content) unless stream.nil?
  when TC_LONGSTRING
    content = LongUtf.decode(io, stream)
    stream.add_reference(content) unless stream.nil?
  when TC_ENUM
    content = NewEnum.decode(io, stream)
  when TC_CLASSDESC
    content = NewClassDesc.decode(io, stream)
  when TC_PROXYCLASSDESC
    raise ::RuntimeError, 'Failed to unserialize unsupported TC_PROXYCLASSDESC content'
  when TC_REFERENCE
    content = Reference.decode(io, stream)
  when TC_NULL
    content = NullReference.decode(io, stream)
  when TC_EXCEPTION
    raise ::RuntimeError, 'Failed to unserialize unsupported TC_EXCEPTION content'
  when TC_RESET
    content = Reset.decode(io, stream)
  else
    raise ::RuntimeError, 'Failed to unserialize content'
  end

  content
end

#encode_content(content) ⇒ String

Serializes a content

Parameters:

Returns:

  • (String)

    if serialization succeeds

Raises:



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rex/java/serialization/model/contents.rb', line 66

def encode_content(content)
  encoded = ''

  case content
  when BlockData
    encoded << [TC_BLOCKDATA].pack('C')
  when BlockDataLong
    encoded << [TC_BLOCKDATALONG].pack('C')
  when EndBlockData
    encoded << [TC_ENDBLOCKDATA].pack('C')
  when NewObject
    encoded << [TC_OBJECT].pack('C')
  when ClassDesc
    encoded << [TC_CLASS].pack('C')
  when NewArray
    encoded << [TC_ARRAY].pack('C')
  when Utf
    encoded << [TC_STRING].pack('C')
  when LongUtf
    encoded << [TC_LONGSTRING].pack('C')
  when NewEnum
    encoded << [TC_ENUM].pack('C')
  when NewClassDesc
    encoded << [TC_CLASSDESC].pack('C')
  when NullReference
    encoded << [TC_NULL].pack('C')
  when Reset
    encoded << [TC_RESET].pack('C')
  when Reference
    encoded << [TC_REFERENCE].pack('C')
  else
    raise ::RuntimeError, 'Failed to serialize content'
  end

  encoded << content.encode
  encoded
end

Creates a print-friendly string representation of the content class

Parameters:

Returns:

  • (String)


149
150
151
# File 'lib/rex/java/serialization/model/contents.rb', line 149

def print_class(content)
  content.class.name.split('::').last
end

Creates a print-friendly string representation

Parameters:

Returns:

  • (String)


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rex/java/serialization/model/contents.rb', line 108

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