mirror of
https://github.com/SabreTools/BinaryObjectScanner.git
synced 2026-09-22 14:54:56 +00:00
Get fancy
This commit is contained in:
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IContentCheck types
|
||||
/// </summary>
|
||||
private static readonly IEnumerable<IContentCheck> contentCheckClasses = Initializer.InitContentCheckClasses();
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all INEContentCheck types
|
||||
/// </summary>
|
||||
private static readonly IEnumerable<INEContentCheck> neContentCheckClasses = Initializer.InitNEContentCheckClasses();
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPEContentCheck types
|
||||
/// </summary>
|
||||
private static readonly IEnumerable<IPEContentCheck> peContentCheckClasses = Initializer.InitPEContentCheckClasses();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <inheritdoc/>
|
||||
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);
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BurnOutSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Internal static initializers using reflection
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// These can probably be consolidated if Type variables are used
|
||||
/// </remarks>
|
||||
internal static class Initializer
|
||||
{
|
||||
/// <summary>
|
||||
/// Initialize all IContentCheck implementations
|
||||
/// </summary>
|
||||
public static IEnumerable<IContentCheck> InitContentCheckClasses()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(t => t.IsClass && t.GetInterface(nameof(IContentCheck)) != null)
|
||||
.Select(t => Activator.CreateInstance(t) as IContentCheck);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize all INEContentCheck implementations
|
||||
/// </summary>
|
||||
public static IEnumerable<INEContentCheck> InitNEContentCheckClasses()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(t => t.IsClass && t.GetInterface(nameof(INEContentCheck)) != null)
|
||||
.Select(t => Activator.CreateInstance(t) as INEContentCheck);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize all IPathCheck implementations
|
||||
/// </summary>
|
||||
public static IEnumerable<IPathCheck> InitPathCheckClasses()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(t => t.IsClass && t.GetInterface(nameof(IPathCheck)) != null)
|
||||
.Select(t => Activator.CreateInstance(t) as IPathCheck);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialize all IPEContentCheck implementations
|
||||
/// </summary>
|
||||
public static IEnumerable<IPEContentCheck> InitPEContentCheckClasses()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(t => t.IsClass && t.GetInterface(nameof(IPEContentCheck)) != null)
|
||||
.Select(t => Activator.CreateInstance(t) as IPEContentCheck);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -35,15 +35,6 @@ namespace BurnOutSharp
|
||||
|
||||
#endregion
|
||||
|
||||
#region Checking Class Instances
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPathCheck types
|
||||
/// </summary>
|
||||
private static readonly IEnumerable<IPathCheck> pathCheckClasses = Initializer.InitPathCheckClasses();
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
@@ -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<string> protection = pathCheckClass.CheckDirectoryPath(path, files);
|
||||
if (protection != null)
|
||||
@@ -235,7 +226,7 @@ namespace BurnOutSharp
|
||||
var protections = new ConcurrentQueue<string>();
|
||||
|
||||
// Iterate through all path checks
|
||||
Parallel.ForEach(pathCheckClasses, pathCheckClass =>
|
||||
Parallel.ForEach(ScanningClasses.PathCheckClasses, pathCheckClass =>
|
||||
{
|
||||
string protection = pathCheckClass.CheckFilePath(path.Replace("\\", "/"));
|
||||
if (!string.IsNullOrWhiteSpace(protection))
|
||||
|
||||
111
BurnOutSharp/ScanningClasses.cs
Normal file
111
BurnOutSharp/ScanningClasses.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace BurnOutSharp
|
||||
{
|
||||
/// <summary>
|
||||
/// Statically-generated lists of scanning classes
|
||||
/// </summary>
|
||||
internal static class ScanningClasses
|
||||
{
|
||||
#region Public Collections
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IContentCheck types
|
||||
/// </summary>
|
||||
public static IEnumerable<IContentCheck> ContentCheckClasses
|
||||
{
|
||||
get
|
||||
{
|
||||
if (contentCheckClasses == null)
|
||||
contentCheckClasses = InitCheckClasses<IContentCheck>();
|
||||
|
||||
return contentCheckClasses;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all INEContentCheck types
|
||||
/// </summary>
|
||||
public static IEnumerable<INEContentCheck> NEContentCheckClasses
|
||||
{
|
||||
get
|
||||
{
|
||||
if (neContentCheckClasses == null)
|
||||
neContentCheckClasses = InitCheckClasses<INEContentCheck>();
|
||||
|
||||
return neContentCheckClasses;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPathCheck types
|
||||
/// </summary>
|
||||
public static IEnumerable<IPathCheck> PathCheckClasses
|
||||
{
|
||||
get
|
||||
{
|
||||
if (pathCheckClasses == null)
|
||||
pathCheckClasses = InitCheckClasses<IPathCheck>();
|
||||
|
||||
return pathCheckClasses;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPEContentCheck types
|
||||
/// </summary>
|
||||
public static IEnumerable<IPEContentCheck> PEContentCheckClasses
|
||||
{
|
||||
get
|
||||
{
|
||||
if (peContentCheckClasses == null)
|
||||
peContentCheckClasses = InitCheckClasses<IPEContentCheck>();
|
||||
|
||||
return peContentCheckClasses;
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Internal Instances
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IContentCheck types
|
||||
/// </summary>
|
||||
private static IEnumerable<IContentCheck> contentCheckClasses;
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all INEContentCheck types
|
||||
/// </summary>
|
||||
private static IEnumerable<INEContentCheck> neContentCheckClasses;
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPathCheck types
|
||||
/// </summary>
|
||||
private static IEnumerable<IPathCheck> pathCheckClasses;
|
||||
|
||||
/// <summary>
|
||||
/// Cache for all IPEContentCheck types
|
||||
/// </summary>
|
||||
private static IEnumerable<IPEContentCheck> peContentCheckClasses;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Initializers
|
||||
|
||||
/// <summary>
|
||||
/// Initialize all implementations of a type
|
||||
/// </summary>
|
||||
private static IEnumerable<T> InitCheckClasses<T>()
|
||||
{
|
||||
return Assembly.GetExecutingAssembly().GetTypes()
|
||||
.Where(t => t.IsClass && t.GetInterface(typeof(T).Name) != null)
|
||||
.Select(t => (T)Activator.CreateInstance(t));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user