-
Notifications
You must be signed in to change notification settings - Fork 18.1k
x/sys/windows: check for admin #28804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Just after writing this issue I've got the idea what about using Closing this, sorry for the inconvenience. |
Would you mind sharing the updated code sample that works? I don't quite know enough about windows yet to understand which part of the code you're implying that you changed. |
Solution// +build windows
package main
import (
"fmt"
"log"
"golang.org/x/sys/windows"
)
func main() {
var sid *windows.SID
// Although this looks scary, it is directly copied from the
// official windows documentation. The Go API for this is a
// direct wrap around the official C++ API.
// See https://docs.microsoft.com/en-us/windows/desktop/api/securitybaseapi/nf-securitybaseapi-checktokenmembership
err := windows.AllocateAndInitializeSid(
&windows.SECURITY_NT_AUTHORITY,
2,
windows.SECURITY_BUILTIN_DOMAIN_RID,
windows.DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0,
&sid)
if err != nil {
log.Fatalf("SID Error: %s", err)
return
}
defer windows.FreeSid(sid)
// This appears to cast a null pointer so I'm not sure why this
// works, but this guy says it does and it Works for Me™:
// https://github.com/golang/go/issues/28804#issuecomment-438838144
token := windows.Token(0)
member, err := token.IsMember(sid)
if err != nil {
log.Fatalf("Token Membership Error: %s", err)
return
}
// Also note that an admin is _not_ necessarily considered
// elevated.
// For elevation see https://github.com/mozey/run-as-admin
fmt.Println("Elevated?", token.IsElevated())
fmt.Println("Admin?", member)
} See Also |
…or windows shell. Why: the old way of accessing "\\\\.\\PHYSICALDRIVE0" may not work on every system, because.. what if I don't have that folder? (clearly my case :D ) Ref for the new way: golang/go#28804
You're forgetting a |
Seeing as how this issue is google's 2nd hit for "windows FreeSid golang"... I'm guessing I'm not the only one. Thanks for the tip. Not used to having to |
…ows 2004 Checking if opening \\\\.\\PHYSICALDRIVE0 does not work on Windows 2004. See golang/go#28804 and https://docs.microsoft.com/en-us/windows/win32/api/securitybaseapi/nf-securitybaseapi-checktokenmembership for more details.
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
Microsoft provides an example to check if the current process has admin rights here. I've transformed this example to go:
What did you expect to see?
Showing if the user has admin rights.
What did you see instead?
Solution
Output:
2018/11/14 22:58:13 false
For what I found CurrentProcessToken does not have the rights to check against this "admin" SID. I've tried to use
ImpersonateSelf()
in combination withOpenCurrentProcessToken()
which results in the same error.ImpersonateSelf()
in combination withOpenCurrentThreadToken()
allows to execute the membership check. Should this feature be supported withinx/sys/windows
or is is intended to be solved by the user in it's program? If this is accepted to be inx/sys/windows
I'm happy to prepare a CL.The text was updated successfully, but these errors were encountered: