From 69a4a327b88733efa2a24e61f248681c2d24254c Mon Sep 17 00:00:00 2001 From: Dylan Davis Date: Fri, 11 Nov 2016 10:15:36 -0700 Subject: [PATCH 1/5] Add begin-rescue blocks that prevent individual hosts from bailing out a threaded multi-host execution --- modules/auxiliary/admin/smb/psexec_command.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/modules/auxiliary/admin/smb/psexec_command.rb b/modules/auxiliary/admin/smb/psexec_command.rb index 5747494726..1035295c44 100644 --- a/modules/auxiliary/admin/smb/psexec_command.rb +++ b/modules/auxiliary/admin/smb/psexec_command.rb @@ -136,7 +136,11 @@ class MetasploitModule < Msf::Auxiliary # check if our process is done using these files def exclusive_access(*files) + begin simple.connect("\\\\#{@ip}\\#{@smbshare}") + rescue + return false + end files.each do |file| begin print_status("checking if the file is unlocked") @@ -154,7 +158,12 @@ class MetasploitModule < Msf::Auxiliary # Removes files created during execution. def cleanup_after(*files) - simple.connect("\\\\#{@ip}\\#{@smbshare}") + begin + simple.connect("\\\\#{@ip}\\#{@smbshare}") + rescue + print_error("Unable to connect for cleanup. Maybe you'll need to manually remove #{left.join(", ")} from the target.") + return + end print_status("Executing cleanup...") files.each do |file| begin From fddc2c221fa1232e2c92fe130ea9d601e18a2b47 Mon Sep 17 00:00:00 2001 From: Dylan Davis Date: Fri, 11 Nov 2016 10:24:05 -0700 Subject: [PATCH 2/5] Catch the specific exception. Include the error code in the error message. --- modules/auxiliary/admin/smb/psexec_command.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/auxiliary/admin/smb/psexec_command.rb b/modules/auxiliary/admin/smb/psexec_command.rb index 1035295c44..19b54a1824 100644 --- a/modules/auxiliary/admin/smb/psexec_command.rb +++ b/modules/auxiliary/admin/smb/psexec_command.rb @@ -138,10 +138,11 @@ class MetasploitModule < Msf::Auxiliary def exclusive_access(*files) begin simple.connect("\\\\#{@ip}\\#{@smbshare}") - rescue + rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror + print_status("Unable to get handle: #{accesserror}") return false end - files.each do |file| + files.each do |file| begin print_status("checking if the file is unlocked") fd = smb_open(file, 'rwo') @@ -154,14 +155,14 @@ class MetasploitModule < Msf::Auxiliary end return true end - - + + # Removes files created during execution. def cleanup_after(*files) begin simple.connect("\\\\#{@ip}\\#{@smbshare}") - rescue - print_error("Unable to connect for cleanup. Maybe you'll need to manually remove #{left.join(", ")} from the target.") + rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror + print_error("Unable to connect for cleanup: #{accesserror}. Maybe you'll need to manually remove #{left.join(", ")} from the target.") return end print_status("Executing cleanup...") From db32c5fdccea04728dd31cdac749ff5b9ecb5273 Mon Sep 17 00:00:00 2001 From: Jenna Magius Date: Fri, 11 Nov 2016 10:28:37 -0700 Subject: [PATCH 3/5] msftidy whitespace fixes --- modules/auxiliary/admin/smb/psexec_command.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/auxiliary/admin/smb/psexec_command.rb b/modules/auxiliary/admin/smb/psexec_command.rb index 19b54a1824..56e045687b 100644 --- a/modules/auxiliary/admin/smb/psexec_command.rb +++ b/modules/auxiliary/admin/smb/psexec_command.rb @@ -155,8 +155,8 @@ class MetasploitModule < Msf::Auxiliary end return true end - - + + # Removes files created during execution. def cleanup_after(*files) begin From a8a09261e118506d28ecc28b9f24763c961682ac Mon Sep 17 00:00:00 2001 From: Dylan Davis Date: Fri, 11 Nov 2016 21:49:06 -0700 Subject: [PATCH 4/5] Use files for rescue error, because left is not available --- modules/auxiliary/admin/smb/psexec_command.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/auxiliary/admin/smb/psexec_command.rb b/modules/auxiliary/admin/smb/psexec_command.rb index 56e045687b..5648158c9d 100644 --- a/modules/auxiliary/admin/smb/psexec_command.rb +++ b/modules/auxiliary/admin/smb/psexec_command.rb @@ -162,7 +162,7 @@ class MetasploitModule < Msf::Auxiliary begin simple.connect("\\\\#{@ip}\\#{@smbshare}") rescue Rex::Proto::SMB::Exceptions::ErrorCode => accesserror - print_error("Unable to connect for cleanup: #{accesserror}. Maybe you'll need to manually remove #{left.join(", ")} from the target.") + print_error("Unable to connect for cleanup: #{accesserror}. Maybe you'll need to manually remove #{files.join(", ")} from the target.") return end print_status("Executing cleanup...") From e5d3289c18c91f992a27f5bb0964b1a702dfd13d Mon Sep 17 00:00:00 2001 From: wchen-r7 Date: Tue, 15 Nov 2016 12:14:58 -0600 Subject: [PATCH 5/5] Fix name for exception --- modules/auxiliary/admin/smb/psexec_command.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/auxiliary/admin/smb/psexec_command.rb b/modules/auxiliary/admin/smb/psexec_command.rb index 5648158c9d..593db5af84 100644 --- a/modules/auxiliary/admin/smb/psexec_command.rb +++ b/modules/auxiliary/admin/smb/psexec_command.rb @@ -99,9 +99,9 @@ class MetasploitModule < Msf::Auxiliary print_status("Executing the command...") begin return psexec(execute) - rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error => exec_command_error + rescue Rex::Proto::DCERPC::Exceptions::Error, Rex::Proto::SMB::Exceptions::Error => e elog("#{e.class} #{e.message}\n#{e.backtrace * "\n"}", 'rex', LEV_3) - print_error("Unable to execute specified command: #{exec_command_error}") + print_error("Unable to execute specified command: #{e}") return false end end