diff --git a/src/BaseContext.cs b/src/BaseContext.cs index a8db491..c83a4a9 100644 --- a/src/BaseContext.cs +++ b/src/BaseContext.cs @@ -9,6 +9,7 @@ using Sharphound.Runtime; using SharpHoundCommonLib; using SharpHoundCommonLib.Enums; +using SharpHoundCommonLib.Processors; using Timer = System.Timers.Timer; namespace Sharphound @@ -29,6 +30,7 @@ public BaseContext(ILogger logger, LdapConfig ldapConfig, Flags flags) Flags = flags; LDAPUtils = new LdapUtils(); LDAPUtils.SetLdapConfig(ldapConfig); + ACLProcessorContext = new ACLProcessorContext(); CancellationTokenSource = new CancellationTokenSource(); AdminSDHolderHash = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase); } @@ -59,6 +61,7 @@ public BaseContext(ILogger logger, LdapConfig ldapConfig, Flags flags) public int PortScanTimeout { get; set; } = 500; public CancellationTokenSource CancellationTokenSource { get; set; } public ILdapUtils LDAPUtils { get; set; } + public ACLProcessorContext ACLProcessorContext { get; } public Task CollectionTask { get; set; } public Flags Flags { get; set; } @@ -129,40 +132,15 @@ public string ResolveFileName(string filename, string extension, bool addTimesta /// public ConcurrentDictionary AdminSDHolderHash { get; set; } - // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources - // ~Context() - // { - // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - // Dispose(disposing: false); - // } - - /// - /// TODO: Implement the primary dispose pattern - /// public void Dispose() { - // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method - Dispose(true); - GC.SuppressFinalize(this); - } - - /// - /// TODO: Implement the primary dispose pattern - /// - /// - private void Dispose(bool disposing) - { - if (!disposedValue) + if (disposedValue) { - if (disposing) - { - // TODO: dispose managed state (managed objects) - } - - // TODO: free unmanaged resources (unmanaged objects) and override finalizer - // TODO: set large fields to null - disposedValue = true; + return; } + + ACLProcessorContext.Dispose(); + disposedValue = true; } } } diff --git a/src/Client/Context.cs b/src/Client/Context.cs index 55a6628..d128169 100644 --- a/src/Client/Context.cs +++ b/src/Client/Context.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Logging; using SharpHoundCommonLib; using SharpHoundCommonLib.Enums; +using SharpHoundCommonLib.Processors; using Timer = System.Timers.Timer; namespace Sharphound.Client @@ -49,6 +50,7 @@ public interface IContext ILogger Logger { get; set; } ILdapUtils LDAPUtils { get; set; } + ACLProcessorContext ACLProcessorContext { get; } string OutputPrefix { get; set; } string OutputDirectory { get; set; } @@ -83,4 +85,4 @@ public interface IContext /// ConcurrentDictionary AdminSDHolderHash { get; set; } } -} \ No newline at end of file +} diff --git a/src/Producers/LdapProducer.cs b/src/Producers/LdapProducer.cs index c5c602a..3014038 100644 --- a/src/Producers/LdapProducer.cs +++ b/src/Producers/LdapProducer.cs @@ -94,7 +94,7 @@ public override async Task Produce() //Context.Logger.LogDebug("{Domain} AdminSDHolder SD Bytes: {Bytes}", domain.Name, B64); // Create an instance of ACLProcessor - _aclProcessor from ObjectProcessors isn't in this context - var aclProcessor = new ACLProcessor(Context.LDAPUtils); + var aclProcessor = Context.ACLProcessorContext.CreateACLProcessor(Context.LDAPUtils); // Calculate the authoritative SD based on a hash of the implicit ACLs & AclProtected var authoritativeSd = aclProcessor.CalculateImplicitACLHash(sd); @@ -242,4 +242,4 @@ public override async Task ProduceConfigNC() } } } -} \ No newline at end of file +} diff --git a/src/Runtime/ObjectProcessors.cs b/src/Runtime/ObjectProcessors.cs index 457ddbf..2036b21 100644 --- a/src/Runtime/ObjectProcessors.cs +++ b/src/Runtime/ObjectProcessors.cs @@ -46,7 +46,7 @@ public class ObjectProcessors { public ObjectProcessors(IContext context, ILogger log, Channel compStatusChannel) { _context = context; - _aclProcessor = new ACLProcessor(context.LDAPUtils); + _aclProcessor = context.ACLProcessorContext.CreateACLProcessor(context.LDAPUtils); _spnProcessor = new SPNProcessors(context.LDAPUtils); _ldapPropertyProcessor = new LdapPropertyProcessor(context.LDAPUtils); _domainTrustProcessor = new DomainTrustProcessor(context.LDAPUtils); @@ -956,4 +956,4 @@ private async Task ProcessIssuancePolicy(IDirectoryObject entry, return ret; } } -} \ No newline at end of file +} diff --git a/src/Sharphound.cs b/src/Sharphound.cs index 1229650..a739306 100644 --- a/src/Sharphound.cs +++ b/src/Sharphound.cs @@ -198,7 +198,7 @@ await options.WithParsedAsync(async options => private static async Task StartCollection(Options options, BasicLogger logger, CollectionMethod resolved, Flags flags, LdapConfig ldapOptions) { - IContext context = new BaseContext(logger, ldapOptions, flags) + using var baseContext = new BaseContext(logger, ldapOptions, flags) { DomainName = options.Domain, CacheFileName = options.CacheName, @@ -222,6 +222,7 @@ private static async Task StartCollection(Options options, BasicLogger logger, C LocalAdminUsername = options.LocalAdminUsername, LocalAdminPassword = options.LocalAdminPassword }; + IContext context = baseContext; var cancellationTokenSource = new CancellationTokenSource(); context.CancellationTokenSource = cancellationTokenSource; @@ -264,4 +265,4 @@ public static void InvokeSharpHound(string[] args) { } #endregion -} \ No newline at end of file +}