From 3d8134bbd38e60e394f471a449841ca26be5a567 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 13 Mar 2023 22:36:28 -0400 Subject: [PATCH] Map out Executable extraction skeleton --- BurnOutSharp/Scanner.cs | 93 +++++++++++++++++++++++++++++------------ 1 file changed, 67 insertions(+), 26 deletions(-) diff --git a/BurnOutSharp/Scanner.cs b/BurnOutSharp/Scanner.cs index a7432191..33cb8821 100644 --- a/BurnOutSharp/Scanner.cs +++ b/BurnOutSharp/Scanner.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using BinaryObjectScanner.FileType; using BinaryObjectScanner.Interfaces; using BinaryObjectScanner.Utilities; using static BinaryObjectScanner.Utilities.Dictionary; @@ -385,37 +386,34 @@ namespace BurnOutSharp // If we're scanning archives if (extractable != null && ScanArchives) { - // If the extractable file itself fails - try + // If we have an executable, it needs to bypass normal handling + if (extractable is Executable) { - // Extract and get the output path - string tempPath = extractable.Extract(stream, fileName, IncludeDebug); - if (tempPath == null) - return null; + var subProtections = HandleExtractable(extractable, fileName, stream); + if (subProtections != null) + AppendToDictionary(protections, subProtections); - // Collect and format all found protections - var subProtections = GetProtections(tempPath); + // The following code is disabled because it is untested, though it represents the likely path + // for how this implementation will be completed. - // If temp directory cleanup fails - try - { - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - StripFromKeys(protections, tempPath); - PrependToKeys(subProtections, fileName); - AppendToDictionary(protections, subProtections); - - return protections; + //Parallel.ForEach(ScanningClasses.ExtractableClasses, extractableClass => + //{ + // string tempDir = extractableClass.Extract(stream, fileName, IncludeDebug); + // if (!string.IsNullOrWhiteSpace(tempDir)) + // { + // var subProtections = HandleExtractable(extractable, fileName, stream); + // if (subProtections != null) + // AppendToDictionary(protections, subProtections); + // } + //}); } - catch (Exception ex) + + // Otherwise, use the default implementation + else { - if (IncludeDebug) Console.WriteLine(ex); + var subProtections = HandleExtractable(extractable, fileName, stream); + if (subProtections != null) + AppendToDictionary(protections, subProtections); } } @@ -513,6 +511,49 @@ namespace BurnOutSharp } } + /// + /// Handle extractable files based on an IExtractable implementation + /// + /// IExtractable class representing the file type + /// Name of the source file of the stream, for tracking + /// Stream to scan the contents of + /// Set of protections in internal files, null on error + private ConcurrentDictionary> HandleExtractable(IExtractable extractable, string fileName, Stream stream) + { + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = extractable.Extract(stream, fileName, IncludeDebug); + if (tempPath == null) + return null; + + // Collect and format all found protections + var subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + StripFromKeys(subProtections, tempPath); + PrependToKeys(subProtections, fileName); + return subProtections; + } + catch (Exception ex) + { + if (IncludeDebug) Console.WriteLine(ex); + } + + return null; + } + #endregion } }