Skip to content

Commit

Permalink
Exclude MSAs when searching for Computers affected by GPOs
Browse files Browse the repository at this point in the history
gMSAs and sMSAs have the same samaccounttype as Computers. So they have to be filtered out.
  • Loading branch information
JonasBK committed May 3, 2023
1 parent 1ce194f commit 1bcf1a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/CommonLib/LDAPQueries/LDAPFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ public LDAPFilter AddContainers(params string[] conditions)

/// <summary>
/// Add a filter that will include Computer objects
///
/// Note that gMSAs and sMSAs have this samaccounttype as well
/// </summary>
/// <param name="conditions"></param>
/// <returns></returns>
Expand All @@ -164,6 +166,17 @@ public LDAPFilter AddSchemaID(params string[] conditions)
return this;
}

/// <summary>
/// Add a filter that will include Computer objects but exclude gMSA and sMSA objects
/// </summary>
/// <param name="conditions"></param>
/// <returns></returns>
public LDAPFilter AddComputersWoutMSAs(params string[] conditions)
{
_filterParts.Add(BuildString("(&(samaccounttype=805306369)(!(objectclass=msDS-GroupManagedServiceAccount))(!(objectclass=msDS-ManagedServiceAccount)))", conditions));
return this;
}

/// <summary>
/// Adds a generic user specified filter
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/CommonLib/Processors/GPOLocalGroupProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public async Task<ResultingGPOChanges> ReadGPOLocalGroups(string gpLink, string
// Its cheaper to fetch the affected computers from LDAP first and then process the GPLinks
var options = new LDAPQueryOptions
{
Filter = new LDAPFilter().AddComputers().GetFilter(),
Filter = new LDAPFilter().AddComputersWoutMSAs().GetFilter(),
Scope = SearchScope.Subtree,
Properties = CommonProperties.ObjectSID,
AdsPath = distinguishedName
Expand Down
2 changes: 1 addition & 1 deletion test/unit/GPOLocalGroupProcessorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public async Task GPOLocalGroupProcessor_ReadGPOLocalGroups_Null_Gpcfilesyspath(
mockSearchResults.Add(mockSearchResultEntry.Object);
mockLDAPUtils.Setup(x => x.QueryLDAP(new LDAPQueryOptions
{
Filter = "(samaccounttype=805306369)",
Filter = "(&(samaccounttype=805306369)(!(objectclass=msDS-GroupManagedServiceAccount))(!(objectclass=msDS-ManagedServiceAccount)))",
Scope = SearchScope.Subtree,
Properties = CommonProperties.ObjectSID,
AdsPath = null
Expand Down

0 comments on commit 1bcf1a8

Please sign in to comment.