Skip to content

Commit

Permalink
Land #18021, Fix #cd for Powershell Sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
jheysel-r7 committed May 25, 2023
2 parents 8368b80 + ed5d516 commit d825515
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/msf/core/exploit/remote/smb/client/psexec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ def execute_command_payload(smbshare)

def execute_command(text, bat, cmd)
# Try and execute the provided command
cmd = cmd.gsub('&', '^&')
cmd = Msf::Post::Windows.escape_cmd_literal(cmd, spaces: false)
execute = "%COMSPEC% /C echo #{cmd} ^> %SYSTEMDRIVE%#{text} > #{bat} & %COMSPEC% /C start %COMSPEC% /C #{bat}"
vprint_status("Executing the command: #{execute}")
begin
Expand Down
2 changes: 1 addition & 1 deletion lib/msf/core/post/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def cd(path)
if session.type == 'meterpreter'
session.fs.dir.chdir(e_path)
elsif session.type == 'powershell'
cmd_exec("Set-Location -Path \"#{e_path}\"")
cmd_exec("Set-Location -Path \"#{e_path}\";[System.IO.Directory]::SetCurrentDirectory($(Get-Location))")
else
session.shell_command_token("cd \"#{e_path}\"")
end
Expand Down
14 changes: 14 additions & 0 deletions lib/msf/core/post/windows.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
# -*- coding: binary -*-

module Msf::Post::Windows
# Escape a string literal value to be included as an argument to cmd.exe. The escaped value *should not* be placed
# within double quotes as this will alter now it is evaluated (e.g. `echo "^"((^&test) Foo^""` is different than
# `echo ^"((^&test) Foo^"`.
#
# @param [String] string The string to escape for use with cmd.exe.
# @param [Boolean] spaces Whether or not to escape spaces. If the string is being passed to echo, set this to false
# otherwise if it's an argument, set it to true.
# @return [String] The escaped string.
def self.escape_cmd_literal(string, spaces:)
string = string.dup
%w[ ^ & < > | " ].each { |char| string.gsub!(char, "^#{char}") }
string.gsub!(' ', '" "') if spaces
string
end
end

0 comments on commit d825515

Please sign in to comment.