-
Notifications
You must be signed in to change notification settings - Fork 254
Expand file tree
/
Copy pathObjectProcessors.cs
More file actions
743 lines (632 loc) · 35.4 KB
/
ObjectProcessors.cs
File metadata and controls
743 lines (632 loc) · 35.4 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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Sharphound.Client;
using SharpHoundCommonLib;
using SharpHoundCommonLib.Enums;
using SharpHoundCommonLib.OutputTypes;
using SharpHoundCommonLib.Processors;
using Container = SharpHoundCommonLib.OutputTypes.Container;
using Group = SharpHoundCommonLib.OutputTypes.Group;
using Label = SharpHoundCommonLib.Enums.Label;
namespace Sharphound.Runtime
{
public class ObjectProcessors
{
private const string StatusSuccess = "Success";
private readonly ACLProcessor _aclProcessor;
private readonly CertAbuseProcessor _certAbuseProcessor;
private readonly CancellationToken _cancellationToken;
private readonly ComputerAvailability _computerAvailability;
private readonly ComputerSessionProcessor _computerSessionProcessor;
private readonly ContainerProcessor _containerProcessor;
private readonly IContext _context;
private readonly DCRegistryProcessor _dCRegistryProcessor;
private readonly DomainTrustProcessor _domainTrustProcessor;
private readonly GroupProcessor _groupProcessor;
private readonly LDAPPropertyProcessor _ldapPropertyProcessor;
private readonly GPOLocalGroupProcessor _gpoLocalGroupProcessor;
private readonly GPOLmCompatibilityLevelProcessor _gpoLmCompatibilityLevelProcessor;
private readonly UserRightsAssignmentProcessor _userRightsAssignmentProcessor;
private readonly LocalGroupProcessor _localGroupProcessor;
private readonly ILogger _log;
private readonly ResolvedCollectionMethod _methods;
private readonly SPNProcessors _spnProcessor;
public ObjectProcessors(IContext context, ILogger log)
{
_context = context;
_aclProcessor = new ACLProcessor(context.LDAPUtils);
_spnProcessor = new SPNProcessors(context.LDAPUtils);
_ldapPropertyProcessor = new LDAPPropertyProcessor(context.LDAPUtils);
_domainTrustProcessor = new DomainTrustProcessor(context.LDAPUtils);
_computerAvailability = new ComputerAvailability(context.PortScanTimeout, skipPortScan: context.Flags.SkipPortScan, skipPasswordCheck: context.Flags.SkipPasswordAgeCheck);
_certAbuseProcessor = new CertAbuseProcessor(context.LDAPUtils);
_dCRegistryProcessor = new DCRegistryProcessor(context.LDAPUtils);
_computerSessionProcessor = new ComputerSessionProcessor(context.LDAPUtils, doLocalAdminSessionEnum: context.Flags.DoLocalAdminSessionEnum, localAdminUsername: context.LocalAdminUsername, localAdminPassword: context.LocalAdminPassword);
_groupProcessor = new GroupProcessor(context.LDAPUtils);
_containerProcessor = new ContainerProcessor(context.LDAPUtils);
_gpoLocalGroupProcessor = new GPOLocalGroupProcessor(context.LDAPUtils);
_gpoLmCompatibilityLevelProcessor = new GPOLmCompatibilityLevelProcessor(context.LDAPUtils);
_userRightsAssignmentProcessor = new UserRightsAssignmentProcessor(context.LDAPUtils);
_localGroupProcessor = new LocalGroupProcessor(context.LDAPUtils);
_methods = context.ResolvedCollectionMethods;
_cancellationToken = context.CancellationTokenSource.Token;
_log = log;
}
internal async Task<OutputBase> ProcessObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult, Channel<CSVComputerStatus> compStatusChannel)
{
switch (resolvedSearchResult.ObjectType)
{
case Label.User:
return await ProcessUserObject(entry, resolvedSearchResult);
case Label.Computer:
return await ProcessComputerObject(entry, resolvedSearchResult, compStatusChannel);
case Label.Group:
return ProcessGroupObject(entry, resolvedSearchResult);
case Label.GPO:
return await ProcessGPOObject(entry, resolvedSearchResult);
case Label.Domain:
return await ProcessDomainObject(entry, resolvedSearchResult);
case Label.OU:
return await ProcessOUObject(entry, resolvedSearchResult);
case Label.Container:
case Label.Configuration:
return ProcessContainerObject(entry, resolvedSearchResult);
case Label.RootCA:
return await ProcessRootCA(entry, resolvedSearchResult);
case Label.AIACA:
return await ProcessAIACA(entry, resolvedSearchResult);
case Label.EnterpriseCA:
return await ProcessEnterpriseCA(entry, resolvedSearchResult);
case Label.NTAuthStore:
return await ProcessNTAuthStore(entry, resolvedSearchResult);
case Label.CertTemplate:
return await ProcessCertTemplate(entry, resolvedSearchResult);
case Label.Base:
return null;
default:
throw new ArgumentOutOfRangeException();
}
}
private async Task<User> ProcessUserObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new User
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
ret.Properties.Add("samaccountname", entry.GetProperty(LDAPProperties.SAMAccountName));
if (entry.IsMSA()) ret.Properties.Add("msa", true);
if (entry.IsGMSA()) ret.Properties.Add("gmsa", true);
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
var aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry);
var gmsa = entry.GetByteProperty(LDAPProperties.GroupMSAMembership);
ret.Aces = aces.Concat(_aclProcessor.ProcessGMSAReaders(gmsa, resolvedSearchResult.Domain)).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.Group) != 0)
{
var pg = entry.GetProperty(LDAPProperties.PrimaryGroupID);
ret.PrimaryGroupSID = GroupProcessor.GetPrimaryGroupInfo(pg, resolvedSearchResult.ObjectId);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
var userProps = await _ldapPropertyProcessor.ReadUserProperties(entry);
ret.Properties = ContextUtils.Merge(ret.Properties, userProps.Props);
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
ret.HasSIDHistory = userProps.SidHistory;
ret.AllowedToDelegate = userProps.AllowedToDelegate;
}
if ((_methods & ResolvedCollectionMethod.SPNTargets) != 0)
{
var spn = entry.GetArrayProperty(LDAPProperties.ServicePrincipalNames);
var targets = new List<SPNPrivilege>();
var enumerator = _spnProcessor.ReadSPNTargets(spn, entry.DistinguishedName)
.GetAsyncEnumerator(_cancellationToken);
while (await enumerator.MoveNextAsync()) targets.Add(enumerator.Current);
ret.SPNTargets = targets.ToArray();
}
if ((_methods & ResolvedCollectionMethod.Container) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
private async Task<Computer> ProcessComputerObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult, Channel<CSVComputerStatus> compStatusChannel)
{
var ret = new Computer
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
ret.Properties.Add("samaccountname", entry.GetProperty(LDAPProperties.SAMAccountName));
var hasLaps = entry.HasLAPS();
ret.Properties.Add("haslaps", hasLaps);
ret.IsDC = resolvedSearchResult.IsDomainController;
ret.DomainSID = resolvedSearchResult.DomainSid;
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.Group) != 0)
{
var pg = entry.GetProperty(LDAPProperties.PrimaryGroupID);
ret.PrimaryGroupSID = GroupProcessor.GetPrimaryGroupInfo(pg, resolvedSearchResult.ObjectId);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
var computerProps = await _ldapPropertyProcessor.ReadComputerProperties(entry);
ret.Properties = ContextUtils.Merge(ret.Properties, computerProps.Props);
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
ret.AllowedToDelegate = computerProps.AllowedToDelegate;
ret.AllowedToAct = computerProps.AllowedToAct;
ret.HasSIDHistory = computerProps.SidHistory;
ret.DumpSMSAPassword = computerProps.DumpSMSAPassword;
}
if ((_methods & ResolvedCollectionMethod.Container) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
if (!_methods.IsComputerCollectionSet())
return ret;
var apiName = _context.RealDNSName != null
? entry.GetDNSName(_context.RealDNSName)
: resolvedSearchResult.DisplayName;
var availability = await _computerAvailability.IsComputerAvailable(resolvedSearchResult, entry);
if (!availability.Connectable)
{
await compStatusChannel.Writer.WriteAsync(availability.GetCSVStatus(resolvedSearchResult.DisplayName),
_cancellationToken);
ret.Status = availability;
return ret;
}
// DCRegistry
if (resolvedSearchResult.IsDomainController &
(_methods & ResolvedCollectionMethod.DCRegistry) != 0)
{
DCRegistryData dCRegistryData = new()
{
CertificateMappingMethods = _dCRegistryProcessor.GetCertificateMappingMethods(apiName),
StrongCertificateBindingEnforcement = _dCRegistryProcessor.GetStrongCertificateBindingEnforcement(apiName)
};
ret.DCRegistryData = dCRegistryData;
}
var samAccountName = entry.GetProperty(LDAPProperties.SAMAccountName)?.TrimEnd('$');
if ((_methods & ResolvedCollectionMethod.Session) != 0)
{
await _context.DoDelay();
var sessionResult = await _computerSessionProcessor.ReadUserSessions(apiName,
resolvedSearchResult.ObjectId, resolvedSearchResult.Domain);
ret.Sessions = sessionResult;
if (_context.Flags.DumpComputerStatus)
await compStatusChannel.Writer.WriteAsync(new CSVComputerStatus
{
Status = sessionResult.Collected ? StatusSuccess : sessionResult.FailureReason,
Task = "NetSessionEnum",
ComputerName = resolvedSearchResult.DisplayName
}, _cancellationToken);
}
if ((_methods & ResolvedCollectionMethod.LoggedOn) != 0)
{
await _context.DoDelay();
var privSessionResult = await _computerSessionProcessor.ReadUserSessionsPrivileged(
resolvedSearchResult.DisplayName, samAccountName,
resolvedSearchResult.ObjectId);
ret.PrivilegedSessions = privSessionResult;
if (_context.Flags.DumpComputerStatus)
await compStatusChannel.Writer.WriteAsync(new CSVComputerStatus
{
Status = privSessionResult.Collected ? StatusSuccess : privSessionResult.FailureReason,
Task = "NetWkstaUserEnum",
ComputerName = resolvedSearchResult.DisplayName
}, _cancellationToken);
if (!_context.Flags.NoRegistryLoggedOn)
{
await _context.DoDelay();
var registrySessionResult = await _computerSessionProcessor.ReadUserSessionsRegistry(apiName,
resolvedSearchResult.Domain, resolvedSearchResult.ObjectId);
ret.RegistrySessions = registrySessionResult;
if (_context.Flags.DumpComputerStatus)
await compStatusChannel.Writer.WriteAsync(new CSVComputerStatus
{
Status = privSessionResult.Collected ? StatusSuccess : privSessionResult.FailureReason,
Task = "RegistrySessions",
ComputerName = resolvedSearchResult.DisplayName
}, _cancellationToken);
}
}
if ((_methods & ResolvedCollectionMethod.UserRights) != 0)
{
await _context.DoDelay();
var userRights = _userRightsAssignmentProcessor.GetUserRightsAssignments(
resolvedSearchResult.DisplayName, resolvedSearchResult.ObjectId,
resolvedSearchResult.Domain, resolvedSearchResult.IsDomainController);
ret.UserRights = await userRights.ToArrayAsync();
}
if (!_methods.IsLocalGroupCollectionSet())
return ret;
await _context.DoDelay();
var localGroups = _localGroupProcessor.GetLocalGroups(resolvedSearchResult.DisplayName,
resolvedSearchResult.ObjectId, resolvedSearchResult.Domain,
resolvedSearchResult.IsDomainController);
ret.LocalGroups = await localGroups.ToArrayAsync();
return ret;
}
private Group ProcessGroupObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new Group
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
ret.Properties.Add("samaccountname", entry.GetProperty(LDAPProperties.SAMAccountName));
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.Group) != 0)
ret.Members = _groupProcessor
.ReadGroupMembers(resolvedSearchResult, entry)
.ToArray();
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
var groupProps = LDAPPropertyProcessor.ReadGroupProperties(entry);
ret.Properties = ContextUtils.Merge(ret.Properties, groupProps);
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
}
if ((_methods & ResolvedCollectionMethod.Container) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
private async Task<Domain> ProcessDomainObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new Domain
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.Trusts) != 0)
ret.Trusts = _domainTrustProcessor.EnumerateDomainTrusts(resolvedSearchResult.Domain).ToArray();
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
ret.Properties = ContextUtils.Merge(ret.Properties, LDAPPropertyProcessor.ReadDomainProperties(entry));
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
}
if ((_methods & ResolvedCollectionMethod.Container) != 0)
{
ret.Links = _containerProcessor.ReadContainerGPLinks(resolvedSearchResult, entry).ToArray();
}
if ((_methods & ResolvedCollectionMethod.GPOLocalGroup) != 0)
{
var gplink = entry.GetProperty(LDAPProperties.GPLink);
ret.GPOChanges = await _gpoLocalGroupProcessor.ReadGPOLocalGroups(gplink, entry.DistinguishedName);
}
return ret;
}
private async Task<GPO> ProcessGPOObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new GPO
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
ret.Properties = ContextUtils.Merge(ret.Properties, LDAPPropertyProcessor.ReadGPOProperties(entry));
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
}
ret.NTLMv1Enabled = await _gpoLmCompatibilityLevelProcessor.ReadGPOLmCompatibilityLevel(entry);
ret.Properties.Add("ntlmv1", ret.NTLMv1Enabled);
return ret;
}
private async Task<OU> ProcessOUObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new OU
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0)
{
ret.Properties = ContextUtils.Merge(ret.Properties, LDAPPropertyProcessor.ReadOUProperties(entry));
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
}
if ((_methods & ResolvedCollectionMethod.Container) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
ret.Properties.Add("blocksinheritance",
ContainerProcessor.ReadBlocksInheritance(entry.GetProperty("gpoptions")));
ret.Links = _containerProcessor.ReadContainerGPLinks(resolvedSearchResult, entry).ToArray();
}
if ((_methods & ResolvedCollectionMethod.GPOLocalGroup) != 0)
{
var gplink = entry.GetProperty(LDAPProperties.GPLink);
ret.GPOChanges = await _gpoLocalGroupProcessor.ReadGPOLocalGroups(gplink, entry.DistinguishedName);
}
return ret;
}
private Container ProcessContainerObject(ISearchResultEntry entry,
ResolvedSearchResult resolvedSearchResult)
{
var ret = new Container
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry)
.ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
if (_context.Flags.CollectAllProperties)
{
ret.Properties = ContextUtils.Merge(_ldapPropertyProcessor.ParseAllProperties(entry),
ret.Properties);
}
//ret.Properties = ContextUtils.Merge(ret.Properties, LDAPPropertyProcessor.)
}
return ret;
}
private async Task<RootCA> ProcessRootCA(ISearchResultEntry entry, ResolvedSearchResult resolvedSearchResult)
{
var ret = new RootCA
{
ObjectIdentifier = resolvedSearchResult.ObjectId,
DomainSID = resolvedSearchResult.DomainSid
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
var props = LDAPPropertyProcessor.ReadRootCAProperties(entry);
ret.Properties.Merge(props);
}
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
private async Task<AIACA> ProcessAIACA(ISearchResultEntry entry, ResolvedSearchResult resolvedSearchResult)
{
var ret = new AIACA
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
var props = LDAPPropertyProcessor.ReadAIACAProperties(entry);
ret.Properties.Merge(props);
}
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
private async Task<EnterpriseCA> ProcessEnterpriseCA(ISearchResultEntry entry, ResolvedSearchResult resolvedSearchResult)
{
var ret = new EnterpriseCA
{
ObjectIdentifier = resolvedSearchResult.ObjectId,
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
var props = LDAPPropertyProcessor.ReadEnterpriseCAProperties(entry);
ret.Properties.Merge(props);
// Enabled cert templates
ret.EnabledCertTemplates = _certAbuseProcessor.ProcessCertTemplates(entry.GetArrayProperty(LDAPProperties.CertificateTemplates), resolvedSearchResult.Domain).ToArray();
}
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
if ((_methods & ResolvedCollectionMethod.CARegistry) != 0)
{
// Collect properties from CA server registry
var cASecurityCollected = false;
var enrollmentAgentRestrictionsCollected = false;
var isUserSpecifiesSanEnabledCollected = false;
var caName = entry.GetProperty(LDAPProperties.Name);
var dnsHostName = entry.GetProperty(LDAPProperties.DNSHostName);
if ((_methods & ResolvedCollectionMethod.CARegistry) != 0 && caName != null && dnsHostName != null)
{
ret.HostingComputer = await _context.LDAPUtils.ResolveHostToSid(dnsHostName, resolvedSearchResult.Domain);
CARegistryData cARegistryData = new()
{
IsUserSpecifiesSanEnabled = _certAbuseProcessor.IsUserSpecifiesSanEnabled(dnsHostName, caName),
EnrollmentAgentRestrictions = await _certAbuseProcessor.ProcessEAPermissions(caName, resolvedSearchResult.Domain, dnsHostName, ret.HostingComputer),
// The CASecurity exist in the AD object DACL and in registry of the CA server. We prefer to use the values from registry as they are the ground truth.
// If changes are made on the CA server, registry and the AD object is updated. If changes are made directly on the AD object, the CA server registry is not updated.
CASecurity = await _certAbuseProcessor.ProcessRegistryEnrollmentPermissions(caName, resolvedSearchResult.Domain, dnsHostName, ret.HostingComputer)
};
cASecurityCollected = cARegistryData.CASecurity.Collected;
enrollmentAgentRestrictionsCollected = cARegistryData.EnrollmentAgentRestrictions.Collected;
isUserSpecifiesSanEnabledCollected = cARegistryData.IsUserSpecifiesSanEnabled.Collected;
ret.CARegistryData = cARegistryData;
}
ret.Properties.Add("casecuritycollected", cASecurityCollected);
ret.Properties.Add("enrollmentagentrestrictionscollected", enrollmentAgentRestrictionsCollected);
ret.Properties.Add("isuserspecifiessanenabledcollected", isUserSpecifiesSanEnabledCollected);
}
return ret;
}
private async Task<NTAuthStore> ProcessNTAuthStore(ISearchResultEntry entry, ResolvedSearchResult resolvedSearchResult)
{
var ret = new NTAuthStore
{
ObjectIdentifier = resolvedSearchResult.ObjectId,
DomainSID = resolvedSearchResult.DomainSid
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
var props = LDAPPropertyProcessor.ReadNTAuthStoreProperties(entry);
// Cert thumbprints
var rawCertificates = entry.GetByteArrayProperty(LDAPProperties.CACertificate);
var certificates = from rawCertificate in rawCertificates
select new X509Certificate2(rawCertificate).Thumbprint;
ret.Properties.Add("certthumbprints", certificates.ToArray());
ret.Properties.Merge(props);
}
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
private async Task<CertTemplate> ProcessCertTemplate(ISearchResultEntry entry, ResolvedSearchResult resolvedSearchResult)
{
var ret = new CertTemplate
{
ObjectIdentifier = resolvedSearchResult.ObjectId
};
ret.Properties.Add("domain", resolvedSearchResult.Domain);
ret.Properties.Add("name", resolvedSearchResult.DisplayName);
ret.Properties.Add("distinguishedname", entry.DistinguishedName.ToUpper());
ret.Properties.Add("domainsid", resolvedSearchResult.DomainSid);
if ((_methods & ResolvedCollectionMethod.ACL) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.Aces = _aclProcessor.ProcessACL(resolvedSearchResult, entry).ToArray();
ret.IsACLProtected = _aclProcessor.IsACLProtected(entry);
ret.Properties.Add("isaclprotected", ret.IsACLProtected);
}
if ((_methods & ResolvedCollectionMethod.ObjectProps) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
var certTemplatesProps = LDAPPropertyProcessor.ReadCertTemplateProperties(entry);
ret.Properties.Merge(certTemplatesProps);
}
if ((_methods & ResolvedCollectionMethod.Container) != 0 || (_methods & ResolvedCollectionMethod.CertServices) != 0)
{
ret.ContainedBy = _containerProcessor.GetContainingObject(entry.DistinguishedName);
}
return ret;
}
}
}