r/PowerShell • • Apr 28 '20

jfhbrook/PSeudo - Execute PowerShell commands as Administrator in Windows 10 "like sudo"

https://github.com/jfhbrook/PSeudo
118 Upvotes

26 comments sorted by

10

u/Jacob_Ernst98 Apr 28 '20

Thank you! I've wanted to make this and just never found the motivation.

6

u/jhizzle4rizzle Apr 28 '20

You're welcome! This was 100% scratching an itch.

5

u/chinpokomon Apr 28 '20

What is the context of a standard user elevated to an administrator using this? I was using Start-Process to elevate a standard user and found that this made $Env:USERNAME the administrator I used to respond to the UAC.

5

u/jhizzle4rizzle Apr 28 '20

Commands run in the Administrator process but variables in script blocks seem to be resolved in the host process. In particular, environment variables in script blocks resolve to those of the standard user.

For string commands, typical string interpolation rules apply, and then the command runs in the Administrator process using Invoke-Expression.

4

u/chinpokomon Apr 28 '20

Had you posted one day earlier... 🤦🏼

I'll have to see if this resolves the problem I was facing. Specifically I was wanting to take ownership of a file by a using takeown.exe. I came up with a process which worked but only for Administrator accounts, not Standard accounts.

I have a feeling that while I might actually be able to resolve as the correct owner by the environment, it's still going to be owned by the Administrator account. 🤔

It's a difference than using Start-Process -Verb RunAs which should probably be investigated and documented.

3

u/genr8 Apr 28 '20

Makes perfect sense.... /s

3

u/[deleted] Apr 28 '20

So... if I install this on a machine for a user, remove the admin rights of that user, they would be able to execute commands as if they were still an admin?

Even with UAC enabled?

3

u/jhizzle4rizzle Apr 28 '20

It uses Start-Process -Verb RunAs to run the admin process, no funny business with actual privilege escalation.

"like sudo" is about the UX, not about replicating the behavior of the sudoers file. I believe there are projects that attempt to do this sort of thing on the gallery, but my needs had more to do with ergonomics than reinventing privilege escalation and access control.

Edit: In other words, if you installed this on a machine for a user and then removed their admin rights that it would fail if they tried to use it because they don't have the ability to start processes as admin.

3

u/chinpokomon Apr 28 '20

Ah, then I suspect my problem won't go away since that's how I'm currently opening my script. I was hoping to use this in conjunction with a .cmd file to set -ExecutionPolicy Bypass and run just the part of the script where I need elevated access using this, but I also launch that with -NoProfile, so I don't have access to the Invoke-AsAdministrator available and I can't Import-Module because I can't execute scripts.

There's probably a way to do this I'm just not seeing.

4

u/jhizzle4rizzle Apr 28 '20

I don't know how much control over the environment you have or how viable this is, but one suggestion is to create a windows service that exposes a server over some kind of socket - a named pipe like I'm using, or good ol' fashioned tcp, or user-space COM objects if that's a thing - and then connect to that with your user account in your script. This way, your server runs as administrator and your clients can connect to it using whatever out-of-band authentication system you like.

2

u/hedddie Apr 29 '20

Please do not do this as this will make your organisation vulnerable and probably end up with ransomeware or stolen data. Normal users are normal users for a reason!

2

u/jhizzle4rizzle Apr 29 '20

I should at least clarify that sending arbitrary powershell commands this way is a very very bad idea.

1

u/nostril_spiders Apr 29 '20

An ideal solution would likely work just like that but with some filtering on the server side - possibly using a pssc file, but you could simply whitelist allowed commands in the service.

This is the nature of applications, really. If I got up PayPal and authorise a payment, I don't have privileges on the web server. I've authenticated to it, and there are a set of pre-defined commands that are then enabled.

3

u/chinpokomon Apr 29 '20

Or, I change the account to an Administrator account, change the Policy, run the script I made, and set everything back... That was the approach I took. I had to restore a bunch of user files and I needed to reset the ownership after I deleted the original accounts they were associated with.

2

u/get-postanote Apr 29 '20 edited Apr 29 '20

how is this better or different than what is in the MS Powershellgallery.com?

Find-Module -Name '*sudo*' | Format-Table -AutoSize
Version Name   Repository Description
------- ----   ---------- -----------
2.1.0   Sudo   PSGallery  Use functionality similar to sudo in PowerShell. GitHub: https://github.com/pldmgg/Sudo
1.4.0   PSSudo PSGallery  Function for executing programs with adminstrative privileges

