forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/golang.org/x/sys/windows/security_windows.go

2511 lines
49 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
// Copyright 2012 The Go Authors. All rights reserved.
2024-02-18 10:42:21 +00:00
// Use of this source code is governed by a BSD-style
2024-02-18 10:42:21 +00:00
// license that can be found in the LICENSE file.
package windows
import (
"syscall"
"unsafe"
)
const (
NameUnknown = 0
2024-02-18 10:42:21 +00:00
NameFullyQualifiedDN = 1
NameSamCompatible = 2
NameDisplay = 3
NameUniqueId = 6
NameCanonical = 7
NameUserPrincipal = 8
NameCanonicalEx = 9
2024-02-18 10:42:21 +00:00
NameServicePrincipal = 10
NameDnsDomain = 12
2024-02-18 10:42:21 +00:00
)
// This function returns 1 byte BOOLEAN rather than the 4 byte BOOL.
2024-02-18 10:42:21 +00:00
// http://blogs.msdn.com/b/drnick/archive/2007/12/19/windows-and-upn-format-credentials.aspx
2024-02-18 10:42:21 +00:00
//sys TranslateName(accName *uint16, accNameFormat uint32, desiredNameFormat uint32, translatedName *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.TranslateNameW
2024-02-18 10:42:21 +00:00
//sys GetUserNameEx(nameFormat uint32, nameBuffre *uint16, nSize *uint32) (err error) [failretval&0xff==0] = secur32.GetUserNameExW
// TranslateAccountName converts a directory service
2024-02-18 10:42:21 +00:00
// object name from one format to another.
2024-02-18 10:42:21 +00:00
func TranslateAccountName(username string, from, to uint32, initSize int) (string, error) {
2024-02-18 10:42:21 +00:00
u, e := UTF16PtrFromString(username)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := uint32(50)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
e = TranslateName(u, from, to, &b[0], &n)
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b[:n]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
const (
2024-02-18 10:42:21 +00:00
// do not reorder
2024-02-18 10:42:21 +00:00
NetSetupUnknownStatus = iota
2024-02-18 10:42:21 +00:00
NetSetupUnjoined
2024-02-18 10:42:21 +00:00
NetSetupWorkgroupName
2024-02-18 10:42:21 +00:00
NetSetupDomainName
)
type UserInfo10 struct {
Name *uint16
Comment *uint16
2024-02-18 10:42:21 +00:00
UsrComment *uint16
FullName *uint16
2024-02-18 10:42:21 +00:00
}
//sys NetUserGetInfo(serverName *uint16, userName *uint16, level uint32, buf **byte) (neterr error) = netapi32.NetUserGetInfo
2024-02-18 10:42:21 +00:00
//sys NetGetJoinInformation(server *uint16, name **uint16, bufType *uint32) (neterr error) = netapi32.NetGetJoinInformation
2024-02-18 10:42:21 +00:00
//sys NetApiBufferFree(buf *byte) (neterr error) = netapi32.NetApiBufferFree
const (
2024-02-18 10:42:21 +00:00
// do not reorder
2024-02-18 10:42:21 +00:00
SidTypeUser = 1 + iota
2024-02-18 10:42:21 +00:00
SidTypeGroup
2024-02-18 10:42:21 +00:00
SidTypeDomain
2024-02-18 10:42:21 +00:00
SidTypeAlias
2024-02-18 10:42:21 +00:00
SidTypeWellKnownGroup
2024-02-18 10:42:21 +00:00
SidTypeDeletedAccount
2024-02-18 10:42:21 +00:00
SidTypeInvalid
2024-02-18 10:42:21 +00:00
SidTypeUnknown
2024-02-18 10:42:21 +00:00
SidTypeComputer
2024-02-18 10:42:21 +00:00
SidTypeLabel
)
type SidIdentifierAuthority struct {
Value [6]byte
}
var (
SECURITY_NULL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 0}}
SECURITY_WORLD_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 1}}
SECURITY_LOCAL_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 2}}
SECURITY_CREATOR_SID_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 3}}
SECURITY_NON_UNIQUE_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 4}}
SECURITY_NT_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 5}}
2024-02-18 10:42:21 +00:00
SECURITY_MANDATORY_LABEL_AUTHORITY = SidIdentifierAuthority{[6]byte{0, 0, 0, 0, 0, 16}}
)
const (
SECURITY_NULL_RID = 0
SECURITY_WORLD_RID = 0
SECURITY_LOCAL_RID = 0
SECURITY_CREATOR_OWNER_RID = 0
SECURITY_CREATOR_GROUP_RID = 1
SECURITY_DIALUP_RID = 1
SECURITY_NETWORK_RID = 2
SECURITY_BATCH_RID = 3
SECURITY_INTERACTIVE_RID = 4
SECURITY_LOGON_IDS_RID = 5
SECURITY_SERVICE_RID = 6
SECURITY_LOCAL_SYSTEM_RID = 18
SECURITY_BUILTIN_DOMAIN_RID = 32
SECURITY_PRINCIPAL_SELF_RID = 10
SECURITY_CREATOR_OWNER_SERVER_RID = 0x2
SECURITY_CREATOR_GROUP_SERVER_RID = 0x3
SECURITY_LOGON_IDS_RID_COUNT = 0x3
SECURITY_ANONYMOUS_LOGON_RID = 0x7
SECURITY_PROXY_RID = 0x8
2024-02-18 10:42:21 +00:00
SECURITY_ENTERPRISE_CONTROLLERS_RID = 0x9
SECURITY_SERVER_LOGON_RID = SECURITY_ENTERPRISE_CONTROLLERS_RID
SECURITY_AUTHENTICATED_USER_RID = 0xb
SECURITY_RESTRICTED_CODE_RID = 0xc
SECURITY_NT_NON_UNIQUE_RID = 0x15
2024-02-18 10:42:21 +00:00
)
// Predefined domain-relative RIDs for local groups.
2024-02-18 10:42:21 +00:00
// See https://msdn.microsoft.com/en-us/library/windows/desktop/aa379649(v=vs.85).aspx
2024-02-18 10:42:21 +00:00
const (
DOMAIN_ALIAS_RID_ADMINS = 0x220
DOMAIN_ALIAS_RID_USERS = 0x221
DOMAIN_ALIAS_RID_GUESTS = 0x222
DOMAIN_ALIAS_RID_POWER_USERS = 0x223
DOMAIN_ALIAS_RID_ACCOUNT_OPS = 0x224
DOMAIN_ALIAS_RID_SYSTEM_OPS = 0x225
DOMAIN_ALIAS_RID_PRINT_OPS = 0x226
DOMAIN_ALIAS_RID_BACKUP_OPS = 0x227
DOMAIN_ALIAS_RID_REPLICATOR = 0x228
DOMAIN_ALIAS_RID_RAS_SERVERS = 0x229
DOMAIN_ALIAS_RID_PREW2KCOMPACCESS = 0x22a
DOMAIN_ALIAS_RID_REMOTE_DESKTOP_USERS = 0x22b
DOMAIN_ALIAS_RID_NETWORK_CONFIGURATION_OPS = 0x22c
2024-02-18 10:42:21 +00:00
DOMAIN_ALIAS_RID_INCOMING_FOREST_TRUST_BUILDERS = 0x22d
DOMAIN_ALIAS_RID_MONITORING_USERS = 0x22e
DOMAIN_ALIAS_RID_LOGGING_USERS = 0x22f
DOMAIN_ALIAS_RID_AUTHORIZATIONACCESS = 0x230
DOMAIN_ALIAS_RID_TS_LICENSE_SERVERS = 0x231
DOMAIN_ALIAS_RID_DCOM_USERS = 0x232
DOMAIN_ALIAS_RID_IUSERS = 0x238
DOMAIN_ALIAS_RID_CRYPTO_OPERATORS = 0x239
DOMAIN_ALIAS_RID_CACHEABLE_PRINCIPALS_GROUP = 0x23b
2024-02-18 10:42:21 +00:00
DOMAIN_ALIAS_RID_NON_CACHEABLE_PRINCIPALS_GROUP = 0x23c
DOMAIN_ALIAS_RID_EVENT_LOG_READERS_GROUP = 0x23d
DOMAIN_ALIAS_RID_CERTSVC_DCOM_ACCESS_GROUP = 0x23e
2024-02-18 10:42:21 +00:00
)
//sys LookupAccountSid(systemName *uint16, sid *SID, name *uint16, nameLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountSidW
2024-02-18 10:42:21 +00:00
//sys LookupAccountName(systemName *uint16, accountName *uint16, sid *SID, sidLen *uint32, refdDomainName *uint16, refdDomainNameLen *uint32, use *uint32) (err error) = advapi32.LookupAccountNameW
2024-02-18 10:42:21 +00:00
//sys ConvertSidToStringSid(sid *SID, stringSid **uint16) (err error) = advapi32.ConvertSidToStringSidW
2024-02-18 10:42:21 +00:00
//sys ConvertStringSidToSid(stringSid *uint16, sid **SID) (err error) = advapi32.ConvertStringSidToSidW
2024-02-18 10:42:21 +00:00
//sys GetLengthSid(sid *SID) (len uint32) = advapi32.GetLengthSid
2024-02-18 10:42:21 +00:00
//sys CopySid(destSidLen uint32, destSid *SID, srcSid *SID) (err error) = advapi32.CopySid
2024-02-18 10:42:21 +00:00
//sys AllocateAndInitializeSid(identAuth *SidIdentifierAuthority, subAuth byte, subAuth0 uint32, subAuth1 uint32, subAuth2 uint32, subAuth3 uint32, subAuth4 uint32, subAuth5 uint32, subAuth6 uint32, subAuth7 uint32, sid **SID) (err error) = advapi32.AllocateAndInitializeSid
2024-02-18 10:42:21 +00:00
//sys createWellKnownSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID, sid *SID, sizeSid *uint32) (err error) = advapi32.CreateWellKnownSid
2024-02-18 10:42:21 +00:00
//sys isWellKnownSid(sid *SID, sidType WELL_KNOWN_SID_TYPE) (isWellKnown bool) = advapi32.IsWellKnownSid
2024-02-18 10:42:21 +00:00
//sys FreeSid(sid *SID) (err error) [failretval!=0] = advapi32.FreeSid
2024-02-18 10:42:21 +00:00
//sys EqualSid(sid1 *SID, sid2 *SID) (isEqual bool) = advapi32.EqualSid
2024-02-18 10:42:21 +00:00
//sys getSidIdentifierAuthority(sid *SID) (authority *SidIdentifierAuthority) = advapi32.GetSidIdentifierAuthority
2024-02-18 10:42:21 +00:00
//sys getSidSubAuthorityCount(sid *SID) (count *uint8) = advapi32.GetSidSubAuthorityCount
2024-02-18 10:42:21 +00:00
//sys getSidSubAuthority(sid *SID, index uint32) (subAuthority *uint32) = advapi32.GetSidSubAuthority
2024-02-18 10:42:21 +00:00
//sys isValidSid(sid *SID) (isValid bool) = advapi32.IsValidSid
// The security identifier (SID) structure is a variable-length
2024-02-18 10:42:21 +00:00
// structure used to uniquely identify users or groups.
2024-02-18 10:42:21 +00:00
type SID struct{}
// StringToSid converts a string-format security identifier
2024-02-18 10:42:21 +00:00
// SID into a valid, functional SID.
2024-02-18 10:42:21 +00:00
func StringToSid(s string) (*SID, error) {
2024-02-18 10:42:21 +00:00
var sid *SID
2024-02-18 10:42:21 +00:00
p, e := UTF16PtrFromString(s)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
e = ConvertStringSidToSid(p, &sid)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree((Handle)(unsafe.Pointer(sid)))
2024-02-18 10:42:21 +00:00
return sid.Copy()
2024-02-18 10:42:21 +00:00
}
// LookupSID retrieves a security identifier SID for the account
2024-02-18 10:42:21 +00:00
// and the name of the domain on which the account was found.
2024-02-18 10:42:21 +00:00
// System specify target computer to search.
2024-02-18 10:42:21 +00:00
func LookupSID(system, account string) (sid *SID, domain string, accType uint32, err error) {
2024-02-18 10:42:21 +00:00
if len(account) == 0 {
2024-02-18 10:42:21 +00:00
return nil, "", 0, syscall.EINVAL
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
acc, e := UTF16PtrFromString(account)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var sys *uint16
2024-02-18 10:42:21 +00:00
if len(system) > 0 {
2024-02-18 10:42:21 +00:00
sys, e = UTF16PtrFromString(system)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := uint32(50)
2024-02-18 10:42:21 +00:00
dn := uint32(50)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]byte, n)
2024-02-18 10:42:21 +00:00
db := make([]uint16, dn)
2024-02-18 10:42:21 +00:00
sid = (*SID)(unsafe.Pointer(&b[0]))
2024-02-18 10:42:21 +00:00
e = LookupAccountName(sys, acc, sid, &n, &db[0], &dn, &accType)
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return sid, UTF16ToString(db), accType, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return nil, "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return nil, "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// String converts SID to a string format suitable for display, storage, or transmission.
2024-02-18 10:42:21 +00:00
func (sid *SID) String() string {
2024-02-18 10:42:21 +00:00
var s *uint16
2024-02-18 10:42:21 +00:00
e := ConvertSidToStringSid(sid, &s)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree((Handle)(unsafe.Pointer(s)))
2024-02-18 10:42:21 +00:00
return UTF16ToString((*[256]uint16)(unsafe.Pointer(s))[:])
2024-02-18 10:42:21 +00:00
}
// Len returns the length, in bytes, of a valid security identifier SID.
2024-02-18 10:42:21 +00:00
func (sid *SID) Len() int {
2024-02-18 10:42:21 +00:00
return int(GetLengthSid(sid))
2024-02-18 10:42:21 +00:00
}
// Copy creates a duplicate of security identifier SID.
2024-02-18 10:42:21 +00:00
func (sid *SID) Copy() (*SID, error) {
2024-02-18 10:42:21 +00:00
b := make([]byte, sid.Len())
2024-02-18 10:42:21 +00:00
sid2 := (*SID)(unsafe.Pointer(&b[0]))
2024-02-18 10:42:21 +00:00
e := CopySid(uint32(len(b)), sid2, sid)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return sid2, nil
2024-02-18 10:42:21 +00:00
}
// IdentifierAuthority returns the identifier authority of the SID.
2024-02-18 10:42:21 +00:00
func (sid *SID) IdentifierAuthority() SidIdentifierAuthority {
2024-02-18 10:42:21 +00:00
return *getSidIdentifierAuthority(sid)
2024-02-18 10:42:21 +00:00
}
// SubAuthorityCount returns the number of sub-authorities in the SID.
2024-02-18 10:42:21 +00:00
func (sid *SID) SubAuthorityCount() uint8 {
2024-02-18 10:42:21 +00:00
return *getSidSubAuthorityCount(sid)
2024-02-18 10:42:21 +00:00
}
// SubAuthority returns the sub-authority of the SID as specified by
2024-02-18 10:42:21 +00:00
// the index, which must be less than sid.SubAuthorityCount().
2024-02-18 10:42:21 +00:00
func (sid *SID) SubAuthority(idx uint32) uint32 {
2024-02-18 10:42:21 +00:00
if idx >= uint32(sid.SubAuthorityCount()) {
2024-02-18 10:42:21 +00:00
panic("sub-authority index out of range")
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return *getSidSubAuthority(sid, idx)
2024-02-18 10:42:21 +00:00
}
// IsValid returns whether the SID has a valid revision and length.
2024-02-18 10:42:21 +00:00
func (sid *SID) IsValid() bool {
2024-02-18 10:42:21 +00:00
return isValidSid(sid)
2024-02-18 10:42:21 +00:00
}
// Equals compares two SIDs for equality.
2024-02-18 10:42:21 +00:00
func (sid *SID) Equals(sid2 *SID) bool {
2024-02-18 10:42:21 +00:00
return EqualSid(sid, sid2)
2024-02-18 10:42:21 +00:00
}
// IsWellKnown determines whether the SID matches the well-known sidType.
2024-02-18 10:42:21 +00:00
func (sid *SID) IsWellKnown(sidType WELL_KNOWN_SID_TYPE) bool {
2024-02-18 10:42:21 +00:00
return isWellKnownSid(sid, sidType)
2024-02-18 10:42:21 +00:00
}
// LookupAccount retrieves the name of the account for this SID
2024-02-18 10:42:21 +00:00
// and the name of the first domain on which this SID is found.
2024-02-18 10:42:21 +00:00
// System specify target computer to search for.
2024-02-18 10:42:21 +00:00
func (sid *SID) LookupAccount(system string) (account, domain string, accType uint32, err error) {
2024-02-18 10:42:21 +00:00
var sys *uint16
2024-02-18 10:42:21 +00:00
if len(system) > 0 {
2024-02-18 10:42:21 +00:00
sys, err = UTF16PtrFromString(system)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return "", "", 0, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n := uint32(50)
2024-02-18 10:42:21 +00:00
dn := uint32(50)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
db := make([]uint16, dn)
2024-02-18 10:42:21 +00:00
e := LookupAccountSid(sys, sid, &b[0], &n, &db[0], &dn, &accType)
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b), UTF16ToString(db), accType, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return "", "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return "", "", 0, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// Various types of pre-specified SIDs that can be synthesized and compared at runtime.
2024-02-18 10:42:21 +00:00
type WELL_KNOWN_SID_TYPE uint32
const (
WinNullSid = 0
2024-02-18 10:42:21 +00:00
WinWorldSid = 1
WinLocalSid = 2
WinCreatorOwnerSid = 3
WinCreatorGroupSid = 4
WinCreatorOwnerServerSid = 5
WinCreatorGroupServerSid = 6
WinNtAuthoritySid = 7
WinDialupSid = 8
WinNetworkSid = 9
WinBatchSid = 10
WinInteractiveSid = 11
WinServiceSid = 12
WinAnonymousSid = 13
WinProxySid = 14
WinEnterpriseControllersSid = 15
WinSelfSid = 16
WinAuthenticatedUserSid = 17
WinRestrictedCodeSid = 18
WinTerminalServerSid = 19
WinRemoteLogonIdSid = 20
WinLogonIdsSid = 21
WinLocalSystemSid = 22
WinLocalServiceSid = 23
WinNetworkServiceSid = 24
WinBuiltinDomainSid = 25
WinBuiltinAdministratorsSid = 26
WinBuiltinUsersSid = 27
WinBuiltinGuestsSid = 28
WinBuiltinPowerUsersSid = 29
WinBuiltinAccountOperatorsSid = 30
WinBuiltinSystemOperatorsSid = 31
WinBuiltinPrintOperatorsSid = 32
WinBuiltinBackupOperatorsSid = 33
WinBuiltinReplicatorSid = 34
WinBuiltinPreWindows2000CompatibleAccessSid = 35
WinBuiltinRemoteDesktopUsersSid = 36
WinBuiltinNetworkConfigurationOperatorsSid = 37
WinAccountAdministratorSid = 38
WinAccountGuestSid = 39
WinAccountKrbtgtSid = 40
WinAccountDomainAdminsSid = 41
WinAccountDomainUsersSid = 42
WinAccountDomainGuestsSid = 43
WinAccountComputersSid = 44
WinAccountControllersSid = 45
WinAccountCertAdminsSid = 46
WinAccountSchemaAdminsSid = 47
WinAccountEnterpriseAdminsSid = 48
WinAccountPolicyAdminsSid = 49
WinAccountRasAndIasServersSid = 50
WinNTLMAuthenticationSid = 51
WinDigestAuthenticationSid = 52
WinSChannelAuthenticationSid = 53
WinThisOrganizationSid = 54
WinOtherOrganizationSid = 55
WinBuiltinIncomingForestTrustBuildersSid = 56
WinBuiltinPerfMonitoringUsersSid = 57
WinBuiltinPerfLoggingUsersSid = 58
WinBuiltinAuthorizationAccessSid = 59
WinBuiltinTerminalServerLicenseServersSid = 60
WinBuiltinDCOMUsersSid = 61
WinBuiltinIUsersSid = 62
WinIUserSid = 63
WinBuiltinCryptoOperatorsSid = 64
WinUntrustedLabelSid = 65
WinLowLabelSid = 66
WinMediumLabelSid = 67
WinHighLabelSid = 68
WinSystemLabelSid = 69
WinWriteRestrictedCodeSid = 70
WinCreatorOwnerRightsSid = 71
WinCacheablePrincipalsGroupSid = 72
WinNonCacheablePrincipalsGroupSid = 73
WinEnterpriseReadonlyControllersSid = 74
WinAccountReadonlyControllersSid = 75
WinBuiltinEventLogReadersGroup = 76
WinNewEnterpriseReadonlyControllersSid = 77
WinBuiltinCertSvcDComAccessGroup = 78
WinMediumPlusLabelSid = 79
WinLocalLogonSid = 80
WinConsoleLogonSid = 81
WinThisOrganizationCertificateSid = 82
WinApplicationPackageAuthoritySid = 83
WinBuiltinAnyPackageSid = 84
WinCapabilityInternetClientSid = 85
WinCapabilityInternetClientServerSid = 86
WinCapabilityPrivateNetworkClientServerSid = 87
WinCapabilityPicturesLibrarySid = 88
WinCapabilityVideosLibrarySid = 89
WinCapabilityMusicLibrarySid = 90
WinCapabilityDocumentsLibrarySid = 91
WinCapabilitySharedUserCertificatesSid = 92
WinCapabilityEnterpriseAuthenticationSid = 93
WinCapabilityRemovableStorageSid = 94
WinBuiltinRDSRemoteAccessServersSid = 95
WinBuiltinRDSEndpointServersSid = 96
WinBuiltinRDSManagementServersSid = 97
WinUserModeDriversSid = 98
WinBuiltinHyperVAdminsSid = 99
WinAccountCloneableControllersSid = 100
WinBuiltinAccessControlAssistanceOperatorsSid = 101
WinBuiltinRemoteManagementUsersSid = 102
WinAuthenticationAuthorityAssertedSid = 103
WinAuthenticationServiceAssertedSid = 104
WinLocalAccountSid = 105
WinLocalAccountAndAdministratorSid = 106
WinAccountProtectedUsersSid = 107
WinCapabilityAppointmentsSid = 108
WinCapabilityContactsSid = 109
WinAccountDefaultSystemManagedSid = 110
WinBuiltinDefaultSystemManagedGroupSid = 111
WinBuiltinStorageReplicaAdminsSid = 112
WinAccountKeyAdminsSid = 113
WinAccountEnterpriseKeyAdminsSid = 114
WinAuthenticationKeyTrustSid = 115
WinAuthenticationKeyPropertyMFASid = 116
WinAuthenticationKeyPropertyAttestationSid = 117
WinAuthenticationFreshKeyAuthSid = 118
WinBuiltinDeviceOwnersSid = 119
)
// Creates a SID for a well-known predefined alias, generally using the constants of the form
// Win*Sid, for the local machine.
func CreateWellKnownSid(sidType WELL_KNOWN_SID_TYPE) (*SID, error) {
return CreateWellKnownDomainSid(sidType, nil)
}
// Creates a SID for a well-known predefined alias, generally using the constants of the form
// Win*Sid, for the domain specified by the domainSid parameter.
2024-02-18 10:42:21 +00:00
func CreateWellKnownDomainSid(sidType WELL_KNOWN_SID_TYPE, domainSid *SID) (*SID, error) {
2024-02-18 10:42:21 +00:00
n := uint32(50)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]byte, n)
2024-02-18 10:42:21 +00:00
sid := (*SID)(unsafe.Pointer(&b[0]))
2024-02-18 10:42:21 +00:00
err := createWellKnownSid(sidType, domainSid, sid, &n)
2024-02-18 10:42:21 +00:00
if err == nil {
2024-02-18 10:42:21 +00:00
return sid, nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if err != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
const (
2024-02-18 10:42:21 +00:00
// do not reorder
2024-02-18 10:42:21 +00:00
TOKEN_ASSIGN_PRIMARY = 1 << iota
2024-02-18 10:42:21 +00:00
TOKEN_DUPLICATE
2024-02-18 10:42:21 +00:00
TOKEN_IMPERSONATE
2024-02-18 10:42:21 +00:00
TOKEN_QUERY
2024-02-18 10:42:21 +00:00
TOKEN_QUERY_SOURCE
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_PRIVILEGES
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_GROUPS
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_DEFAULT
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_SESSIONID
TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
2024-02-18 10:42:21 +00:00
TOKEN_ASSIGN_PRIMARY |
2024-02-18 10:42:21 +00:00
TOKEN_DUPLICATE |
2024-02-18 10:42:21 +00:00
TOKEN_IMPERSONATE |
2024-02-18 10:42:21 +00:00
TOKEN_QUERY |
2024-02-18 10:42:21 +00:00
TOKEN_QUERY_SOURCE |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_PRIVILEGES |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_GROUPS |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_DEFAULT |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_SESSIONID
TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY
2024-02-18 10:42:21 +00:00
TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_PRIVILEGES |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_GROUPS |
2024-02-18 10:42:21 +00:00
TOKEN_ADJUST_DEFAULT
2024-02-18 10:42:21 +00:00
TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
)
const (
2024-02-18 10:42:21 +00:00
// do not reorder
2024-02-18 10:42:21 +00:00
TokenUser = 1 + iota
2024-02-18 10:42:21 +00:00
TokenGroups
2024-02-18 10:42:21 +00:00
TokenPrivileges
2024-02-18 10:42:21 +00:00
TokenOwner
2024-02-18 10:42:21 +00:00
TokenPrimaryGroup
2024-02-18 10:42:21 +00:00
TokenDefaultDacl
2024-02-18 10:42:21 +00:00
TokenSource
2024-02-18 10:42:21 +00:00
TokenType
2024-02-18 10:42:21 +00:00
TokenImpersonationLevel
2024-02-18 10:42:21 +00:00
TokenStatistics
2024-02-18 10:42:21 +00:00
TokenRestrictedSids
2024-02-18 10:42:21 +00:00
TokenSessionId
2024-02-18 10:42:21 +00:00
TokenGroupsAndPrivileges
2024-02-18 10:42:21 +00:00
TokenSessionReference
2024-02-18 10:42:21 +00:00
TokenSandBoxInert
2024-02-18 10:42:21 +00:00
TokenAuditPolicy
2024-02-18 10:42:21 +00:00
TokenOrigin
2024-02-18 10:42:21 +00:00
TokenElevationType
2024-02-18 10:42:21 +00:00
TokenLinkedToken
2024-02-18 10:42:21 +00:00
TokenElevation
2024-02-18 10:42:21 +00:00
TokenHasRestrictions
2024-02-18 10:42:21 +00:00
TokenAccessInformation
2024-02-18 10:42:21 +00:00
TokenVirtualizationAllowed
2024-02-18 10:42:21 +00:00
TokenVirtualizationEnabled
2024-02-18 10:42:21 +00:00
TokenIntegrityLevel
2024-02-18 10:42:21 +00:00
TokenUIAccess
2024-02-18 10:42:21 +00:00
TokenMandatoryPolicy
2024-02-18 10:42:21 +00:00
TokenLogonSid
2024-02-18 10:42:21 +00:00
MaxTokenInfoClass
)
// Group attributes inside of Tokengroups.Groups[i].Attributes
2024-02-18 10:42:21 +00:00
const (
SE_GROUP_MANDATORY = 0x00000001
2024-02-18 10:42:21 +00:00
SE_GROUP_ENABLED_BY_DEFAULT = 0x00000002
SE_GROUP_ENABLED = 0x00000004
SE_GROUP_OWNER = 0x00000008
SE_GROUP_USE_FOR_DENY_ONLY = 0x00000010
SE_GROUP_INTEGRITY = 0x00000020
SE_GROUP_INTEGRITY_ENABLED = 0x00000040
SE_GROUP_LOGON_ID = 0xC0000000
SE_GROUP_RESOURCE = 0x20000000
SE_GROUP_VALID_ATTRIBUTES = SE_GROUP_MANDATORY | SE_GROUP_ENABLED_BY_DEFAULT | SE_GROUP_ENABLED | SE_GROUP_OWNER | SE_GROUP_USE_FOR_DENY_ONLY | SE_GROUP_LOGON_ID | SE_GROUP_RESOURCE | SE_GROUP_INTEGRITY | SE_GROUP_INTEGRITY_ENABLED
2024-02-18 10:42:21 +00:00
)
// Privilege attributes
2024-02-18 10:42:21 +00:00
const (
SE_PRIVILEGE_ENABLED_BY_DEFAULT = 0x00000001
SE_PRIVILEGE_ENABLED = 0x00000002
SE_PRIVILEGE_REMOVED = 0x00000004
SE_PRIVILEGE_USED_FOR_ACCESS = 0x80000000
SE_PRIVILEGE_VALID_ATTRIBUTES = SE_PRIVILEGE_ENABLED_BY_DEFAULT | SE_PRIVILEGE_ENABLED | SE_PRIVILEGE_REMOVED | SE_PRIVILEGE_USED_FOR_ACCESS
2024-02-18 10:42:21 +00:00
)
// Token types
2024-02-18 10:42:21 +00:00
const (
TokenPrimary = 1
2024-02-18 10:42:21 +00:00
TokenImpersonation = 2
)
// Impersonation levels
2024-02-18 10:42:21 +00:00
const (
SecurityAnonymous = 0
2024-02-18 10:42:21 +00:00
SecurityIdentification = 1
SecurityImpersonation = 2
SecurityDelegation = 3
2024-02-18 10:42:21 +00:00
)
type LUID struct {
LowPart uint32
2024-02-18 10:42:21 +00:00
HighPart int32
}
type LUIDAndAttributes struct {
Luid LUID
2024-02-18 10:42:21 +00:00
Attributes uint32
}
type SIDAndAttributes struct {
Sid *SID
2024-02-18 10:42:21 +00:00
Attributes uint32
}
type Tokenuser struct {
User SIDAndAttributes
}
type Tokenprimarygroup struct {
PrimaryGroup *SID
}
type Tokengroups struct {
GroupCount uint32
Groups [1]SIDAndAttributes // Use AllGroups() for iterating.
2024-02-18 10:42:21 +00:00
}
// AllGroups returns a slice that can be used to iterate over the groups in g.
2024-02-18 10:42:21 +00:00
func (g *Tokengroups) AllGroups() []SIDAndAttributes {
2024-02-18 10:42:21 +00:00
return (*[(1 << 28) - 1]SIDAndAttributes)(unsafe.Pointer(&g.Groups[0]))[:g.GroupCount:g.GroupCount]
2024-02-18 10:42:21 +00:00
}
type Tokenprivileges struct {
PrivilegeCount uint32
Privileges [1]LUIDAndAttributes // Use AllPrivileges() for iterating.
2024-02-18 10:42:21 +00:00
}
// AllPrivileges returns a slice that can be used to iterate over the privileges in p.
2024-02-18 10:42:21 +00:00
func (p *Tokenprivileges) AllPrivileges() []LUIDAndAttributes {
2024-02-18 10:42:21 +00:00
return (*[(1 << 27) - 1]LUIDAndAttributes)(unsafe.Pointer(&p.Privileges[0]))[:p.PrivilegeCount:p.PrivilegeCount]
2024-02-18 10:42:21 +00:00
}
type Tokenmandatorylabel struct {
Label SIDAndAttributes
}
func (tml *Tokenmandatorylabel) Size() uint32 {
2024-02-18 10:42:21 +00:00
return uint32(unsafe.Sizeof(Tokenmandatorylabel{})) + GetLengthSid(tml.Label.Sid)
2024-02-18 10:42:21 +00:00
}
// Authorization Functions
2024-02-18 10:42:21 +00:00
//sys checkTokenMembership(tokenHandle Token, sidToCheck *SID, isMember *int32) (err error) = advapi32.CheckTokenMembership
2024-02-18 10:42:21 +00:00
//sys isTokenRestricted(tokenHandle Token) (ret bool, err error) [!failretval] = advapi32.IsTokenRestricted
2024-02-18 10:42:21 +00:00
//sys OpenProcessToken(process Handle, access uint32, token *Token) (err error) = advapi32.OpenProcessToken
2024-02-18 10:42:21 +00:00
//sys OpenThreadToken(thread Handle, access uint32, openAsSelf bool, token *Token) (err error) = advapi32.OpenThreadToken
2024-02-18 10:42:21 +00:00
//sys ImpersonateSelf(impersonationlevel uint32) (err error) = advapi32.ImpersonateSelf
2024-02-18 10:42:21 +00:00
//sys RevertToSelf() (err error) = advapi32.RevertToSelf
2024-02-18 10:42:21 +00:00
//sys SetThreadToken(thread *Handle, token Token) (err error) = advapi32.SetThreadToken
2024-02-18 10:42:21 +00:00
//sys LookupPrivilegeValue(systemname *uint16, name *uint16, luid *LUID) (err error) = advapi32.LookupPrivilegeValueW
2024-02-18 10:42:21 +00:00
//sys AdjustTokenPrivileges(token Token, disableAllPrivileges bool, newstate *Tokenprivileges, buflen uint32, prevstate *Tokenprivileges, returnlen *uint32) (err error) = advapi32.AdjustTokenPrivileges
2024-02-18 10:42:21 +00:00
//sys AdjustTokenGroups(token Token, resetToDefault bool, newstate *Tokengroups, buflen uint32, prevstate *Tokengroups, returnlen *uint32) (err error) = advapi32.AdjustTokenGroups
2024-02-18 10:42:21 +00:00
//sys GetTokenInformation(token Token, infoClass uint32, info *byte, infoLen uint32, returnedLen *uint32) (err error) = advapi32.GetTokenInformation
2024-02-18 10:42:21 +00:00
//sys SetTokenInformation(token Token, infoClass uint32, info *byte, infoLen uint32) (err error) = advapi32.SetTokenInformation
2024-02-18 10:42:21 +00:00
//sys DuplicateTokenEx(existingToken Token, desiredAccess uint32, tokenAttributes *SecurityAttributes, impersonationLevel uint32, tokenType uint32, newToken *Token) (err error) = advapi32.DuplicateTokenEx
2024-02-18 10:42:21 +00:00
//sys GetUserProfileDirectory(t Token, dir *uint16, dirLen *uint32) (err error) = userenv.GetUserProfileDirectoryW
2024-02-18 10:42:21 +00:00
//sys getSystemDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemDirectoryW
2024-02-18 10:42:21 +00:00
//sys getWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetWindowsDirectoryW
2024-02-18 10:42:21 +00:00
//sys getSystemWindowsDirectory(dir *uint16, dirLen uint32) (len uint32, err error) = kernel32.GetSystemWindowsDirectoryW
// An access token contains the security information for a logon session.
2024-02-18 10:42:21 +00:00
// The system creates an access token when a user logs on, and every
2024-02-18 10:42:21 +00:00
// process executed on behalf of the user has a copy of the token.
2024-02-18 10:42:21 +00:00
// The token identifies the user, the user's groups, and the user's
2024-02-18 10:42:21 +00:00
// privileges. The system uses the token to control access to securable
2024-02-18 10:42:21 +00:00
// objects and to control the ability of the user to perform various
2024-02-18 10:42:21 +00:00
// system-related operations on the local computer.
2024-02-18 10:42:21 +00:00
type Token Handle
// OpenCurrentProcessToken opens an access token associated with current
2024-02-18 10:42:21 +00:00
// process with TOKEN_QUERY access. It is a real token that needs to be closed.
2024-02-18 10:42:21 +00:00
//
2024-02-18 10:42:21 +00:00
// Deprecated: Explicitly call OpenProcessToken(CurrentProcess(), ...)
2024-02-18 10:42:21 +00:00
// with the desired access instead, or use GetCurrentProcessToken for a
2024-02-18 10:42:21 +00:00
// TOKEN_QUERY token.
2024-02-18 10:42:21 +00:00
func OpenCurrentProcessToken() (Token, error) {
2024-02-18 10:42:21 +00:00
var token Token
2024-02-18 10:42:21 +00:00
err := OpenProcessToken(CurrentProcess(), TOKEN_QUERY, &token)
2024-02-18 10:42:21 +00:00
return token, err
2024-02-18 10:42:21 +00:00
}
// GetCurrentProcessToken returns the access token associated with
2024-02-18 10:42:21 +00:00
// the current process. It is a pseudo token that does not need
2024-02-18 10:42:21 +00:00
// to be closed.
2024-02-18 10:42:21 +00:00
func GetCurrentProcessToken() Token {
2024-02-18 10:42:21 +00:00
return Token(^uintptr(4 - 1))
2024-02-18 10:42:21 +00:00
}
// GetCurrentThreadToken return the access token associated with
2024-02-18 10:42:21 +00:00
// the current thread. It is a pseudo token that does not need
2024-02-18 10:42:21 +00:00
// to be closed.
2024-02-18 10:42:21 +00:00
func GetCurrentThreadToken() Token {
2024-02-18 10:42:21 +00:00
return Token(^uintptr(5 - 1))
2024-02-18 10:42:21 +00:00
}
// GetCurrentThreadEffectiveToken returns the effective access token
2024-02-18 10:42:21 +00:00
// associated with the current thread. It is a pseudo token that does
2024-02-18 10:42:21 +00:00
// not need to be closed.
2024-02-18 10:42:21 +00:00
func GetCurrentThreadEffectiveToken() Token {
2024-02-18 10:42:21 +00:00
return Token(^uintptr(6 - 1))
2024-02-18 10:42:21 +00:00
}
// Close releases access to access token.
2024-02-18 10:42:21 +00:00
func (t Token) Close() error {
2024-02-18 10:42:21 +00:00
return CloseHandle(Handle(t))
2024-02-18 10:42:21 +00:00
}
// getInfo retrieves a specified type of information about an access token.
2024-02-18 10:42:21 +00:00
func (t Token) getInfo(class uint32, initSize int) (unsafe.Pointer, error) {
2024-02-18 10:42:21 +00:00
n := uint32(initSize)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]byte, n)
2024-02-18 10:42:21 +00:00
e := GetTokenInformation(t, class, &b[0], uint32(len(b)), &n)
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return unsafe.Pointer(&b[0]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// GetTokenUser retrieves access token t user account information.
2024-02-18 10:42:21 +00:00
func (t Token) GetTokenUser() (*Tokenuser, error) {
2024-02-18 10:42:21 +00:00
i, e := t.getInfo(TokenUser, 50)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return (*Tokenuser)(i), nil
2024-02-18 10:42:21 +00:00
}
// GetTokenGroups retrieves group accounts associated with access token t.
2024-02-18 10:42:21 +00:00
func (t Token) GetTokenGroups() (*Tokengroups, error) {
2024-02-18 10:42:21 +00:00
i, e := t.getInfo(TokenGroups, 50)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return (*Tokengroups)(i), nil
2024-02-18 10:42:21 +00:00
}
// GetTokenPrimaryGroup retrieves access token t primary group information.
2024-02-18 10:42:21 +00:00
// A pointer to a SID structure representing a group that will become
2024-02-18 10:42:21 +00:00
// the primary group of any objects created by a process using this access token.
2024-02-18 10:42:21 +00:00
func (t Token) GetTokenPrimaryGroup() (*Tokenprimarygroup, error) {
2024-02-18 10:42:21 +00:00
i, e := t.getInfo(TokenPrimaryGroup, 50)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return nil, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return (*Tokenprimarygroup)(i), nil
2024-02-18 10:42:21 +00:00
}
// GetUserProfileDirectory retrieves path to the
2024-02-18 10:42:21 +00:00
// root directory of the access token t user's profile.
2024-02-18 10:42:21 +00:00
func (t Token) GetUserProfileDirectory() (string, error) {
2024-02-18 10:42:21 +00:00
n := uint32(100)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
e := GetUserProfileDirectory(t, &b[0], &n)
2024-02-18 10:42:21 +00:00
if e == nil {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if e != ERROR_INSUFFICIENT_BUFFER {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if n <= uint32(len(b)) {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// IsElevated returns whether the current token is elevated from a UAC perspective.
2024-02-18 10:42:21 +00:00
func (token Token) IsElevated() bool {
2024-02-18 10:42:21 +00:00
var isElevated uint32
2024-02-18 10:42:21 +00:00
var outLen uint32
2024-02-18 10:42:21 +00:00
err := GetTokenInformation(token, TokenElevation, (*byte)(unsafe.Pointer(&isElevated)), uint32(unsafe.Sizeof(isElevated)), &outLen)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return false
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return outLen == uint32(unsafe.Sizeof(isElevated)) && isElevated != 0
2024-02-18 10:42:21 +00:00
}
// GetLinkedToken returns the linked token, which may be an elevated UAC token.
2024-02-18 10:42:21 +00:00
func (token Token) GetLinkedToken() (Token, error) {
2024-02-18 10:42:21 +00:00
var linkedToken Token
2024-02-18 10:42:21 +00:00
var outLen uint32
2024-02-18 10:42:21 +00:00
err := GetTokenInformation(token, TokenLinkedToken, (*byte)(unsafe.Pointer(&linkedToken)), uint32(unsafe.Sizeof(linkedToken)), &outLen)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return Token(0), err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return linkedToken, nil
2024-02-18 10:42:21 +00:00
}
// GetSystemDirectory retrieves the path to current location of the system
2024-02-18 10:42:21 +00:00
// directory, which is typically, though not always, `C:\Windows\System32`.
2024-02-18 10:42:21 +00:00
func GetSystemDirectory() (string, error) {
2024-02-18 10:42:21 +00:00
n := uint32(MAX_PATH)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
l, e := getSystemDirectory(&b[0], n)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if l <= n {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b[:l]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n = l
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// GetWindowsDirectory retrieves the path to current location of the Windows
2024-02-18 10:42:21 +00:00
// directory, which is typically, though not always, `C:\Windows`. This may
2024-02-18 10:42:21 +00:00
// be a private user directory in the case that the application is running
2024-02-18 10:42:21 +00:00
// under a terminal server.
2024-02-18 10:42:21 +00:00
func GetWindowsDirectory() (string, error) {
2024-02-18 10:42:21 +00:00
n := uint32(MAX_PATH)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
l, e := getWindowsDirectory(&b[0], n)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if l <= n {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b[:l]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n = l
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// GetSystemWindowsDirectory retrieves the path to current location of the
2024-02-18 10:42:21 +00:00
// Windows directory, which is typically, though not always, `C:\Windows`.
2024-02-18 10:42:21 +00:00
func GetSystemWindowsDirectory() (string, error) {
2024-02-18 10:42:21 +00:00
n := uint32(MAX_PATH)
2024-02-18 10:42:21 +00:00
for {
2024-02-18 10:42:21 +00:00
b := make([]uint16, n)
2024-02-18 10:42:21 +00:00
l, e := getSystemWindowsDirectory(&b[0], n)
2024-02-18 10:42:21 +00:00
if e != nil {
2024-02-18 10:42:21 +00:00
return "", e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if l <= n {
2024-02-18 10:42:21 +00:00
return UTF16ToString(b[:l]), nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
n = l
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
}
// IsMember reports whether the access token t is a member of the provided SID.
2024-02-18 10:42:21 +00:00
func (t Token) IsMember(sid *SID) (bool, error) {
2024-02-18 10:42:21 +00:00
var b int32
2024-02-18 10:42:21 +00:00
if e := checkTokenMembership(t, sid, &b); e != nil {
2024-02-18 10:42:21 +00:00
return false, e
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return b != 0, nil
2024-02-18 10:42:21 +00:00
}
// IsRestricted reports whether the access token t is a restricted token.
2024-02-18 10:42:21 +00:00
func (t Token) IsRestricted() (isRestricted bool, err error) {
2024-02-18 10:42:21 +00:00
isRestricted, err = isTokenRestricted(t)
2024-02-18 10:42:21 +00:00
if !isRestricted && err == syscall.EINVAL {
2024-02-18 10:42:21 +00:00
// If err is EINVAL, this returned ERROR_SUCCESS indicating a non-restricted token.
2024-02-18 10:42:21 +00:00
err = nil
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
const (
WTS_CONSOLE_CONNECT = 0x1
WTS_CONSOLE_DISCONNECT = 0x2
WTS_REMOTE_CONNECT = 0x3
WTS_REMOTE_DISCONNECT = 0x4
WTS_SESSION_LOGON = 0x5
WTS_SESSION_LOGOFF = 0x6
WTS_SESSION_LOCK = 0x7
WTS_SESSION_UNLOCK = 0x8
2024-02-18 10:42:21 +00:00
WTS_SESSION_REMOTE_CONTROL = 0x9
WTS_SESSION_CREATE = 0xa
WTS_SESSION_TERMINATE = 0xb
2024-02-18 10:42:21 +00:00
)
const (
WTSActive = 0
WTSConnected = 1
2024-02-18 10:42:21 +00:00
WTSConnectQuery = 2
WTSShadow = 3
2024-02-18 10:42:21 +00:00
WTSDisconnected = 4
WTSIdle = 5
WTSListen = 6
WTSReset = 7
WTSDown = 8
WTSInit = 9
2024-02-18 10:42:21 +00:00
)
type WTSSESSION_NOTIFICATION struct {
Size uint32
2024-02-18 10:42:21 +00:00
SessionID uint32
}
type WTS_SESSION_INFO struct {
SessionID uint32
2024-02-18 10:42:21 +00:00
WindowStationName *uint16
State uint32
2024-02-18 10:42:21 +00:00
}
//sys WTSQueryUserToken(session uint32, token *Token) (err error) = wtsapi32.WTSQueryUserToken
2024-02-18 10:42:21 +00:00
//sys WTSEnumerateSessions(handle Handle, reserved uint32, version uint32, sessions **WTS_SESSION_INFO, count *uint32) (err error) = wtsapi32.WTSEnumerateSessionsW
2024-02-18 10:42:21 +00:00
//sys WTSFreeMemory(ptr uintptr) = wtsapi32.WTSFreeMemory
2024-02-18 10:42:21 +00:00
//sys WTSGetActiveConsoleSessionId() (sessionID uint32)
type ACL struct {
aclRevision byte
sbz1 byte
aclSize uint16
aceCount uint16
sbz2 uint16
2024-02-18 10:42:21 +00:00
}
type SECURITY_DESCRIPTOR struct {
revision byte
sbz1 byte
control SECURITY_DESCRIPTOR_CONTROL
owner *SID
group *SID
sacl *ACL
dacl *ACL
2024-02-18 10:42:21 +00:00
}
type SECURITY_QUALITY_OF_SERVICE struct {
Length uint32
ImpersonationLevel uint32
2024-02-18 10:42:21 +00:00
ContextTrackingMode byte
EffectiveOnly byte
2024-02-18 10:42:21 +00:00
}
// Constants for the ContextTrackingMode field of SECURITY_QUALITY_OF_SERVICE.
2024-02-18 10:42:21 +00:00
const (
SECURITY_STATIC_TRACKING = 0
2024-02-18 10:42:21 +00:00
SECURITY_DYNAMIC_TRACKING = 1
)
type SecurityAttributes struct {
Length uint32
2024-02-18 10:42:21 +00:00
SecurityDescriptor *SECURITY_DESCRIPTOR
InheritHandle uint32
2024-02-18 10:42:21 +00:00
}
type SE_OBJECT_TYPE uint32
// Constants for type SE_OBJECT_TYPE
2024-02-18 10:42:21 +00:00
const (
SE_UNKNOWN_OBJECT_TYPE = 0
SE_FILE_OBJECT = 1
SE_SERVICE = 2
SE_PRINTER = 3
SE_REGISTRY_KEY = 4
SE_LMSHARE = 5
SE_KERNEL_OBJECT = 6
SE_WINDOW_OBJECT = 7
SE_DS_OBJECT = 8
SE_DS_OBJECT_ALL = 9
2024-02-18 10:42:21 +00:00
SE_PROVIDER_DEFINED_OBJECT = 10
SE_WMIGUID_OBJECT = 11
SE_REGISTRY_WOW64_32KEY = 12
SE_REGISTRY_WOW64_64KEY = 13
2024-02-18 10:42:21 +00:00
)
type SECURITY_INFORMATION uint32
// Constants for type SECURITY_INFORMATION
2024-02-18 10:42:21 +00:00
const (
OWNER_SECURITY_INFORMATION = 0x00000001
GROUP_SECURITY_INFORMATION = 0x00000002
DACL_SECURITY_INFORMATION = 0x00000004
SACL_SECURITY_INFORMATION = 0x00000008
LABEL_SECURITY_INFORMATION = 0x00000010
ATTRIBUTE_SECURITY_INFORMATION = 0x00000020
SCOPE_SECURITY_INFORMATION = 0x00000040
BACKUP_SECURITY_INFORMATION = 0x00010000
PROTECTED_DACL_SECURITY_INFORMATION = 0x80000000
PROTECTED_SACL_SECURITY_INFORMATION = 0x40000000
2024-02-18 10:42:21 +00:00
UNPROTECTED_DACL_SECURITY_INFORMATION = 0x20000000
2024-02-18 10:42:21 +00:00
UNPROTECTED_SACL_SECURITY_INFORMATION = 0x10000000
)
type SECURITY_DESCRIPTOR_CONTROL uint16
// Constants for type SECURITY_DESCRIPTOR_CONTROL
2024-02-18 10:42:21 +00:00
const (
SE_OWNER_DEFAULTED = 0x0001
SE_GROUP_DEFAULTED = 0x0002
SE_DACL_PRESENT = 0x0004
SE_DACL_DEFAULTED = 0x0008
SE_SACL_PRESENT = 0x0010
SE_SACL_DEFAULTED = 0x0020
2024-02-18 10:42:21 +00:00
SE_DACL_AUTO_INHERIT_REQ = 0x0100
2024-02-18 10:42:21 +00:00
SE_SACL_AUTO_INHERIT_REQ = 0x0200
SE_DACL_AUTO_INHERITED = 0x0400
SE_SACL_AUTO_INHERITED = 0x0800
SE_DACL_PROTECTED = 0x1000
SE_SACL_PROTECTED = 0x2000
SE_RM_CONTROL_VALID = 0x4000
SE_SELF_RELATIVE = 0x8000
2024-02-18 10:42:21 +00:00
)
type ACCESS_MASK uint32
// Constants for type ACCESS_MASK
2024-02-18 10:42:21 +00:00
const (
DELETE = 0x00010000
READ_CONTROL = 0x00020000
WRITE_DAC = 0x00040000
WRITE_OWNER = 0x00080000
SYNCHRONIZE = 0x00100000
2024-02-18 10:42:21 +00:00
STANDARD_RIGHTS_REQUIRED = 0x000F0000
STANDARD_RIGHTS_READ = READ_CONTROL
STANDARD_RIGHTS_WRITE = READ_CONTROL
STANDARD_RIGHTS_EXECUTE = READ_CONTROL
STANDARD_RIGHTS_ALL = 0x001F0000
SPECIFIC_RIGHTS_ALL = 0x0000FFFF
ACCESS_SYSTEM_SECURITY = 0x01000000
MAXIMUM_ALLOWED = 0x02000000
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
GENERIC_EXECUTE = 0x20000000
GENERIC_ALL = 0x10000000
2024-02-18 10:42:21 +00:00
)
type ACCESS_MODE uint32
// Constants for type ACCESS_MODE
2024-02-18 10:42:21 +00:00
const (
NOT_USED_ACCESS = 0
GRANT_ACCESS = 1
SET_ACCESS = 2
DENY_ACCESS = 3
REVOKE_ACCESS = 4
2024-02-18 10:42:21 +00:00
SET_AUDIT_SUCCESS = 5
2024-02-18 10:42:21 +00:00
SET_AUDIT_FAILURE = 6
)
// Constants for AceFlags and Inheritance fields
2024-02-18 10:42:21 +00:00
const (
NO_INHERITANCE = 0x0
SUB_OBJECTS_ONLY_INHERIT = 0x1
SUB_CONTAINERS_ONLY_INHERIT = 0x2
2024-02-18 10:42:21 +00:00
SUB_CONTAINERS_AND_OBJECTS_INHERIT = 0x3
INHERIT_NO_PROPAGATE = 0x4
INHERIT_ONLY = 0x8
INHERITED_ACCESS_ENTRY = 0x10
INHERITED_PARENT = 0x10000000
INHERITED_GRANDPARENT = 0x20000000
OBJECT_INHERIT_ACE = 0x1
CONTAINER_INHERIT_ACE = 0x2
NO_PROPAGATE_INHERIT_ACE = 0x4
INHERIT_ONLY_ACE = 0x8
INHERITED_ACE = 0x10
VALID_INHERIT_FLAGS = 0x1F
2024-02-18 10:42:21 +00:00
)
type MULTIPLE_TRUSTEE_OPERATION uint32
// Constants for MULTIPLE_TRUSTEE_OPERATION
2024-02-18 10:42:21 +00:00
const (
NO_MULTIPLE_TRUSTEE = 0
2024-02-18 10:42:21 +00:00
TRUSTEE_IS_IMPERSONATE = 1
)
type TRUSTEE_FORM uint32
// Constants for TRUSTEE_FORM
2024-02-18 10:42:21 +00:00
const (
TRUSTEE_IS_SID = 0
TRUSTEE_IS_NAME = 1
TRUSTEE_BAD_FORM = 2
TRUSTEE_IS_OBJECTS_AND_SID = 3
2024-02-18 10:42:21 +00:00
TRUSTEE_IS_OBJECTS_AND_NAME = 4
)
type TRUSTEE_TYPE uint32
// Constants for TRUSTEE_TYPE
2024-02-18 10:42:21 +00:00
const (
TRUSTEE_IS_UNKNOWN = 0
TRUSTEE_IS_USER = 1
TRUSTEE_IS_GROUP = 2
TRUSTEE_IS_DOMAIN = 3
TRUSTEE_IS_ALIAS = 4
2024-02-18 10:42:21 +00:00
TRUSTEE_IS_WELL_KNOWN_GROUP = 5
TRUSTEE_IS_DELETED = 6
TRUSTEE_IS_INVALID = 7
TRUSTEE_IS_COMPUTER = 8
2024-02-18 10:42:21 +00:00
)
// Constants for ObjectsPresent field
2024-02-18 10:42:21 +00:00
const (
ACE_OBJECT_TYPE_PRESENT = 0x1
2024-02-18 10:42:21 +00:00
ACE_INHERITED_OBJECT_TYPE_PRESENT = 0x2
)
type EXPLICIT_ACCESS struct {
AccessPermissions ACCESS_MASK
AccessMode ACCESS_MODE
Inheritance uint32
Trustee TRUSTEE
2024-02-18 10:42:21 +00:00
}
// This type is the union inside of TRUSTEE and must be created using one of the TrusteeValueFrom* functions.
2024-02-18 10:42:21 +00:00
type TrusteeValue uintptr
func TrusteeValueFromString(str string) TrusteeValue {
2024-02-18 10:42:21 +00:00
return TrusteeValue(unsafe.Pointer(StringToUTF16Ptr(str)))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
func TrusteeValueFromSID(sid *SID) TrusteeValue {
2024-02-18 10:42:21 +00:00
return TrusteeValue(unsafe.Pointer(sid))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
func TrusteeValueFromObjectsAndSid(objectsAndSid *OBJECTS_AND_SID) TrusteeValue {
2024-02-18 10:42:21 +00:00
return TrusteeValue(unsafe.Pointer(objectsAndSid))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
func TrusteeValueFromObjectsAndName(objectsAndName *OBJECTS_AND_NAME) TrusteeValue {
2024-02-18 10:42:21 +00:00
return TrusteeValue(unsafe.Pointer(objectsAndName))
2024-02-18 10:42:21 +00:00
}
type TRUSTEE struct {
MultipleTrustee *TRUSTEE
2024-02-18 10:42:21 +00:00
MultipleTrusteeOperation MULTIPLE_TRUSTEE_OPERATION
TrusteeForm TRUSTEE_FORM
TrusteeType TRUSTEE_TYPE
TrusteeValue TrusteeValue
2024-02-18 10:42:21 +00:00
}
type OBJECTS_AND_SID struct {
ObjectsPresent uint32
ObjectTypeGuid GUID
2024-02-18 10:42:21 +00:00
InheritedObjectTypeGuid GUID
Sid *SID
2024-02-18 10:42:21 +00:00
}
type OBJECTS_AND_NAME struct {
ObjectsPresent uint32
ObjectType SE_OBJECT_TYPE
ObjectTypeName *uint16
2024-02-18 10:42:21 +00:00
InheritedObjectTypeName *uint16
Name *uint16
2024-02-18 10:42:21 +00:00
}
//sys getSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner **SID, group **SID, dacl **ACL, sacl **ACL, sd **SECURITY_DESCRIPTOR) (ret error) = advapi32.GetSecurityInfo
2024-02-18 10:42:21 +00:00
//sys SetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) (ret error) = advapi32.SetSecurityInfo
2024-02-18 10:42:21 +00:00
//sys getNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner **SID, group **SID, dacl **ACL, sacl **ACL, sd **SECURITY_DESCRIPTOR) (ret error) = advapi32.GetNamedSecurityInfoW
2024-02-18 10:42:21 +00:00
//sys SetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION, owner *SID, group *SID, dacl *ACL, sacl *ACL) (ret error) = advapi32.SetNamedSecurityInfoW
2024-02-18 10:42:21 +00:00
//sys SetKernelObjectSecurity(handle Handle, securityInformation SECURITY_INFORMATION, securityDescriptor *SECURITY_DESCRIPTOR) (err error) = advapi32.SetKernelObjectSecurity
//sys buildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, countAccessEntries uint32, accessEntries *EXPLICIT_ACCESS, countAuditEntries uint32, auditEntries *EXPLICIT_ACCESS, oldSecurityDescriptor *SECURITY_DESCRIPTOR, sizeNewSecurityDescriptor *uint32, newSecurityDescriptor **SECURITY_DESCRIPTOR) (ret error) = advapi32.BuildSecurityDescriptorW
2024-02-18 10:42:21 +00:00
//sys initializeSecurityDescriptor(absoluteSD *SECURITY_DESCRIPTOR, revision uint32) (err error) = advapi32.InitializeSecurityDescriptor
//sys getSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, control *SECURITY_DESCRIPTOR_CONTROL, revision *uint32) (err error) = advapi32.GetSecurityDescriptorControl
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent *bool, dacl **ACL, daclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorDacl
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent *bool, sacl **ACL, saclDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorSacl
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner **SID, ownerDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorOwner
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group **SID, groupDefaulted *bool) (err error) = advapi32.GetSecurityDescriptorGroup
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorLength(sd *SECURITY_DESCRIPTOR) (len uint32) = advapi32.GetSecurityDescriptorLength
2024-02-18 10:42:21 +00:00
//sys getSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) (ret error) [failretval!=0] = advapi32.GetSecurityDescriptorRMControl
2024-02-18 10:42:21 +00:00
//sys isValidSecurityDescriptor(sd *SECURITY_DESCRIPTOR) (isValid bool) = advapi32.IsValidSecurityDescriptor
//sys setSecurityDescriptorControl(sd *SECURITY_DESCRIPTOR, controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) (err error) = advapi32.SetSecurityDescriptorControl
2024-02-18 10:42:21 +00:00
//sys setSecurityDescriptorDacl(sd *SECURITY_DESCRIPTOR, daclPresent bool, dacl *ACL, daclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorDacl
2024-02-18 10:42:21 +00:00
//sys setSecurityDescriptorSacl(sd *SECURITY_DESCRIPTOR, saclPresent bool, sacl *ACL, saclDefaulted bool) (err error) = advapi32.SetSecurityDescriptorSacl
2024-02-18 10:42:21 +00:00
//sys setSecurityDescriptorOwner(sd *SECURITY_DESCRIPTOR, owner *SID, ownerDefaulted bool) (err error) = advapi32.SetSecurityDescriptorOwner
2024-02-18 10:42:21 +00:00
//sys setSecurityDescriptorGroup(sd *SECURITY_DESCRIPTOR, group *SID, groupDefaulted bool) (err error) = advapi32.SetSecurityDescriptorGroup
2024-02-18 10:42:21 +00:00
//sys setSecurityDescriptorRMControl(sd *SECURITY_DESCRIPTOR, rmControl *uint8) = advapi32.SetSecurityDescriptorRMControl
//sys convertStringSecurityDescriptorToSecurityDescriptor(str string, revision uint32, sd **SECURITY_DESCRIPTOR, size *uint32) (err error) = advapi32.ConvertStringSecurityDescriptorToSecurityDescriptorW
2024-02-18 10:42:21 +00:00
//sys convertSecurityDescriptorToStringSecurityDescriptor(sd *SECURITY_DESCRIPTOR, revision uint32, securityInformation SECURITY_INFORMATION, str **uint16, strLen *uint32) (err error) = advapi32.ConvertSecurityDescriptorToStringSecurityDescriptorW
//sys makeAbsoluteSD(selfRelativeSD *SECURITY_DESCRIPTOR, absoluteSD *SECURITY_DESCRIPTOR, absoluteSDSize *uint32, dacl *ACL, daclSize *uint32, sacl *ACL, saclSize *uint32, owner *SID, ownerSize *uint32, group *SID, groupSize *uint32) (err error) = advapi32.MakeAbsoluteSD
2024-02-18 10:42:21 +00:00
//sys makeSelfRelativeSD(absoluteSD *SECURITY_DESCRIPTOR, selfRelativeSD *SECURITY_DESCRIPTOR, selfRelativeSDSize *uint32) (err error) = advapi32.MakeSelfRelativeSD
//sys setEntriesInAcl(countExplicitEntries uint32, explicitEntries *EXPLICIT_ACCESS, oldACL *ACL, newACL **ACL) (ret error) = advapi32.SetEntriesInAclW
// Control returns the security descriptor control bits.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) Control() (control SECURITY_DESCRIPTOR_CONTROL, revision uint32, err error) {
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorControl(sd, &control, &revision)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetControl sets the security descriptor control bits.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) SetControl(controlBitsOfInterest SECURITY_DESCRIPTOR_CONTROL, controlBitsToSet SECURITY_DESCRIPTOR_CONTROL) error {
2024-02-18 10:42:21 +00:00
return setSecurityDescriptorControl(sd, controlBitsOfInterest, controlBitsToSet)
2024-02-18 10:42:21 +00:00
}
// RMControl returns the security descriptor resource manager control bits.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) RMControl() (control uint8, err error) {
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorRMControl(sd, &control)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetRMControl sets the security descriptor resource manager control bits.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) SetRMControl(rmControl uint8) {
2024-02-18 10:42:21 +00:00
setSecurityDescriptorRMControl(sd, &rmControl)
2024-02-18 10:42:21 +00:00
}
// DACL returns the security descriptor DACL and whether it was defaulted. The dacl return value may be nil
2024-02-18 10:42:21 +00:00
// if a DACL exists but is an "empty DACL", meaning fully permissive. If the DACL does not exist, err returns
2024-02-18 10:42:21 +00:00
// ERROR_OBJECT_NOT_FOUND.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) DACL() (dacl *ACL, defaulted bool, err error) {
2024-02-18 10:42:21 +00:00
var present bool
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorDacl(sd, &present, &dacl, &defaulted)
2024-02-18 10:42:21 +00:00
if !present {
2024-02-18 10:42:21 +00:00
err = ERROR_OBJECT_NOT_FOUND
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetDACL sets the absolute security descriptor DACL.
2024-02-18 10:42:21 +00:00
func (absoluteSD *SECURITY_DESCRIPTOR) SetDACL(dacl *ACL, present, defaulted bool) error {
2024-02-18 10:42:21 +00:00
return setSecurityDescriptorDacl(absoluteSD, present, dacl, defaulted)
2024-02-18 10:42:21 +00:00
}
// SACL returns the security descriptor SACL and whether it was defaulted. The sacl return value may be nil
2024-02-18 10:42:21 +00:00
// if a SACL exists but is an "empty SACL", meaning fully permissive. If the SACL does not exist, err returns
2024-02-18 10:42:21 +00:00
// ERROR_OBJECT_NOT_FOUND.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) SACL() (sacl *ACL, defaulted bool, err error) {
2024-02-18 10:42:21 +00:00
var present bool
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorSacl(sd, &present, &sacl, &defaulted)
2024-02-18 10:42:21 +00:00
if !present {
2024-02-18 10:42:21 +00:00
err = ERROR_OBJECT_NOT_FOUND
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetSACL sets the absolute security descriptor SACL.
2024-02-18 10:42:21 +00:00
func (absoluteSD *SECURITY_DESCRIPTOR) SetSACL(sacl *ACL, present, defaulted bool) error {
2024-02-18 10:42:21 +00:00
return setSecurityDescriptorSacl(absoluteSD, present, sacl, defaulted)
2024-02-18 10:42:21 +00:00
}
// Owner returns the security descriptor owner and whether it was defaulted.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) Owner() (owner *SID, defaulted bool, err error) {
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorOwner(sd, &owner, &defaulted)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetOwner sets the absolute security descriptor owner.
2024-02-18 10:42:21 +00:00
func (absoluteSD *SECURITY_DESCRIPTOR) SetOwner(owner *SID, defaulted bool) error {
2024-02-18 10:42:21 +00:00
return setSecurityDescriptorOwner(absoluteSD, owner, defaulted)
2024-02-18 10:42:21 +00:00
}
// Group returns the security descriptor group and whether it was defaulted.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) Group() (group *SID, defaulted bool, err error) {
2024-02-18 10:42:21 +00:00
err = getSecurityDescriptorGroup(sd, &group, &defaulted)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// SetGroup sets the absolute security descriptor owner.
2024-02-18 10:42:21 +00:00
func (absoluteSD *SECURITY_DESCRIPTOR) SetGroup(group *SID, defaulted bool) error {
2024-02-18 10:42:21 +00:00
return setSecurityDescriptorGroup(absoluteSD, group, defaulted)
2024-02-18 10:42:21 +00:00
}
// Length returns the length of the security descriptor.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) Length() uint32 {
2024-02-18 10:42:21 +00:00
return getSecurityDescriptorLength(sd)
2024-02-18 10:42:21 +00:00
}
// IsValid returns whether the security descriptor is valid.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) IsValid() bool {
2024-02-18 10:42:21 +00:00
return isValidSecurityDescriptor(sd)
2024-02-18 10:42:21 +00:00
}
// String returns the SDDL form of the security descriptor, with a function signature that can be
2024-02-18 10:42:21 +00:00
// used with %v formatting directives.
2024-02-18 10:42:21 +00:00
func (sd *SECURITY_DESCRIPTOR) String() string {
2024-02-18 10:42:21 +00:00
var sddl *uint16
2024-02-18 10:42:21 +00:00
err := convertSecurityDescriptorToStringSecurityDescriptor(sd, 1, 0xff, &sddl, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return ""
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(sddl)))
2024-02-18 10:42:21 +00:00
return UTF16PtrToString(sddl)
2024-02-18 10:42:21 +00:00
}
// ToAbsolute converts a self-relative security descriptor into an absolute one.
2024-02-18 10:42:21 +00:00
func (selfRelativeSD *SECURITY_DESCRIPTOR) ToAbsolute() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
control, _, err := selfRelativeSD.Control()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if control&SE_SELF_RELATIVE == 0 {
2024-02-18 10:42:21 +00:00
err = ERROR_INVALID_PARAMETER
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var absoluteSDSize, daclSize, saclSize, ownerSize, groupSize uint32
2024-02-18 10:42:21 +00:00
err = makeAbsoluteSD(selfRelativeSD, nil, &absoluteSDSize,
2024-02-18 10:42:21 +00:00
nil, &daclSize, nil, &saclSize, nil, &ownerSize, nil, &groupSize)
2024-02-18 10:42:21 +00:00
switch err {
2024-02-18 10:42:21 +00:00
case ERROR_INSUFFICIENT_BUFFER:
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
// makeAbsoluteSD is expected to fail, but it succeeds.
2024-02-18 10:42:21 +00:00
return nil, ERROR_INTERNAL_ERROR
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if absoluteSDSize > 0 {
2024-02-18 10:42:21 +00:00
absoluteSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, absoluteSDSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var (
dacl *ACL
sacl *ACL
2024-02-18 10:42:21 +00:00
owner *SID
2024-02-18 10:42:21 +00:00
group *SID
)
2024-02-18 10:42:21 +00:00
if daclSize > 0 {
2024-02-18 10:42:21 +00:00
dacl = (*ACL)(unsafe.Pointer(&make([]byte, daclSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if saclSize > 0 {
2024-02-18 10:42:21 +00:00
sacl = (*ACL)(unsafe.Pointer(&make([]byte, saclSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if ownerSize > 0 {
2024-02-18 10:42:21 +00:00
owner = (*SID)(unsafe.Pointer(&make([]byte, ownerSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if groupSize > 0 {
2024-02-18 10:42:21 +00:00
group = (*SID)(unsafe.Pointer(&make([]byte, groupSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = makeAbsoluteSD(selfRelativeSD, absoluteSD, &absoluteSDSize,
2024-02-18 10:42:21 +00:00
dacl, &daclSize, sacl, &saclSize, owner, &ownerSize, group, &groupSize)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// ToSelfRelative converts an absolute security descriptor into a self-relative one.
2024-02-18 10:42:21 +00:00
func (absoluteSD *SECURITY_DESCRIPTOR) ToSelfRelative() (selfRelativeSD *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
control, _, err := absoluteSD.Control()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if control&SE_SELF_RELATIVE != 0 {
2024-02-18 10:42:21 +00:00
err = ERROR_INVALID_PARAMETER
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var selfRelativeSDSize uint32
2024-02-18 10:42:21 +00:00
err = makeSelfRelativeSD(absoluteSD, nil, &selfRelativeSDSize)
2024-02-18 10:42:21 +00:00
switch err {
2024-02-18 10:42:21 +00:00
case ERROR_INSUFFICIENT_BUFFER:
2024-02-18 10:42:21 +00:00
case nil:
2024-02-18 10:42:21 +00:00
// makeSelfRelativeSD is expected to fail, but it succeeds.
2024-02-18 10:42:21 +00:00
return nil, ERROR_INTERNAL_ERROR
2024-02-18 10:42:21 +00:00
default:
2024-02-18 10:42:21 +00:00
return nil, err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
if selfRelativeSDSize > 0 {
2024-02-18 10:42:21 +00:00
selfRelativeSD = (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&make([]byte, selfRelativeSDSize)[0]))
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = makeSelfRelativeSD(absoluteSD, selfRelativeSD, &selfRelativeSDSize)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
func (selfRelativeSD *SECURITY_DESCRIPTOR) copySelfRelativeSecurityDescriptor() *SECURITY_DESCRIPTOR {
2024-02-18 10:42:21 +00:00
sdLen := int(selfRelativeSD.Length())
2024-02-18 10:42:21 +00:00
const min = int(unsafe.Sizeof(SECURITY_DESCRIPTOR{}))
2024-02-18 10:42:21 +00:00
if sdLen < min {
2024-02-18 10:42:21 +00:00
sdLen = min
2024-02-18 10:42:21 +00:00
}
src := unsafe.Slice((*byte)(unsafe.Pointer(selfRelativeSD)), sdLen)
2024-02-18 10:42:21 +00:00
// SECURITY_DESCRIPTOR has pointers in it, which means checkptr expects for it to
2024-02-18 10:42:21 +00:00
// be aligned properly. When we're copying a Windows-allocated struct to a
2024-02-18 10:42:21 +00:00
// Go-allocated one, make sure that the Go allocation is aligned to the
2024-02-18 10:42:21 +00:00
// pointer size.
2024-02-18 10:42:21 +00:00
const psize = int(unsafe.Sizeof(uintptr(0)))
2024-02-18 10:42:21 +00:00
alloc := make([]uintptr, (sdLen+psize-1)/psize)
2024-02-18 10:42:21 +00:00
dst := unsafe.Slice((*byte)(unsafe.Pointer(&alloc[0])), sdLen)
2024-02-18 10:42:21 +00:00
copy(dst, src)
2024-02-18 10:42:21 +00:00
return (*SECURITY_DESCRIPTOR)(unsafe.Pointer(&dst[0]))
2024-02-18 10:42:21 +00:00
}
// SecurityDescriptorFromString converts an SDDL string describing a security descriptor into a
2024-02-18 10:42:21 +00:00
// self-relative security descriptor object allocated on the Go heap.
2024-02-18 10:42:21 +00:00
func SecurityDescriptorFromString(sddl string) (sd *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
var winHeapSD *SECURITY_DESCRIPTOR
2024-02-18 10:42:21 +00:00
err = convertStringSecurityDescriptorToSecurityDescriptor(sddl, 1, &winHeapSD, nil)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
2024-02-18 10:42:21 +00:00
return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
2024-02-18 10:42:21 +00:00
}
// GetSecurityInfo queries the security information for a given handle and returns the self-relative security
2024-02-18 10:42:21 +00:00
// descriptor result on the Go heap.
2024-02-18 10:42:21 +00:00
func GetSecurityInfo(handle Handle, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
var winHeapSD *SECURITY_DESCRIPTOR
2024-02-18 10:42:21 +00:00
err = getSecurityInfo(handle, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
2024-02-18 10:42:21 +00:00
return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
2024-02-18 10:42:21 +00:00
}
// GetNamedSecurityInfo queries the security information for a given named object and returns the self-relative security
2024-02-18 10:42:21 +00:00
// descriptor result on the Go heap.
2024-02-18 10:42:21 +00:00
func GetNamedSecurityInfo(objectName string, objectType SE_OBJECT_TYPE, securityInformation SECURITY_INFORMATION) (sd *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
var winHeapSD *SECURITY_DESCRIPTOR
2024-02-18 10:42:21 +00:00
err = getNamedSecurityInfo(objectName, objectType, securityInformation, nil, nil, nil, nil, &winHeapSD)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
2024-02-18 10:42:21 +00:00
return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
2024-02-18 10:42:21 +00:00
}
// BuildSecurityDescriptor makes a new security descriptor using the input trustees, explicit access lists, and
2024-02-18 10:42:21 +00:00
// prior security descriptor to be merged, any of which can be nil, returning the self-relative security descriptor
2024-02-18 10:42:21 +00:00
// result on the Go heap.
2024-02-18 10:42:21 +00:00
func BuildSecurityDescriptor(owner *TRUSTEE, group *TRUSTEE, accessEntries []EXPLICIT_ACCESS, auditEntries []EXPLICIT_ACCESS, mergedSecurityDescriptor *SECURITY_DESCRIPTOR) (sd *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
var winHeapSD *SECURITY_DESCRIPTOR
2024-02-18 10:42:21 +00:00
var winHeapSDSize uint32
2024-02-18 10:42:21 +00:00
var firstAccessEntry *EXPLICIT_ACCESS
2024-02-18 10:42:21 +00:00
if len(accessEntries) > 0 {
2024-02-18 10:42:21 +00:00
firstAccessEntry = &accessEntries[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var firstAuditEntry *EXPLICIT_ACCESS
2024-02-18 10:42:21 +00:00
if len(auditEntries) > 0 {
2024-02-18 10:42:21 +00:00
firstAuditEntry = &auditEntries[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
err = buildSecurityDescriptor(owner, group, uint32(len(accessEntries)), firstAccessEntry, uint32(len(auditEntries)), firstAuditEntry, mergedSecurityDescriptor, &winHeapSDSize, &winHeapSD)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(winHeapSD)))
2024-02-18 10:42:21 +00:00
return winHeapSD.copySelfRelativeSecurityDescriptor(), nil
2024-02-18 10:42:21 +00:00
}
// NewSecurityDescriptor creates and initializes a new absolute security descriptor.
2024-02-18 10:42:21 +00:00
func NewSecurityDescriptor() (absoluteSD *SECURITY_DESCRIPTOR, err error) {
2024-02-18 10:42:21 +00:00
absoluteSD = &SECURITY_DESCRIPTOR{}
2024-02-18 10:42:21 +00:00
err = initializeSecurityDescriptor(absoluteSD, 1)
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
// ACLFromEntries returns a new ACL on the Go heap containing a list of explicit entries as well as those of another ACL.
2024-02-18 10:42:21 +00:00
// Both explicitEntries and mergedACL are optional and can be nil.
2024-02-18 10:42:21 +00:00
func ACLFromEntries(explicitEntries []EXPLICIT_ACCESS, mergedACL *ACL) (acl *ACL, err error) {
2024-02-18 10:42:21 +00:00
var firstExplicitEntry *EXPLICIT_ACCESS
2024-02-18 10:42:21 +00:00
if len(explicitEntries) > 0 {
2024-02-18 10:42:21 +00:00
firstExplicitEntry = &explicitEntries[0]
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
var winHeapACL *ACL
2024-02-18 10:42:21 +00:00
err = setEntriesInAcl(uint32(len(explicitEntries)), firstExplicitEntry, mergedACL, &winHeapACL)
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
defer LocalFree(Handle(unsafe.Pointer(winHeapACL)))
2024-02-18 10:42:21 +00:00
aclBytes := make([]byte, winHeapACL.aclSize)
2024-02-18 10:42:21 +00:00
copy(aclBytes, (*[(1 << 31) - 1]byte)(unsafe.Pointer(winHeapACL))[:len(aclBytes):len(aclBytes)])
2024-02-18 10:42:21 +00:00
return (*ACL)(unsafe.Pointer(&aclBytes[0])), nil
2024-02-18 10:42:21 +00:00
}