|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.DirectoryServices.Protocols; |
| 3 | +using System.Linq; |
3 | 4 | using System.Security.Principal; |
4 | 5 | using Microsoft.Extensions.Logging; |
5 | 6 | using SharpHoundCommonLib.Enums; |
@@ -27,6 +28,20 @@ public DomainTrustProcessor(ILdapUtils utils, ILogger log = null) |
27 | 28 | public async IAsyncEnumerable<DomainTrust> EnumerateDomainTrusts(string domain) |
28 | 29 | { |
29 | 30 | _log.LogDebug("Running trust enumeration for {Domain}", domain); |
| 31 | + |
| 32 | + // Attempt to get trust type |
| 33 | + var trustInfoList = new List<(string TargetName, System.DirectoryServices.ActiveDirectory.TrustType TrustType)>(); |
| 34 | + try |
| 35 | + { |
| 36 | + _utils.GetDomain(domain, out var domainObject); |
| 37 | + trustInfoList.AddRange(from System.DirectoryServices.ActiveDirectory.TrustRelationshipInformation trust in domainObject.GetAllTrustRelationships() |
| 38 | + select (trust.TargetName, trust.TrustType)); |
| 39 | + } |
| 40 | + catch |
| 41 | + { |
| 42 | + _log.LogWarning("Trust type enumeration using non-LDAP for {Domain} failed", domain); |
| 43 | + } |
| 44 | + |
30 | 45 | await foreach (var result in _utils.Query(new LdapQueryParameters { |
31 | 46 | LDAPFilter = CommonFilters.TrustedDomains, |
32 | 47 | Attributes = CommonProperties.DomainTrustProps, |
@@ -90,7 +105,9 @@ public async IAsyncEnumerable<DomainTrust> EnumerateDomainTrusts(string domain) |
90 | 105 | (attributes.HasFlag(TrustAttributes.WithinForest) || |
91 | 106 | attributes.HasFlag(TrustAttributes.CrossOrganizationEnableTGTDelegation)); |
92 | 107 |
|
93 | | - trust.TrustType = TrustAttributesToType(attributes); |
| 108 | + var match = trustInfoList.FirstOrDefault(t => |
| 109 | + t.TargetName.ToUpper().Equals(trust.TargetDomainName)); |
| 110 | + trust.TrustType = !string.IsNullOrEmpty(match.TargetName) ? (TrustType) match.TrustType : TrustAttributesToType(attributes); |
94 | 111 |
|
95 | 112 | yield return trust; |
96 | 113 | } |
|
0 commit comments