Documentation ¶
Overview ¶
Package winsudo is a sudo "like" framework for dealing with Windows UAC. credit: https://gist.github.com/jerblack/d0eb182cc5a1c1d92d92a4c4fcc416c6
I later came across this PowerShell script: https://github.com/lukesampson/psutils/blob/master/sudo.ps1
I even tried to use the FreeConsole() & AttachConsole(pid) methods from kernel32.dll but I couldn't make it work in the same way as the PowerShell, hence this gRPC approach.
Also intresting: https://stackoverflow.com/questions/62759757
I guess the advantage of using this over the PowerShell script is the performance / latency. Starting PowerShell isn't exactly fast. Maybe one day I'll do some benchmarks.
This also allows you to run any arbitrary go code in an elevated environment so thats something I guess.
Index ¶
- func Elevate(config *ElevateConfig) (err error)
- func ElevatedExec(cmd string, decorators ...func(*exec.Cmd) error) (exitCode int, err error)
- func ElevatedFork(setup func(*grpc.Server) error, action func(*grpc.ClientConn)) (err error)
- func IsElevated() bool
- func StripParentArg(args []string) []string
- type ElevateConfig
- type ElevatedForkResults
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Elevate ¶
func Elevate(config *ElevateConfig) (err error)
Elevate is a wrapper around the lower level "ShellExecute" function.
To relaunch the tool as Admin with a UAC prompt, I used the ShellExecute function in the golang.org/x/sys/windows package, using the "runas" verb that I learned about from here: https://bit.ly/31u4Iw1
func ElevatedExec ¶
ElevatedExec wraps ElevatedFork to allow you to execute any executable in an elevated context. It uses the same API as "github.com/brad-jones/goexec/v2".
For example usage refer to "cmd/sudo/main.go".
func ElevatedFork ¶
ElevatedFork wraps Elevate to provide a fork-like pattern.
ShellExecute does not provide a way to connect to the underlying process in the normal manner, via STDIN/STDOUT/STDERR. So we use GRPC to communicate between the unprivileged parent process and the privilaged child process.
The first function allows you to register a custom grpc server.
The second function will be run by the privilaged child process and is passed a grpc client connection. You can then initate your grpc client with that connection.
For example usage refer to the ElevatedExec function.
func IsElevated ¶
func IsElevated() bool
IsElevated returns true if the current process is privilaged otherwise false.
I found this post on Reddit that recommended attempting to os.Open \\.\PHYSICALDRIVE0 which is not something that is virtualized, and this worked well for my purpose.
func StripParentArg ¶
StripParentArg removes the "--winsudoParent <port>" arguments from the given string slice, usually os.Args.
Types ¶
type ElevateConfig ¶
type ElevateConfig struct { // The path to the executable to run in an elevated environment Exe string // The arguments to pass to the executable Args []string // The initial working directory for the new process Cwd string // If true then a Window will not be displayed on the // desktop even if the application is a GUI. Hidden bool }
type ElevatedForkResults ¶
type ElevatedForkResults struct { // An exit code from the privilaged child process ExitCode int }