SSH login has full admin rights and there's no way to not grand them
Please answer the following
"OpenSSH for Windows" version 7.7.2.2
Server OperatingSystem Windows 10 Pro
Client OperatingSystem Linux
What is failing SSH login has admin rights.
When I log in through SSH to Windows 10, the log in has full admin rights. For example I write to C:\Windows. There should be a way to prevent this, but there doesn't seem to be one. If one of my tools/scripts/etc does something stupid, I could wipe out my system. There should be a way to get a login that is the same as when double-clicking on "cmd.exe", which gives you a non-admin command line.
The documentation says that to disable admin login, I should use DenyGroups Administrators. That is a non-solution, as then I can't log in and I get an permission denied ssh error.
For admin users, ssh connection is an elevated session. Given you are an admin and has elevated session, you can write to c:\windows.
This is by design.
@bagajjal Is it possible to prevent this and not get an elevated session? Or run OpenSSH without admin rights so that the sessions it creates are not elevated?
You can choose to go with non-admin user. Admin users requires elevated session to perform admin operations..
In a regular Windows session, for example when I work locally with cmd.exe / powershell / bash, I can work with non-admin privileges even though my user is an administrator. I explicetely need to grant an application elevated privileges in an UAC prompt. With ssh, I'm always elevated by default. It should be possible to use ssh with an admin user in an unelevated session.
I understand that this is currently not supported. So please consider this a feature request that would make working with ssh on Windows more secure by default :).
The biggest issue when you were to use the limited token after an SSH logon is that you cannot then get your elevated token. On an interactive logon you can right click and say Run as administrator and confirm through the UAC prompt but on a network logon like SSH there is no way for UAC to prompt you. So if this was a functionality you will never be able to swap from the limited to full token of your admin account.
The current design also causes problems, though. For example, I cannot SSH to my Windows 10 box and access mapped drives without jumping through hoops, because mapped drives are not accessible in elevated command prompts.
Please just provide the option.
@shoaniki the mapped drive problem is unrelated to being an admin. They just aren’t mapped on a network logon session. You will find even if you set the registry key to share mapped drives between the limited and admin session you will not see them when logging on through ssh.
oh right! thanks for the explanation and sorry for the noise then.
No worries, it’s a common problem but the reasoning behind it is complex so you’re not alone :)
The biggest issue when you were to use the limited token after an SSH logon is that you cannot then get your elevated token.
Perhaps this could be managed by authenticating with a different key? (This would require an option to be present to adjust this behavior.) However, I think some of us would be fine with being unable to perform admin functions over ssh on certain accounts that are in the Administrators group, and create a new admin account for that situation.
You can choose to go with non-admin user. Admin users requires elevated session to perform admin operations..
This perspective runs counter to the existence of UAC in Windows at all. Yes, one can create a non-admin user, but it requires entire workflow changes.
Please add an option to disable the forced elevation of users in the Administrators group.
Alternatively, is there a method/command to de-elevate? I've tried runas /trustlevel:0x20000 and it doesn't work in an ssh session (usually pops up a new window).
This also makes docker for windows not useable over SSH because docker login doesn't work in elevated shell.
FYI, the latest release of BusyBox for Windows includes an applet to drop privileges.
To use it standalone download the latest binary, either the 64-bit (busybox64.exe) or 32-bit (busybox.exe) version, as appropriate. Put it somewhere it can be executed, renamed as drop.exe, cdrop.exe or pdrop.exe. When run without arguments:
dropstarts an interactive BusyBox shell without elevated privilegescdropdoes the same forcmd.exepdropdoes likewise for PowerShell
Each variant also supports the -c option to allow a single command to be run without elevated privileges.
An SSH session invoking pdrop:
See also:
- The release notes for more details regarding
drop. - My BusyBox for Windows page for general busybox-w32 information.
According to @rmyorston’s mention of BusyBox for Windows, you can add the following code to your PowerShell configuration to automatically drop privileges upon SSH login.
$PROFILE
Set-Alias pdrop "C:\users\bruce\.local\bin\pdrop.exe"
function pdpwsh {
if (Test-Path $PROFILE) {
pdrop.exe -- pwsh.exe -NoLogo -NoExit -ExecutionPolicy Bypass -Command "& { . '$PROFILE' }"
} else {
pdrop.exe -- pwsh.exe -NoLogo -NoExit -ExecutionPolicy Bypass
}
}
# --- SSH login execute pdpwsh just one time
if ($env:SSH_CONNECTION -and -not $env:PDROP_DONE) {
$env:PDROP_DONE = '1'
try {
pdpwsh
exit
}
catch {
Write-Warning "Automatic privilege downgrade failed.:$($_.Exception.Message)"
}
}
I found that the latest PSReadLine imported by some TUI can't be loaded in busybox pdrop in OpenSSH with more than one session . It will throw on 2nd remote ssh session. If the 1st one exited completely then the 2nd one can proceed. Local pwsh session is ok. This is the error after running 2nd pdrop:
System.UnauthorizedAccessException: Access to the path 'PSReadLineHistoryFile_xxxxxxxxxx' is denied
Not sure if this is worth to report to the repo. Also early version of sudo has been announced in case it will bring this back to the table.
This is super annoying. I don't want an elevated session - makes certain commands not work as expected. Like git commands wont be able to use credentials etc.
Wouldn't it make sense to login as the user specified. And they can use sudo if they wish to elevate?
Wouldn't it make sense to login as the user specified. And they can use sudo if they wish to elevate?
There is no way on Windows to elevate from a limited user context to admin without UAC and UAC does not work on a network logon because there is no interactive desktop to show. The new sudo on Windows is really just a wrapper over UAC and provides a way to bring up the UAC prompt as well as potentially read the stdout/stderr of the sub process but it still requires that interactive UAC prompt.
Wouldn't it make sense to login as the user specified. And they can use sudo if they wish to elevate?
There is no way on Windows to elevate from a limited user context to admin without UAC and UAC does not work on a network logon because there is no interactive desktop to show. The new sudo on Windows is really just a wrapper over UAC and provides a way to bring up the UAC prompt as well as potentially read the stdout/stderr of the sub process but it still requires that interactive UAC prompt.
Hope this helps
Putting aside that Microsoft does not recommend this and while yes setting it to that level still has UAC present and running just without the explicit prompt the next problem is that a Network logon that SSH is doing does not have a split token like a normal interactive session.
Running this code to get the TOKEN_ELEVATION_TYPE of the current process shows that on SSH it is TokenElevationTypeDefault which has no split/linked token
#Requires -Module Ctypes
using namespace System.Security.Principal
$PROCESS_QUERY_INFORMATION = 0x0400
$TokenElevationType = 18
enum TOKEN_ELEVATION_TYPE {
TokenElevationTypeDefault = 1
TokenElevationTypeFull
TokenElevationTypeLimited
}
$k32 = New-CtypesLib Kernel32.dll
$a32 = New-CtypesLib Advapi32.dll
$procHandle = $tokenHandle = [IntPtr]::Zero
try {
$procHandle = $k32.SetLastError().OpenProcess[IntPtr](
$PROCESS_QUERY_INFORMATION,
$false,
$pid)
if ($procHandle -eq [IntPtr]::Zero) {
$k32.ThrowLastErrorException()
}
$res = $a32.SetLastError().OpenProcessToken[bool](
$procHandle,
[TokenAccessLevels]::Query,
[ref]$tokenHandle)
if (-not $res) {
$a32.ThrowLastErrorException()
}
$elevationType = 0
$returnLength = 0
$res = $a32.SetLastError().GetTokenInformation[bool](
$tokenHandle,
$TokenElevationType,
[ref]$elevationType,
4,
[ref]$returnLength)
if (-not $res) {
$a32.ThrowLastErrorException()
}
[TOKEN_ELEVATION_TYPE]$elevationType
}
finally {
if ($procHandle -ne [IntPtr]::Zero) {
$k32.CloseHandle[void]($procHandle)
}
if ($tokenHandle -ne [IntPtr]::Zero) {
$k32.CloseHandle[void]($tokenHandle)
}
}
There is no limited account or full account to deal with, you just have the full token as that is what Windows provides for this type of logon. This is the same regardless of the UAC slider and is how Network (and I think maybe Batch) type logons work. From a technical perspective SSH could instead create an Interactive logon token but this won't work for SSH key based logons (no password is available so S4U is used) and I doubt that they could choose to even do so for password based logons as it wouldn't pass any internal security reviews in Microsoft.
Approaching it from another angle and say we logon with a limited user account and just want to run a process as an admin user. This won't work with the default UAC settings due to not having an interactive desktop available on an SSH logon. If you were to set UAC to never notify won't work as it still checks for an interactive session but putting that aside from now there are other issues with this approach in general.
- You need the password of the admin user
Start-Process -Credential ...in PowerShell has a bug that stops it from working in session 0- Using
runas.exedoesn't make it easy to specify a password non-interactively or redirect the output - You can call CreateProcessWithLogon but there's nothing builtin to Windows
Even if you did the latter you'll find that even when the UAC slider is set to not notify the default token used here is going to be the limited token. Using Invoke-ProcessWith which wraps CreateProcessWithLogon and redirects the output to the caller's console we can see this by invoking as an admin user and their credentials.
Even if you were to have sudo available (I don't on my host) or manually call Start-Process ... -Verb Runas with UAC set to not notify it'll still fail as it still checks for the interactive session regardless.
Maybe sudo on Windows bypasses this altogether but I would be very surprised, I just don't have a host to test that assumption with.
The only option available would be to disable UAC entirely with the registry key allowing you to connect as a limited user and then spawn as an admin user using something that wraps CreateProcessWithLogon. Other alternatives would be 3rd party implementations like gsudo which work around the limitations around UAC in various ways that Microsoft may or may not condone.
Personally I would love this functionality but getting it would require work to be done in Windows and in the UAC area to support. Not something that the team behind the OpenSSH fork here really has any control over.
It is possible to change the default shell for SSH so that it uses an unprivileged PowerShell 7 session with default profile settings.
The code below should be run as an administrator
winget install busybox-w32 --scope machine
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShell -Value "C:\Program Files\WinGet\Links\busybox.exe" -PropertyType String -Force
New-ItemProperty -Path "HKLM:\SOFTWARE\OpenSSH" -Name DefaultShellArguments -Value "drop pwsh" -PropertyType String -Force
One simple workaround that worked for me is to use psmux (a tmux port for Windows).
- Install psmux:
winget install psmux - Add the following 2 lines to the config file
~/.psmux.conf:
# Keep tmux server running even with no session
set -g destroy-unattached off
set -g exit-empty off
- On the server, sign in as the target (non-admin) user and start a tmux-server there:
tmux start-server(This creates a tmux server that is owned by the target user). If you want the tmux server to be available after reboots/logins, create a Task Scheduler task that runs the same tmux command at the target user's logon (do NOT run it as an administrator; run it as the target user) - When you SSH in (and land in an admin-privileged shell), create a new tmux session (or attaching to existing session) will give you access to an unprivileged shell
- Detach with
tmux detach(or exit the session) to return to the ssh login shell (with admin privilege). The tmux server is kept even without any session so you can create a new unprivileged session again after exiting the last session.
- The tmux server and its sessions run with the target user's privileges so it can see mapped network drives and other per-user resources that an elevated shell (or shells created by
busybox drop) cannot access. - You still have the elevated/admin shell available by exiting/detaching the tmux session.