-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[client] Derive Windows SSH privilege checks from the token and group membership #6966
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
Open
lixmal
wants to merge
5
commits into
main
Choose a base branch
from
ssh-windows-privilege-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c4bc479
Detect Windows privilege from the token and group membership instead …
lixmal 3c745a8
Fail closed when a local group name cannot be resolved to a SID
lixmal 6f8dd82
Identify the Administrators group by the name resolved from its well-…
lixmal c4fec48
Resolve test account names from well-known SIDs and RIDs instead of E…
lixmal 7aa9395
Require an S4U comparison and check elevation against the token eleva…
lixmal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| //go:build !windows | ||
|
|
||
| package server | ||
|
|
||
| // isProcessElevated is only meaningful on Windows; other platforms use the | ||
| // effective UID check in isCurrentProcessPrivileged. | ||
| func isProcessElevated() bool { | ||
| return false | ||
| } | ||
|
|
||
| // isWindowsAccountPrivileged is only reachable on Windows. Fail closed if it | ||
| // is ever called on another platform. | ||
| func isWindowsAccountPrivileged(string) bool { | ||
| return true | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,231 @@ | ||
| //go:build windows | ||
|
|
||
| package server | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
| "unsafe" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
||
| var ( | ||
| netapi32 = windows.NewLazySystemDLL("netapi32.dll") | ||
| procNetUserGetLocalGroups = netapi32.NewProc("NetUserGetLocalGroups") | ||
|
|
||
| // lookupGroupSID resolves a group name to its SID, indirected for testing. | ||
| lookupGroupSID = func(name string) (*windows.SID, error) { | ||
| sid, _, _, err := windows.LookupSID("", name) | ||
| return sid, err | ||
| } | ||
| ) | ||
|
|
||
| const ( | ||
| // lgIncludeIndirect makes NetUserGetLocalGroups also return local groups | ||
| // the user belongs to through a global group. | ||
| lgIncludeIndirect = 0x1 | ||
| maxPreferredLength = 0xFFFFFFFF | ||
| ) | ||
|
|
||
| // localGroupUsersInfo0 mirrors LOCALGROUP_USERS_INFO_0. | ||
| type localGroupUsersInfo0 struct { | ||
| name *uint16 | ||
| } | ||
|
|
||
| // isProcessElevated reports whether the current process token is elevated | ||
| // (TokenElevation): true for elevated administrators, the built-in | ||
| // Administrator, administrators with UAC disabled, and SYSTEM; false for | ||
| // standard users and administrators running with a UAC-filtered token. | ||
| func isProcessElevated() bool { | ||
| return windows.GetCurrentProcessToken().IsElevated() | ||
| } | ||
|
|
||
| // isWindowsAccountPrivileged reports whether the account is privileged on this | ||
| // machine: a well-known service account, a built-in Administrator (RID 500), | ||
| // or a member of the local Administrators group, directly or through nested | ||
| // groups. Evaluation errors count as privileged so policy checks fail closed. | ||
| func isWindowsAccountPrivileged(username string) bool { | ||
| sid, _, _, err := windows.LookupSID("", username) | ||
| if err != nil { | ||
| log.Warnf("privilege check: SID lookup for %q failed, treating as privileged: %v", username, err) | ||
| return true | ||
| } | ||
|
|
||
| if isPrivilegedUserSID(sid) { | ||
| return true | ||
| } | ||
|
|
||
| member, err := isLocalAdminsMember(username) | ||
| if err != nil { | ||
| log.Warnf("privilege check: cannot determine Administrators membership for %q, treating as privileged: %v", username, err) | ||
| return true | ||
| } | ||
| return member | ||
| } | ||
|
|
||
| // isPrivilegedUserSID reports whether the SID itself identifies a privileged | ||
| // principal, without consulting group membership. | ||
| func isPrivilegedUserSID(sid *windows.SID) bool { | ||
| wellKnown := []windows.WELL_KNOWN_SID_TYPE{ | ||
| windows.WinLocalSystemSid, | ||
| windows.WinLocalServiceSid, | ||
| windows.WinNetworkServiceSid, | ||
| windows.WinBuiltinAdministratorsSid, | ||
| } | ||
| for _, sidType := range wellKnown { | ||
| if sid.IsWellKnown(sidType) { | ||
| return true | ||
| } | ||
| } | ||
| return isBuiltinAdministratorSID(sid) | ||
| } | ||
|
|
||
| // isBuiltinAdministratorSID reports whether the SID is a machine or domain | ||
| // built-in Administrator account (S-1-5-21-...-500). RID 500 is reserved for | ||
| // that account; it can be renamed but cannot be removed from the | ||
| // Administrators group. | ||
| func isBuiltinAdministratorSID(sid *windows.SID) bool { | ||
| if sid.IdentifierAuthority() != windows.SECURITY_NT_AUTHORITY { | ||
| return false | ||
| } | ||
| count := sid.SubAuthorityCount() | ||
| if count < 2 || sid.SubAuthority(0) != 21 { | ||
| return false | ||
| } | ||
| return sid.SubAuthority(uint32(count-1)) == 500 | ||
| } | ||
|
|
||
| // isLocalAdminsMember reports whether the account is a member of the local | ||
| // Administrators group. Local accounts are checked against the local SAM, | ||
| // which is authoritative for them. For domain accounts an S4U identification | ||
| // token is preferred because its group list is LSA's transitive expansion | ||
| // (nested and universal groups included); NetUserGetLocalGroups expands only | ||
| // one global-group hop but needs no logon, so it serves as fallback. | ||
| func isLocalAdminsMember(username string) (bool, error) { | ||
| adminSid, err := windows.CreateWellKnownSid(windows.WinBuiltinAdministratorsSid) | ||
| if err != nil { | ||
| return false, fmt.Errorf("create Administrators SID: %w", err) | ||
| } | ||
|
|
||
| account, domain := parseUsername(username) | ||
| if NewPrivilegeDropper().isLocalUser(domain) { | ||
| return localGroupsContainSID(account, adminSid) | ||
| } | ||
|
|
||
| member, s4uErr := s4uTokenIsMember(account, domain, adminSid) | ||
| if s4uErr == nil { | ||
| return member, nil | ||
| } | ||
| log.Debugf("privilege check: S4U membership check for %q failed, falling back to local group enumeration: %v", username, s4uErr) | ||
|
|
||
| member, err = localGroupsContainSID(buildUserCpn(account, domain), adminSid) | ||
| if err != nil { | ||
| return false, fmt.Errorf("S4U check: %w; local group enumeration: %w", s4uErr, err) | ||
| } | ||
| return member, nil | ||
| } | ||
|
|
||
| // s4uTokenIsMember obtains an S4U token for the account and checks whether the | ||
| // given SID is enabled in it. | ||
| func s4uTokenIsMember(account, domain string, sid *windows.SID) (bool, error) { | ||
| token, err := generateS4UUserToken(log.NewEntry(log.StandardLogger()), account, domain) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
| defer func() { | ||
| if err := windows.CloseHandle(token); err != nil { | ||
| log.Debugf("close S4U token: %v", err) | ||
| } | ||
| }() | ||
| return windows.Token(token).IsMember(sid) | ||
| } | ||
|
|
||
| // localGroupsContainSID enumerates the local groups the account belongs to | ||
| // (including membership through global groups) and reports whether the wanted | ||
| // SID is among them. Group names are resolved to SIDs so the comparison is not | ||
| // sensitive to localization. | ||
| // | ||
| // A group whose name does not resolve is compared against the wanted SID's own | ||
| // account name rather than skipped: skipping it would under-report membership, | ||
| // which for a privilege check means reporting a privileged account as | ||
| // unprivileged. If neither comparison is possible the error is returned so the | ||
| // caller can fail closed. | ||
| func localGroupsContainSID(username string, want *windows.SID) (bool, error) { | ||
| groups, err := netUserGetLocalGroups(username) | ||
| if err != nil { | ||
| return false, err | ||
| } | ||
|
|
||
| wantName, _, _, wantNameErr := want.LookupAccount("") | ||
|
|
||
| for _, group := range groups { | ||
| groupSid, err := lookupGroupSID(group) | ||
| if err == nil { | ||
| if groupSid.Equals(want) { | ||
| return true, nil | ||
| } | ||
| continue | ||
| } | ||
|
|
||
| if wantNameErr != nil { | ||
| return false, fmt.Errorf("resolve group %q: %w (and resolve wanted SID to a name: %w)", group, err, wantNameErr) | ||
| } | ||
|
|
||
| // Local group names are unique per machine, so a name mismatch is | ||
| // conclusive even though the SID is unavailable. | ||
| if strings.EqualFold(group, wantName) { | ||
| return true, nil | ||
| } | ||
| log.Debugf("local group %q does not resolve to a SID and does not match %q: %v", group, wantName, err) | ||
| } | ||
| return false, nil | ||
| } | ||
|
|
||
| // netUserGetLocalGroups returns the names of the local groups the account is a | ||
| // member of, including indirect membership through global groups. | ||
| func netUserGetLocalGroups(username string) ([]string, error) { | ||
| name16, err := windows.UTF16PtrFromString(username) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("convert username: %w", err) | ||
| } | ||
|
|
||
| var buf *byte | ||
| var entriesRead, totalEntries uint32 | ||
| status, _, _ := procNetUserGetLocalGroups.Call( | ||
| 0, // local server | ||
| uintptr(unsafe.Pointer(name16)), | ||
| 0, // level 0: LOCALGROUP_USERS_INFO_0 | ||
| lgIncludeIndirect, | ||
| uintptr(unsafe.Pointer(&buf)), | ||
| maxPreferredLength, | ||
| uintptr(unsafe.Pointer(&entriesRead)), | ||
| uintptr(unsafe.Pointer(&totalEntries)), | ||
| ) | ||
| if status != 0 { | ||
| return nil, fmt.Errorf("NetUserGetLocalGroups for %q: status %d", username, status) | ||
| } | ||
| if buf == nil { | ||
| return nil, nil | ||
| } | ||
| defer func() { | ||
| if err := windows.NetApiBufferFree(buf); err != nil { | ||
| log.Debugf("free NetApi buffer: %v", err) | ||
| } | ||
| }() | ||
|
|
||
| // MAX_PREFERRED_LENGTH makes the API allocate as much as it needs, so a | ||
| // short read is not expected. Report it rather than silently returning a | ||
| // subset of the account's groups. | ||
| if entriesRead != totalEntries { | ||
| return nil, fmt.Errorf("NetUserGetLocalGroups for %q returned %d of %d groups", username, entriesRead, totalEntries) | ||
| } | ||
|
|
||
| entries := unsafe.Slice((*localGroupUsersInfo0)(unsafe.Pointer(buf)), entriesRead) | ||
| groups := make([]string, 0, entriesRead) | ||
| for _, entry := range entries { | ||
| groups = append(groups, windows.UTF16PtrToString(entry.name)) | ||
| } | ||
| return groups, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.