Skip to content

Commit

Permalink
Merge branch 'v3' into additionalOUACLs
Browse files Browse the repository at this point in the history
  • Loading branch information
rvazarkar authored Jul 24, 2024
2 parents eb3af9a + ea6b097 commit e3ebe45
Show file tree
Hide file tree
Showing 19 changed files with 712 additions and 266 deletions.
2 changes: 1 addition & 1 deletion src/CommonLib/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private Cache()
/// </summary>
/// <param name="key"></param>
/// <param name="value"></param>
internal static void AddSidToDomain(string key, string value)
internal static void AddDomainSidMapping(string key, string value)
{
CacheInstance?.SIDToDomainCache.TryAdd(key, value);
}
Expand Down
20 changes: 20 additions & 0 deletions src/CommonLib/DomainInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.DirectoryServices.Protocols;

namespace SharpHoundCommonLib
{
public class DomainInfo
{
public string DomainSID { get; set; }
public string DomainFQDN { get; set; }
public string DomainSearchBase { get; set; }
public string DomainConfigurationPath { get; set; }
public string DomainNetbiosName { get; set; }

public override string ToString()
{
return $"{nameof(DomainSID)}: {DomainSID}, {nameof(DomainFQDN)}: {DomainFQDN}, {nameof(DomainSearchBase)}: {DomainSearchBase}, {nameof(DomainConfigurationPath)}: {DomainConfigurationPath}, {nameof(DomainNetbiosName)}: {DomainNetbiosName}";
}
}
}
3 changes: 2 additions & 1 deletion src/CommonLib/Enums/LdapErrorCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum LdapErrorCodes : int
Success = 0,
Busy = 51,
ServerDown = 81,
LocalError = 82
LocalError = 82,
KerberosAuthType = 83
}
}
12 changes: 12 additions & 0 deletions src/CommonLib/Exceptions/LdapAuthenticationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.DirectoryServices.Protocols;

namespace SharpHoundCommonLib.Exceptions
{
public class LdapAuthenticationException : Exception
{
public LdapAuthenticationException(LdapException exception) : base("Error authenticating to LDAP", exception)
{
}
}
}
14 changes: 14 additions & 0 deletions src/CommonLib/Exceptions/LdapConnectionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.DirectoryServices.Protocols;

namespace SharpHoundCommonLib.Exceptions
{
public class LdapConnectionException : Exception
{
public int ErrorCode { get; }
public LdapConnectionException(LdapException innerException) : base("Failed during ldap connection tests", innerException)
{
ErrorCode = innerException.ErrorCode;
}
}
}
13 changes: 13 additions & 0 deletions src/CommonLib/Exceptions/NoLdapDataException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace SharpHoundCommonLib.Exceptions
{
public class NoLdapDataException : Exception
{
public int ErrorCode { get; set; }
public NoLdapDataException(int errorCode)
{
ErrorCode = errorCode;
}
}
}
2 changes: 2 additions & 0 deletions src/CommonLib/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SharpHoundCommonLib.Enums;
using SharpHoundCommonLib.LDAPQueries;
using SearchScope = System.DirectoryServices.Protocols.SearchScope;

namespace SharpHoundCommonLib
{
Expand Down
4 changes: 4 additions & 0 deletions src/CommonLib/ILDAPUtils.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.DirectoryServices.ActiveDirectory;
using System.DirectoryServices.Protocols;
using System.Security.Principal;
using System.Threading;
using System.Threading.Tasks;
using SharpHoundCommonLib.Enums;
Expand Down Expand Up @@ -41,6 +42,9 @@ public interface ILDAPUtils
string GetSidFromDomainName(string domainName);
string ConvertWellKnownPrincipal(string sid, string domain);
bool GetWellKnownPrincipal(string sid, string domain, out TypedPrincipal commonPrincipal);

bool ConvertLocalWellKnownPrincipal(SecurityIdentifier sid, string computerDomainSid, string computerDomain,
out TypedPrincipal principal);
Domain GetDomain(string domainName = null);
void AddDomainController(string domainControllerSID);
IEnumerable<OutputBase> GetWellKnownPrincipalOutput(string domain);
Expand Down
17 changes: 14 additions & 3 deletions src/CommonLib/LDAPConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,25 @@ public class LDAPConfig
public string Password { get; set; } = null;
public string Server { get; set; } = null;
public int Port { get; set; } = 0;
public bool SSL { get; set; } = false;
public bool ForceSSL { get; set; } = false;
public bool DisableSigning { get; set; } = false;
public bool DisableCertVerification { get; set; } = false;
public AuthType AuthType { get; set; } = AuthType.Kerberos;

public int GetPort()
//Returns the port for connecting to LDAP. Will always respect a user's overridden config over anything else
public int GetPort(bool ssl)
{
return Port == 0 ? SSL ? 636 : 389 : Port;
if (Port != 0)
{
return Port;
}

return ssl ? 636 : 389;
}

public int GetGCPort(bool ssl)
{
return ssl ? 3269 : 3268;
}
}
}
36 changes: 36 additions & 0 deletions src/CommonLib/LDAPConnectionCacheKey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace SharpHoundCommonLib
{
public class LDAPConnectionCacheKey
{
public bool GlobalCatalog { get; }
public string Domain { get; }
public string Server { get; set; }

public LDAPConnectionCacheKey(string domain, bool globalCatalog)
{
GlobalCatalog = globalCatalog;
Domain = domain;
}

protected bool Equals(LDAPConnectionCacheKey other)
{
return GlobalCatalog == other.GlobalCatalog && Domain == other.Domain;
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((LDAPConnectionCacheKey)obj);
}

public override int GetHashCode()
{
unchecked
{
return (GlobalCatalog.GetHashCode() * 397) ^ (Domain != null ? Domain.GetHashCode() : 0);
}
}
}
}
5 changes: 5 additions & 0 deletions src/CommonLib/LDAPProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ public static class LDAPProperties
public const string CertificateTemplates = "certificatetemplates";
public const string CrossCertificatePair = "crosscertificatepair";
public const string Flags = "flags";
public const string RootDomainNamingContext = "rootdomainnamingcontext";
public const string ConfigurationNamingContext = "configurationnamingcontext";
public const string NetbiosName = "netbiosName";
public const string DnsRoot = "dnsroot";
public const string ServerName = "servername";
}
}
Loading

0 comments on commit e3ebe45

Please sign in to comment.