This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
/
LdapBuilder.cs
136 lines (120 loc) · 6.41 KB
/
LdapBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System.Collections.Generic;
using System.Linq;
using SharpHound3.Enums;
namespace SharpHound3
{
internal class LdapBuilder
{
/// <summary>
/// Builds the necessary attributes and ldap query for the specified set of options
/// </summary>
/// <param name="methods"></param>
/// <returns></returns>
internal static LdapQueryData BuildLdapQuery(CollectionMethodResolved methods)
{
var ldapFilterParts = new List<string>();
var ldapProperties = new List<string>();
//We always want these properties to ensure we can at least pass type finding: "samaccounttype", "objectsid", "objectguid", "objectclass"
ldapProperties.AddRange(Helpers.ResolutionProps);
ldapProperties.Add("samaccountname");
//Add this property to check for GMSAs
ldapProperties.Add("msds-groupmsamembership");
//LAPS is weird and several collection methods depend on it, but its easier to just have the property in all our collections
ldapProperties.Add("ms-mcs-admpwdexpirationtime");
//Add the operatingsystem property for WindowsOnly so we can pre-filter hosts
if (Options.Instance.WindowsOnly)
ldapProperties.Add("operatingsystem");
//Group membership collection
if (methods.HasFlag(CollectionMethodResolved.Group))
{
ldapFilterParts.Add("(|(samaccounttype=268435456)(samaccounttype=268435457)(samaccounttype=536870912)(samaccounttype=536870913)(primarygroupid=*))");
ldapProperties.AddRange(new[] { "member", "primarygroupid" });
}
//Computer collection methods: ask for non-disabled computer objects
if (methods.HasFlag(CollectionMethodResolved.LocalAdmin) ||
methods.HasFlag(CollectionMethodResolved.Sessions) ||
methods.HasFlag(CollectionMethodResolved.LoggedOn) || methods.HasFlag(CollectionMethodResolved.RDP) ||
methods.HasFlag(CollectionMethodResolved.DCOM) || methods.HasFlag(CollectionMethodResolved.PSRemote))
{
ldapFilterParts.Add("(&(sAMAccountType=805306369)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))");
}
//ACL Collection
if (methods.HasFlag(CollectionMethodResolved.ACL))
{
ldapFilterParts.Add("(|(samAccountType=805306368)(samAccountType=805306369)(samAccountType=268435456)(samAccountType=268435457)(samAccountType=536870912)(samAccountType=536870913)(objectClass=domain)(&(objectcategory=groupPolicyContainer)(flags=*))(objectcategory=organizationalUnit))");
ldapProperties.AddRange(new[]
{
"ntsecuritydescriptor", "displayname", "name"
});
}
//Trust enumeration
if (methods.HasFlag(CollectionMethodResolved.Trusts))
{
ldapFilterParts.Add("(objectclass=domain)");
}
//Object Properties
if (methods.HasFlag(CollectionMethodResolved.ObjectProps))
{
ldapFilterParts.Add("(|(samaccounttype=268435456)(samaccounttype=268435457)(samaccounttype=536870912)(samaccounttype=536870913)(samaccounttype=805306368)(samaccounttype=805306369)(objectclass=domain)(objectclass=organizationalUnit)(&(objectcategory=groupPolicyContainer)(flags=*)))");
ldapProperties.AddRange(new[]
{
"pwdlastset", "lastlogon", "lastlogontimestamp",
"sidhistory", "useraccountcontrol", "operatingsystem",
"operatingsystemservicepack", "serviceprincipalname", "displayname", "mail", "title",
"homedirectory","description","admincount","userpassword","gpcfilesyspath","objectclass",
"msds-behavior-version","objectguid", "name", "gpoptions", "msds-allowedToDelegateTo", "msDS-AllowedToActOnBehalfOfOtherIdentity", "displayname",
"sidhistory"
});
}
//Container enumeration
if (methods.HasFlag(CollectionMethodResolved.Container))
{
ldapFilterParts.Add("(|(&(&(objectcategory=groupPolicyContainer)(flags=*))(name=*)(gpcfilesyspath=*))(objectcategory=organizationalUnit)(objectClass=domain))");
ldapProperties.AddRange(new[] { "gplink", "gpoptions", "name", "displayname" });
}
//GPO Local group enumeration
if (methods.HasFlag(CollectionMethodResolved.GPOLocalGroup))
{
//ldapFilterParts.Add("(&(&(objectcategory=groupPolicyContainer)(flags=*))(name=*)(gpcfilesyspath=*))");
//ldapProperties.AddRange(new[] {"gpcfilesyspath", "displayname"});
ldapFilterParts.Add("(&(|(objectcategory=organizationalUnit)(objectclass=domain))(gplink=*)(flags=*))");
ldapProperties.AddRange(new[] { "gplink", "name" });
}
//SPN Target Enumeration
if (methods.HasFlag(CollectionMethodResolved.SPNTargets))
{
ldapFilterParts.Add("(&(samaccounttype=805306368)(serviceprincipalname=*))");
ldapProperties.AddRange(new[]
{
"serviceprincipalname"
});
}
//Take our query parts, and join them together
var finalFilter = string.Join("", ldapFilterParts.ToArray());
//Surround the filters with (|), which will OR them together
finalFilter = ldapFilterParts.Count == 1 ? ldapFilterParts[0] : $"(|{finalFilter})";
//Add the user specified filter if it exists
var userFilter = Options.Instance.LdapFilter;
if (userFilter != null)
{
finalFilter = $"(&({finalFilter})({userFilter}))";
}
if (Options.Instance.CollectAllProperties)
{
ldapProperties = new List<string>();
ldapProperties.Add("*");
}
//Distinct the attributes
return new LdapQueryData
{
LdapFilter = finalFilter,
LdapProperties = ldapProperties.Distinct().ToArray()
};
}
}
internal class LdapQueryData
{
public string LdapFilter { get; set; }
public string[] LdapProperties { get; set; }
}
}