Skip to content

Commit

Permalink
Merge pull request #36 from BloodHoundAD/4.2
Browse files Browse the repository at this point in the history
CommonLib update for BH 4.2
  • Loading branch information
rvazarkar authored Aug 2, 2022
2 parents a2cc6c1 + f1a93e7 commit a13a94c
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 40 deletions.
76 changes: 38 additions & 38 deletions src/CommonLib/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,29 @@

namespace SharpHoundCommonLib
{
[DataContract]
public class Cache
{
[DataMember]private ConcurrentDictionary<string, string[]> _globalCatalogCache;
private Cache()
{
ValueToIdCache = new ConcurrentDictionary<string, string>();
IdToTypeCache = new ConcurrentDictionary<string, Label>();
GlobalCatalogCache = new ConcurrentDictionary<string, string[]>();
MachineSidCache = new ConcurrentDictionary<string, string>();
SIDToDomainCache = new ConcurrentDictionary<string, string>();
}

[DataMember]private ConcurrentDictionary<string, Label> _idToTypeCache;
[DataMember] public ConcurrentDictionary<string, string[]> GlobalCatalogCache { get; private set; }

[DataMember]private ConcurrentDictionary<string, string> _machineSidCache;
[DataMember] public ConcurrentDictionary<string, Label> IdToTypeCache { get; private set; }

[DataMember]private ConcurrentDictionary<string, string> _sidToDomainCache;
[DataMember] public ConcurrentDictionary<string, string> MachineSidCache { get; private set; }

[DataMember]private ConcurrentDictionary<string, string> _valueToIDCache;
[DataMember] public ConcurrentDictionary<string, string> SIDToDomainCache { get; private set; }

private Cache()
{
_valueToIDCache = new ConcurrentDictionary<string, string>();
_idToTypeCache = new ConcurrentDictionary<string, Label>();
_globalCatalogCache = new ConcurrentDictionary<string, string[]>();
_machineSidCache = new ConcurrentDictionary<string, string>();
_sidToDomainCache = new ConcurrentDictionary<string, string>();
}
[DataMember] public ConcurrentDictionary<string, string> ValueToIdCache { get; private set; }

[IgnoreDataMember]
private static Cache CacheInstance { get; set; }
[IgnoreDataMember] private static Cache CacheInstance { get; set; }

/// <summary>
/// Add a SID to/from Domain mapping to the cache
Expand All @@ -35,7 +35,7 @@ private Cache()
/// <param name="value"></param>
internal static void AddSidToDomain(string key, string value)
{
CacheInstance?._sidToDomainCache.TryAdd(key, value);
CacheInstance?.SIDToDomainCache.TryAdd(key, value);
}

/// <summary>
Expand All @@ -46,7 +46,7 @@ internal static void AddSidToDomain(string key, string value)
/// <returns></returns>
internal static bool GetDomainSidMapping(string key, out string value)
{
if (CacheInstance != null) return CacheInstance._machineSidCache.TryGetValue(key, out value);
if (CacheInstance != null) return CacheInstance.MachineSidCache.TryGetValue(key, out value);
value = null;
return false;
}
Expand All @@ -58,61 +58,61 @@ internal static bool GetDomainSidMapping(string key, out string value)
/// <param name="value"></param>
internal static void AddMachineSid(string key, string value)
{
CacheInstance?._machineSidCache.TryAdd(key, value);
CacheInstance?.MachineSidCache.TryAdd(key, value);
}

internal static bool GetMachineSid(string key, out string value)
{
if (CacheInstance != null) return CacheInstance._machineSidCache.TryGetValue(key, out value);
if (CacheInstance != null) return CacheInstance.MachineSidCache.TryGetValue(key, out value);
value = null;
return false;
}

internal static void AddConvertedValue(string key, string value)
{
CacheInstance?._valueToIDCache.TryAdd(key, value);
CacheInstance?.ValueToIdCache.TryAdd(key, value);
}

internal static void AddPrefixedValue(string key, string domain, string value)
{
CacheInstance?._valueToIDCache.TryAdd(GetPrefixKey(key, domain), value);
CacheInstance?.ValueToIdCache.TryAdd(GetPrefixKey(key, domain), value);
}

internal static void AddType(string key, Label value)
{
CacheInstance?._idToTypeCache.TryAdd(key, value);
CacheInstance?.IdToTypeCache.TryAdd(key, value);
}

internal static void AddGCCache(string key, string[] value)
{
CacheInstance?._globalCatalogCache?.TryAdd(key, value);
CacheInstance?.GlobalCatalogCache?.TryAdd(key, value);
}

internal static bool GetGCCache(string key, out string[] value)
{
if (CacheInstance != null) return CacheInstance._globalCatalogCache.TryGetValue(key, out value);
if (CacheInstance != null) return CacheInstance.GlobalCatalogCache.TryGetValue(key, out value);
value = null;
return false;
}

internal static bool GetConvertedValue(string key, out string value)
{
if (CacheInstance != null) return CacheInstance._valueToIDCache.TryGetValue(key, out value);
if (CacheInstance != null) return CacheInstance.ValueToIdCache.TryGetValue(key, out value);
value = null;
return false;
}

internal static bool GetPrefixedValue(string key, string domain, out string value)
{
if (CacheInstance != null)
return CacheInstance._valueToIDCache.TryGetValue(GetPrefixKey(key, domain), out value);
return CacheInstance.ValueToIdCache.TryGetValue(GetPrefixKey(key, domain), out value);
value = null;
return false;
}

internal static bool GetIDType(string key, out Label value)
{
if (CacheInstance != null) return CacheInstance._idToTypeCache.TryGetValue(key, out value);
if (CacheInstance != null) return CacheInstance.IdToTypeCache.TryGetValue(key, out value);
value = Label.Base;
return false;
}
Expand All @@ -123,7 +123,7 @@ private static string GetPrefixKey(string key, string domain)
}

/// <summary>
/// Creates a new empty cache instance
/// Creates a new empty cache instance
/// </summary>
/// <returns></returns>
public static Cache CreateNewCache()
Expand All @@ -132,7 +132,7 @@ public static Cache CreateNewCache()
}

