| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.DirectoryServices.Protocols; |
| | 3 | | using System.Security.Principal; |
| | 4 | | using Microsoft.Extensions.Logging; |
| | 5 | | using SharpHoundCommonLib.Enums; |
| | 6 | | using SharpHoundCommonLib.LDAPQueries; |
| | 7 | | using SharpHoundCommonLib.OutputTypes; |
| | 8 | |
|
| | 9 | | namespace SharpHoundCommonLib.Processors |
| | 10 | | { |
| | 11 | | public class DomainTrustProcessor |
| | 12 | | { |
| | 13 | | private readonly ILogger _log; |
| | 14 | | private readonly ILdapUtils _utils; |
| | 15 | |
|
| 2 | 16 | | public DomainTrustProcessor(ILdapUtils utils, ILogger log = null) |
| 2 | 17 | | { |
| 2 | 18 | | _utils = utils; |
| 2 | 19 | | _log = log ?? Logging.LogProvider.CreateLogger("DomainTrustProc"); |
| 2 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Processes domain trusts for a domain object |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="domain"></param> |
| | 26 | | /// <returns></returns> |
| | 27 | | public async IAsyncEnumerable<DomainTrust> EnumerateDomainTrusts(string domain) |
| 2 | 28 | | { |
| 2 | 29 | | _log.LogDebug("Running trust enumeration for {Domain}", domain); |
| 16 | 30 | | await foreach (var result in _utils.Query(new LdapQueryParameters { |
| 2 | 31 | | LDAPFilter = CommonFilters.TrustedDomains, |
| 2 | 32 | | Attributes = CommonProperties.DomainTrustProps, |
| 2 | 33 | | DomainName = domain |
| 2 | 34 | | })) |
| 5 | 35 | | { |
| 5 | 36 | | if (!result.IsSuccess) { |
| 0 | 37 | | yield break; |
| | 38 | | } |
| | 39 | |
|
| 5 | 40 | | var entry = result.Value; |
| 5 | 41 | | var trust = new DomainTrust(); |
| 9 | 42 | | if (!entry.TryGetByteProperty(LDAPProperties.SecurityIdentifier, out var targetSidBytes) || targetSidByt |
| 4 | 43 | | _log.LogDebug("Trust sid is null or empty for target: {Domain}", domain); |
| 4 | 44 | | continue; |
| | 45 | | } |
| | 46 | |
|
| | 47 | | string sid; |
| | 48 | | try |
| 1 | 49 | | { |
| 1 | 50 | | sid = new SecurityIdentifier(targetSidBytes, 0).Value; |
| 1 | 51 | | } |
| 0 | 52 | | catch |
| 0 | 53 | | { |
| 0 | 54 | | _log.LogTrace("Failed to convert bytes to SID for target: {Domain}", domain); |
| 0 | 55 | | continue; |
| | 56 | | } |
| | 57 | |
|
| 1 | 58 | | trust.TargetDomainSid = sid; |
| | 59 | |
|
| 1 | 60 | | if (!entry.TryGetLongProperty(LDAPProperties.TrustDirection, out var td)) { |
| 0 | 61 | | _log.LogTrace("Failed to convert trustdirection for target: {Domain}", domain); |
| 0 | 62 | | continue; |
| | 63 | | } |
| | 64 | |
|
| 1 | 65 | | trust.TrustDirection = (TrustDirection) td; |
| | 66 | |
|
| | 67 | | TrustAttributes attributes; |
| | 68 | |
|
| 1 | 69 | | if (!entry.TryGetLongProperty(LDAPProperties.TrustAttributes, out var ta)) { |
| 0 | 70 | | _log.LogTrace("Failed to convert trustattributes for target: {Domain}", domain); |
| 0 | 71 | | continue; |
| | 72 | | } |
| | 73 | |
|
| 1 | 74 | | trust.TrustAttributes = ta.ToString(); |
| 1 | 75 | | attributes = (TrustAttributes) ta; |
| | 76 | |
|
| 1 | 77 | | trust.IsTransitive = !attributes.HasFlag(TrustAttributes.NonTransitive); |
| 2 | 78 | | if (entry.TryGetProperty(LDAPProperties.CanonicalName, out var cn)) { |
| 1 | 79 | | trust.TargetDomainName = cn.ToUpper(); |
| 1 | 80 | | } |
| | 81 | |
|
| 1 | 82 | | trust.SidFilteringEnabled = |
| 1 | 83 | | attributes.HasFlag(TrustAttributes.QuarantinedDomain) || |
| 1 | 84 | | (attributes.HasFlag(TrustAttributes.ForestTransitive) && |
| 1 | 85 | | !attributes.HasFlag(TrustAttributes.TreatAsExternal)); |
| | 86 | |
|
| 1 | 87 | | trust.TGTDelegationEnabled = |
| 1 | 88 | | !attributes.HasFlag(TrustAttributes.QuarantinedDomain) && |
| 1 | 89 | | (attributes.HasFlag(TrustAttributes.WithinForest) || |
| 1 | 90 | | attributes.HasFlag(TrustAttributes.CrossOrganizationEnableTGTDelegation)); |
| | 91 | |
|
| 1 | 92 | | trust.TrustType = TrustAttributesToType(attributes); |
| | 93 | |
|
| 1 | 94 | | yield return trust; |
| 1 | 95 | | } |
| 2 | 96 | | } |
| | 97 | |
|
| | 98 | | public static TrustType TrustAttributesToType(TrustAttributes attributes) |
| 6 | 99 | | { |
| | 100 | | TrustType trustType; |
| | 101 | |
|
| 6 | 102 | | if (attributes.HasFlag(TrustAttributes.WithinForest)) |
| 2 | 103 | | trustType = TrustType.ParentChild; |
| 4 | 104 | | else if (attributes.HasFlag(TrustAttributes.ForestTransitive)) |
| 1 | 105 | | trustType = TrustType.Forest; |
| 3 | 106 | | else if (!attributes.HasFlag(TrustAttributes.WithinForest) && |
| 3 | 107 | | !attributes.HasFlag(TrustAttributes.ForestTransitive)) |
| 3 | 108 | | trustType = TrustType.External; |
| | 109 | | else |
| 0 | 110 | | trustType = TrustType.Unknown; |
| | 111 | |
|
| 6 | 112 | | return trustType; |
| 6 | 113 | | } |
| | 114 | | } |
| | 115 | | } |