IsElevated reports whether the current process has the Administrators group in its effective token — i.e. WinDivertOpen will succeed (modulo driver state). We check via CheckTokenMembership against the well-known SECURITY_BUILTIN_DOMAIN_RID \ DOMAIN_ALIAS_RID_ADMINS SID. This is the standard Win32
()
| 43 | // standard Win32 pattern and works for both Run-as-admin sessions and |
| 44 | // services running under LocalSystem. |
| 45 | func IsElevated() (bool, error) { |
| 46 | var sid *windows.SID |
| 47 | err := windows.AllocateAndInitializeSid( |
| 48 | &windows.SECURITY_NT_AUTHORITY, |
| 49 | 2, |
| 50 | windows.SECURITY_BUILTIN_DOMAIN_RID, |
| 51 | windows.DOMAIN_ALIAS_RID_ADMINS, |
| 52 | 0, 0, 0, 0, 0, 0, |
| 53 | &sid, |
| 54 | ) |
| 55 | if err != nil { |
| 56 | return false, err |
| 57 | } |
| 58 | defer windows.FreeSid(sid) |
| 59 | |
| 60 | // Passing a zero token uses the impersonation token of the calling thread. |
| 61 | return checkTokenMembership(0, sid) |
| 62 | } |
no test coverage detected