Skip to content

Commit

Permalink
Add missing early exists and warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-r7 committed Feb 29, 2024
1 parent 2951b49 commit 7723904
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions lib/rex/post/smb/ui/console/command_dispatcher/shares.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand All @@ -364,6 +368,7 @@ def cmd_upload(*args)
else
print_warning('Too many parameters')
cmd_upload_help
return
end
end

Expand All @@ -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)
Expand All @@ -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

Expand All @@ -412,6 +422,7 @@ def cmd_download(*args)
else
print_warning('Too many parameters')
cmd_download_help
return
end
end

Expand All @@ -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
Expand All @@ -453,13 +465,16 @@ def cmd_delete(*args)
else
print_warning('Too many parameters')
cmd_delete_help
return
end
end

full_path = as_ntpath(Pathname.new(shell.cwd).join(remote_path).to_s)
fd = simple_client.open(full_path, 'o')
fd.delete
print_good("Deleted #{full_path}")
ensure
fd.close if fd
end

def cmd_delete_help
Expand All @@ -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|
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 7723904

Please sign in to comment.