Skip to content

Commit cc7b6a2

Browse files
authored
LDAP Connection Logic Rewrite (#127)
* wip: ldap connection consistency rewrite * wip: ldap connection crap * wip: start caching domain info and returning * wip: more plumbing * wip: more cleanup, fix returns so they're compatible * chore: remove weird annotation * chore: remove domain controller cache * chore: add implementation for convert * chore: exception handling for setup * chore: some nits * fix: check correct var * chore: useless code * chore: add extra cache step * chore: more cache
1 parent 46723de commit cc7b6a2

18 files changed

Lines changed: 710 additions & 264 deletions

src/CommonLib/Cache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private Cache()
5050
/// </summary>
5151
/// <param name="key"></param>
5252
/// <param name="value"></param>
53-
internal static void AddSidToDomain(string key, string value)
53+
internal static void AddDomainSidMapping(string key, string value)
5454
{
5555
CacheInstance?.SIDToDomainCache.TryAdd(key, value);
5656
}

src/CommonLib/DomainInfo.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections.Concurrent;
2+
using System.Collections.Generic;
3+
using System.DirectoryServices.Protocols;
4+
5+
namespace SharpHoundCommonLib
6+
{
7+
public class DomainInfo
8+
{
9+
public string DomainSID { get; set; }
10+
public string DomainFQDN { get; set; }
11+
public string DomainSearchBase { get; set; }
12+
public string DomainConfigurationPath { get; set; }
13+
public string DomainNetbiosName { get; set; }
14+
15+
public override string ToString()
16+
{
17+
return $"{nameof(DomainSID)}: {DomainSID}, {nameof(DomainFQDN)}: {DomainFQDN}, {nameof(DomainSearchBase)}: {DomainSearchBase}, {nameof(DomainConfigurationPath)}: {DomainConfigurationPath}, {nameof(DomainNetbiosName)}: {DomainNetbiosName}";
18+
}
19+
}
20+
}

src/CommonLib/Enums/LdapErrorCodes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public enum LdapErrorCodes : int
55
Success = 0,
66
Busy = 51,
77
ServerDown = 81,
8-
LocalError = 82
8+
LocalError = 82,
9+
KerberosAuthType = 83
910
}
1011
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.DirectoryServices.Protocols;
3+
4+
namespace SharpHoundCommonLib.Exceptions
5+
{
6+
public class LdapAuthenticationException : Exception
7+
{
8+
public LdapAuthenticationException(LdapException exception) : base("Error authenticating to LDAP", exception)
9+
{
10+
}
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.DirectoryServices.Protocols;
3+
4+
namespace SharpHoundCommonLib.Exceptions
5+
{
6+
public class LdapConnectionException : Exception
7+
{
8+
public int ErrorCode { get; }
9+
public LdapConnectionException(LdapException innerException) : base("Failed during ldap connection tests", innerException)
10+
{
11+
ErrorCode = innerException.ErrorCode;
12+
}
13+
}
14+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
3+
namespace SharpHoundCommonLib.Exceptions
4+
{
5+
public class NoLdapDataException : Exception
6+
{
7+
public int ErrorCode { get; set; }
8+
public NoLdapDataException(int errorCode)
9+
{
10+
ErrorCode = errorCode;
11+
}
12+
}
13+
}

src/CommonLib/Extensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
using System.Threading.Tasks;
1010
using Microsoft.Extensions.Logging;
1111
using SharpHoundCommonLib.Enums;
12+
using SharpHoundCommonLib.LDAPQueries;
13+
using SearchScope = System.DirectoryServices.Protocols.SearchScope;
1214

1315
namespace SharpHoundCommonLib
1416
{

src/CommonLib/ILDAPUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.DirectoryServices.ActiveDirectory;
33
using System.DirectoryServices.Protocols;
4+
using System.Security.Principal;
45
using System.Threading;
56
using System.Threading.Tasks;
67
using SharpHoundCommonLib.Enums;
@@ -41,6 +42,9 @@ public interface ILDAPUtils
4142
string GetSidFromDomainName(string domainName);
4243
string ConvertWellKnownPrincipal(string sid, string domain);
4344
bool GetWellKnownPrincipal(string sid, string domain, out TypedPrincipal commonPrincipal);
45+
46+
bool ConvertLocalWellKnownPrincipal(SecurityIdentifier sid, string computerDomainSid, string computerDomain,
47+
out TypedPrincipal principal);
4448
Domain GetDomain(string domainName = null);
4549
void AddDomainController(string domainControllerSID);
4650
IEnumerable<OutputBase> GetWellKnownPrincipalOutput(string domain);

src/CommonLib/LDAPConfig.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,25 @@ public class LDAPConfig
88
public string Password { get; set; } = null;
99
public string Server { get; set; } = null;
1010
public int Port { get; set; } = 0;
11-
public bool SSL { get; set; } = false;
11+
public bool ForceSSL { get; set; } = false;
1212
public bool DisableSigning { get; set; } = false;
1313
public bool DisableCertVerification { get; set; } = false;
1414
public AuthType AuthType { get; set; } = AuthType.Kerberos;
1515

16-
public int GetPort()
16+
//Returns the port for connecting to LDAP. Will always respect a user's overridden config over anything else
17+
public int GetPort(bool ssl)
1718
{
18-
return Port == 0 ? SSL ? 636 : 389 : Port;
19+
if (Port != 0)
20+
{
21+
return Port;
22+
}
23+
24+
return ssl ? 636 : 389;
25+
}
26+
27+
public int GetGCPort(bool ssl)
28+
{
29+
return ssl ? 3269 : 3268;
1930
}
2031
}
2132
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace SharpHoundCommonLib
2+
{
3+
public class LDAPConnectionCacheKey
4+
{
5+
public bool GlobalCatalog { get; }
6+
public string Domain { get; }
7+
public string Server { get; set; }
8+
9+
public LDAPConnectionCacheKey(string domain, bool globalCatalog)
10+
{
11+
GlobalCatalog = globalCatalog;
12+
Domain = domain;
13+
}
14+
15+
protected bool Equals(LDAPConnectionCacheKey other)
16+
{
17+
return GlobalCatalog == other.GlobalCatalog && Domain == other.Domain;
18+
}
19+
20+
public override bool Equals(object obj)
21+
{
22+
if (ReferenceEquals(null, obj)) return false;
23+
if (ReferenceEquals(this, obj)) return true;
24+
if (obj.GetType() != this.GetType()) return false;
25+
return Equals((LDAPConnectionCacheKey)obj);
26+
}
27+
28+
public override int GetHashCode()
29+
{
30+
unchecked
31+
{
32+
return (GlobalCatalog.GetHashCode() * 397) ^ (Domain != null ? Domain.GetHashCode() : 0);
33+
}
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)