Skip to content

Commit

Permalink
wip: more wip stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar committed Jun 24, 2024
1 parent 284aa99 commit 3a6e165
Show file tree
Hide file tree
Showing 11 changed files with 973 additions and 1,019 deletions.
38 changes: 33 additions & 5 deletions src/CommonLib/ConnectionPoolManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,41 @@
using System.DirectoryServices;
using System.Security.Principal;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpHoundCommonLib.Processors;

namespace SharpHoundCommonLib;

public class ConnectionPoolManager : IDisposable{
private readonly ConcurrentDictionary<string, LdapConnectionPool> _pools = new();
private readonly LDAPConfig _ldapConfig;
private readonly string[] _translateNames = { "Administrator", "admin" };
private readonly ConcurrentDictionary<string, string> _resolvedIdentifiers = new(StringComparer.OrdinalIgnoreCase);
private readonly ILogger _log;
private readonly PortScanner _portScanner;

public ConnectionPoolManager(LDAPConfig config) {
public ConnectionPoolManager(LDAPConfig config, ILogger log = null, PortScanner scanner = null) {
_ldapConfig = config;
_log = log ?? Logging.LogProvider.CreateLogger("ConnectionPoolManager");
_portScanner = scanner ?? new PortScanner();
}

public void ReleaseConnection(LdapConnectionWrapperNew connectionWrapper, bool connectionFaulted = false) {
//I dont think this is possible, but at least account for it
if (!_pools.TryGetValue(connectionWrapper.PoolIdentifier, out var pool)) {
connectionWrapper.Connection.Dispose();
return;
}

pool.ReleaseConnection(connectionWrapper, connectionFaulted);
}

public async Task<(bool Success, LdapConnectionWrapperNew connectionWrapper, string Message)> GetLdapConnection(
string identifier, bool globalCatalog) {
var resolved = ResolveIdentifier(identifier);

if (!_pools.TryGetValue(identifier, out var pool)) {
pool = new LdapConnectionPool(resolved, _ldapConfig);
pool = new LdapConnectionPool(resolved, _ldapConfig,scanner: _portScanner);
_pools.TryAdd(identifier, pool);
}

Expand All @@ -35,15 +52,26 @@ public ConnectionPoolManager(LDAPConfig config) {
var resolved = ResolveIdentifier(identifier);

if (!_pools.TryGetValue(identifier, out var pool)) {
pool = new LdapConnectionPool(resolved, _ldapConfig);
pool = new LdapConnectionPool(resolved, _ldapConfig,scanner: _portScanner);
_pools.TryAdd(identifier, pool);
}

return await pool.GetConnectionForSpecificServerAsync(server, globalCatalog);
}

private string ResolveIdentifier(string identifier) {
return GetDomainSidFromDomainName(identifier, out var sid) ? sid : identifier;
if (_resolvedIdentifiers.TryGetValue(identifier, out var resolved)) {
return resolved;
}


if (GetDomainSidFromDomainName(identifier, out var sid)) {
_log.LogDebug("Resolved identifier {Identifier} to {Resolved}", identifier, sid);
_resolvedIdentifiers.TryAdd(identifier, sid);
return sid;
}

return identifier;
}

private bool GetDomainSidFromDomainName(string domainName, out string domainSid) {
Expand All @@ -64,7 +92,7 @@ private bool GetDomainSidFromDomainName(string domainName, out string domainSid)
//we expect this to fail sometimes
}

if (LDAPUtilsNew.GetDomain(domainName, _ldapConfig, out var domainObject))
if (LdapUtilsNew.GetDomain(domainName, _ldapConfig, out var domainObject))
try {
domainSid = domainObject.GetDirectoryEntry().GetSid();
if (domainSid != null) {
Expand Down
18 changes: 18 additions & 0 deletions src/CommonLib/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ public static string LdapValue(this Guid s)
return output;
}

public static string GetProperty(this DirectoryEntry entry, string propertyName) {
try {
if (!entry.Properties.Contains(propertyName)) {
return null;
}
}
catch {
return null;
}

var s = entry.Properties[propertyName][0];
return s switch
{
string st => st,
_ => null
};
}

public static string GetSid(this DirectoryEntry result)
{
try
Expand Down
9 changes: 9 additions & 0 deletions src/CommonLib/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public static string DistinguishedNameToDomain(string distinguishedName)
temp = DCReplaceRegex.Replace(temp, "").Replace(",", ".").ToUpper();
return temp;
}

/// <summary>
/// Converts a domain name to a distinguished name using simple string substitution
/// </summary>
/// <param name="domainName"></param>
/// <returns></returns>
public static string DomainNameToDistinguishedName(string domainName) {
return $"DC={domainName.Replace(".", ",DC=")}";
}

/// <summary>
/// Strips a "serviceprincipalname" entry down to just its hostname
Expand Down
Loading

0 comments on commit 3a6e165

Please sign in to comment.