From 12a33578bcd6577bf4dd7e0d8f25aa7acec99081 Mon Sep 17 00:00:00 2001 From: anemeth Date: Fri, 5 Jul 2024 11:30:47 -0700 Subject: [PATCH] Namespace syntax corrections --- src/CommonLib/DirectoryEntryExtensions.cs | 203 +++++----- src/CommonLib/LdapUtils.cs | 6 +- src/CommonLib/SearchResultEntryExtensions.cs | 371 ++++++++++--------- 3 files changed, 289 insertions(+), 291 deletions(-) diff --git a/src/CommonLib/DirectoryEntryExtensions.cs b/src/CommonLib/DirectoryEntryExtensions.cs index e0159bcd..3469b926 100644 --- a/src/CommonLib/DirectoryEntryExtensions.cs +++ b/src/CommonLib/DirectoryEntryExtensions.cs @@ -7,127 +7,128 @@ using SharpHoundCommonLib.LDAPQueries; using SharpHoundCommonLib.OutputTypes; -namespace SharpHoundCommonLib; +namespace SharpHoundCommonLib { -public static class DirectoryEntryExtensions { - public static string GetProperty(this DirectoryEntry entry, string propertyName) { - try { - if (!entry.Properties.Contains(propertyName)) - entry.RefreshCache(new[] { propertyName }); - - if (!entry.Properties.Contains(propertyName)) + public static class DirectoryEntryExtensions { + public static string GetProperty(this DirectoryEntry entry, string propertyName) { + try { + if (!entry.Properties.Contains(propertyName)) + entry.RefreshCache(new[] { propertyName }); + + if (!entry.Properties.Contains(propertyName)) + return null; + } + catch { return null; - } - catch { - return null; - } + } - var s = entry.Properties[propertyName][0]; - return s switch - { - string st => st, - _ => null - }; - } + var s = entry.Properties[propertyName][0]; + return s switch + { + string st => st, + _ => null + }; + } - public static string[] GetPropertyAsArray(this DirectoryEntry entry, string propertyName) { - try { - if (!entry.Properties.Contains(propertyName)) - entry.RefreshCache(new[] { propertyName }); - - if (!entry.Properties.Contains(propertyName)) + public static string[] GetPropertyAsArray(this DirectoryEntry entry, string propertyName) { + try { + if (!entry.Properties.Contains(propertyName)) + entry.RefreshCache(new[] { propertyName }); + + if (!entry.Properties.Contains(propertyName)) + return null; + } + catch { return null; - } - catch { - return null; - } + } - var dest = new List(); - foreach (var val in entry.Properties[propertyName]) { - if (val is string s) { - dest.Add(s); + var dest = new List(); + foreach (var val in entry.Properties[propertyName]) { + if (val is string s) { + dest.Add(s); + } } - } - return dest.ToArray(); - } + return dest.ToArray(); + } - public static bool GetTypedPrincipal(this DirectoryEntry entry, out TypedPrincipal principal) { - var identifier = entry.GetObjectIdentifier(); - var success = entry.GetLabel(out var label); - principal = new TypedPrincipal(identifier, label); - return (success && !string.IsNullOrWhiteSpace(identifier)); - } + public static bool GetTypedPrincipal(this DirectoryEntry entry, out TypedPrincipal principal) { + var identifier = entry.GetObjectIdentifier(); + var success = entry.GetLabel(out var label); + principal = new TypedPrincipal(identifier, label); + return (success && !string.IsNullOrWhiteSpace(identifier)); + } - public static string GetObjectIdentifier(this DirectoryEntry entry) { - return entry.GetSid() ?? entry.GetGuid(); - } + public static string GetObjectIdentifier(this DirectoryEntry entry) { + return entry.GetSid() ?? entry.GetGuid(); + } - public static string GetSid(this DirectoryEntry entry) - { - try + public static string GetSid(this DirectoryEntry entry) { - if (!entry.Properties.Contains(LDAPProperties.ObjectSID)) - entry.RefreshCache(new[] { LDAPProperties.ObjectSID }); + try + { + if (!entry.Properties.Contains(LDAPProperties.ObjectSID)) + entry.RefreshCache(new[] { LDAPProperties.ObjectSID }); - if (!entry.Properties.Contains(LDAPProperties.ObjectSID)) + if (!entry.Properties.Contains(LDAPProperties.ObjectSID)) + return null; + } + catch + { return null; - } - catch - { - return null; - } + } - var s = entry.Properties[LDAPProperties.ObjectSID][0]; - return s switch - { - byte[] b => new SecurityIdentifier(b, 0).ToString(), - string st => new SecurityIdentifier(Encoding.ASCII.GetBytes(st), 0).ToString(), - _ => null - }; - } - - public static string GetGuid(this DirectoryEntry entry) - { - try + var s = entry.Properties[LDAPProperties.ObjectSID][0]; + return s switch + { + byte[] b => new SecurityIdentifier(b, 0).ToString(), + string st => new SecurityIdentifier(Encoding.ASCII.GetBytes(st), 0).ToString(), + _ => null + }; + } + + public static string GetGuid(this DirectoryEntry entry) { - //Attempt to refresh the props first - if (!entry.Properties.Contains(LDAPProperties.ObjectGUID)) - entry.RefreshCache(new[] { LDAPProperties.ObjectGUID }); + try + { + //Attempt to refresh the props first + if (!entry.Properties.Contains(LDAPProperties.ObjectGUID)) + entry.RefreshCache(new[] { LDAPProperties.ObjectGUID }); - if (!entry.Properties.Contains(LDAPProperties.ObjectGUID)) + if (!entry.Properties.Contains(LDAPProperties.ObjectGUID)) + return null; + } + catch + { return null; - } - catch - { - return null; - } + } - var s = entry.Properties[LDAPProperties.ObjectGUID][0]; - return s switch - { - byte[] b => new Guid(b).ToString(), - string st => st, - _ => null - }; - } - - - public static bool GetLabel(this DirectoryEntry entry, out Label type) { - try { - entry.RefreshCache(CommonProperties.TypeResolutionProps); - } - catch { - //pass + var s = entry.Properties[LDAPProperties.ObjectGUID][0]; + return s switch + { + byte[] b => new Guid(b).ToString(), + string st => st, + _ => null + }; } + + + public static bool GetLabel(this DirectoryEntry entry, out Label type) { + try { + entry.RefreshCache(CommonProperties.TypeResolutionProps); + } + catch { + //pass + } - var flagString = entry.GetProperty(LDAPProperties.Flags); - if (!int.TryParse(flagString, out var flags)) { - flags = 0; - } + var flagString = entry.GetProperty(LDAPProperties.Flags); + if (!int.TryParse(flagString, out var flags)) { + flags = 0; + } - return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.GetProperty(LDAPProperties.DistinguishedName), - entry.GetProperty(LDAPProperties.SAMAccountType), - entry.GetPropertyAsArray(LDAPProperties.SAMAccountType), flags, out type); - } + return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.GetProperty(LDAPProperties.DistinguishedName), + entry.GetProperty(LDAPProperties.SAMAccountType), + entry.GetPropertyAsArray(LDAPProperties.SAMAccountType), flags, out type); + } + } } \ No newline at end of file diff --git a/src/CommonLib/LdapUtils.cs b/src/CommonLib/LdapUtils.cs index acb6a1ec..c8461f61 100644 --- a/src/CommonLib/LdapUtils.cs +++ b/src/CommonLib/LdapUtils.cs @@ -1433,11 +1433,7 @@ public void SetLdapConfig(LDAPConfig config) { } public Task<(bool Success, string Message)> TestLdapConnection(string domain) { - try { - return _connectionPool.TestDomainConnection(domain, false); - } catch (Exception e) { - return (false, e.Message); - } + return _connectionPool.TestDomainConnection(domain, false); } public async Task<(bool Success, string Path)> GetNamingContextPath(string domain, NamingContext context) { diff --git a/src/CommonLib/SearchResultEntryExtensions.cs b/src/CommonLib/SearchResultEntryExtensions.cs index bebb3dc5..264f6544 100644 --- a/src/CommonLib/SearchResultEntryExtensions.cs +++ b/src/CommonLib/SearchResultEntryExtensions.cs @@ -6,227 +6,228 @@ using System.Text; using SharpHoundCommonLib.Enums; -namespace SharpHoundCommonLib; - -public static class SearchResultEntryExtensions { - /// - /// Extension method to determine the BloodHound type of a SearchResultEntry using LDAP properties - /// Requires ldap properties objectsid, samaccounttype, objectclass - /// - /// - /// - public static bool GetLabel(this SearchResultEntry entry, out Label type) - { - if (!entry.GetPropertyAsInt(LDAPProperties.Flags, out var flags)) { - flags = 0; - } +namespace SharpHoundCommonLib { - return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.DistinguishedName, - entry.GetProperty(LDAPProperties.SAMAccountType), entry.GetPropertyAsArray(LDAPProperties.ObjectClass), - flags, out type); - } - - /// - /// Gets the specified property as a string from the SearchResultEntry + public static class SearchResultEntryExtensions { + /// + /// Extension method to determine the BloodHound type of a SearchResultEntry using LDAP properties + /// Requires ldap properties objectsid, samaccounttype, objectclass /// /// - /// The LDAP name of the property you want to get - /// The string value of the property if it exists or null - public static string GetProperty(this SearchResultEntry entry, string property) + /// + public static bool GetLabel(this SearchResultEntry entry, out Label type) { - if (!entry.Attributes.Contains(property)) - return null; - - var collection = entry.Attributes[property]; - //Use GetValues to auto-convert to the proper type - var lookups = collection.GetValues(typeof(string)); - if (lookups.Length == 0) - return null; - - if (lookups[0] is not string prop || prop.Length == 0) - return null; + if (!entry.GetPropertyAsInt(LDAPProperties.Flags, out var flags)) { + flags = 0; + } - return prop; + return LdapUtils.ResolveLabel(entry.GetObjectIdentifier(), entry.DistinguishedName, + entry.GetProperty(LDAPProperties.SAMAccountType), entry.GetPropertyAsArray(LDAPProperties.ObjectClass), + flags, out type); } - + /// - /// Get's the string representation of the "objectguid" property from the SearchResultEntry - /// - /// - /// The string representation of the object's GUID if possible, otherwise null - public static string GetGuid(this SearchResultEntry entry) - { - if (entry.Attributes.Contains(LDAPProperties.ObjectGUID)) + /// Gets the specified property as a string from the SearchResultEntry + /// + /// + /// The LDAP name of the property you want to get + /// The string value of the property if it exists or null + public static string GetProperty(this SearchResultEntry entry, string property) { - var guidBytes = entry.GetPropertyAsBytes(LDAPProperties.ObjectGUID); + if (!entry.Attributes.Contains(property)) + return null; - return new Guid(guidBytes).ToString().ToUpper(); - } + var collection = entry.Attributes[property]; + //Use GetValues to auto-convert to the proper type + var lookups = collection.GetValues(typeof(string)); + if (lookups.Length == 0) + return null; - return null; - } + if (lookups[0] is not string prop || prop.Length == 0) + return null; - /// - /// Gets the "objectsid" property as a string from the SearchResultEntry - /// - /// - /// The string representation of the object's SID if possible, otherwise null - public static string GetSid(this SearchResultEntry entry) - { - if (!entry.Attributes.Contains(LDAPProperties.ObjectSID)) return null; - - object[] s; - try - { - s = entry.Attributes[LDAPProperties.ObjectSID].GetValues(typeof(byte[])); + return prop; } - catch (NotSupportedException) + + /// + /// Get's the string representation of the "objectguid" property from the SearchResultEntry + /// + /// + /// The string representation of the object's GUID if possible, otherwise null + public static string GetGuid(this SearchResultEntry entry) { - return null; - } + if (entry.Attributes.Contains(LDAPProperties.ObjectGUID)) + { + var guidBytes = entry.GetPropertyAsBytes(LDAPProperties.ObjectGUID); - if (s.Length == 0) - return null; + return new Guid(guidBytes).ToString().ToUpper(); + } - if (s[0] is not byte[] sidBytes || sidBytes.Length == 0) return null; - - try - { - var sid = new SecurityIdentifier(sidBytes, 0); - return sid.Value.ToUpper(); } - catch (ArgumentNullException) + + /// + /// Gets the "objectsid" property as a string from the SearchResultEntry + /// + /// + /// The string representation of the object's SID if possible, otherwise null + public static string GetSid(this SearchResultEntry entry) { - return null; + if (!entry.Attributes.Contains(LDAPProperties.ObjectSID)) return null; + + object[] s; + try + { + s = entry.Attributes[LDAPProperties.ObjectSID].GetValues(typeof(byte[])); + } + catch (NotSupportedException) + { + return null; + } + + if (s.Length == 0) + return null; + + if (s[0] is not byte[] sidBytes || sidBytes.Length == 0) + return null; + + try + { + var sid = new SecurityIdentifier(sidBytes, 0); + return sid.Value.ToUpper(); + } + catch (ArgumentNullException) + { + return null; + } } - } - /// - /// Gets the specified property as a string array from the SearchResultEntry - /// - /// - /// The LDAP name of the property you want to get - /// The specified property as an array of strings if possible, else an empty array - public static string[] GetPropertyAsArray(this SearchResultEntry entry, string property) - { - if (!entry.Attributes.Contains(property)) - return Array.Empty(); - - var values = entry.Attributes[property]; - var strings = values.GetValues(typeof(string)); + /// + /// Gets the specified property as a string array from the SearchResultEntry + /// + /// + /// The LDAP name of the property you want to get + /// The specified property as an array of strings if possible, else an empty array + public static string[] GetPropertyAsArray(this SearchResultEntry entry, string property) + { + if (!entry.Attributes.Contains(property)) + return Array.Empty(); - return strings is not string[] result ? Array.Empty() : result; - } + var values = entry.Attributes[property]; + var strings = values.GetValues(typeof(string)); - /// - /// Gets the specified property as an array of byte arrays from the SearchResultEntry - /// Used for SIDHistory - /// - /// - /// The LDAP name of the property you want to get - /// The specified property as an array of bytes if possible, else an empty array - public static byte[][] GetPropertyAsArrayOfBytes(this SearchResultEntry entry, string property) - { - if (!entry.Attributes.Contains(property)) - return Array.Empty(); + return strings is not string[] result ? Array.Empty() : result; + } - var values = entry.Attributes[property]; - var bytes = values.GetValues(typeof(byte[])); + /// + /// Gets the specified property as an array of byte arrays from the SearchResultEntry + /// Used for SIDHistory + /// + /// + /// The LDAP name of the property you want to get + /// The specified property as an array of bytes if possible, else an empty array + public static byte[][] GetPropertyAsArrayOfBytes(this SearchResultEntry entry, string property) + { + if (!entry.Attributes.Contains(property)) + return Array.Empty(); - return bytes is not byte[][] result ? Array.Empty() : result; - } + var values = entry.Attributes[property]; + var bytes = values.GetValues(typeof(byte[])); - /// - /// Gets the specified property as a byte array - /// - /// - /// The LDAP name of the property you want to get - /// An array of bytes if possible, else null - public static byte[] GetPropertyAsBytes(this SearchResultEntry searchResultEntry, string property) - { - if (!searchResultEntry.Attributes.Contains(property)) - return null; + return bytes is not byte[][] result ? Array.Empty() : result; + } - var collection = searchResultEntry.Attributes[property]; - var lookups = collection.GetValues(typeof(byte[])); + /// + /// Gets the specified property as a byte array + /// + /// + /// The LDAP name of the property you want to get + /// An array of bytes if possible, else null + public static byte[] GetPropertyAsBytes(this SearchResultEntry searchResultEntry, string property) + { + if (!searchResultEntry.Attributes.Contains(property)) + return null; - if (lookups.Length == 0) - return Array.Empty(); + var collection = searchResultEntry.Attributes[property]; + var lookups = collection.GetValues(typeof(byte[])); - if (lookups[0] is not byte[] bytes || bytes.Length == 0) - return Array.Empty(); + if (lookups.Length == 0) + return Array.Empty(); - return bytes; - } + if (lookups[0] is not byte[] bytes || bytes.Length == 0) + return Array.Empty(); - /// - /// Gets the specified property as an int - /// - /// - /// - /// - /// - public static bool GetPropertyAsInt(this SearchResultEntry entry, string property, out int value) - { - var prop = entry.GetProperty(property); - if (prop != null) return int.TryParse(prop, out value); - value = 0; - return false; - } + return bytes; + } - /// - /// Gets the specified property as an array of X509 certificates. - /// - /// - /// - /// - public static X509Certificate2[] GetPropertyAsArrayOfCertificates(this SearchResultEntry searchResultEntry, - string property) - { - if (!searchResultEntry.Attributes.Contains(property)) - return null; + /// + /// Gets the specified property as an int + /// + /// + /// + /// + /// + public static bool GetPropertyAsInt(this SearchResultEntry entry, string property, out int value) + { + var prop = entry.GetProperty(property); + if (prop != null) return int.TryParse(prop, out value); + value = 0; + return false; + } - return searchResultEntry.GetPropertyAsArrayOfBytes(property).Select(x => new X509Certificate2(x)).ToArray(); - } + /// + /// Gets the specified property as an array of X509 certificates. + /// + /// + /// + /// + public static X509Certificate2[] GetPropertyAsArrayOfCertificates(this SearchResultEntry searchResultEntry, + string property) + { + if (!searchResultEntry.Attributes.Contains(property)) + return null; + return searchResultEntry.GetPropertyAsArrayOfBytes(property).Select(x => new X509Certificate2(x)).ToArray(); + } - /// - /// Attempts to get the unique object identifier as used by BloodHound for the Search Result Entry. Tries to get - /// objectsid first, and then objectguid next. - /// - /// - /// String representation of the entry's object identifier or null - public static string GetObjectIdentifier(this SearchResultEntry entry) - { - return entry.GetSid() ?? entry.GetGuid(); - } - /// - /// Checks the isDeleted LDAP property to determine if an entry has been deleted from the directory - /// - /// - /// - public static bool IsDeleted(this SearchResultEntry entry) - { - var deleted = entry.GetProperty(LDAPProperties.IsDeleted); - return bool.TryParse(deleted, out var isDeleted) && isDeleted; - } - - /// - /// Helper function to print attributes of a SearchResultEntry - /// - /// - public static string PrintEntry(this SearchResultEntry searchResultEntry) - { - var sb = new StringBuilder(); - if (searchResultEntry.Attributes.AttributeNames == null) return sb.ToString(); - foreach (var propertyName in searchResultEntry.Attributes.AttributeNames) + /// + /// Attempts to get the unique object identifier as used by BloodHound for the Search Result Entry. Tries to get + /// objectsid first, and then objectguid next. + /// + /// + /// String representation of the entry's object identifier or null + public static string GetObjectIdentifier(this SearchResultEntry entry) { - var property = propertyName.ToString(); - sb.Append(property).Append("\t").Append(searchResultEntry.GetProperty(property)).Append("\n"); + return entry.GetSid() ?? entry.GetGuid(); } - return sb.ToString(); - } + /// + /// Checks the isDeleted LDAP property to determine if an entry has been deleted from the directory + /// + /// + /// + public static bool IsDeleted(this SearchResultEntry entry) + { + var deleted = entry.GetProperty(LDAPProperties.IsDeleted); + return bool.TryParse(deleted, out var isDeleted) && isDeleted; + } + + /// + /// Helper function to print attributes of a SearchResultEntry + /// + /// + public static string PrintEntry(this SearchResultEntry searchResultEntry) + { + var sb = new StringBuilder(); + if (searchResultEntry.Attributes.AttributeNames == null) return sb.ToString(); + foreach (var propertyName in searchResultEntry.Attributes.AttributeNames) + { + var property = propertyName.ToString(); + sb.Append(property).Append("\t").Append(searchResultEntry.GetProperty(property)).Append("\n"); + } + + return sb.ToString(); + } + } } \ No newline at end of file