From c8efc1430aff1d5c055c48c58027e1a8e48d725f Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 1 May 2022 14:46:01 -0700 Subject: [PATCH] Get fancy --- BurnOutSharp/FileType/Executable.cs | 26 +------ BurnOutSharp/Initializer.cs | 56 -------------- BurnOutSharp/Scanner.cs | 13 +--- BurnOutSharp/ScanningClasses.cs | 111 ++++++++++++++++++++++++++++ 4 files changed, 116 insertions(+), 90 deletions(-) delete mode 100644 BurnOutSharp/Initializer.cs create mode 100644 BurnOutSharp/ScanningClasses.cs diff --git a/BurnOutSharp/FileType/Executable.cs b/BurnOutSharp/FileType/Executable.cs index 9b63834d..ed75bfa5 100644 --- a/BurnOutSharp/FileType/Executable.cs +++ b/BurnOutSharp/FileType/Executable.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; @@ -12,25 +11,6 @@ namespace BurnOutSharp.FileType { public class Executable : IScannable { - #region Checking Class Instances - - /// - /// Cache for all IContentCheck types - /// - private static readonly IEnumerable contentCheckClasses = Initializer.InitContentCheckClasses(); - - /// - /// Cache for all INEContentCheck types - /// - private static readonly IEnumerable neContentCheckClasses = Initializer.InitNEContentCheckClasses(); - - /// - /// Cache for all IPEContentCheck types - /// - private static readonly IEnumerable peContentCheckClasses = Initializer.InitPEContentCheckClasses(); - - #endregion - /// public bool ShouldScan(byte[] magic) { @@ -111,7 +91,7 @@ namespace BurnOutSharp.FileType // Iterate through all generic content checks if (fileContent != null) { - Parallel.ForEach(contentCheckClasses, contentCheckClass => + Parallel.ForEach(ScanningClasses.ContentCheckClasses, contentCheckClass => { string protection = contentCheckClass.CheckContents(file, fileContent, scanner.IncludeDebug, pex, nex); if (ShouldAddProtection(contentCheckClass, scanner.ScanPackers, protection)) @@ -133,7 +113,7 @@ namespace BurnOutSharp.FileType // If we have a NE executable, iterate through all NE content checks if (nex?.Initialized == true) { - Parallel.ForEach(neContentCheckClasses, contentCheckClass => + Parallel.ForEach(ScanningClasses.NEContentCheckClasses, contentCheckClass => { // Check using custom content checks first string protection = contentCheckClass.CheckNEContents(file, nex, scanner.IncludeDebug); @@ -156,7 +136,7 @@ namespace BurnOutSharp.FileType // If we have a PE executable, iterate through all PE content checks if (pex?.Initialized == true) { - Parallel.ForEach(peContentCheckClasses, contentCheckClass => + Parallel.ForEach(ScanningClasses.PEContentCheckClasses, contentCheckClass => { // Check using custom content checks first string protection = contentCheckClass.CheckPEContents(file, pex, scanner.IncludeDebug); diff --git a/BurnOutSharp/Initializer.cs b/BurnOutSharp/Initializer.cs deleted file mode 100644 index b7e5288e..00000000 --- a/BurnOutSharp/Initializer.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; - -namespace BurnOutSharp -{ - /// - /// Internal static initializers using reflection - /// - /// - /// These can probably be consolidated if Type variables are used - /// - internal static class Initializer - { - /// - /// Initialize all IContentCheck implementations - /// - public static IEnumerable InitContentCheckClasses() - { - return Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface(nameof(IContentCheck)) != null) - .Select(t => Activator.CreateInstance(t) as IContentCheck); - } - - /// - /// Initialize all INEContentCheck implementations - /// - public static IEnumerable InitNEContentCheckClasses() - { - return Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface(nameof(INEContentCheck)) != null) - .Select(t => Activator.CreateInstance(t) as INEContentCheck); - } - - /// - /// Initialize all IPathCheck implementations - /// - public static IEnumerable InitPathCheckClasses() - { - return Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null) - .Select(t => Activator.CreateInstance(t) as IPathCheck); - } - - /// - /// Initialize all IPEContentCheck implementations - /// - public static IEnumerable InitPEContentCheckClasses() - { - return Assembly.GetExecutingAssembly().GetTypes() - .Where(t => t.IsClass && t.GetInterface(nameof(IPEContentCheck)) != null) - .Select(t => Activator.CreateInstance(t) as IPEContentCheck); - } - } -} diff --git a/BurnOutSharp/Scanner.cs b/BurnOutSharp/Scanner.cs index 1004aeac..e77a7c44 100644 --- a/BurnOutSharp/Scanner.cs +++ b/BurnOutSharp/Scanner.cs @@ -35,15 +35,6 @@ namespace BurnOutSharp #endregion - #region Checking Class Instances - - /// - /// Cache for all IPathCheck types - /// - private static readonly IEnumerable pathCheckClasses = Initializer.InitPathCheckClasses(); - - #endregion - /// /// Constructor /// @@ -210,7 +201,7 @@ namespace BurnOutSharp files = files.Select(f => f.Replace('\\', '/')).ToList(); // Iterate through all path checks - Parallel.ForEach(pathCheckClasses, pathCheckClass => + Parallel.ForEach(ScanningClasses.PathCheckClasses, pathCheckClass => { ConcurrentQueue protection = pathCheckClass.CheckDirectoryPath(path, files); if (protection != null) @@ -235,7 +226,7 @@ namespace BurnOutSharp var protections = new ConcurrentQueue(); // Iterate through all path checks - Parallel.ForEach(pathCheckClasses, pathCheckClass => + Parallel.ForEach(ScanningClasses.PathCheckClasses, pathCheckClass => { string protection = pathCheckClass.CheckFilePath(path.Replace("\\", "/")); if (!string.IsNullOrWhiteSpace(protection)) diff --git a/BurnOutSharp/ScanningClasses.cs b/BurnOutSharp/ScanningClasses.cs new file mode 100644 index 00000000..6738fcee --- /dev/null +++ b/BurnOutSharp/ScanningClasses.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; + +namespace BurnOutSharp +{ + /// + /// Statically-generated lists of scanning classes + /// + internal static class ScanningClasses + { + #region Public Collections + + /// + /// Cache for all IContentCheck types + /// + public static IEnumerable ContentCheckClasses + { + get + { + if (contentCheckClasses == null) + contentCheckClasses = InitCheckClasses(); + + return contentCheckClasses; + } + } + + /// + /// Cache for all INEContentCheck types + /// + public static IEnumerable NEContentCheckClasses + { + get + { + if (neContentCheckClasses == null) + neContentCheckClasses = InitCheckClasses(); + + return neContentCheckClasses; + } + } + + /// + /// Cache for all IPathCheck types + /// + public static IEnumerable PathCheckClasses + { + get + { + if (pathCheckClasses == null) + pathCheckClasses = InitCheckClasses(); + + return pathCheckClasses; + } + } + + /// + /// Cache for all IPEContentCheck types + /// + public static IEnumerable PEContentCheckClasses + { + get + { + if (peContentCheckClasses == null) + peContentCheckClasses = InitCheckClasses(); + + return peContentCheckClasses; + } + } + + #endregion + + #region Internal Instances + + /// + /// Cache for all IContentCheck types + /// + private static IEnumerable contentCheckClasses; + + /// + /// Cache for all INEContentCheck types + /// + private static IEnumerable neContentCheckClasses; + + /// + /// Cache for all IPathCheck types + /// + private static IEnumerable pathCheckClasses; + + /// + /// Cache for all IPEContentCheck types + /// + private static IEnumerable peContentCheckClasses; + + #endregion + + #region Initializers + + /// + /// Initialize all implementations of a type + /// + private static IEnumerable InitCheckClasses() + { + return Assembly.GetExecutingAssembly().GetTypes() + .Where(t => t.IsClass && t.GetInterface(typeof(T).Name) != null) + .Select(t => (T)Activator.CreateInstance(t)); + } + + #endregion + } +}