Or PowerShell packages

 Find-Package -Name '*sudo*' | Format-Table -AutoSize

Name                          Version        Source    Summary                                                                                                      
----                          -------        ------    -------                                                                                                      
Sudo                          2.1.0          PSGallery Use functionality similar to sudo in PowerShell. GitHub: https://github.com/pldmgg/Sudo                      
PSSudo                        1.4.0          PSGallery Function for executing programs with adminstrative privileges                                                
Retyped.sudo-block            1.2.6733       nuget.org Sudo Block (sudo-block) binding library for Bridge.NET projects.                                             
WQYeo.SudoMediaPlayer         1.0.0          nuget.org A .NET android Library that simplifies the process of preparing and setting up MediaPlayer.                  
SudokuSharp                   2.0.5850.28298 nuget.org A C# library for working with Sudoku puzzles                                                                 
SudokuSolverLib               1.1.1          nuget.org General Sudoku solver and creator library                                                                    
TrueMagic.SudokuGenerator.dll 1.2.0          nuget.org This project offers to generate and solve sudoku puzzle on 5 difficulty levels.                              
Janet-Sudoku                  1.1.1          nuget.org 1. Sudoku Generator - generate Sudoku puzzles using variety of different methods. 2. Sudoku Solver - Solve...
Golden.Man.Sudoku.Solver      1.0.0          nuget.org A portable and lightweight Sudoku solver implementation in C#.                                               
SimpleSudokuSolver            1.0.0          nuget.org SimpleSudokuSolver is a library for solving sudoku puzzles written in C#....                                 
StrobotSudoku                 0.9.5619.30455 nuget.org A library for working with Sudoku Boards.                                                                    
Sudoku.Solver.Lite            1.0.1          nuget.org This is a portable and lightweight Sudoku solver implementation in C#. It supports .Net Standard 1.0.        
Sudoku-Solver                 0.0.0          nuget.org Sudoko Solver + Get all Solutions of Sudoku                                                                  
SudokuSpice                   1.1.0          nuget.org An efficient sudoku solving and generating library targeting .NET Core 3.1 and .NET Standard 2.1....         
ZL.Sudoku.Lib                 1.0.0          nuget.org A simple Lib for Sudoku.

3

u/jhizzle4rizzle Apr 29 '20 edited Apr 29 '20

That second search is a bunch of sudoku solvers but go off 🙄

But I did look at existing packages. This package is in fact based on code I found in the PS Gallery! I found an unmaintained library on the gallery that did about half of what I wanted and, after contacting the owner, we came to an agreement where I renamed my module and took over their copyright and they unlisted theirs. It was their wish to be not be involved in the project going forward and I've tried to not put its origins on blast in order to respect these wishes, but this is not completely novel work - I just iterated on an already good idea.

As for the other module named "sudo", what it does is actually a different thing. I wanted to use Windows' standard permissions escalation mechanisms with Start-Process but have the output show up in my terminal (like the code I based this off), but the linked project uses CredSSP and New-PSSession to pull off something similar but distinct. Both projects hadn't seen a commit in a while and needed work, no offense to anybody involved, so I chose the one that was the closest to what I wanted and that was the easiest for me to understand.

Edit: As to what makes this better than the closest thing I found on the PS Gallery: I have a changelog that details the updates that I put into it - there are many.

3

u/zoredache Apr 29 '20

This doesn't solve my Sudoku puzzles? /s

But it is similar to all the other PS sudo tools, in that they aren't actually like sudo. Almost all of them are instead similar more similar to su. I do understand you can't really have a tool like sudo on Windows, at least not without something really complicated. Though JEA does get closer to what sudo does over all the powershell 'sudo' scripts I have seen.

2

u/jhizzle4rizzle Apr 29 '20

This JEA link is great - and yeah, the reason "like sudo" is in quotes is because that was a set of problems I 100% didn't want to be mucking around with.

2

u/get-postanote Apr 29 '20

This doesn't solve my Sudoku puzzles? /s ;-} ;-} ;-} ;-} Channeling my inner Pet Detective' https://www.youtube.com/watch?v=0tJGk4ofc18

1

u/QuarterBall Apr 28 '20

This is beautiful <3

1

u/[deleted] Apr 29 '20

why?

11

u/[deleted] Apr 28 '20

[removed] — view removed comment

8

u/jhizzle4rizzle Apr 28 '20 edited Apr 28 '20

I wonder if you could make that work with PSReadLine

Edit: Got a working snippet:

Invoke-AsAdministrator (Get-History -Count 1).CommandLine

I'll add it to the examples. Great idea!