Skip to content

Instantly share code, notes, and snippets.

@zjorz
Last active July 10, 2025 17:33
Show Gist options
  • Select an option

  • Save zjorz/e49657dab853470b683908ab32a0f51f to your computer and use it in GitHub Desktop.

Select an option

Save zjorz/e49657dab853470b683908ab32a0f51f to your computer and use it in GitHub Desktop.
Bad Successor - VIEW DATA IN ATTRIBUTES "msDS-groupMSAMembership", "msDS-DelegatedMSAState", "msDS-ManagedAccountPrecededByLink"
# ================================================================================================================
# VIEW DATA IN ATTRIBUTES "msDS-groupMSAMembership", "msDS-DelegatedMSAState", "msDS-ManagedAccountPrecededByLink"
# ================================================================================================================
#
# SOURCE: https://gist.github.com/zjorz/e49657dab853470b683908ab32a0f51f
#
<#
MIT License
Copyright (c) 2025 Semperis Technologies Inc. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
DISCLAIMER: This project is licensed under the terms of the MIT license, and
is provided for educational and informational purposes only. It is intended
to promote awareness and responsible remediation of security vulnerabilities
that may exist on systems you own or are authorized to test. Unauthorized use
of this information for malicious purposes, exploitation, or unlawful access
is strictly prohibited. Semperis does not endorse or condone any illegal
activity and disclaims any liability arising from misuse of the material.
Additionally, Semperis does not guarantee the accuracy or completeness of the
content and assumes no liability for any damages resulting from its use.
#>
# The DN Of The dMSA To Target
$dMSADN = "<DN Of The dMSA To Target>"
Invoke-Command -ArgumentList $dMSADN -ScriptBlock {
Param (
$dMSADN
)
Clear-Host
$thisADDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain()
$rwdcPDCFQDN = $thisADDomain.PdcRoleOwner.Name
Add-Type -AssemblyName System.DirectoryServices.Protocols -ErrorAction Stop
[Timespan]$requestTimeout = (New-Object System.TimeSpan(0,0,120)) # Default: 120 seconds
$ldapDirectoryID = New-Object -TypeName System.DirectoryServices.Protocols.LdapDirectoryIdentifier -ArgumentList $ldapServerFQDN, $ldapServerPort, $true, $false
$ldapSearchRequest = New-Object System.DirectoryServices.Protocols.SearchRequest
$ldapAuthType = [System.DirectoryServices.Protocols.AuthType]::Negotiate
$ldapConnection = New-Object -TypeName System.DirectoryServices.Protocols.LdapConnection -ArgumentList $ldapDirectoryID, $null, $ldapAuthType
$ldapConnection.SessionOptions.Sealing = $true
$ldapConnection.SessionOptions.Signing = $true
$ldapSearchRequest.DistinguishedName = $dMSADN
$ldapSearchRequest.Scope = [System.DirectoryServices.Protocols.SearchScope]::Base
$securityMasks = [System.DirectoryServices.Protocols.SecurityMasks]'Owner' -bor [System.DirectoryServices.Protocols.SecurityMasks]'Group' -bor [System.DirectoryServices.Protocols.SecurityMasks]'Dacl' -bor [System.DirectoryServices.Protocols.SecurityMasks]'Sacl'
$sdFlagLdapControl = New-Object System.DirectoryServices.Protocols.SecurityDescriptorFlagControl($securityMasks)
[void]($ldapSearchRequest.Controls.Add($sdFlagLdapControl))
[void]($ldapSearchRequest.Attributes.Add("distinguishedName"))
[void]($ldapSearchRequest.Attributes.Add("sAMAccountName"))
[void]($ldapSearchRequest.Attributes.Add("msDS-groupMSAMembership"))
[void]($ldapSearchRequest.Attributes.Add("msDS-DelegatedMSAState"))
[void]($ldapSearchRequest.Attributes.Add("msDS-ManagedAccountPrecededByLink"))
[System.DirectoryServices.Protocols.SearchResponse]$ldapSearchResponse = $ldapConnection.SendRequest($ldapSearchRequest, $requestTimeout)
Write-Host "Distinguished Name...: $($ldapSearchResponse.Entries.Attributes.'distinguishedname'[0])" -ForegroundColor Yellow
Write-Host "Sam Account Name.....: $($ldapSearchResponse.Entries.Attributes.'samaccountname'[0])" -ForegroundColor Yellow
If (-not [string]::IsNullOrEmpty($ldapSearchResponse.Entries.Attributes."msds-groupmsamembership")) {
$adSecurity = New-Object System.DirectoryServices.ActiveDirectorySecurity
$adSecurity.SetSecurityDescriptorBinaryForm($ldapSearchResponse.Entries.Attributes.'msds-groupmsamembership'[0])
$adSecurity.Access.IdentityReference.Value | ForEach-Object {
Write-Host "Pwd Allowed 2 Get By.: $($_)" -ForegroundColor Yellow
}
}
Write-Host "dMSA State...........: $($ldapSearchResponse.Entries.Attributes.'msds-delegatedmsastate'[0])" -ForegroundColor Yellow
If (-not [string]::IsNullOrEmpty($ldapSearchResponse.Entries.Attributes."msds-managedaccountprecededbylink")) {
Write-Host "Linked Account DN....: $($ldapSearchResponse.Entries.Attributes.'msds-managedaccountprecededbylink'[0])" -ForegroundColor Yellow
}
Write-Host ""
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment