Skip to content

Commit

Permalink
fix: account for weird DNs in deleted objects when getting domain info
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Jan 30, 2023
1 parent b06c2f6 commit 24e749e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/CommonLib/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,17 @@ public static string ConvertGuidToHexGuid(string guid)
/// <returns>String representing the domain name of this object</returns>
public static string DistinguishedNameToDomain(string distinguishedName)
{
var idx = distinguishedName.IndexOf("DC=",
StringComparison.CurrentCultureIgnoreCase);
int idx;
if (distinguishedName.ToUpper().Contains("DELETED OBJECTS"))
{
idx = distinguishedName.IndexOf("DC=", 3, StringComparison.Ordinal);
}
else
{
idx = distinguishedName.IndexOf("DC=",
StringComparison.CurrentCultureIgnoreCase);
}

if (idx < 0)
return null;

Expand Down
19 changes: 19 additions & 0 deletions test/unit/LDAPUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public void GetWellKnownPrincipal_WithDomain_ConvertsSID()
Assert.Equal(Label.Group, typedPrincipal.ObjectType);
Assert.Equal($"{_testDomainName}-S-1-5-32-544", typedPrincipal.ObjectIdentifier);
}

[Fact]
public void DistinguishedNameToDomain_RegularObject_CorrectDomain()
{
var result = SharpHoundCommonLib.Helpers.DistinguishedNameToDomain(
"CN=Account Operators,CN=Builtin,DC=testlab,DC=local");
Assert.Equal("TESTLAB.LOCAL", result);

result = SharpHoundCommonLib.Helpers.DistinguishedNameToDomain("DC=testlab,DC=local");
Assert.Equal("TESTLAB.LOCAL", result);
}

[Fact]
public void DistinguishedNameToDomain_DeletedObjects_CorrectDomain()
{
var result = SharpHoundCommonLib.Helpers.DistinguishedNameToDomain(
@"DC=..Deleted-_msdcs.testlab.local\0ADEL:af1f072f-28d7-4b86-9b87-a408bfc9cb0d,CN=Deleted Objects,DC=testlab,DC=local");
Assert.Equal("TESTLAB.LOCAL", result);
}

[Fact]
public void QueryLDAP_With_Exception()
Expand Down

0 comments on commit 24e749e

Please sign in to comment.