英文:
Request UAC elevation in Go
问题
有没有办法在Go语言中将新的进程或线程提升为管理员权限?
在Go语言中似乎没有ShellExecute()
函数,并且使用CreateProcess()
函数也无法提升权限。我不知道还有什么其他可能的方法。
英文:
Is there any way to elevate a new process or thread to administrator in Go?
It seems like ShellExecute()
isn't available in Go and it isn't possible to elevate with CreateProcess()
. I have no idea what else could be possible.
答案1
得分: 4
你可以尝试查看项目w32:它有一个shell32.go ShellExecute()
函数可能会有用。
w32
是Go编程语言的Windows API包装器。它将win32 API包装成“Go风格”,使其更易于使用。
更新于2020年,自2014年以来,golang.org/x/sys/windows
包确实有一个ShellExecute()
函数。
Cliven提到了Jeremy Black的博客文章“在需要管理员权限时重新启动Windows Golang程序并提升UAC”。
-
Jeremy首先通过尝试打开
\\.\PHYSICALDRIVE0
(根据这个reddit建议)来检测程序是否已以管理员身份运行。_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
-
然后Jeremy使用
ShellExecute()
函数调用runas
(在Michael Haephrati的“运行时提升”中描述)func runMeElevated() { verb := "runas" exe, _ := os.Executable() cwd, _ := os.Getwd() args := strings.Join(os.Args[1:], " ") verbPtr, _ := syscall.UTF16PtrFromString(verb) exePtr, _ := syscall.UTF16PtrFromString(exe) cwdPtr, _ := syscall.UTF16PtrFromString(cwd) argPtr, _ := syscall.UTF16PtrFromString(args) var showCmd int32 = 1 //SW_NORMAL err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd) if err != nil { fmt.Println(err) } }
英文:
You can try and check the project w32: it has a shell32.go ShellExecute()
that could be useful.
> w32
is a wrapper of windows apis for the Go Programming Language.
>
> It wraps win32 apis to "Go style" to make them easier to use.
Update 2020, since 2014, the package golang.org/x/sys/windows
does have a ShellExecute()
function.
Cliven mentions Jeremy Black's blog post "Relaunch Windows Golang program with UAC elevation when admin rights needed"
-
Jeremy first detects if the program is already running as admin by attempting to open
\\.\PHYSICALDRIVE0
(per this reddit suggestion)_, err := os.Open("\\\\.\\PHYSICALDRIVE0")
-
Then Jeremy uses the
ShellExecute()
function, to callrunas
(described in "Elevating During Runtime" from Michael Haephrati)func runMeElevated() { verb := "runas" exe, _ := os.Executable() cwd, _ := os.Getwd() args := strings.Join(os.Args[1:], " ") verbPtr, _ := syscall.UTF16PtrFromString(verb) exePtr, _ := syscall.UTF16PtrFromString(exe) cwdPtr, _ := syscall.UTF16PtrFromString(cwd) argPtr, _ := syscall.UTF16PtrFromString(args) var showCmd int32 = 1 //SW_NORMAL err := windows.ShellExecute(0, verbPtr, exePtr, argPtr, cwdPtr, showCmd) if err != nil { fmt.Println(err) } }
答案2
得分: 0
CreateProcess()
不支持运行提升的进程,但是在以下文章中有一个第三方的CreateProcessElevated()
函数可以使用:
Vista UAC: The Definitive Guide
该文章还提供了其他几个函数的提升版本,包括CreateProcessAsUserElevated()
和ShellExecuteElevated()
。
英文:
CreateProcess()
does not support running elevated processes, but there is a third-party CreateProcessElevated()
function in the following article that can:
Vista UAC: The Definitive Guide
It also provides elevation versions of several other functions, including CreateProcessAsUserElevated()
and ShellExecuteElevated()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论