/// <summary>
/// Sets the cache instance being used by the common library
/// Sets the cache instance being used by the common library
/// </summary>
/// <param name="cache"></param>
public static void SetCacheInstance(Cache cache)
Expand All @@ -142,24 +142,24 @@ public static void SetCacheInstance(Cache cache)
}

/// <summary>
/// Gets stats from the currently loaded cache
/// Gets stats from the currently loaded cache
/// </summary>
/// <returns></returns>
public string GetCacheStats()
{
try
{
return
$"{_idToTypeCache.Count} ID to type mappings.\n {_valueToIDCache.Count} name to SID mappings.\n {_machineSidCache.Count} machine sid mappings.\n {_sidToDomainCache.Count} sid to domain mappings.\n {_globalCatalogCache.Count} global catalog mappings.";
$"{IdToTypeCache.Count} ID to type mappings.\n {ValueToIdCache.Count} name to SID mappings.\n {MachineSidCache.Count} machine sid mappings.\n {SIDToDomainCache.Count} sid to domain mappings.\n {GlobalCatalogCache.Count} global catalog mappings.";
}
catch
{
return "";
}
}

/// <summary>
/// Returns the currently loaded cache instance
/// Returns the currently loaded cache instance
/// </summary>
/// <returns></returns>
public static Cache GetCacheInstance()
Expand All @@ -170,11 +170,11 @@ public static Cache GetCacheInstance()
private static void CreateMissingDictionaries()
{
CacheInstance ??= new Cache();
CacheInstance._idToTypeCache ??= new ConcurrentDictionary<string, Label>();
CacheInstance._globalCatalogCache ??= new ConcurrentDictionary<string, string[]>();
CacheInstance._machineSidCache ??= new ConcurrentDictionary<string, string>();
CacheInstance._sidToDomainCache ??= new ConcurrentDictionary<string, string>();
CacheInstance._valueToIDCache ??= new ConcurrentDictionary<string, string>();
CacheInstance.IdToTypeCache ??= new ConcurrentDictionary<string, Label>();
CacheInstance.GlobalCatalogCache ??= new ConcurrentDictionary<string, string[]>();
CacheInstance.MachineSidCache ??= new ConcurrentDictionary<string, string>();
CacheInstance.SIDToDomainCache ??= new ConcurrentDictionary<string, string>();
CacheInstance.ValueToIdCache ??= new ConcurrentDictionary<string, string>();
}
}
}
1 change: 1 addition & 0 deletions src/CommonLib/LDAPProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@ public class LDAPProperties
public const string UnixUserPassword = "unixuserpassword";
public const string UnicodePassword = "unicodepwd";
public const string MsSFU30Password = "msSFU30Password";
public const string ScriptPath = "scriptpath";
}
}
1 change: 1 addition & 0 deletions src/CommonLib/OutputTypes/MetaTag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SharpHoundCommonLib.OutputTypes
{
[DataContract]
public class MetaTag
{
[DataMember(Name="methods")] public long CollectionMethods { get; set; }
Expand Down
3 changes: 2 additions & 1 deletion src/CommonLib/Processors/LDAPPropertyProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ public async Task<UserProperties> ReadUserProperties(ISearchResultEntry entry)
props.Add("unixpassword", entry.GetProperty(LDAPProperties.UnixUserPassword));
props.Add("unicodepassword", entry.GetProperty(LDAPProperties.UnicodePassword));
props.Add("sfupassword", entry.GetProperty(LDAPProperties.MsSFU30Password));
props.Add("logonscript", entry.GetProperty(LDAPProperties.ScriptPath));

var ac = entry.GetProperty(LDAPProperties.AdminCount);
if (ac != null)
Expand Down Expand Up @@ -385,7 +386,7 @@ public async Task<ComputerProperties> ReadComputerProperties(ISearchResultEntry
/// format using a best guess
/// </summary>
/// <param name="entry"></param>
private static Dictionary<string, object> ParseAllProperties(ISearchResultEntry entry)
public Dictionary<string, object> ParseAllProperties(ISearchResultEntry entry)
{
var flag = IsTextUnicodeFlags.IS_TEXT_UNICODE_STATISTICS;
var props = new Dictionary<string, object>();
Expand Down
2 changes: 1 addition & 1 deletion src/CommonLib/SharpHoundCommonLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageDescription>Common library for C# BloodHound enumeration tasks</PackageDescription>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<RepositoryUrl>https://github.com/BloodHoundAD/SharpHoundCommon</RepositoryUrl>
<Version>2.0.15</Version>
<Version>2.1.0</Version>
<AssemblyName>SharpHoundCommonLib</AssemblyName>
<RootNamespace>SharpHoundCommonLib</RootNamespace>
</PropertyGroup>
Expand Down

0 comments on commit a13a94c

Please sign in to comment.