Repository navigation
Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugWG-Enginecore PowerShell engine, interpreter, and runtime
Milestone
Description
Summary of the new feature/enhancement
Summarize #3232 to get working in the same way on all platforms:
sudo Remove-Item foo.txt where foo.txt is owned by root
We have two common scenario for sudo - run a native command and run a script.
- Run a native command
This can be easily implemented like:
Start-Process <nativecommand> -ArgumentList … -Credential $userCred -Wait
Side effect on Windows: new window will be opened because:
- Windows does not allow elevate current process
- Windows does not allow attach elevated process to current console
.Net Core (and I guess Windows API too) doesn't allow to run new process attached to the same console.
So we can not get elevatedStart-Process pwsh -NoNewWindow -Credential $cred
- Run a script
2.1. We could implement this as for native command:
Start-Process pwsh -ArgumentList "-c","<scriptblockstring>" -Credential $userCred -Wait
2.2. We could implement this in most power way using PowerShell remoting:
Invoke-Command -ComputerName LocalHost -ScriptBlock <scriptblock> -Credential $userCred -ArgumentList ...
Current implementation proposal is 2.2.
Alternative proposal - start another pwsh process
Proposed name
- pssudo to avoid a conflict with Unix native sudo.
- Invoke-AsUser
Proposed UX behavior
- interactively ask for credentials at first run
- store credentials for a time and doesn't ask in follow runs
It is still not clear:
- PowerShell remoting to localhost by default has disabled elevated accounts in WinRM loopback - how could we address this in the context sudo? Even if PowerShell remoting does not allow us to do sudo by default, we could implement it turned off by default or allow only for an interactive session.
- PS sudo implementation should works over any transport - WinRM and SSH. But again there is a question about elevation in loopback - regardless of a protocol, security should remain at the same level, which implies that SSH loopback should behave the same as for WinRM taking in account how Windows internals work but currently SSH loopback works (with manual connection).
Reacted by Richard Simpson, Mike Sigsworth, Simon Chan, Stephan Troyer, Suhail Malik, Martin Brown, regs01, Ben Lucato, Luan Vitor Simião Oliveira, Gregory Ott and 9 more
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon Dec 14, 2019
Steve Lee (@SteveL-MSFT) Joey Aiello (@joeyaiello) My current conclusion is that we have only single way to implement cross-planform sudo - using PowerShell remoting like Andrew (@anmenaga) implemented Windows Compatibility.
We gathered a lot of information in #3232 and now MSFT team could have been brainstorming to make a conclusion.
rjmholt commentedon Dec 15, 2019
This has been discussed on and off.
There are a few thoughts that I haven't seen in the other thread. Since that one is focused more on the UX, I'll do the dump on implementation here.
Basically because PowerShell runs everything except for native commands in-process, this is very tricky.
There are essentially two options, both with significant drawbacks.
sudobecomes an alias to a remote/subprocess invocation in PowerShell to an Administrator/root process, where it runs the given command and gives back the result. This works fine for native commands, but for cmdlets only gives back deserialised objects. It also means that to make things performant we basically need to set up a persistent remote root session to host those commands. The main drawback is then the deserialisation (sudocmdlets work subtly differently from non-sudo), with the two other drawbacks of having to overridesudo(makes sense, but not ideal) and having to manage the security boundary and lifetime of the administrator remote host.PowerShell itself becomes a
setuidcommand andsudoworks in process as a builtin (again we must override the util), becoming an administrator process when asudocommand is run. The major drawbacks here are (1) being asetuidcommand makes PowerShell quite dangerous and requires authentication to use in whatever scenario and (2) upgrading the entire process to root does not cater to the abstraction of runspaces or indeed any threading at all.Given the two options, I really don't like either of them, but certainly think (2) is the more flawed and should be excluded at the outset.
This is just a braindump off the top of my head, so feel free to share, expand on or poke holes in implementation ideas here.
iSazonov commentedon Dec 15, 2019
Rob Holt (@rjmholt) If you continue your thinking you will finish on a kind (JEA?) of "PowerShell remoting" as implementation like me.
rjmholt commentedon Dec 15, 2019
Yeah I think we agree that the best path would be an implementation using a kind of “remoting as security boundary” architecture.
The part I don’t like about it is the lifetime of the elevated session. If we spin a new one up every time, it could be terribly slow. If we keep one open indefinitely, it leaves open a large attack surface.
Anyway, James Truher (@JamesWTruher) Paul Higinbotham (@PaulHigin) Steve Lee (@SteveL-MSFT) might have thoughts here
iSazonov commentedon Dec 15, 2019
Yes, it is the same as "ForEach -Parallel" - it is slow for inappropriate scenarios but works great for target scenarios.
I guess we get acceptable response time for interactive sessions (pwsh startup time is ~400 ms, with -NonInteractive it is less). For background, non-interactive scenarios I believe that using of pssudo is not right thing.
rjmholt commentedon Dec 15, 2019
Sounds like we want
sudoto be an entry point into a wider effort to allow offloading to and management of an elevated PowerShell worker processgerardog commentedon Jan 13, 2020
Sudo implementations in the wild resolve this by spawing a new hidden process, then using the AttachConsole Api to attach the elevated process to the non-elevated caller. Check Luke Sampson's sudo.ps1 implementation. This doesnt work if you want to redirect StdIn/StdOut. So on my own implementation gsudo (windows-only) I did a workaround for this scenario capturing StdIn/Out/Err and streaming via RPC to the non-elevated instance (when redirected, as in 'gsudo dir > somefile'.
SteveL-MSFT commentedon Feb 24, 2020
Looks like Gerardo Grignoli (@gerardog) is willing to contribute his code to a cmdlet in PowerShell (for Windows). We should discuss the cmdlet design first. I'm ok with a
pssudoalias, but the cmdlet still needs a verb-noun and adding a new parameterset toStart-Processmight not be the way to go.vexx32 commentedon Feb 24, 2020
I'd go for something like
Invoke-ElevatedCommandperhaps?gdoenlen commentedon Feb 24, 2020
Is there any thought of an additional common parameter to elevate a cmdlet instead of a specific cmdlet that does it?
sba923 commentedon Feb 24, 2020
The hidden elevated helper process should allow getting a close as possible to 'same UX on all platforms', shouldn't it?
SteveL-MSFT commentedon Feb 27, 2020
We definitely want pipeline support and also between cmdlets and native tools to work as expected. We could add a
-Elevatedtype switch toInvoke-Commandand have a wrapperpssudofunction make use of that since PowerShell aliases are a bit limited today rather than having a separate cmdlet just for sudo-type operation. We should also keep in mind that this should work on non-Windows as well where we would have to sudo the pwsh process and handle redirection. The limitation of deserialized objects in the pipeline vs live objects is an acceptable limitation in my mind.6 remaining items
PaulHigin commentedon Mar 5, 2020
I spent some time reading through the various links. This is great investigatory work. AFAIK there is no way to bypass UAC other than disabling it system wide, so for Windows this will have to be part of the experience.
I don't immediately see a security issue with passing an elevated console through the un-elevated console, when it is the same user only elevated. Only admin users can elevate. I don't know about supporting a RunAs option, where alternate credentials would be passed in, and could be a security concern. But in any case a full security review would have to be done on the final design.
At this point, I think an RFC document should be created that outlines the feature, user experience and different design approaches.
SeeminglyScience commentedon Mar 5, 2020
Worth noting that UAC can prompt for credentials depending on how UAC: Behavior of the elevation prompt for standard users is configured. It's pretty common to be configured as something other than "Automatically deny elevation requests".
agowa commentedon Mar 5, 2020
I don't see a problem by attaching an elevated command prompt to an non elevated one in general. Also the UAC is not a security feature. There are multiple ways to bypass it in a network. Even without credential delegation.
Or simpler using a non standard winrm client to connect via loopback to it, as mentioned in one of the links I attached earlier.
Also another thing to consider: If there are two persons, one with a limited user account and the other with an administrative one, why should the 2nd person enter his credentials into a non trusted session of the first one? This is the only case where detaching the terminal really would change anything, but the 1st user could as well just intercept the keyboard using a hardware accessory.
of topic: He has physical access and regarding Microsoft security concepts it is game over, he could even get System rights by just applying windows updates, even if bitlocker disk encryption is on, yes that vulnerability is publicly known and Microsoft refused to patch it...
back to topic what are possible threads:
For the scenario where the user is compromised using e.g. (what is currently popular?) lets say Emoted, but it was not able to get system rights yet, what could it do? Right, it could connect to the command prompt running with the same user credentials in the same session. So if we attach the administrative one it could hijack the connection. But with that permissions if we spawn a new console/window it would just intercept the creation of that process and append some parameters to it so that the administrator is tricked into acknowledging it...
Now see how unix systems handle these threads:
Processes with user rights cannot initiate communication with the sudo process (except for stdin) which is only attached to the terminal. Therefore the administrator has to check before he grants access using his credentials that:
What was uac designed for regarding enterprises?
From what I've read it was designed to help admins to "not shoot them selves into the food that easily", e.g. "del /S /Q C:" in the search bar... Basically it provides a kind of "-Confirm".
And regarding private and/or standalone computers?
It just inadvertently helps to obey good security habits of two separate user accounts for non administrative and administrative tasks. For the advantage of having separate contexts at all, because most people just have used the administrative account for everything including browsing the internet and so on.
I'd be totally fine with the attach to unprivileged process solution, as it provides the same guarantee uac currently does, but also for comandline applications. Also it is a feature that could be made to be disableable, like uac.
But anyway, we do not need to start from scratch for the security part, there is already something similar that we can hook onto its called UIAccess Applications: https://docs.microsoft.com/en-us/windows/security/threat-protection/security-policy-settings/user-account-control-allow-uiaccess-applications-to-prompt-for-elevation-without-using-the-secure-desktop
minecraftchest1 commentedon Mar 5, 2020
So, how does this enable us to elevate a running powershell session?
Thought. Is their a command we can run (such as "Powershell /elevated /no-new-window) that would allow us to do the *nix command su root to start an elevated sub-session?
minecraftchest1 commentedon Apr 13, 2021
Reading about UAC elevating the current shell, what about creating a new subshell, elevating that shell, running the specified command, then exiting that shell.
agowa commentedon Apr 16, 2021
Minecraftchest1 (@minecraftchest1) UAC doesn't allow you to pass input into the new shell. You could only spawn a new window. Or alternatively you would need to go through e.g. IPC to let both processes talk to each other. It's not as simple as "just spawning" another shell.
It won't, I was talking about why UAC is NOT a security boundary and related security concerns...
minecraftchest1 commentedon Apr 16, 2021
agowa commentedon Apr 17, 2021
Minecraftchest1 (@minecraftchest1) The cleanest implementation would be an addition to windows internals it self. It would provide the most standardized way of handling this especially because this is a core functionality that not only PowerShell is looking for.
But that said there are different ways to implement it.
Edit:
After thinking a bit more about this. It doesn't work quite that easily as windows doesn't allow to change/overwrite the primary token of a process after it was launched (maaybe a kernel driver could do that though). But even that we could work around. But than we'd have two use cases:
For the 1st one we could just use:
For the 2nd one we would have to do a bit more advanced handling:
sudo powershell-IPCSession.exe(for details about what sudo does see above) in the backgroundpowershell-IPCSession.exeonce started spawns a new powershell session and connects it to the named pipe created by the non privileged process.minecraftchest1 commentedon Apr 17, 2021
I understand. I shared this with the ReactOS project to ask what they could do. I personally don't have the patience to do things like this.
Akarinnnnn commentedon May 27, 2022
I perfer to open UAC popup on Windows personally, just like the installers.
In my envisage,
pssudohave following design:sudo pwsh ...or RunAs equalvent, and specify IPC channel, lifetime(if we have subsequent command to sudo), communication token(maybe not necessary), etc. on command line parameters. For examplesudo pwsh -SudoMode -SudoLifetime 5min -SudoToken 1234567890abcdef -SudoIpcPipe pipenamegerardog commentedon May 27, 2022
Hi! just wanted to let you know that
gsudoships with anInvoke-gsudocmdlet to elevate like thepssudoproposal.It runs a
ScriptBlockon an elevated PS instance, so it is a different lexical scope: theScriptBlockcan't access your$variables, but you can use$using:variableto apply it´s serialized value. Pipeline inputs and outputs are automatically serialized and deserialized.Example usage from the docs.
(The cmdlet is optional, you can still just prepend
gsudoto elevate a command string)Sorry if I am spamming this thread. I thought that mentioning it here once was on-topic. My apologies if it's not.
I created this discussion at gsudo's repo for anyone interested in discussing the cmdlet design, usage, troubleshooting, etc.
0009-0009-1013-986X commentedon Dec 1, 2023