Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #cd for Powershell Sessions #18021

Merged
merged 2 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spaces is optional here because sometimes they need to be escaped, and other times they do not.

This snippet demonstrates the issue. A small Python stub is used to print the arguments then one" "argument is passed to it showing that the space is kept but when passed to echo, the double quotes are kept.

Microsoft Windows [Version 10.0.17763.4377](c) 2018 Microsoft Corporation. All rights reserved.
msflab\smcintyre@DC C:\Users\smcintyre>type args.py
import sys; print(repr(sys.argv))
msflab\smcintyre@DC C:\Users\smcintyre>python args.py one" "argument
['args.py', 'one argument']
msflab\smcintyre@DC C:\Users\smcintyre>echo one" "argument 
one" "argument
msflab\smcintyre@DC C:\Users\smcintyre>

Allowing the user to specify means the escaped value can be used in both contexts. Psexec should not escape spaces because the command is passed to echo.

string = string.dup
%w[ ^ & < > | " ].each { |char| string.gsub!(char, "^#{char}") }
string.gsub!(' ', '" "') if spaces
string
end
end