Skip to content

Commit f50e609

Browse files
committed
Land rapid7#7556, Prevent psexec_command from dying when one host errors
2 parents b6f097c + e5d3289 commit f50e609

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

modules/auxiliary/admin/smb/psexec_command.rb

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def execute_command(text, bat)
9999
print_status("Executing the command...")
100100
begin
101101
return psexec(execute)
102-
rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error => exec_command_error
102+
rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error => e
103103
elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}", 'rex', LEV_3)
104-
print_error("Unable to execute specified command: #{exec_command_error}")
104+
print_error("Unable to execute specified command: #{e}")
105105
return false
106106
end
107107
end
@@ -136,8 +136,13 @@ def get_output(file)
136136

137137
# check if our process is done using these files
138138
def exclusive_access(*files)
139+
begin
139140
simple.connect("\\\\#{@ip}\\#{@smbshare}")
140-
files.each do |file|
141+
rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror
142+
print_status("Unable to get handle: #{accesserror}")
143+
return false
144+
end
145+
files.each do |file|
141146
begin
142147
print_status("checking if the file is unlocked")
143148
fd = smb_open(file, 'rwo')
@@ -154,7 +159,12 @@ def exclusive_access(*files)
154159

155160
# Removes files created during execution.
156161
def cleanup_after(*files)
157-
simple.connect("\\\\#{@ip}\\#{@smbshare}")
162+
begin
163+
simple.connect("\\\\#{@ip}\\#{@smbshare}")
164+
rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror
165+
print_error("Unable to connect for cleanup: #{accesserror}. Maybe you'll need to manually remove #{files.join(", ")} from the target.")
166+
return
167+
end
158168
print_status("Executing cleanup...")
159169
files.each do |file|
160170
begin

0 commit comments

Comments
 (0)