You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Remote SSH commands require double escaping before hitting the DefaultShell #1082
Server OperatingSystem
Windows Server 2016 Standard
Client OperatingSystem
Fedora 27
What is failing
Attempting to run remote commands using SSH with my DefaultShell set to powershell.exe requires escaping the command to pass through both my local shell and the Command Prompt on the remote Windows server before PowerShell handles the command. As a simple example, this is the command as run directly inside Powershell:
echo "`"hello`""
Which produces this output:
"hello"
Attempting to run the above command from a Bash script on Linux, I expect to have to use single quotes around the command to avoid Bash interpresting the double-quotes and back-ticks, but not much else:
ssh windows 'echo "`"hello`""'
It's hard to read, but that ends in back-tick, double-quote, double-quote, single-quote. However, I find that this only produces the following output:
hello`
Which seems to be due to the Command Prompt first interpreting the double-quotes before handing it off to PowerShell. Instead, I must quote the command for the Command Prompt and then for Bash in order for it to pass all the way to PowerShell unencumbered. The following command produces the correct output:
ssh windows '"echo \"`\"hello`\"\""'
This is far from intuitive and gets worse as I try to do more complex commands including pipes, where-object and foreach-object using the ?, %, and {} metacharacters.
My concern isn't about the client-side escaping, it's that I need two levels of server-side escaping. My example still applies when running ssh.exe from a Windows box. When running a remote PowerShell command with ssh, I first have to write it thinking about proper PowerShell syntax, naturally, but then I have to escape it to pass through the remote cmd.exe process that is started by the sshd.exe server on the Windows server which then runs powershell.exe as it's child. Then, as a third layer, I have to escape it to pass through the local cmd.exe shell. It's the middle layer, the remote server cmd.exe that I would like to eliminate. This is in stark contrast to how sshd runs on any UNIX box, whether it's Linux, macOS, or cygwin on Windows layer. If I use csh as my personal shell on a UNIX box, it has a distinct syntax incompatible from standard Bourne shell or it's derivatives like bash. I only have to worry about writing a valid csh command, and then escaping it for my local shell whether that's bash, csh, cmd.exe, or powershell.exe. The command, as passed through the ssh protocol is handed directly to the final shell that interprets it, which I would like to be powershell.exe and not a cmd.exe wrapping powershell.exe.
It does indeed look like PowerShell is being run as a child of cmd.exe. This is an interactive session to a Linux server where the user's shell is csh, and not sh or bash as is typical:
PID TTY STAT TIME COMMAND
18695 ? Ss 0:00 sshd: user [priv]
18701 ? S 0:00 \_ sshd: user@pts/86
18714 pts/86 Ss 0:00 \_ -csh
18817 pts/86 R+ 0:00 \_ ps axwf
Where as the desired UNIX shell is invoked directly on a traditional UNIX\Linux box. Even if the cmd.exe layer on the server side can't be removed, it would be nice for it to escape commands going through it so it is transparent to the remote user running a command in the selected target shell, PowerShell or otherwise.
Here's a better step-by-step example to demonstrate the different behavior of sshd.exe on Windows and sshd on a Linux box. The local and remote shell for Linux is bash, but for Windows, I'll use cmd.exe as the local shell for ssh.exe and powershell.exe for the server-side shell called by sshd.exe on Windows. The goal will be to echo out the string "hello" with quotes included in the output. In Bash, the double quotes have a special meaning and need to be escaped. When running the command locally, there are two ways to do it:
echo '"hello"'
echo \"hello\"
To run this remotely from a Linux box, I will need to escape the command so that the local Bash shell passes it through ssh to the remote Linux shell. The simplest mechanism is to use single-quotes with the second command above. Inside single-quotes no characters other than a single-quote, which ends the quoting, have any special meaning. That makes it pretty simple to implement:
ssh linux 'echo \"hello\"'
With just two layers of quoting, I can print "hello". Now, to do the same from a cmd.exe shell running ssh.exe, it's not much different, but the quoting rules are different. With cmd.exe, only two characters have any special meaning in quotes, the double-quote and backslash. To keep the example simple, I'll use the first command from above that only has double-quotes. To quote a double-quote in cmd.exe, it just needs to be doubled up, but the string much already be inside double quotes so I add double quotes around the whole command after doubling each double-quote in the original command:
ssh.exe linux "echo '""hello""'"
It looks a little confusing, but it's just two layers, still, of quoting rules to handle. Now, to do this same action in a local PowerShell, there are again two ways of writing it:
echo '"hello"'
echo `"hello`"
Both commands will print "hello" to the screen when run in a local PowerShell. If I run this from a cmd.exe shell on Windows, I would expect to be able run either by just adding the quoting needed to pass through the local cmd.exe shell. Taking the second command from above, I double up the quotes and then surround the entire command in quotes:
ssh.exe windows "echo `""`"""
But that produces hello` as output, not "hello". It turns out, I need to apply the cmd.exe quoting rules twice, once for the local cmd.exe and once for the remote cmd.exe:
ssh.exe windows """echo `""""`"""""""
So now I have 4 quotes for double-quotes that must appear for PowerShell, and 2 quotes where I'm wrapping the command for the remote cmd.exe giving 7 quotes at the end once I wrap it again for the local cmd.exe. Running the command from a local Linux box to a remote Windows server using PowerShell, I see the same behavior, but this time, I quote the PowerShell command to pass through cmd.exe, and then to pass through Bash by using single-quotes around it:
ssh windows '"echo `""hello`"""'
For every remote PowerShell command I run on Windows, I have to also quote to handle passing through cmd.exe regardless whether it's initiated from a Linux or Window box.
This whole thing above might seem like a silly example because it really is, so I'll give a more realistic example based on what we are actually doing. Here's a pretty short PowerShell command that we would like to run from a Linux box. This is the command as run on the target PowerShell:
And it gives us the information we need as output. Trying this from ssh.exe inside cmd.exe, I know I need to double up the quotes and then surround it in double-quotes. Luckily, there's no backslashes to worry about. So that produces this command:
ssh.exe windows "Get-NetAdapter | ?{$_.MACAddress -eq ""12-34-56-12-34-56""}"
However, I find that this produced a syntax error since I failed to account for the remote cmd.exe interpreting this command. Taking that into account and escaping again produces this command:
ssh.exe windows "Get-NetAdapter | ?{$_.MACAddress -eq """"""12-34-56-12-34-56""""""}"
Which is starting to get hard to read. However, the whole reason I'm using ssh is to be able to run PowerShell commands like this from a Linux box. If I do the same thing from from ssh in Bash, I will run into that same syntax error if I don't first quote the PowerShell command for cmd.exe and then escape it for bash:
ssh windows 'Get-NetAdapter | ?{$_.MACAddress -eq """52-54-00-AB-A7-1F"""}'
I would like to be able to take a working PowerShell command and run it through ssh only worrying about my local shell or scripting language whatever that may be. Currently, I need to always add in this cmd.exe translation layer and don't always get it right.
Thank you for the detailed analysis. I understand the issue. Its stemming from the fact that while launching default shell (other than cmd.exe) with PTY, its actually launched via an intermediate cmd.exe and this is consuming the extra escape.
To fix this, we need to internally excape the shell payload before passing it to cmd.exe. AFAIK, we only need to escaple any double quotes ("s), by doubling them. Is this understanding correct?
Here' how the command payload would flow through:
ssh ""command"" -->
"command" (over the wire to sshd) -->
base64encoded("command") to ssh-shellhost -->
"command"(decoded) in ssh-shellhost -->
""command"" (after escape'ing) to cmd.exe --> //this is the escape step that needs to be added
"command" to default shell
After a little more research, I'm thinking that this might have a little more to do with a fundamental difference in process creation between Windows and UNIX-like operating systems. The command-line is actually passed to it's child process as a single string which the child parses with it's own rules. What I'm seeing as the remote cmd.exe layer of quoting might actually be the C/C++ Runtime library inside PowerShell.exe following these rules: https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
In that case, it might require shell-host.c to escape the string it receives over SSH to pass through as the argument to /c for the child cmd.exe/powershell.exe. I should also test with cmd.exe and bash.exe and see how the quoting works for those remote shells.
Using the expected level of quoting to handle the local invocation of PowerShel.exe and produces the desired output. Turning that into SSH directly, though breaks:
ssh.exe windows "Get-NetAdapter | ?{$_.MACAddress -eq \"12-34-56-12-34-56\"}"
As before, I have to double up the backslashes to handle two layers of C/C++ Runtime parsing the same argument:
ssh.exe windows "Get-NetAdapter | ?{$_.MACAddress -eq \\\"12-34-56-12-34-56\\\"}"
The equivalent test on Linux, for comparison, works with the same quoting level:
bash -c "/sbin/ip -o link | grep \"12:34:56:12:34:56\""
ssh linux "/sbin/ip -o link | grep \"12:34:56:12:34:56\""
Asad Saeeduddin (@masaeedu) raised a good point. For those custom shell, which might have different rules of quotes and escaping than cmd or powershell, user might need to receive the raw cmdline from sshd. But I still think we need to process quotes and spaces to make common shells to work. How about we escape/process the commands for cmd.exe and powershell.exe, and for other customer shells, leave community and user to process?
int main()
{
LPWSTR cmdline = GetCommandLineW();
wprintf(L"Command line: %s\n", cmdline);
int nArgs;
LPWSTR *szArglist = CommandLineToArgvW(cmdline, &nArgs);
if (NULL == szArglist)
{
wprintf(L"CommandLineToArgW failed\n");
return 0;
}
else
{
for (int i = 0; i < nArgs; i++)
{
wprintf(L"argv[%d]: %s\n", i, szArglist[i]);
}
}
LocalFree(szArglist);
return 0;
}
Yanbing (@bingbing8) I think it would be a good idea to put the escaping logic for built in shells like cmd and powershell into small executables as well. These can be built and distributed alongside win32-openssh and set up as the DefaultShell out of the box. It seems to me this would make things easier and more consistent in terms of implementation, since you don't need to inspect the shell or command string and make decisions inside SSHD. There is however the marginal cost of an additional process call.
It's essentially an implementation detail though, so as an end user it wouldn't matter if it was baked into SSHD either (so long as it doesn't accidentally catch and process command strings intended for other shells).
Danny Maertens (@maertendMSFT), we still need to add double quotes to the executable, in this case, the full path to the default shell. otherwise, it may not even find the path to the executable.
for existing common shells, like powershell, bash, cygwin, we add double quotes to the path of default shell and its arguments, escape the double quotes and backslash inside args.
For cmd and ssh-shellhost.exe, we simplily add double quotes to shell and its arguments without any escaping.
For shell other than above, we add a registry value DefaultShellEscapeArguments" under "HKEY_LOCAL_MACHINE\SOFTWARE\OpenSSH". The default value when missing is 1, which mean the escaping will happen like no.1 above. If user does not want escaping, they need to explicitly set this value to 0. in later case, only double quotes are added to shell and args, like no.2 above.
Kevin Brubeck Unhammer (@unhammer), openssh code does not add extra single-quotes. I believe the single quotes are added by git. I close this double quotes escaping issue now. If want, we can discuss in a different issue for the extra single quotes issue.
Kevin Brubeck Unhammer (@unhammer), look like the issue you saw are due to the issue single quoted are not interpreted as file path by cmd and ssh-shellhost.exe. singe quotes are added by git client, but cmd.exe and ssh-shellhost.exe does not recognize the quoted string as file path. #895.
Please refer to Setting up a Git server on Windows using Git for Windows and Win32_OpenSSH for workaroud. setting default shell to powershell should also works.
When can we expect that fix in the supported version (Windows Server 2019)?
I don't feel comfortable running beta versions in production environments...
Activity
manojampalam commentedon Feb 26, 2018
Behavior wrt to double escaping is the same even with cmd.exe. See the following from Windows client to Windows server.
hello` output seems to the behavior specific to powershell.exe
penguin359 commentedon Mar 1, 2018
My concern isn't about the client-side escaping, it's that I need two levels of server-side escaping. My example still applies when running ssh.exe from a Windows box. When running a remote PowerShell command with ssh, I first have to write it thinking about proper PowerShell syntax, naturally, but then I have to escape it to pass through the remote cmd.exe process that is started by the sshd.exe server on the Windows server which then runs powershell.exe as it's child. Then, as a third layer, I have to escape it to pass through the local cmd.exe shell. It's the middle layer, the remote server cmd.exe that I would like to eliminate. This is in stark contrast to how sshd runs on any UNIX box, whether it's Linux, macOS, or cygwin on Windows layer. If I use csh as my personal shell on a UNIX box, it has a distinct syntax incompatible from standard Bourne shell or it's derivatives like bash. I only have to worry about writing a valid csh command, and then escaping it for my local shell whether that's bash, csh, cmd.exe, or powershell.exe. The command, as passed through the ssh protocol is handed directly to the final shell that interprets it, which I would like to be powershell.exe and not a cmd.exe wrapping powershell.exe.
penguin359 commentedon Mar 1, 2018
Looking at the process hierarchy from an interactive PowerShell session on Windows, I see this:
It does indeed look like PowerShell is being run as a child of cmd.exe. This is an interactive session to a Linux server where the user's shell is csh, and not sh or bash as is typical:
Where as the desired UNIX shell is invoked directly on a traditional UNIX\Linux box. Even if the cmd.exe layer on the server side can't be removed, it would be nice for it to escape commands going through it so it is transparent to the remote user running a command in the selected target shell, PowerShell or otherwise.
penguin359 commentedon Mar 2, 2018
Here's a better step-by-step example to demonstrate the different behavior of sshd.exe on Windows and sshd on a Linux box. The local and remote shell for Linux is bash, but for Windows, I'll use cmd.exe as the local shell for ssh.exe and powershell.exe for the server-side shell called by sshd.exe on Windows. The goal will be to echo out the string "hello" with quotes included in the output. In Bash, the double quotes have a special meaning and need to be escaped. When running the command locally, there are two ways to do it:
To run this remotely from a Linux box, I will need to escape the command so that the local Bash shell passes it through ssh to the remote Linux shell. The simplest mechanism is to use single-quotes with the second command above. Inside single-quotes no characters other than a single-quote, which ends the quoting, have any special meaning. That makes it pretty simple to implement:
With just two layers of quoting, I can print "hello". Now, to do the same from a cmd.exe shell running ssh.exe, it's not much different, but the quoting rules are different. With cmd.exe, only two characters have any special meaning in quotes, the double-quote and backslash. To keep the example simple, I'll use the first command from above that only has double-quotes. To quote a double-quote in cmd.exe, it just needs to be doubled up, but the string much already be inside double quotes so I add double quotes around the whole command after doubling each double-quote in the original command:
It looks a little confusing, but it's just two layers, still, of quoting rules to handle. Now, to do this same action in a local PowerShell, there are again two ways of writing it:
Both commands will print "hello" to the screen when run in a local PowerShell. If I run this from a cmd.exe shell on Windows, I would expect to be able run either by just adding the quoting needed to pass through the local cmd.exe shell. Taking the second command from above, I double up the quotes and then surround the entire command in quotes:
But that produces hello` as output, not "hello". It turns out, I need to apply the cmd.exe quoting rules twice, once for the local cmd.exe and once for the remote cmd.exe:
So now I have 4 quotes for double-quotes that must appear for PowerShell, and 2 quotes where I'm wrapping the command for the remote cmd.exe giving 7 quotes at the end once I wrap it again for the local cmd.exe. Running the command from a local Linux box to a remote Windows server using PowerShell, I see the same behavior, but this time, I quote the PowerShell command to pass through cmd.exe, and then to pass through Bash by using single-quotes around it:
For every remote PowerShell command I run on Windows, I have to also quote to handle passing through cmd.exe regardless whether it's initiated from a Linux or Window box.
This whole thing above might seem like a silly example because it really is, so I'll give a more realistic example based on what we are actually doing. Here's a pretty short PowerShell command that we would like to run from a Linux box. This is the command as run on the target PowerShell:
And it gives us the information we need as output. Trying this from ssh.exe inside cmd.exe, I know I need to double up the quotes and then surround it in double-quotes. Luckily, there's no backslashes to worry about. So that produces this command:
However, I find that this produced a syntax error since I failed to account for the remote cmd.exe interpreting this command. Taking that into account and escaping again produces this command:
Which is starting to get hard to read. However, the whole reason I'm using ssh is to be able to run PowerShell commands like this from a Linux box. If I do the same thing from from ssh in Bash, I will run into that same syntax error if I don't first quote the PowerShell command for cmd.exe and then escape it for bash:
I would like to be able to take a working PowerShell command and run it through ssh only worrying about my local shell or scripting language whatever that may be. Currently, I need to always add in this cmd.exe translation layer and don't always get it right.
manojampalam commentedon Mar 5, 2018
Thank you for the detailed analysis. I understand the issue. Its stemming from the fact that while launching default shell (other than cmd.exe) with PTY, its actually launched via an intermediate cmd.exe and this is consuming the extra escape.
To fix this, we need to internally excape the shell payload before passing it to cmd.exe. AFAIK, we only need to escaple any double quotes ("s), by doubling them. Is this understanding correct?
Here' how the command payload would flow through:
ssh ""command"" -->
"command" (over the wire to sshd) -->
base64encoded("command") to ssh-shellhost -->
"command"(decoded) in ssh-shellhost -->
""command"" (after escape'ing) to cmd.exe --> //this is the escape step that needs to be added
"command" to default shell
bagajjal thoughts?
penguin359 commentedon Mar 5, 2018
After a little more research, I'm thinking that this might have a little more to do with a fundamental difference in process creation between Windows and UNIX-like operating systems. The command-line is actually passed to it's child process as a single string which the child parses with it's own rules. What I'm seeing as the remote cmd.exe layer of quoting might actually be the C/C++ Runtime library inside PowerShell.exe following these rules:
https://msdn.microsoft.com/en-us/library/17w5ykft.aspx
In that case, it might require shell-host.c to escape the string it receives over SSH to pass through as the argument to /c for the child cmd.exe/powershell.exe. I should also test with cmd.exe and bash.exe and see how the quoting works for those remote shells.
Here one test I tried from a local cmd.exe shell:
Using the expected level of quoting to handle the local invocation of PowerShel.exe and produces the desired output. Turning that into SSH directly, though breaks:
As before, I have to double up the backslashes to handle two layers of C/C++ Runtime parsing the same argument:
The equivalent test on Linux, for comparison, works with the same quoting level:
penguin359 commentedon Mar 5, 2018
OK, you beat me to my last comment. Thanks for looking into this!
21 remaining items
bingbing8 commentedon Oct 23, 2018
Asad Saeeduddin (@masaeedu) raised a good point. For those custom shell, which might have different rules of quotes and escaping than cmd or powershell, user might need to receive the raw cmdline from sshd. But I still think we need to process quotes and spaces to make common shells to work. How about we escape/process the commands for cmd.exe and powershell.exe, and for other customer shells, leave community and user to process?
masaeedu commentedon Oct 23, 2018
Yanbing (@bingbing8) I think it would be a good idea to put the escaping logic for built in shells like cmd and powershell into small executables as well. These can be built and distributed alongside win32-openssh and set up as the DefaultShell out of the box. It seems to me this would make things easier and more consistent in terms of implementation, since you don't need to inspect the shell or command string and make decisions inside SSHD. There is however the marginal cost of an additional process call.
It's essentially an implementation detail though, so as an end user it wouldn't matter if it was baked into SSHD either (so long as it doesn't accidentally catch and process command strings intended for other shells).
bingbing8 commentedon Oct 29, 2018
Danny Maertens (@maertendMSFT), we still need to add double quotes to the executable, in this case, the full path to the default shell. otherwise, it may not even find the path to the executable.
bingbing8 commentedon Nov 3, 2018
Asad Saeeduddin (@masaeedu), here is what will do,
The PR is at: PowerShell/openssh-portable#349
Fix of 1211 and 1082 (#349)
unhammer commentedon Dec 19, 2018
Yanbing (@bingbing8) will 1. help with
git clone windowsmachine/c/Users/user/repo.gitturning into''/c/Users/user/repo.git''(extra single-quotes)? Cf. https://stackoverflow.com/questions/53834304/how-do-i-git-clone-from-a-windows-machine-over-ssh/53847484bingbing8 commentedon Jan 11, 2019
Kevin Brubeck Unhammer (@unhammer), openssh code does not add extra single-quotes. I believe the single quotes are added by git. I close this double quotes escaping issue now. If want, we can discuss in a different issue for the extra single quotes issue.
unhammer commentedon Jan 12, 2019
That make sense – I managed to avoid the issue by switching the openssh defaultshell from cmd.exe to bash :)
bingbing8 commentedon Jan 13, 2019
Kevin Brubeck Unhammer (@unhammer), look like the issue you saw are due to the issue single quoted are not interpreted as file path by cmd and ssh-shellhost.exe. singe quotes are added by git client, but cmd.exe and ssh-shellhost.exe does not recognize the quoted string as file path. #895.
Please refer to Setting up a Git server on Windows using Git for Windows and Win32_OpenSSH for workaroud. setting default shell to powershell should also works.
bielawb commentedon Mar 8, 2019
Hi
When can we expect that fix in the supported version (Windows Server 2019)?
I don't feel comfortable running beta versions in production environments...
Regards
Bartek
cpriest commentedon Apr 8, 2021
Here's a quick workaround AutoHotkey script that takes care of the problem for me:
https://gist.github.com/cpriest/b08543648ba513d977e6c54e23f9f682
Fix of 1211 and 1082 (PowerShell#349)