From a05bf6caf5609a5d3ac90c6729c026b0eae75add Mon Sep 17 00:00:00 2001 From: Mario C Date: Tue, 10 Nov 2026 13:02:25 -0500 Subject: [PATCH 01/11] feat(ad): add gMSA password authorization tests --- .../ad/queries/Get-MtLdapServiceAccount.ps1 | 176 +++++++++++++++++- .../ADGmsaPasswordAuthorization.Tests.ps1 | 39 ++++ tests/ad/security/Test.AD-MSA-02.md | 9 + tests/ad/security/Test.AD-MSA-02.ps1 | 10 + tests/ad/security/Test.AD-MSA-03.md | 7 + tests/ad/security/Test.AD-MSA-03.ps1 | 10 + 6 files changed, 246 insertions(+), 5 deletions(-) create mode 100644 powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 create mode 100644 tests/ad/security/Test.AD-MSA-02.md create mode 100644 tests/ad/security/Test.AD-MSA-02.ps1 create mode 100644 tests/ad/security/Test.AD-MSA-03.md create mode 100644 tests/ad/security/Test.AD-MSA-03.ps1 diff --git a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 index 4a60f57a4a..9338efad10 100644 --- a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 +++ b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 @@ -1,15 +1,181 @@ function Get-MtLdapServiceAccount { + <# + .SYNOPSIS + Collects gMSA identity and managed-password retrieval authorization without requesting the password. + .NOTES + Author: Mario Contestabile + LinkedIn: https://www.linkedin.com/in/mario-contestabile/ + #> [CmdletBinding()] [OutputType([object[]])] param( [Parameter(Mandatory)] [System.DirectoryServices.Protocols.LdapConnection] $Connection, - [string] $SearchBase ) - if ([string]::IsNullOrWhiteSpace($SearchBase)) { $SearchBase = (Get-MtLdapRootDse -Connection $Connection).DefaultNamingContext } - $attributes = @('distinguishedName', 'name', 'sAMAccountName', 'dNSHostName', 'whenCreated', 'whenChanged', 'objectSid') + if ([string]::IsNullOrWhiteSpace($SearchBase)) { + $SearchBase = (Get-MtLdapRootDse -Connection $Connection).DefaultNamingContext + } + + function ConvertTo-MtGmsaSidFilterValue { + param([Parameter(Mandatory)][object]$Sid) + + # Convert the textual SID to the escaped little-endian binary form required by an LDAP objectSid equality filter. + $sidValue = if ($null -ne $Sid.PSObject.Properties['Value']) { [string]$Sid.Value } else { [string]$Sid } + $parts = $sidValue -split '-' + if ($parts.Count -lt 4 -or $parts[0] -ne 'S') { return $null } + + $bytes = [System.Collections.Generic.List[byte]]::new() + $bytes.Add([byte]$parts[1]) + $bytes.Add([byte]($parts.Count - 3)) + $authority = [uint64]$parts[2] + for ($shift = 40; $shift -ge 0; $shift -= 8) { + $bytes.Add([byte](($authority -shr $shift) -band 0xff)) + } + for ($index = 3; $index -lt $parts.Count; $index++) { + $bytes.AddRange([BitConverter]::GetBytes([uint32]$parts[$index])) + } + return (($bytes | ForEach-Object { '\{0:x2}' -f $_ }) -join '') + } + + # Request the authorization descriptor only. The managed-password value is intentionally excluded. + $attributes = @( + 'distinguishedName','name','sAMAccountName','dNSHostName','whenCreated','whenChanged', + 'objectSid','msDS-GroupMSAMembership' + ) $entries = @(Invoke-MtLdapSearch -Connection $Connection -SearchBase $SearchBase -Scope Subtree -Filter '(objectClass=msDS-GroupManagedServiceAccount)' -Attributes $attributes) - return @($entries | ForEach-Object { [PSCustomObject]@{ DistinguishedName = [string]$_.DistinguishedName; Name = [string]$_.name; SamAccountName = [string]$_.sAMAccountName; DnsHostName = [string]$_.dNSHostName; Created = $_.whenCreated; Modified = $_.whenChanged; Sid = $_.objectSid } }) -} + + $broadSidMap = [ordered]@{ + 'S-1-1-0'='Everyone' + 'S-1-5-7'='Anonymous Logon' + 'S-1-5-11'='Authenticated Users' + } + + # Cache SID lookups for this collection because the same host or group often authorizes several gMSAs. + $sidResolutionCache = @{} + + return @($entries | ForEach-Object { + $entry = $_ + $descriptorError = $null + $descriptorStatus = 'Unknown' + $descriptor = $null + $rawDescriptor = $entry.'msDS-GroupMSAMembership' + + if ($null -eq $rawDescriptor) { + $descriptorStatus = 'AuthorizationDescriptorAbsent' + } else { + if ($null -ne $rawDescriptor.PSObject.Properties['Value']) { + $rawDescriptor = $rawDescriptor.Value + } + while ($rawDescriptor -is [System.Array] -and $rawDescriptor -isnot [byte[]]) { + $items=@($rawDescriptor) + $allBytes=$items.Count -gt 0 -and @($items|Where-Object{$_ -isnot [byte] -and -not($_ -is [int] -and $_ -ge 0 -and $_ -le 255)}).Count -eq 0 + if($allBytes){$rawDescriptor=[byte[]]@($items|ForEach-Object{[byte]$_});break} + if($items.Count -eq 0){$descriptorStatus='AuthorizationDescriptorEmpty';break} + if($items.Count -ne 1){$descriptorStatus='AuthorizationDescriptorUnsupportedShape';$descriptorError="Authorization descriptor returned $($items.Count) values.";break} + $rawDescriptor=$items[0] + if ($null -ne $rawDescriptor -and $null -ne $rawDescriptor.PSObject.Properties['Value']) {$rawDescriptor=$rawDescriptor.Value} + } + + if($rawDescriptor -is [byte[]]){ + if($rawDescriptor.Length -eq 0){ + $descriptorStatus='AuthorizationDescriptorEmpty' + } else { + try{ + $descriptor=ConvertFrom-MtLdapSecurityDescriptor -RawSecurityDescriptor $rawDescriptor + if($null -ne $descriptor){$descriptorStatus='Complete'}else{$descriptorStatus='AuthorizationDescriptorParseFailure';$descriptorError='Security descriptor parser returned no result.'} + }catch{$descriptorStatus='AuthorizationDescriptorParseFailure';$descriptorError=$_.Exception.Message} + } + } elseif($descriptorStatus -eq 'Unknown') { + $descriptorStatus='AuthorizationDescriptorUnsupportedShape' + $descriptorError="Unsupported authorization descriptor type: $($rawDescriptor.GetType().FullName)" + } + } + + $trustees=@() + if($null -ne $descriptor){ + $trustees=@($descriptor.Access | Where-Object {$_.AccessControlType -eq 'Allow'} | ForEach-Object { + $ace = $_ + $sid=[string]$ace.Sid + $resolution = $null + + if($broadSidMap.Contains($sid)){ + $resolution=[pscustomobject][ordered]@{ + Status='WellKnownBroadPrincipal';Name=$broadSidMap[$sid];SamAccountName=$null + DistinguishedName=$null;ObjectClass=$null;Error=$null + } + } elseif($sidResolutionCache.ContainsKey($sid)) { + $resolution=$sidResolutionCache[$sid] + } else { + try { + $sidFilter=ConvertTo-MtGmsaSidFilterValue -Sid $sid + if($null -eq $sidFilter){ + $resolution=[pscustomobject][ordered]@{Status='ResolutionError';Name=$null;SamAccountName=$null;DistinguishedName=$null;ObjectClass=$null;Error='SID could not be converted to an LDAP filter value.'} + } else { + # Resolve the authorized SID through LDAP without reading the managed-password attribute. + $principal=Invoke-MtLdapSearch -Connection $Connection -SearchBase $SearchBase -Scope Subtree -Filter "(objectSid=$sidFilter)" -Attributes @('distinguishedName','name','sAMAccountName','objectClass','objectSid') -PageSize 0 | Select-Object -First 1 + if($null -eq $principal){ + $resolution=[pscustomobject][ordered]@{Status='Unresolved';Name=$null;SamAccountName=$null;DistinguishedName=$null;ObjectClass=$null;Error=$null} + } else { + $classes=@($principal.objectClass) + $leafClass=if($classes.Count){[string]$classes[-1]}else{$null} + $status=switch -Regex ($leafClass) { + '^group$' {'ResolvedGroup';break} + '^computer$' {'ResolvedComputer';break} + '^user$' {'ResolvedUser';break} + '^foreignSecurityPrincipal$' {'ForeignSecurityPrincipal';break} + default {'ResolvedPrincipal'} + } + $resolution=[pscustomobject][ordered]@{ + Status=$status;Name=[string]$principal.name;SamAccountName=[string]$principal.sAMAccountName + DistinguishedName=[string]$principal.DistinguishedName;ObjectClass=$leafClass;Error=$null + } + } + } + } catch { + $resolution=[pscustomobject][ordered]@{Status='ResolutionError';Name=$null;SamAccountName=$null;DistinguishedName=$null;ObjectClass=$null;Error=$_.Exception.Message} + } + $sidResolutionCache[$sid]=$resolution + } + + $isUnresolved=$resolution.Status -in @('Unresolved','ResolutionError','ForeignSecurityPrincipal') + [pscustomobject][ordered]@{ + Sid=$sid + Name=if($resolution.Name){$resolution.Name}else{[string]$ace.IdentityReference} + SamAccountName=$resolution.SamAccountName + DistinguishedName=$resolution.DistinguishedName + ObjectClass=$resolution.ObjectClass + ResolutionStatus=$resolution.Status + ResolutionError=$resolution.Error + Classification=if($broadSidMap.Contains($sid)){'Broad'}elseif($isUnresolved){'Unresolved'}else{'Delegated'} + BroadPrincipalName=if($broadSidMap.Contains($sid)){$broadSidMap[$sid]}else{$null} + AccessMask=$ace.AccessMask + IsInherited=[bool]$ace.IsInherited + AuthorizationSource='msDS-GroupMSAMembership' + AuthorizationType=if($resolution.Status -eq 'ResolvedGroup'){'DirectGroup'}else{'DirectPrincipal'} + NestedExpansionStatus=if($resolution.Status -eq 'ResolvedGroup'){'NotEvaluated'}else{'NotApplicable'} + } + }) + } + + [pscustomobject][ordered]@{ + DistinguishedName=[string]$entry.DistinguishedName + Name=[string]$entry.name + SamAccountName=[string]$entry.sAMAccountName + DnsHostName=[string]$entry.dNSHostName + Created=$entry.whenCreated + Modified=$entry.whenChanged + Sid=$entry.objectSid + ObjectClass=@('msDS-GroupManagedServiceAccount') + ServiceAccountType='GroupManagedServiceAccount' + Enabled=$true + PasswordRetrievalTrustees=$trustees + BroadPasswordRetrievalTrustees=@($trustees|Where-Object Classification -eq 'Broad') + UnresolvedPasswordRetrievalTrustees=@($trustees|Where-Object Classification -eq 'Unresolved') + AuthorizationEvidenceComplete=($descriptorStatus -eq 'Complete') + AuthorizationEvidenceStatus=$descriptorStatus + AuthorizationCollectionError=$descriptorError + } + }) +} \ No newline at end of file diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 new file mode 100644 index 0000000000..1f073fe5eb --- /dev/null +++ b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 @@ -0,0 +1,39 @@ +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } +Describe 'gMSA managed-password authorization source contract' { + It 'never requests the managed password value' { + $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source | Should -Not -Match '(?i)[''"]msDS-ManagedPassword[''"]' + $source | Should -Match 'msDS-GroupMSAMembership' + } + It 'does not contain credential-use operations' { + $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source | Should -Not -Match 'ConvertFrom-.*ManagedPassword|kinit|New-PSSession|Get-Credential|Invoke-Command' + } + It 'resolves authorization SIDs through LDAP and records resolution status' { + $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source | Should -Match 'ConvertTo-MtGmsaSidFilterValue' + $source | Should -Match 'ResolutionStatus' + $source | Should -Match 'NestedExpansionStatus' + $source | Should -Match 'sidResolutionCache' + $source | Should -Match 'function ConvertTo-MtGmsaSidFilterValue' + $source | Should -Not -Match 'ConvertTo-SidFilterValue -Sid' + } + It 'records explicit authorization evidence status values' { + $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source | Should -Match 'AuthorizationDescriptorAbsent' + $source | Should -Match 'AuthorizationDescriptorEmpty' + $source | Should -Match 'AuthorizationDescriptorParseFailure' + $source | Should -Match 'AuthorizationDescriptorUnsupportedShape' + } + It 'uses registered Maester taxonomy values' { + foreach($testPath in @( + "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1", + "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" + )) { + $testSource=Get-Content $testPath -Raw + $testSource | Should -Match "Feature='account-security'" + $testSource | Should -Match "Area='privileged-access'" + $testSource | Should -Match "ResourceType='Computer'" + } + } +} \ No newline at end of file diff --git a/tests/ad/security/Test.AD-MSA-02.md b/tests/ad/security/Test.AD-MSA-02.md new file mode 100644 index 0000000000..d928e1b251 --- /dev/null +++ b/tests/ad/security/Test.AD-MSA-02.md @@ -0,0 +1,9 @@ +The `msDS-GroupMSAMembership` authorization descriptor controls which principals can retrieve a group Managed Service Account password. Broad trustees can extend retrieval eligibility beyond the intended service hosts. + +This test reads authorization metadata only. It does not request, decode, export, store, or use `msDS-ManagedPassword`. + +#### Remediation action +Replace broad trustees with purpose-specific computer or security groups. Validate service dependencies before changing retrieval authorization. + + +%TestResult% \ No newline at end of file diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 new file mode 100644 index 0000000000..dfa8aa0194 --- /dev/null +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -0,0 +1,10 @@ +function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { + [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='High',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] + [CmdletBinding()][OutputType([bool])]param() + $state=Get-MtADDomainState -Categories ServiceAccounts + if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} + $items=@($state.ServiceAccounts|ForEach-Object{$gmsa=$_;@($gmsa.BroadPasswordRetrievalTrustees)|ForEach-Object{[pscustomobject]@{Account=$gmsa.SamAccountName;Trustee=$_.Name;Sid=$_.Sid;Source=$_.AuthorizationSource}}}) + $table="| gMSA | Trustee | SID | Source |`n| --- | --- | --- | --- |`n";foreach($i in $items){$table+="| $($i.Account) | $($i.Trustee) | $($i.Sid) | $($i.Source) |`n"} + Add-MtTestResultDetail -Result "Found $($items.Count) broad-principal gMSA password-retrieval authorization candidate(s). This proves authorization evidence only; no managed password was requested or used.`n`n$table" + return ($items.Count -eq 0) +} \ No newline at end of file diff --git a/tests/ad/security/Test.AD-MSA-03.md b/tests/ad/security/Test.AD-MSA-03.md new file mode 100644 index 0000000000..846aa197a1 --- /dev/null +++ b/tests/ad/security/Test.AD-MSA-03.md @@ -0,0 +1,7 @@ +Incomplete authorization evidence or unresolved SIDs prevent a defensible conclusion about who can retrieve a gMSA managed password. + +#### Remediation action +Resolve the SID against the authoritative domain or trust, confirm the intended service hosts, and verify read access to `msDS-GroupMSAMembership`. Do not retrieve the managed password as part of validation. + + +%TestResult% \ No newline at end of file diff --git a/tests/ad/security/Test.AD-MSA-03.ps1 b/tests/ad/security/Test.AD-MSA-03.ps1 new file mode 100644 index 0000000000..a8a81b7bb4 --- /dev/null +++ b/tests/ad/security/Test.AD-MSA-03.ps1 @@ -0,0 +1,10 @@ +function Test-MtAdGmsaAuthorizationEvidenceCompleteness { + [MaesterTest(Id='AD-MSA-03',Title='gMSA managed-password authorization evidence should be complete and resolvable',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='Info',Mitre='None',Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] + [CmdletBinding()][OutputType([bool])]param() + $state=Get-MtADDomainState -Categories ServiceAccounts + if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} + $incomplete=@($state.ServiceAccounts|Where-Object{-not $_.AuthorizationEvidenceComplete -or @($_.UnresolvedPasswordRetrievalTrustees).Count -gt 0}) + $table="| gMSA | Complete | Evidence status | Unresolved trustees | Error |`n| --- | --- | --- | ---: | --- |`n";foreach($i in $incomplete){$table+="| $($i.SamAccountName) | $($i.AuthorizationEvidenceComplete) | $($i.AuthorizationEvidenceStatus) | $(@($i.UnresolvedPasswordRetrievalTrustees).Count) | $($i.AuthorizationCollectionError) |`n"} + Add-MtTestResultDetail -Result "Found $($incomplete.Count) gMSA object(s) with incomplete or unresolved authorization evidence.`n`n$table" -Investigate + return $true +} \ No newline at end of file From 7928ac038d854f9e2a1017c31bd8575ce94242ff Mon Sep 17 00:00:00 2001 From: Mario C Date: Tue, 10 Nov 2026 14:30:08 -0500 Subject: [PATCH 02/11] fix(ad): harden gMSA password authorization evidence --- .../ad/queries/Get-MtLdapServiceAccount.ps1 | 35 +++++++++-- .../ADGmsaPasswordAuthorization.Tests.ps1 | 58 ++++++++++++------- tests/ad/security/Test.AD-MSA-02.md | 2 +- tests/ad/security/Test.AD-MSA-02.ps1 | 11 +++- tests/ad/security/Test.AD-MSA-03.ps1 | 9 ++- 5 files changed, 84 insertions(+), 31 deletions(-) diff --git a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 index 9338efad10..c1f9e4c8c0 100644 --- a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 +++ b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 @@ -50,6 +50,29 @@ 'S-1-1-0'='Everyone' 'S-1-5-7'='Anonymous Logon' 'S-1-5-11'='Authenticated Users' + 'S-1-5-32-545'='BUILTIN\Users' + } + + function Get-MtGmsaBroadPrincipalName { + param([string]$Sid) + if ($broadSidMap.Contains($Sid)) { return $broadSidMap[$Sid] } + if ($Sid -match '^S-1-5-21-\d+-\d+-\d+-513$') { return 'Domain Users' } + if ($Sid -match '^S-1-5-21-\d+-\d+-\d+-515$') { return 'Domain Computers' } + return $null + } + + function Test-MtGmsaPasswordRetrievalAccessMask { + param([object]$AccessMask) + try { + $signedMask=[int64]$AccessMask + if($signedMask -lt 0){$signedMask+=4294967296L} + if($signedMask -gt [uint32]::MaxValue){return $false} + $mask=[uint32]$signedMask + } catch { return $false } + $rightDsReadProperty=[uint32]0x00000010 + $genericAll=[uint32]0x10000000 + $genericRead=[uint32]2147483648 + return (($mask -band $rightDsReadProperty) -ne 0) -or (($mask -band $genericAll) -ne 0) -or (($mask -band $genericRead) -ne 0) } # Cache SID lookups for this collection because the same host or group often authorizes several gMSAs. @@ -95,14 +118,16 @@ $trustees=@() if($null -ne $descriptor){ - $trustees=@($descriptor.Access | Where-Object {$_.AccessControlType -eq 'Allow'} | ForEach-Object { + $trustees=@($descriptor.Access | Where-Object { $_.AccessControlType -eq 'Allow' -and (Test-MtGmsaPasswordRetrievalAccessMask -AccessMask $_.AccessMask) } | ForEach-Object { $ace = $_ $sid=[string]$ace.Sid $resolution = $null - if($broadSidMap.Contains($sid)){ + + $broadPrincipalName=Get-MtGmsaBroadPrincipalName -Sid $sid + if($broadPrincipalName){ $resolution=[pscustomobject][ordered]@{ - Status='WellKnownBroadPrincipal';Name=$broadSidMap[$sid];SamAccountName=$null + Status='WellKnownBroadPrincipal';Name=$broadPrincipalName;SamAccountName=$null DistinguishedName=$null;ObjectClass=$null;Error=$null } } elseif($sidResolutionCache.ContainsKey($sid)) { @@ -148,8 +173,8 @@ ObjectClass=$resolution.ObjectClass ResolutionStatus=$resolution.Status ResolutionError=$resolution.Error - Classification=if($broadSidMap.Contains($sid)){'Broad'}elseif($isUnresolved){'Unresolved'}else{'Delegated'} - BroadPrincipalName=if($broadSidMap.Contains($sid)){$broadSidMap[$sid]}else{$null} + Classification=if($broadPrincipalName){'Broad'}elseif($isUnresolved){'Unresolved'}else{'Delegated'} + BroadPrincipalName=$broadPrincipalName AccessMask=$ace.AccessMask IsInherited=[bool]$ace.IsInherited AuthorizationSource='msDS-GroupMSAMembership' diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 index 1f073fe5eb..96fc8d3c93 100644 --- a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 @@ -1,39 +1,55 @@ BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } Describe 'gMSA managed-password authorization source contract' { - It 'never requests the managed password value' { + It 'never requests or decodes the managed password value' { $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw $source | Should -Not -Match '(?i)[''"]msDS-ManagedPassword[''"]' + $source | Should -Not -Match 'ConvertFrom-.*ManagedPassword|kinit|New-PSSession|Get-Credential|Invoke-Command' $source | Should -Match 'msDS-GroupMSAMembership' } - It 'does not contain credential-use operations' { + It 'requires an applicable retrieval access mask' { $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw - $source | Should -Not -Match 'ConvertFrom-.*ManagedPassword|kinit|New-PSSession|Get-Credential|Invoke-Command' + $source | Should -Match 'rightDsReadProperty' + $source | Should -Match 'genericRead' + $source | Should -Match 'genericAll' + $source | Should -Match 'Test-MtGmsaPasswordRetrievalAccessMask' } - It 'resolves authorization SIDs through LDAP and records resolution status' { + It 'recognizes broad domain and builtin groups' { $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw - $source | Should -Match 'ConvertTo-MtGmsaSidFilterValue' - $source | Should -Match 'ResolutionStatus' - $source | Should -Match 'NestedExpansionStatus' - $source | Should -Match 'sidResolutionCache' - $source | Should -Match 'function ConvertTo-MtGmsaSidFilterValue' - $source | Should -Not -Match 'ConvertTo-SidFilterValue -Sid' + $source | Should -Match 'S-1-5-32-545' + $source | Should -Match '-513\$' + $source | Should -Match '-515\$' + $source | Should -Match 'Get-MtGmsaBroadPrincipalName' } - It 'records explicit authorization evidence status values' { + It 'records explicit resolution and evidence status values' { $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source | Should -Match 'ResolutionStatus' $source | Should -Match 'AuthorizationDescriptorAbsent' $source | Should -Match 'AuthorizationDescriptorEmpty' $source | Should -Match 'AuthorizationDescriptorParseFailure' $source | Should -Match 'AuthorizationDescriptorUnsupportedShape' } - It 'uses registered Maester taxonomy values' { - foreach($testPath in @( - "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1", - "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" - )) { - $testSource=Get-Content $testPath -Raw - $testSource | Should -Match "Feature='account-security'" - $testSource | Should -Match "Area='privileged-access'" - $testSource | Should -Match "ResourceType='Computer'" + It 'uses registered taxonomy and Medium severity for AD-MSA-02' { + $test02=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1" -Raw + $test03=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw + foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "Area='privileged-access'";$testSource|Should -Match "ResourceType='Computer'"} + $test02 | Should -Match "Severity='Medium'" + } + It 'does not permit a clean pass when ServiceAccounts state is unavailable' { + foreach($path in @("$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1","$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1")){(Get-Content $path -Raw)|Should -Match '\$null -eq \$state\.ServiceAccounts'} + } + It 'reports unresolved SID resolution details' { + $source=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw + $source|Should -Match 'ResolutionStatus';$source|Should -Match 'ResolutionError';$source|Should -Match '\$trustee\.Sid' + } + It 'normalizes the signed GENERIC_READ access mask' { + $signedGenericRead = -2147483648 + $normalizedMask = [int64]$signedGenericRead + + if ($normalizedMask -lt 0) { + $normalizedMask += 4294967296L } + + [uint32]$normalizedMask | Should -Be ([uint32]2147483648) + ('{0:X8}' -f [uint32]$normalizedMask) | Should -Be '80000000' } -} \ No newline at end of file +} diff --git a/tests/ad/security/Test.AD-MSA-02.md b/tests/ad/security/Test.AD-MSA-02.md index d928e1b251..ad1af7d75f 100644 --- a/tests/ad/security/Test.AD-MSA-02.md +++ b/tests/ad/security/Test.AD-MSA-02.md @@ -1,4 +1,4 @@ -The `msDS-GroupMSAMembership` authorization descriptor controls which principals can retrieve a group Managed Service Account password. Broad trustees can extend retrieval eligibility beyond the intended service hosts. +The `msDS-GroupMSAMembership` authorization descriptor controls which principals can retrieve a group Managed Service Account password. Broad trustees can extend retrieval eligibility beyond the intended service hosts. This includes well-known broad identities, BUILTIN\Users, Domain Users, and Domain Computers when an applicable password-retrieval Allow ACE is present. This test reads authorization metadata only. It does not request, decode, export, store, or use `msDS-ManagedPassword`. diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 index dfa8aa0194..680573d1ea 100644 --- a/tests/ad/security/Test.AD-MSA-02.ps1 +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -1,10 +1,15 @@ function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { - [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='High',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] + [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='Medium',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} + if($null -eq $state.ServiceAccounts){Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'ServiceAccounts collection failed or returned no state.';return $null} + + $incomplete=@($state.ServiceAccounts|Where-Object{-not $_.AuthorizationEvidenceComplete}) $items=@($state.ServiceAccounts|ForEach-Object{$gmsa=$_;@($gmsa.BroadPasswordRetrievalTrustees)|ForEach-Object{[pscustomobject]@{Account=$gmsa.SamAccountName;Trustee=$_.Name;Sid=$_.Sid;Source=$_.AuthorizationSource}}}) $table="| gMSA | Trustee | SID | Source |`n| --- | --- | --- | --- |`n";foreach($i in $items){$table+="| $($i.Account) | $($i.Trustee) | $($i.Sid) | $($i.Source) |`n"} - Add-MtTestResultDetail -Result "Found $($items.Count) broad-principal gMSA password-retrieval authorization candidate(s). This proves authorization evidence only; no managed password was requested or used.`n`n$table" - return ($items.Count -eq 0) + if($items.Count -gt 0){Add-MtTestResultDetail -Result "Found $($items.Count) broad-principal gMSA password-retrieval authorization candidate(s). This proves authorization evidence only; no managed password was requested or used.`n`n$table";return $false} + if($incomplete.Count -gt 0){Add-MtTestResultDetail -Result "No broad trustee was identified, but authorization evidence was incomplete for $($incomplete.Count) gMSA object(s)." -Investigate;return $null} + Add-MtTestResultDetail -Result 'No broad-principal gMSA password-retrieval authorization was detected in complete collected evidence.' + return $true } \ No newline at end of file diff --git a/tests/ad/security/Test.AD-MSA-03.ps1 b/tests/ad/security/Test.AD-MSA-03.ps1 index a8a81b7bb4..126bed923b 100644 --- a/tests/ad/security/Test.AD-MSA-03.ps1 +++ b/tests/ad/security/Test.AD-MSA-03.ps1 @@ -3,8 +3,15 @@ [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} + if($null -eq $state.ServiceAccounts){Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'ServiceAccounts collection failed or returned no state.';return $null} + $incomplete=@($state.ServiceAccounts|Where-Object{-not $_.AuthorizationEvidenceComplete -or @($_.UnresolvedPasswordRetrievalTrustees).Count -gt 0}) - $table="| gMSA | Complete | Evidence status | Unresolved trustees | Error |`n| --- | --- | --- | ---: | --- |`n";foreach($i in $incomplete){$table+="| $($i.SamAccountName) | $($i.AuthorizationEvidenceComplete) | $($i.AuthorizationEvidenceStatus) | $(@($i.UnresolvedPasswordRetrievalTrustees).Count) | $($i.AuthorizationCollectionError) |`n"} + $table="| gMSA | Complete | Evidence status | Unresolved SID | Resolution status | Resolution error | Collection error |`n| --- | --- | --- | --- | --- | --- | --- |`n" + foreach($gmsa in $incomplete){ + $unresolved=@($gmsa.UnresolvedPasswordRetrievalTrustees) + if($unresolved.Count -eq 0){$table+="| $($gmsa.SamAccountName) | $($gmsa.AuthorizationEvidenceComplete) | $($gmsa.AuthorizationEvidenceStatus) | | | | $($gmsa.AuthorizationCollectionError) |`n";continue} + foreach($trustee in $unresolved){$table+="| $($gmsa.SamAccountName) | $($gmsa.AuthorizationEvidenceComplete) | $($gmsa.AuthorizationEvidenceStatus) | $($trustee.Sid) | $($trustee.ResolutionStatus) | $($trustee.ResolutionError) | $($gmsa.AuthorizationCollectionError) |`n"} + } Add-MtTestResultDetail -Result "Found $($incomplete.Count) gMSA object(s) with incomplete or unresolved authorization evidence.`n`n$table" -Investigate return $true } \ No newline at end of file From 07f19325b9b982bf31357ae228f67f20a9e9c975 Mon Sep 17 00:00:00 2001 From: Mario C Date: Tue, 10 Nov 2026 21:41:23 -0500 Subject: [PATCH 03/11] fix(ad): align gMSA test area with attribute rubric --- .../tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 | 6 ++++-- tests/ad/security/Test.AD-MSA-02.ps1 | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 index 96fc8d3c93..2fd5aed78d 100644 --- a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } +BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } Describe 'gMSA managed-password authorization source contract' { It 'never requests or decodes the managed password value' { $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw @@ -31,7 +31,9 @@ Describe 'gMSA managed-password authorization source contract' { It 'uses registered taxonomy and Medium severity for AD-MSA-02' { $test02=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1" -Raw $test03=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw - foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "Area='privileged-access'";$testSource|Should -Match "ResourceType='Computer'"} +foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "ResourceType='Computer'"} +$test02 | Should -Match "Area='applications'" +$test03 | Should -Match "Area='privileged-access'" $test02 | Should -Match "Severity='Medium'" } It 'does not permit a clean pass when ServiceAccounts state is unavailable' { diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 index 680573d1ea..28c679d597 100644 --- a/tests/ad/security/Test.AD-MSA-02.ps1 +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -1,5 +1,5 @@ -function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { - [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='Medium',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] +function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { + [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='Medium',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} From f2ce758a2d905a417ffc9731ae15fcf0ba1b22c3 Mon Sep 17 00:00:00 2001 From: Mario C Date: Tue, 10 Nov 2026 21:51:43 -0500 Subject: [PATCH 04/11] fix(ad): align gMSA evidence test area with rubric --- .../tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 | 2 +- tests/ad/security/Test.AD-MSA-03.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 index 2fd5aed78d..3d83c95a66 100644 --- a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 @@ -33,7 +33,7 @@ Describe 'gMSA managed-password authorization source contract' { $test03=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "ResourceType='Computer'"} $test02 | Should -Match "Area='applications'" -$test03 | Should -Match "Area='privileged-access'" +$test03 | Should -Match "Area='applications'" $test02 | Should -Match "Severity='Medium'" } It 'does not permit a clean pass when ServiceAccounts state is unavailable' { diff --git a/tests/ad/security/Test.AD-MSA-03.ps1 b/tests/ad/security/Test.AD-MSA-03.ps1 index 126bed923b..a6a5eb22f2 100644 --- a/tests/ad/security/Test.AD-MSA-03.ps1 +++ b/tests/ad/security/Test.AD-MSA-03.ps1 @@ -1,5 +1,5 @@ -function Test-MtAdGmsaAuthorizationEvidenceCompleteness { - [MaesterTest(Id='AD-MSA-03',Title='gMSA managed-password authorization evidence should be complete and resolvable',Product='ad',Feature='account-security',Area='privileged-access',ResourceType='Computer',Severity='Info',Mitre='None',Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] +function Test-MtAdGmsaAuthorizationEvidenceCompleteness { + [MaesterTest(Id='AD-MSA-03',Title='gMSA managed-password authorization evidence should be complete and resolvable',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='Info',Mitre='None',Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} From 26d9bd4f79a09b32e79df449cb885219fe411c23 Mon Sep 17 00:00:00 2001 From: Mario C Date: Tue, 10 Nov 2026 22:01:30 -0500 Subject: [PATCH 05/11] fix(ad): align gMSA severity with attribute rubric --- .../tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 | 4 ++-- tests/ad/security/Test.AD-MSA-02.ps1 | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 index 3d83c95a66..2fb8c351b7 100644 --- a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 @@ -28,13 +28,13 @@ Describe 'gMSA managed-password authorization source contract' { $source | Should -Match 'AuthorizationDescriptorParseFailure' $source | Should -Match 'AuthorizationDescriptorUnsupportedShape' } - It 'uses registered taxonomy and Medium severity for AD-MSA-02' { + It 'uses registered taxonomy and High severity for AD-MSA-02' { $test02=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1" -Raw $test03=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "ResourceType='Computer'"} $test02 | Should -Match "Area='applications'" $test03 | Should -Match "Area='applications'" - $test02 | Should -Match "Severity='Medium'" + $test02 | Should -Match "Severity='High'" } It 'does not permit a clean pass when ServiceAccounts state is unavailable' { foreach($path in @("$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1","$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1")){(Get-Content $path -Raw)|Should -Match '\$null -eq \$state\.ServiceAccounts'} diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 index 28c679d597..62c88166cb 100644 --- a/tests/ad/security/Test.AD-MSA-02.ps1 +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -1,5 +1,5 @@ function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { - [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='Medium',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] + [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='High',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts if($null -eq $state){Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory;return $null} From 8ff92dffd3ed692b992c9d75d2320cd72caa3cde Mon Sep 17 00:00:00 2001 From: Mario C Date: Wed, 11 Nov 2026 15:14:22 -0500 Subject: [PATCH 06/11] test(ad): align gMSA collector test layout --- .../ADGmsaPasswordAuthorization.Tests.ps1 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) rename powershell/tests/{functions => ad/collection}/ADGmsaPasswordAuthorization.Tests.ps1 (68%) diff --git a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 similarity index 68% rename from powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 rename to powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 index 2fb8c351b7..a5e08ff093 100644 --- a/powershell/tests/functions/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 @@ -1,27 +1,27 @@ -BeforeAll { Import-Module "$PSScriptRoot/../../Maester.psd1" -Force } +BeforeAll { Import-Module "$PSScriptRoot/../../../Maester.psd1" -Force } Describe 'gMSA managed-password authorization source contract' { It 'never requests or decodes the managed password value' { - $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source=Get-Content "$PSScriptRoot/../../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw $source | Should -Not -Match '(?i)[''"]msDS-ManagedPassword[''"]' $source | Should -Not -Match 'ConvertFrom-.*ManagedPassword|kinit|New-PSSession|Get-Credential|Invoke-Command' $source | Should -Match 'msDS-GroupMSAMembership' } It 'requires an applicable retrieval access mask' { - $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source=Get-Content "$PSScriptRoot/../../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw $source | Should -Match 'rightDsReadProperty' $source | Should -Match 'genericRead' $source | Should -Match 'genericAll' $source | Should -Match 'Test-MtGmsaPasswordRetrievalAccessMask' } It 'recognizes broad domain and builtin groups' { - $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source=Get-Content "$PSScriptRoot/../../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw $source | Should -Match 'S-1-5-32-545' $source | Should -Match '-513\$' $source | Should -Match '-515\$' $source | Should -Match 'Get-MtGmsaBroadPrincipalName' } It 'records explicit resolution and evidence status values' { - $source=Get-Content "$PSScriptRoot/../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw + $source=Get-Content "$PSScriptRoot/../../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw $source | Should -Match 'ResolutionStatus' $source | Should -Match 'AuthorizationDescriptorAbsent' $source | Should -Match 'AuthorizationDescriptorEmpty' @@ -29,18 +29,18 @@ Describe 'gMSA managed-password authorization source contract' { $source | Should -Match 'AuthorizationDescriptorUnsupportedShape' } It 'uses registered taxonomy and High severity for AD-MSA-02' { - $test02=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1" -Raw - $test03=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw + $test02=Get-Content "$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-02.ps1" -Raw + $test03=Get-Content "$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw foreach($testSource in @($test02,$test03)){$testSource|Should -Match "Feature='account-security'";$testSource|Should -Match "ResourceType='Computer'"} $test02 | Should -Match "Area='applications'" $test03 | Should -Match "Area='applications'" $test02 | Should -Match "Severity='High'" } It 'does not permit a clean pass when ServiceAccounts state is unavailable' { - foreach($path in @("$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-02.ps1","$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1")){(Get-Content $path -Raw)|Should -Match '\$null -eq \$state\.ServiceAccounts'} + foreach($path in @("$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-02.ps1","$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-03.ps1")){(Get-Content $path -Raw)|Should -Match '\$null -eq \$state\.ServiceAccounts'} } It 'reports unresolved SID resolution details' { - $source=Get-Content "$PSScriptRoot/../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw + $source=Get-Content "$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw $source|Should -Match 'ResolutionStatus';$source|Should -Match 'ResolutionError';$source|Should -Match '\$trustee\.Sid' } It 'normalizes the signed GENERIC_READ access mask' { From 8b6c394a9f2ef9abaa79713c0ffb9c2f984aafaf Mon Sep 17 00:00:00 2001 From: Mario C Date: Wed, 11 Nov 2026 22:32:51 -0500 Subject: [PATCH 07/11] test(ad): exercise signed gMSA access masks --- .../ADGmsaPasswordAuthorization.Tests.ps1 | 64 ++++++++++++++++--- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 index a5e08ff093..344631be19 100644 --- a/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 @@ -1,4 +1,4 @@ -BeforeAll { Import-Module "$PSScriptRoot/../../../Maester.psd1" -Force } +BeforeAll { Import-Module "$PSScriptRoot/../../../Maester.psd1" -Force } Describe 'gMSA managed-password authorization source contract' { It 'never requests or decodes the managed password value' { $source=Get-Content "$PSScriptRoot/../../../internal/services/ad/queries/Get-MtLdapServiceAccount.ps1" -Raw @@ -43,15 +43,61 @@ $test03 | Should -Match "Area='applications'" $source=Get-Content "$PSScriptRoot/../../../../tests/ad/security/Test.AD-MSA-03.ps1" -Raw $source|Should -Match 'ResolutionStatus';$source|Should -Match 'ResolutionError';$source|Should -Match '\$trustee\.Sid' } - It 'normalizes the signed GENERIC_READ access mask' { - $signedGenericRead = -2147483648 - $normalizedMask = [int64]$signedGenericRead + It 'classifies signed GENERIC_READ through the collector and rejects READ_CONTROL-only ACEs' { + InModuleScope Maester { + $connection = [Runtime.Serialization.FormatterServices]::GetUninitializedObject( + [System.DirectoryServices.Protocols.LdapConnection] + ) - if ($normalizedMask -lt 0) { - $normalizedMask += 4294967296L - } + Mock Invoke-MtLdapSearch { + [pscustomobject]@{ + DistinguishedName = 'CN=svc-web,CN=Managed Service Accounts,DC=contoso,DC=com' + name = 'svc-web' + sAMAccountName = 'svc-web$' + dNSHostName = 'svc-web.contoso.com' + objectSid = 'S-1-5-21-1-2-3-1101' + 'msDS-GroupMSAMembership' = [byte[]](1, 2, 3, 4) + } + } -ParameterFilter { + $Filter -eq '(objectClass=msDS-GroupManagedServiceAccount)' + } + + Mock ConvertFrom-MtLdapSecurityDescriptor { + [pscustomobject]@{ + Access = @( + [pscustomobject]@{ + AccessControlType = 'Allow' + AccessMask = -2147483648 + Sid = 'S-1-5-32-545' + IdentityReference = 'BUILTIN\Users' + IsInherited = $false + } + [pscustomobject]@{ + AccessControlType = 'Allow' + AccessMask = 131072 + Sid = 'S-1-5-11' + IdentityReference = 'NT AUTHORITY\Authenticated Users' + IsInherited = $false + } + ) + } + } + + $result = @( + Get-MtLdapServiceAccount ` + -Connection $connection ` + -SearchBase 'DC=contoso,DC=com' + ) - [uint32]$normalizedMask | Should -Be ([uint32]2147483648) - ('{0:X8}' -f [uint32]$normalizedMask) | Should -Be '80000000' + $result.Count | Should -Be 1 + $result[0].PasswordRetrievalTrustees.Count | Should -Be 1 + $result[0].BroadPasswordRetrievalTrustees.Count | Should -Be 1 + + $trustee = $result[0].BroadPasswordRetrievalTrustees[0] + $trustee.Sid | Should -Be 'S-1-5-32-545' + $trustee.Classification | Should -Be 'Broad' + $trustee.BroadPrincipalName | Should -Be 'BUILTIN\Users' + $result[0].PasswordRetrievalTrustees.Sid | Should -Not -Contain 'S-1-5-11' + } } } From 49f4a7d2be481e21ed0c276ad3de8728e1b5c4dd Mon Sep 17 00:00:00 2001 From: Mario C Date: Fri, 13 Nov 2026 10:33:38 -0500 Subject: [PATCH 08/11] Fix UTF-8 BOM encoding for PR #2786 --- tests/ad/security/Test.AD-MSA-02.ps1 | 2 +- tests/ad/security/Test.AD-MSA-03.ps1 | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 index 62c88166cb..cea99e8d99 100644 --- a/tests/ad/security/Test.AD-MSA-02.ps1 +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -1,4 +1,4 @@ -function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { +function Test-MtAdGmsaBroadPasswordRetrievalAuthorization { [MaesterTest(Id='AD-MSA-02',Title='Broad principals should not be authorized to retrieve gMSA managed passwords',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='High',UserImpact='Low',ImplementationCost='Medium',Mitre=('T1555'),Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts diff --git a/tests/ad/security/Test.AD-MSA-03.ps1 b/tests/ad/security/Test.AD-MSA-03.ps1 index a6a5eb22f2..c01abf0fdf 100644 --- a/tests/ad/security/Test.AD-MSA-03.ps1 +++ b/tests/ad/security/Test.AD-MSA-03.ps1 @@ -1,4 +1,4 @@ -function Test-MtAdGmsaAuthorizationEvidenceCompleteness { +function Test-MtAdGmsaAuthorizationEvidenceCompleteness { [MaesterTest(Id='AD-MSA-03',Title='gMSA managed-password authorization evidence should be complete and resolvable',Product='ad',Feature='account-security',Area='applications',ResourceType='Computer',Severity='Info',Mitre='None',Service='ActiveDirectory',Permission=('ActiveDirectory:Ldap:msDS-GroupManagedServiceAccount.msDS-GroupMSAMembership:ReadProperty'),Platform='Windows',Author='rolling-code')] [CmdletBinding()][OutputType([bool])]param() $state=Get-MtADDomainState -Categories ServiceAccounts From a2a4dd97bf04418b530207d3da24a93d4ce7c0da Mon Sep 17 00:00:00 2001 From: Mario C Date: Fri, 13 Nov 2026 10:38:56 -0500 Subject: [PATCH 09/11] Fix Markdown encoding for PR #2786 --- tests/ad/security/Test.AD-MSA-02.md | 2 +- tests/ad/security/Test.AD-MSA-03.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ad/security/Test.AD-MSA-02.md b/tests/ad/security/Test.AD-MSA-02.md index ad1af7d75f..34dc213c00 100644 --- a/tests/ad/security/Test.AD-MSA-02.md +++ b/tests/ad/security/Test.AD-MSA-02.md @@ -1,4 +1,4 @@ -The `msDS-GroupMSAMembership` authorization descriptor controls which principals can retrieve a group Managed Service Account password. Broad trustees can extend retrieval eligibility beyond the intended service hosts. This includes well-known broad identities, BUILTIN\Users, Domain Users, and Domain Computers when an applicable password-retrieval Allow ACE is present. +The `msDS-GroupMSAMembership` authorization descriptor controls which principals can retrieve a group Managed Service Account password. Broad trustees can extend retrieval eligibility beyond the intended service hosts. This includes well-known broad identities, BUILTIN\Users, Domain Users, and Domain Computers when an applicable password-retrieval Allow ACE is present. This test reads authorization metadata only. It does not request, decode, export, store, or use `msDS-ManagedPassword`. diff --git a/tests/ad/security/Test.AD-MSA-03.md b/tests/ad/security/Test.AD-MSA-03.md index 846aa197a1..577ccdc4ee 100644 --- a/tests/ad/security/Test.AD-MSA-03.md +++ b/tests/ad/security/Test.AD-MSA-03.md @@ -1,4 +1,4 @@ -Incomplete authorization evidence or unresolved SIDs prevent a defensible conclusion about who can retrieve a gMSA managed password. +Incomplete authorization evidence or unresolved SIDs prevent a defensible conclusion about who can retrieve a gMSA managed password. #### Remediation action Resolve the SID against the authoritative domain or trust, confirm the intended service hosts, and verify read access to `msDS-GroupMSAMembership`. Do not retrieve the managed password as part of validation. From 41df048bfa914072e3787639e92facbbd8d2ab75 Mon Sep 17 00:00:00 2001 From: Mario C Date: Mon, 16 Nov 2026 10:25:55 -0500 Subject: [PATCH 10/11] fix(ad): avoid investigate result for complete gMSA evidence --- .../collection/ADGmsaAuthorizationDisposition.Tests.ps1 | 8 ++++++++ tests/ad/security/Test.AD-MSA-03.ps1 | 7 ++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 powershell/tests/ad/collection/ADGmsaAuthorizationDisposition.Tests.ps1 diff --git a/powershell/tests/ad/collection/ADGmsaAuthorizationDisposition.Tests.ps1 b/powershell/tests/ad/collection/ADGmsaAuthorizationDisposition.Tests.ps1 new file mode 100644 index 0000000000..b0cbc7970a --- /dev/null +++ b/powershell/tests/ad/collection/ADGmsaAuthorizationDisposition.Tests.ps1 @@ -0,0 +1,8 @@ +BeforeAll { $script:Source = Get-Content -LiteralPath (Join-Path $PSScriptRoot '../../../../tests/ad/security/Test.AD-MSA-03.ps1') -Raw } +Describe 'AD-MSA-03 clean evidence disposition contract' { + It 'returns success without Investigate when all evidence is complete' { + $branch=[regex]::Match($script:Source,'if \(\$incomplete.Count -eq 0\).*?\n\s*}','Singleline').Value + $branch | Should -Match 'return \$true' + $branch | Should -Not -Match '-Investigate' + } +} diff --git a/tests/ad/security/Test.AD-MSA-03.ps1 b/tests/ad/security/Test.AD-MSA-03.ps1 index c01abf0fdf..aaf31b3ed5 100644 --- a/tests/ad/security/Test.AD-MSA-03.ps1 +++ b/tests/ad/security/Test.AD-MSA-03.ps1 @@ -6,6 +6,11 @@ if($null -eq $state.ServiceAccounts){Add-MtTestResultDetail -SkippedBecause Custom -SkippedCustomReason 'ServiceAccounts collection failed or returned no state.';return $null} $incomplete=@($state.ServiceAccounts|Where-Object{-not $_.AuthorizationEvidenceComplete -or @($_.UnresolvedPasswordRetrievalTrustees).Count -gt 0}) + if ($incomplete.Count -eq 0) { + Add-MtTestResultDetail -Result 'gMSA managed-password authorization evidence was complete and resolvable for all collected objects.' + return $true + } + $table="| gMSA | Complete | Evidence status | Unresolved SID | Resolution status | Resolution error | Collection error |`n| --- | --- | --- | --- | --- | --- | --- |`n" foreach($gmsa in $incomplete){ $unresolved=@($gmsa.UnresolvedPasswordRetrievalTrustees) @@ -14,4 +19,4 @@ } Add-MtTestResultDetail -Result "Found $($incomplete.Count) gMSA object(s) with incomplete or unresolved authorization evidence.`n`n$table" -Investigate return $true -} \ No newline at end of file +} From 9c6d5ef20d443ead8ad467fdbfdea05d1b83383c Mon Sep 17 00:00:00 2001 From: Mario C Date: Wed, 18 Nov 2026 08:05:38 -0500 Subject: [PATCH 11/11] fix(ad): finalize gMSA authorization review corrections --- .../ad/queries/Get-MtLdapServiceAccount.ps1 | 14 +++++++++++--- .../ADGmsaPasswordAuthorization.Tests.ps1 | 2 ++ tests/ad/security/Test.AD-MSA-02.ps1 | 4 ++-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 index c1f9e4c8c0..db49342b6e 100644 --- a/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 +++ b/powershell/internal/services/ad/queries/Get-MtLdapServiceAccount.ps1 @@ -42,7 +42,7 @@ # Request the authorization descriptor only. The managed-password value is intentionally excluded. $attributes = @( 'distinguishedName','name','sAMAccountName','dNSHostName','whenCreated','whenChanged', - 'objectSid','msDS-GroupMSAMembership' + 'objectSid','userAccountControl','msDS-GroupMSAMembership' ) $entries = @(Invoke-MtLdapSearch -Connection $Connection -SearchBase $SearchBase -Scope Subtree -Filter '(objectClass=msDS-GroupManagedServiceAccount)' -Attributes $attributes) @@ -184,6 +184,14 @@ }) } + $rawUserAccountControl=$entry.userAccountControl + if($null -ne $rawUserAccountControl -and $null -ne $rawUserAccountControl.PSObject.Properties['Value']){$rawUserAccountControl=$rawUserAccountControl.Value} + if($rawUserAccountControl -is [System.Array]){$rawUserAccountControl=@($rawUserAccountControl)[0]} + $enabled=$null + if($null -ne $rawUserAccountControl){ + try{$enabled=(([uint32]$rawUserAccountControl -band [uint32]0x00000002) -eq 0)}catch{$enabled=$null} + } + [pscustomobject][ordered]@{ DistinguishedName=[string]$entry.DistinguishedName Name=[string]$entry.name @@ -194,7 +202,7 @@ Sid=$entry.objectSid ObjectClass=@('msDS-GroupManagedServiceAccount') ServiceAccountType='GroupManagedServiceAccount' - Enabled=$true + Enabled=$enabled PasswordRetrievalTrustees=$trustees BroadPasswordRetrievalTrustees=@($trustees|Where-Object Classification -eq 'Broad') UnresolvedPasswordRetrievalTrustees=@($trustees|Where-Object Classification -eq 'Unresolved') @@ -203,4 +211,4 @@ AuthorizationCollectionError=$descriptorError } }) -} \ No newline at end of file +} diff --git a/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 index 344631be19..24f1b4651e 100644 --- a/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 +++ b/powershell/tests/ad/collection/ADGmsaPasswordAuthorization.Tests.ps1 @@ -56,6 +56,7 @@ $test03 | Should -Match "Area='applications'" sAMAccountName = 'svc-web$' dNSHostName = 'svc-web.contoso.com' objectSid = 'S-1-5-21-1-2-3-1101' + userAccountControl = 2 'msDS-GroupMSAMembership' = [byte[]](1, 2, 3, 4) } } -ParameterFilter { @@ -92,6 +93,7 @@ $test03 | Should -Match "Area='applications'" $result.Count | Should -Be 1 $result[0].PasswordRetrievalTrustees.Count | Should -Be 1 $result[0].BroadPasswordRetrievalTrustees.Count | Should -Be 1 + $result[0].Enabled | Should -BeFalse $trustee = $result[0].BroadPasswordRetrievalTrustees[0] $trustee.Sid | Should -Be 'S-1-5-32-545' diff --git a/tests/ad/security/Test.AD-MSA-02.ps1 b/tests/ad/security/Test.AD-MSA-02.ps1 index cea99e8d99..c5057b6c5b 100644 --- a/tests/ad/security/Test.AD-MSA-02.ps1 +++ b/tests/ad/security/Test.AD-MSA-02.ps1 @@ -9,7 +9,7 @@ $items=@($state.ServiceAccounts|ForEach-Object{$gmsa=$_;@($gmsa.BroadPasswordRetrievalTrustees)|ForEach-Object{[pscustomobject]@{Account=$gmsa.SamAccountName;Trustee=$_.Name;Sid=$_.Sid;Source=$_.AuthorizationSource}}}) $table="| gMSA | Trustee | SID | Source |`n| --- | --- | --- | --- |`n";foreach($i in $items){$table+="| $($i.Account) | $($i.Trustee) | $($i.Sid) | $($i.Source) |`n"} if($items.Count -gt 0){Add-MtTestResultDetail -Result "Found $($items.Count) broad-principal gMSA password-retrieval authorization candidate(s). This proves authorization evidence only; no managed password was requested or used.`n`n$table";return $false} - if($incomplete.Count -gt 0){Add-MtTestResultDetail -Result "No broad trustee was identified, but authorization evidence was incomplete for $($incomplete.Count) gMSA object(s)." -Investigate;return $null} + if($incomplete.Count -gt 0){Add-MtTestResultDetail -Result "No broad trustee was identified, but authorization evidence was incomplete for $($incomplete.Count) gMSA object(s)." -Investigate;return $true} Add-MtTestResultDetail -Result 'No broad-principal gMSA password-retrieval authorization was detected in complete collected evidence.' return $true -} \ No newline at end of file +}