| | 1 | | using System; |
| | 2 | | using System.Collections.Concurrent; |
| | 3 | | using System.Collections.Generic; |
| | 4 | | using System.DirectoryServices; |
| | 5 | | using System.Security.Principal; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Microsoft.Extensions.Logging; |
| | 9 | | using SharpHoundCommonLib.Processors; |
| | 10 | |
|
| | 11 | | namespace SharpHoundCommonLib { |
| | 12 | | internal class ConnectionPoolManager : IDisposable{ |
| 60 | 13 | | private readonly ConcurrentDictionary<string, LdapConnectionPool> _pools = new(); |
| | 14 | | private readonly LdapConfig _ldapConfig; |
| 60 | 15 | | private readonly string[] _translateNames = { "Administrator", "admin" }; |
| 60 | 16 | | private readonly ConcurrentDictionary<string, string> _resolvedIdentifiers = new(StringComparer.OrdinalIgnoreCas |
| | 17 | | private readonly ILogger _log; |
| | 18 | | private readonly PortScanner _portScanner; |
| | 19 | |
|
| 120 | 20 | | public ConnectionPoolManager(LdapConfig config, ILogger log = null, PortScanner scanner = null) { |
| 60 | 21 | | _ldapConfig = config; |
| 60 | 22 | | _log = log ?? Logging.LogProvider.CreateLogger("ConnectionPoolManager"); |
| 60 | 23 | | _portScanner = scanner ?? new PortScanner(); |
| 60 | 24 | | } |
| | 25 | |
|
| | 26 | | public IAsyncEnumerable<Result<string>> RangedRetrieval(string distinguishedName, |
| 0 | 27 | | string attributeName, CancellationToken cancellationToken = new()) { |
| 0 | 28 | | var domain = Helpers.DistinguishedNameToDomain(distinguishedName); |
| | 29 | |
|
| 0 | 30 | | if (!GetPool(domain, out var pool)) { |
| 0 | 31 | | return new List<Result<string>> {Result<string>.Fail("Failed to resolve a connection pool")}.ToAsyncEnum |
| | 32 | | } |
| | 33 | |
|
| 0 | 34 | | return pool.RangedRetrieval(distinguishedName, attributeName, cancellationToken); |
| 0 | 35 | | } |
| | 36 | |
|
| | 37 | | public IAsyncEnumerable<LdapResult<IDirectoryObject>> PagedQuery(LdapQueryParameters queryParameters, |
| 0 | 38 | | CancellationToken cancellationToken = new()) { |
| 0 | 39 | | if (!GetPool(queryParameters.DomainName, out var pool)) { |
| 0 | 40 | | return new List<LdapResult<IDirectoryObject>> {LdapResult<IDirectoryObject>.Fail("Failed to resolve a co |
| | 41 | | } |
| | 42 | |
|
| 0 | 43 | | return pool.PagedQuery(queryParameters, cancellationToken); |
| 0 | 44 | | } |
| | 45 | |
|
| | 46 | | public IAsyncEnumerable<LdapResult<IDirectoryObject>> Query(LdapQueryParameters queryParameters, |
| 1 | 47 | | CancellationToken cancellationToken = new()) { |
| 1 | 48 | | if (!GetPool(queryParameters.DomainName, out var pool)) { |
| 0 | 49 | | return new List<LdapResult<IDirectoryObject>> {LdapResult<IDirectoryObject>.Fail("Failed to resolve a co |
| | 50 | | } |
| | 51 | |
|
| 1 | 52 | | return pool.Query(queryParameters, cancellationToken); |
| 1 | 53 | | } |
| | 54 | |
|
| 0 | 55 | | public void ReleaseConnection(LdapConnectionWrapper connectionWrapper, bool connectionFaulted = false) { |
| 0 | 56 | | if (connectionWrapper == null) { |
| 0 | 57 | | return; |
| | 58 | | } |
| | 59 | | //I don't think this is possible, but at least account for it |
| 0 | 60 | | if (!_pools.TryGetValue(connectionWrapper.PoolIdentifier, out var pool)) { |
| 0 | 61 | | _log.LogWarning("Could not find pool for {Identifier}", connectionWrapper.PoolIdentifier); |
| 0 | 62 | | connectionWrapper.Connection.Dispose(); |
| 0 | 63 | | return; |
| | 64 | | } |
| | 65 | |
|
| 0 | 66 | | pool.ReleaseConnection(connectionWrapper, connectionFaulted); |
| 0 | 67 | | } |
| | 68 | |
|
| 0 | 69 | | public async Task<(bool Success, string Message)> TestDomainConnection(string identifier, bool globalCatalog) { |
| 0 | 70 | | var (success, connection, message) = await GetLdapConnection(identifier, globalCatalog); |
| 0 | 71 | | ReleaseConnection(connection); |
| 0 | 72 | | return (success, message); |
| 0 | 73 | | } |
| | 74 | |
|
| 1 | 75 | | private bool GetPool(string identifier, out LdapConnectionPool pool) { |
| 1 | 76 | | if (identifier == null) { |
| 0 | 77 | | pool = default; |
| 0 | 78 | | return false; |
| | 79 | | } |
| | 80 | |
|
| 1 | 81 | | var resolved = ResolveIdentifier(identifier); |
| 2 | 82 | | if (!_pools.TryGetValue(resolved, out pool)) { |
| 1 | 83 | | pool = new LdapConnectionPool(identifier, resolved, _ldapConfig,scanner: _portScanner); |
| 1 | 84 | | _pools.TryAdd(resolved, pool); |
| 1 | 85 | | } |
| | 86 | |
|
| 1 | 87 | | return true; |
| 1 | 88 | | } |
| | 89 | |
|
| | 90 | | public async Task<(bool Success, LdapConnectionWrapper ConnectionWrapper, string Message)> GetLdapConnection( |
| 0 | 91 | | string identifier, bool globalCatalog) { |
| 0 | 92 | | if (!GetPool(identifier, out var pool)) { |
| 0 | 93 | | return (false, default, $"Unable to resolve a pool for {identifier}"); |
| | 94 | | } |
| | 95 | |
|
| 0 | 96 | | if (globalCatalog) { |
| 0 | 97 | | return await pool.GetGlobalCatalogConnectionAsync(); |
| | 98 | | } |
| 0 | 99 | | return await pool.GetConnectionAsync(); |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | public (bool Success, LdapConnectionWrapper connectionWrapper, string Message) GetLdapConnectionForServer( |
| 0 | 103 | | string identifier, string server, bool globalCatalog) { |
| 0 | 104 | | if (!GetPool(identifier, out var pool)) { |
| 0 | 105 | | return (false, default, $"Unable to resolve a pool for {identifier}"); |
| | 106 | | } |
| | 107 | |
|
| 0 | 108 | | return pool.GetConnectionForSpecificServerAsync(server, globalCatalog); |
| 0 | 109 | | } |
| | 110 | |
|
| 1 | 111 | | private string ResolveIdentifier(string identifier) { |
| 1 | 112 | | if (_resolvedIdentifiers.TryGetValue(identifier, out var resolved)) { |
| 0 | 113 | | return resolved; |
| | 114 | | } |
| | 115 | |
|
| 1 | 116 | | if (GetDomainSidFromDomainName(identifier, out var sid)) { |
| 0 | 117 | | _log.LogDebug("Resolved identifier {Identifier} to {Resolved}", identifier, sid); |
| 0 | 118 | | _resolvedIdentifiers.TryAdd(identifier, sid); |
| 0 | 119 | | return sid; |
| | 120 | | } |
| | 121 | |
|
| 1 | 122 | | return identifier; |
| 1 | 123 | | } |
| | 124 | |
|
| 1 | 125 | | private bool GetDomainSidFromDomainName(string domainName, out string domainSid) { |
| 1 | 126 | | if (Cache.GetDomainSidMapping(domainName, out domainSid)) return true; |
| | 127 | |
|
| 1 | 128 | | try { |
| 1 | 129 | | var entry = new DirectoryEntry($"LDAP://{domainName}").ToDirectoryObject(); |
| 1 | 130 | | if (entry.TryGetSecurityIdentifier(out var sid)) { |
| 0 | 131 | | Cache.AddDomainSidMapping(domainName, sid); |
| 0 | 132 | | domainSid = sid; |
| 0 | 133 | | return true; |
| | 134 | | } |
| 1 | 135 | | } |
| 0 | 136 | | catch { |
| | 137 | | //we expect this to fail sometimes |
| 0 | 138 | | } |
| | 139 | |
|
| 1 | 140 | | if (LdapUtils.GetDomain(domainName, _ldapConfig, out var domainObject)) |
| 0 | 141 | | try { |
| 0 | 142 | | if (domainObject.GetDirectoryEntry().ToDirectoryObject().TryGetSecurityIdentifier(out domainSid)) { |
| 0 | 143 | | Cache.AddDomainSidMapping(domainName, domainSid); |
| 0 | 144 | | return true; |
| | 145 | | } |
| 0 | 146 | | } |
| 0 | 147 | | catch { |
| | 148 | | //we expect this to fail sometimes (not sure why, but better safe than sorry) |
| 0 | 149 | | } |
| | 150 | |
|
| 7 | 151 | | foreach (var name in _translateNames) |
| 2 | 152 | | try { |
| 2 | 153 | | var account = new NTAccount(domainName, name); |
| 2 | 154 | | var sid = (SecurityIdentifier)account.Translate(typeof(SecurityIdentifier)); |
| 0 | 155 | | domainSid = sid.AccountDomainSid.ToString(); |
| 0 | 156 | | Cache.AddDomainSidMapping(domainName, domainSid); |
| 0 | 157 | | return true; |
| | 158 | | } |
| 4 | 159 | | catch { |
| | 160 | | //We expect this to fail if the username doesn't exist in the domain |
| 2 | 161 | | } |
| | 162 | |
|
| 1 | 163 | | return false; |
| 1 | 164 | | } |
| | 165 | |
|
| 0 | 166 | | public void Dispose() { |
| 0 | 167 | | foreach (var kv in _pools) |
| 0 | 168 | | { |
| 0 | 169 | | kv.Value.Dispose(); |
| 0 | 170 | | } |
| | 171 | |
|
| 0 | 172 | | _pools.Clear(); |
| 0 | 173 | | } |
| | 174 | | } |
| | 175 | | } |