Skip to content

Commit 6b83bf3

Browse files
authored
Trust account (#207)
* collect trust accounts * add netbios for domains
1 parent 9d1e859 commit 6b83bf3

9 files changed

Lines changed: 49 additions & 6 deletions

File tree

src/CommonLib/Enums/LDAPProperties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static class LDAPProperties
9494
public const string LockoutDuration = "lockoutduration";
9595
public const string LockoutThreshold = "lockoutthreshold";
9696
public const string LockOutObservationWindow = "lockoutobservationwindow";
97+
public const string PrincipalName = "msds-principalname";
9798
public const string GroupType = "grouptype";
9899
}
99100
}

src/CommonLib/Helpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace SharpHoundCommonLib {
1616
public static class Helpers {
1717
private static readonly HashSet<string> Groups = new() { "268435456", "268435457", "536870912", "536870913" };
1818
private static readonly HashSet<string> Computers = new() { "805306369" };
19-
private static readonly HashSet<string> Users = new() { "805306368" };
19+
private static readonly HashSet<string> Users = new() { "805306368", "805306370" };
2020

2121
private static readonly Regex DCReplaceRegex = new("DC=", RegexOptions.IgnoreCase | RegexOptions.Compiled);
2222
private static readonly Regex SPNRegex = new(@".*\/.*", RegexOptions.Compiled);

src/CommonLib/LdapQueries/CommonProperties.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public static class CommonProperties
6060
LDAPProperties.SupportedEncryptionTypes, LDAPProperties.DSHeuristics,
6161
LDAPProperties.MinPwdLength, LDAPProperties.PwdProperties, LDAPProperties.MinPwdAge,
6262
LDAPProperties.MaxPwdAge, LDAPProperties.PwdHistoryLength, LDAPProperties.LockoutDuration,
63-
LDAPProperties.LockoutThreshold, LDAPProperties.LockOutObservationWindow, LDAPProperties.GroupType
63+
LDAPProperties.LockoutThreshold, LDAPProperties.LockOutObservationWindow, LDAPProperties.GroupType,
64+
LDAPProperties.PrincipalName
6465
};
6566

6667
public static readonly string[] ContainerProps =

src/CommonLib/LdapQueries/LdapFilter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public LdapFilter AddAllObjects(params string[] conditions) {
5656
/// <param name="conditions"></param>
5757
/// <returns></returns>
5858
public LdapFilter AddUsers(params string[] conditions) {
59-
_filterParts.Add(BuildString("(samaccounttype=805306368)", conditions));
59+
_filterParts.Add(BuildString("(|(samaccounttype=805306368)(samaccounttype=805306370))", conditions));
6060

6161
return this;
6262
}

src/CommonLib/Processors/LdapPropertyProcessor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,15 @@ public async Task<Dictionary<string, object>> ReadDomainProperties(IDirectoryObj
116116
if (!entry.TryGetLongProperty(LDAPProperties.DomainFunctionalLevel, out var functionalLevel)) {
117117
functionalLevel = -1;
118118
}
119-
120119
props.Add("functionallevel", FunctionalLevelToString((int)functionalLevel));
121120

121+
if (entry.TryGetProperty(LDAPProperties.PrincipalName, out var principalname)) {
122+
if (!string.IsNullOrEmpty(principalname) && principalname.IndexOf('\\') > 0) {
123+
var netBios = principalname.Split('\\')[0];
124+
props.Add("netbios", netBios);
125+
}
126+
}
127+
122128
var dn = entry.GetProperty(LDAPProperties.DistinguishedName);
123129
var dsh = await _utils.GetDSHueristics(domain, dn);
124130
props.Add("dsheuristics", dsh.DSHeuristics);

test/unit/CommonLibHelperTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ public void SamAccountTypeToType_ValidString_CorrectLabel() {
8282
(accountType: "536870912", label: Label.Group),
8383
(accountType: "536870913", label: Label.Group),
8484
(accountType: "805306369", Label.Computer),
85-
(accountType: "805306368", Label.User)
85+
(accountType: "805306368", Label.User),
86+
(accountType: "805306370", Label.User)
8687
};
8788

8889
foreach (var e in accountTypeLookup) {

test/unit/LDAPFilterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public void LDAPFilter_GetFilterList()
6464
IEnumerable<string> filters = test.GetFilterList();
6565

6666
int i = 0;
67-
string userFilter = "(samaccounttype=805306368)";
67+
string userFilter = "(|(samaccounttype=805306368)(samaccounttype=805306370))";
6868
string computerFilter = "(samaccounttype=805306369)";
6969
string[] expected = {userFilter, computerFilter};
7070

test/unit/LDAPUtilsTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,31 @@ public async Task Test_ResolveSearchResult_MSAGMSA() {
224224
Assert.False(result.Deleted);
225225
}
226226

227+
[Fact]
228+
public async Task Test_ResolveSearchResult_TrustAccount() {
229+
var utils = new MockLdapUtils();
230+
var attribs = new Dictionary<string, object> {
231+
{ LDAPProperties.ObjectClass, new[] { "top"} },
232+
{ LDAPProperties.SAMAccountType, "805306370" },
233+
{ LDAPProperties.SAMAccountName, "DOMAIN1$" }
234+
};
235+
236+
const string sid = "S-1-5-21-3130019616-2776909439-2417379446-2105";
237+
const string dn = "CN=DOMAIN1$,CN=USERS,DC=TESTLAB,DC=LOCAL";
238+
var guid = new Guid().ToString();
239+
240+
var mock = new MockDirectoryObject(dn, attribs, sid, guid);
241+
242+
var (success, result) = await LdapUtils.ResolveSearchResult(mock, utils);
243+
Assert.True(success);
244+
Assert.Equal(sid, result.ObjectId);
245+
Assert.Equal(Label.User, result.ObjectType);
246+
Assert.Equal("DOMAIN1$@TESTLAB.LOCAL", result.DisplayName);
247+
Assert.Equal("S-1-5-21-3130019616-2776909439-2417379446", result.DomainSid);
248+
Assert.Equal("TESTLAB.LOCAL", result.Domain);
249+
Assert.False(result.Deleted);
250+
}
251+
227252
[Fact]
228253
public async Task Test_ResolveHostToSid_BlankHost() {
229254
var spn = "MSSQLSvc/:1433";

test/unit/LdapPropertyTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,15 @@ public async void LDAPPropertyProcessor_ReadDomainProperties<T>(MockDirectoryObj
11391139
}, "S-1-5-21-3130019616-2776909439-2417379446",""),
11401140
"maxpwdage",
11411141
"12 days, 23 hours, 25 minutes, 10 seconds"
1142+
},
1143+
new object[]
1144+
{
1145+
new MockDirectoryObject("DC\u003dtestlab,DC\u003dlocal", new Dictionary<string, object>
1146+
{
1147+
{LDAPProperties.PrincipalName, "TESTLAB\\S-1-5-21-3130019616-2776909439-2417379446"}
1148+
}, "S-1-5-21-3130019616-2776909439-2417379446",""),
1149+
"netbios",
1150+
"TESTLAB"
11421151
}
11431152
};
11441153

0 commit comments

Comments
 (0)