-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathACLProcessor.cs
More file actions
495 lines (445 loc) · 22 KB
/
ACLProcessor.cs
File metadata and controls
495 lines (445 loc) · 22 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Security.AccessControl;
using System.Security.Principal;
using Microsoft.Extensions.Logging;
using SharpHoundCommonLib.Enums;
using SharpHoundCommonLib.OutputTypes;
using SearchScope = System.DirectoryServices.Protocols.SearchScope;
namespace SharpHoundCommonLib.Processors
{
public class ACLProcessor
{
private static readonly Dictionary<Label, string> BaseGuids;
private static readonly ConcurrentDictionary<string, string> GuidMap = new();
private static bool _isCacheBuilt;
private readonly ILogger _log;
private readonly ILDAPUtils _utils;
static ACLProcessor()
{
//Create a dictionary with the base GUIDs of each object type
BaseGuids = new Dictionary<Label, string>
{
{ Label.User, "bf967aba-0de6-11d0-a285-00aa003049e2" },
{ Label.Computer, "bf967a86-0de6-11d0-a285-00aa003049e2" },
{ Label.Group, "bf967a9c-0de6-11d0-a285-00aa003049e2" },
{ Label.Domain, "19195a5a-6da0-11d0-afd3-00c04fd930c9" },
{ Label.GPO, "f30e3bc2-9ff0-11d1-b603-0000f80367c1" },
{ Label.OU, "bf967aa5-0de6-11d0-a285-00aa003049e2" },
{ Label.Container, "bf967a8b-0de6-11d0-a285-00aa003049e2" }
};
}
public ACLProcessor(ILDAPUtils utils, bool noGuidCache = false, ILogger log = null, string domain = null)
{
_utils = utils;
_log = log ?? Logging.LogProvider.CreateLogger("ACLProc");
if (!noGuidCache)
BuildGUIDCache(domain);
}
/// <summary>
/// Builds a mapping of GUID -> Name for LDAP rights. Used for rights that are created using an extended schema such as
/// LAPS
/// </summary>
private void BuildGUIDCache(string domain)
{
if (_isCacheBuilt)
return;
var forest = _utils.GetForest(domain);
if (forest == null)
{
_log.LogError("BuildGUIDCache - Unable to resolve forest");
return;
}
var schema = forest.Schema.Name;
if (string.IsNullOrEmpty(schema))
{
_log.LogError("BuildGUIDCache - Schema string is null or empty");
return;
}
_log.LogTrace("Requesting schema from {Schema}", schema);
foreach (var entry in _utils.QueryLDAP("(schemaIDGUID=*)", SearchScope.Subtree,
new[] { LDAPProperties.SchemaIDGUID, LDAPProperties.Name }, adsPath: schema))
{
var name = entry.GetProperty(LDAPProperties.Name)?.ToLower();
var guid = new Guid(entry.GetByteProperty(LDAPProperties.SchemaIDGUID)).ToString();
GuidMap.TryAdd(guid, name);
}
_log.LogTrace("BuildGUIDCache - Successfully grabbed schema");
_isCacheBuilt = true;
}
/// <summary>
/// Helper function to use commonlib types in IsACLProtected
/// </summary>
/// <param name="entry"></param>
/// <returns></returns>
public bool IsACLProtected(ISearchResultEntry entry)
{
var ntsd = entry.GetByteProperty(LDAPProperties.SecurityDescriptor);
return IsACLProtected(ntsd);
}
/// <summary>
/// Gets the protection state of the access control list
/// </summary>
/// <param name="ntSecurityDescriptor"></param>
/// <returns></returns>
public bool IsACLProtected(byte[] ntSecurityDescriptor)
{
if (ntSecurityDescriptor == null)
return false;
var descriptor = _utils.MakeSecurityDescriptor();
descriptor.SetSecurityDescriptorBinaryForm(ntSecurityDescriptor);
return descriptor.AreAccessRulesProtected();
}
/// <summary>
/// Helper function to use common lib types and pass appropriate vars to ProcessACL
/// </summary>
/// <param name="result"></param>
/// <param name="searchResult"></param>
/// <returns></returns>
public IEnumerable<ACE> ProcessACL(ResolvedSearchResult result, ISearchResultEntry searchResult)
{
var descriptor = searchResult.GetByteProperty(LDAPProperties.SecurityDescriptor);
var domain = result.Domain;
var type = result.ObjectType;
var hasLaps = searchResult.HasLAPS();
var name = result.DisplayName;
return ProcessACL(descriptor, domain, type, hasLaps, name);
}
/// <summary>
/// Read's the ntSecurityDescriptor from a SearchResultEntry and processes the ACEs in the ACL, filtering out ACEs that
/// BloodHound is not interested in
/// </summary>
/// <param name="ntSecurityDescriptor"></param>
/// <param name="objectDomain"></param>
/// <param name="objectName"></param>
/// <param name="objectType"></param>
/// <param name="hasLaps"></param>
/// <returns></returns>
public IEnumerable<ACE> ProcessACL(byte[] ntSecurityDescriptor, string objectDomain,
Label objectType,
bool hasLaps, string objectName = "")
{
if (ntSecurityDescriptor == null)
{
_log.LogDebug("Security Descriptor is null for {Name}", objectName);
yield break;
}
var descriptor = _utils.MakeSecurityDescriptor();
descriptor.SetSecurityDescriptorBinaryForm(ntSecurityDescriptor);
var ownerSid = PreProcessSID(descriptor.GetOwner(typeof(SecurityIdentifier)));
if (ownerSid != null)
{
var resolvedOwner = _utils.ResolveIDAndType(ownerSid, objectDomain);
if (resolvedOwner != null)
yield return new ACE
{
PrincipalType = resolvedOwner.ObjectType,
PrincipalSID = resolvedOwner.ObjectIdentifier,
RightName = EdgeNames.Owns,
IsInherited = false
};
}
else
{
_log.LogDebug("Owner is null for {Name}", objectName);
}
foreach (var ace in descriptor.GetAccessRules(true, true, typeof(SecurityIdentifier)))
{
if (ace == null)
{
_log.LogTrace("Skipping null ACE for {Name}", objectName);
continue;
}
if (ace.AccessControlType() == AccessControlType.Deny)
{
_log.LogTrace("Skipping deny ACE for {Name}", objectName);
continue;
}
if (!ace.IsAceInheritedFrom(BaseGuids[objectType]))
{
_log.LogTrace("Skipping ACE with unmatched GUID/inheritance for {Name}", objectName);
continue;
}
var ir = ace.IdentityReference();
var principalSid = PreProcessSID(ir);
if (principalSid == null)
{
_log.LogTrace("Pre-Process excluded SID {SID} on {Name}", ir ?? "null", objectName);
continue;
}
var resolvedPrincipal = _utils.ResolveIDAndType(principalSid, objectDomain);
var aceRights = ace.ActiveDirectoryRights();
//Lowercase this just in case. As far as I know it should always come back that way anyways, but better safe than sorry
var aceType = ace.ObjectType().ToString().ToLower();
var inherited = ace.IsInherited();
GuidMap.TryGetValue(aceType, out var mappedGuid);
_log.LogTrace("Processing ACE with rights {Rights} and guid {GUID} on object {Name}", aceRights,
aceType, objectName);
//GenericAll applies to every object
if (aceRights.HasFlag(ActiveDirectoryRights.GenericAll))
{
if (aceType is ACEGuids.AllGuid or "")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.GenericAll
};
//This is a special case. If we don't continue here, every other ACE will match because GenericAll includes all other permissions
continue;
}
//WriteDACL and WriteOwner are always useful no matter what the object type is as well because they enable all other attacks
if (aceRights.HasFlag(ActiveDirectoryRights.WriteDacl))
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.WriteDacl
};
if (aceRights.HasFlag(ActiveDirectoryRights.WriteOwner))
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.WriteOwner
};
//Cool ACE courtesy of @rookuu. Allows a principal to add itself to a group and no one else
if (aceRights.HasFlag(ActiveDirectoryRights.Self) &&
!aceRights.HasFlag(ActiveDirectoryRights.WriteProperty) &&
!aceRights.HasFlag(ActiveDirectoryRights.GenericWrite) && objectType == Label.Group)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AddSelf
};
//Process object type specific ACEs. Extended rights apply to users, domains, and computers
if (aceRights.HasFlag(ActiveDirectoryRights.ExtendedRight))
{
if (objectType == Label.Domain)
{
if (aceType == ACEGuids.DSReplicationGetChanges)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.GetChanges
};
else if (aceType == ACEGuids.DSReplicationGetChangesAll)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.GetChangesAll
};
else if (aceType == ACEGuids.DSReplicationGetChangesInFilteredSet)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.GetChangesInFilteredSet
};
else if (aceType is ACEGuids.AllGuid or "")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AllExtendedRights
};
}
else if (objectType == Label.User)
{
if (aceType == ACEGuids.UserForceChangePassword)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.ForceChangePassword
};
else if (aceType is ACEGuids.AllGuid or "")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AllExtendedRights
};
}
else if (objectType == Label.Computer)
{
//ReadLAPSPassword is only applicable if the computer actually has LAPS. Check the world readable property ms-mcs-admpwdexpirationtime
if (hasLaps)
{
if (aceType is ACEGuids.AllGuid or "")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AllExtendedRights
};
else if (mappedGuid is "ms-mcs-admpwd")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.ReadLAPSPassword
};
}
}
}
//GenericWrite encapsulates WriteProperty, so process them in tandem to avoid duplicate edges
if (aceRights.HasFlag(ActiveDirectoryRights.GenericWrite) ||
aceRights.HasFlag(ActiveDirectoryRights.WriteProperty))
{
if (objectType is Label.User or Label.Group or Label.Computer or Label.GPO)
if (aceType is ACEGuids.AllGuid or "")
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.GenericWrite
};
if (objectType == Label.User && aceType == ACEGuids.WriteSPN)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.WriteSPN
};
else if (objectType == Label.Computer && aceType == ACEGuids.WriteAllowedToAct)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AddAllowedToAct
};
else if (objectType == Label.Computer && aceType == ACEGuids.UserAccountRestrictions && !resolvedPrincipal.ObjectIdentifier.EndsWith("-512"))
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.WriteAccountRestrictions
};
else if (objectType == Label.Group && aceType == ACEGuids.WriteMember)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AddMember
};
else if (objectType is Label.User or Label.Computer && aceType == ACEGuids.AddKeyPrincipal)
yield return new ACE
{
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = inherited,
RightName = EdgeNames.AddKeyCredentialLink
};
}
}
}
/// <summary>
/// Helper function to use commonlib types and pass to ProcessGMSAReaders
/// </summary>
/// <param name="resolvedSearchResult"></param>
/// <param name="searchResultEntry"></param>
/// <returns></returns>
public IEnumerable<ACE> ProcessGMSAReaders(ResolvedSearchResult resolvedSearchResult,
ISearchResultEntry searchResultEntry)
{
var descriptor = searchResultEntry.GetByteProperty(LDAPProperties.GroupMSAMembership);
var domain = resolvedSearchResult.Domain;
var name = resolvedSearchResult.DisplayName;
return ProcessGMSAReaders(descriptor, name, domain);
}
/// <summary>
/// ProcessGMSAMembership with no account name
/// </summary>
/// <param name="groupMSAMembership"></param>
/// <param name="objectDomain"></param>
/// <returns></returns>
public IEnumerable<ACE> ProcessGMSAReaders(byte[] groupMSAMembership, string objectDomain)
{
return ProcessGMSAReaders(groupMSAMembership, "", objectDomain);
}
/// <summary>
/// Processes the msds-groupmsamembership property and returns ACEs representing principals that can read the GMSA
/// password from an object
/// </summary>
/// <param name="groupMSAMembership"></param>
/// <param name="objectName"></param>
/// <param name="objectDomain"></param>
/// <returns></returns>
public IEnumerable<ACE> ProcessGMSAReaders(byte[] groupMSAMembership, string objectName, string objectDomain)
{
if (groupMSAMembership == null)
{
_log.LogTrace("GMSA bytes are null for {Name}", objectName);
yield break;
}
var descriptor = _utils.MakeSecurityDescriptor();
descriptor.SetSecurityDescriptorBinaryForm(groupMSAMembership);
foreach (var ace in descriptor.GetAccessRules(true, true, typeof(SecurityIdentifier)))
{
if (ace == null)
{
_log.LogTrace("Skipping null GMSA ACE for {Name}", objectName);
continue;
}
if (ace.AccessControlType() == AccessControlType.Deny)
{
_log.LogTrace("Skipping deny GMSA ACE for {Name}", objectName);
continue;
}
var ir = ace.IdentityReference();
var principalSid = PreProcessSID(ir);
if (principalSid == null)
{
_log.LogTrace("Pre-Process excluded SID {SID} on {Name}", ir ?? "null", objectName);
continue;
}
_log.LogTrace("Processing GMSA ACE with principal {Principal}", principalSid);
var resolvedPrincipal = _utils.ResolveIDAndType(principalSid, objectDomain);
if (resolvedPrincipal != null)
yield return new ACE
{
RightName = EdgeNames.ReadGMSAPassword,
PrincipalType = resolvedPrincipal.ObjectType,
PrincipalSID = resolvedPrincipal.ObjectIdentifier,
IsInherited = ace.IsInherited()
};
}
}
/// <summary>
/// Removes some commonly seen SIDs that have no use in the schema
/// </summary>
/// <param name="sid"></param>
/// <returns></returns>
private static string PreProcessSID(string sid)
{
sid = sid?.ToUpper();
if (sid != null)
//Ignore Local System/Creator Owner/Principal Self
return sid is "S-1-5-18" or "S-1-3-0" or "S-1-5-10" ? null : sid;
return null;
}
}
}