diff --git a/lib/rex/post/smb/ui/console/command_dispatcher/shares.rb b/lib/rex/post/smb/ui/console/command_dispatcher/shares.rb index e6f7cb79ba31d..d8c7c0589b9c9 100644 --- a/lib/rex/post/smb/ui/console/command_dispatcher/shares.rb +++ b/lib/rex/post/smb/ui/console/command_dispatcher/shares.rb @@ -284,7 +284,6 @@ def cmd_cd(*args) begin response = active_share.open_directory(directory: new_path) directory = RubySMB::SMB2::File.new(name: new_path, tree: active_share, response: response, encrypt: @tree_connect_encrypt_data) - directory.close rescue RubySMB::Error::UnexpectedStatusCode => e # Special case this error to provide better feedback to the user # since I think trying to `cd` to a non-existent directory is pretty likely to accidentally happen @@ -298,6 +297,8 @@ def cmd_cd(*args) print_error('Unknown error occurred while trying to change directory') elog(e) return + ensure + directory.close if directory end shell.cwd = new_path @@ -319,14 +320,14 @@ def cmd_cat(*args) return end - return print_no_share_selected if !active_share + return print_no_share_selected unless active_share path = args[0] new_path = as_ntpath(Pathname.new(shell.cwd).join(path).to_s) begin - file = active_share.open_file(filename: new_path) + file = simple_client.open(new_path, 'o') result = file.read print_line(result) rescue StandardError => e @@ -352,6 +353,9 @@ def cmd_upload(*args) cmd_upload_help return end + + return print_no_share_selected unless active_share + local_path = nil remote_path = nil @@ -364,6 +368,7 @@ def cmd_upload(*args) else print_warning('Too many parameters') cmd_upload_help + return end end @@ -372,16 +377,18 @@ def cmd_upload(*args) return end + data = ::File.binread(local_path) + remote_path = Rex::Post::File.basename(local_path) if remote_path.nil? full_path = as_ntpath(Pathname.new(shell.cwd).join(remote_path).to_s) fd = simple_client.open(full_path, 'wct', write: true) - data = ::File.read(local_path, ::File.size(local_path), mode: 'rb') fd.write(data) - fd.close print_good("#{local_path} uploaded to #{full_path}") + ensure + fd.close if fd end def cmd_upload_tabs(str, words) @@ -400,6 +407,9 @@ def cmd_download(*args) cmd_download_help return end + + return print_no_share_selected unless active_share + remote_path = nil local_path = nil @@ -412,6 +422,7 @@ def cmd_download(*args) else print_warning('Too many parameters') cmd_download_help + return end end @@ -425,11 +436,12 @@ def cmd_download(*args) fd = simple_client.open(full_path, 'o') data = fd.read - fd.close - ::File.open(local_path, 'wb') { |local_file| local_file.write(data) } + ::File.binwrite(local_path, data) print_good("Downloaded #{full_path} to #{local_path}") + ensure + fd.close if fd end def cmd_download_help @@ -453,6 +465,7 @@ def cmd_delete(*args) else print_warning('Too many parameters') cmd_delete_help + return end end @@ -460,6 +473,8 @@ def cmd_delete(*args) fd = simple_client.open(full_path, 'o') fd.delete print_good("Deleted #{full_path}") + ensure + fd.close if fd end def cmd_delete_help @@ -474,6 +489,9 @@ def cmd_mkdir(*args) cmd_upload_help return end + + return print_no_share_selected unless active_share + remote_path = nil @@mkdir_opts.parse(args) do |_opt, idx, val| @@ -488,9 +506,11 @@ def cmd_mkdir(*args) full_path = as_ntpath(Pathname.new(shell.cwd).join(remote_path).to_s) - simple_client.open(full_path, 'wc', dir: true) + fd = simple_client.open(full_path, 'wc', dir: true) print_good("Directory #{full_path} created") + ensure + fd.close if fd end def cmd_mkdir_help @@ -505,15 +525,19 @@ def cmd_rmdir(*args) cmd_download_help return end + + return print_no_share_selected unless active_share + remote_path = nil - @@delete_opts.parse(args) do |_opt, idx, val| + @@rmdir_opts.parse(args) do |_opt, idx, val| case idx when 0 remote_path = val else print_warning('Too many parameters') - cmd_delete_help + cmd_rmdir_help + return end end @@ -528,7 +552,8 @@ def cmd_rmdir(*args) directory.delete print_good("Deleted #{full_path}") - + ensure + directory.close if directory end def cmd_rmdir_help