From 7d6248a3bf375934585ce3d508e973b5cf6fe00a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 4 Nov 2024 21:53:59 -0500 Subject: [PATCH] Re-merge some code from Handler to Scanner --- BinaryObjectScanner/Handler.cs | 231 --------------------------------- BinaryObjectScanner/Scanner.cs | 211 ++++++++++++++++++++++++++---- 2 files changed, 184 insertions(+), 258 deletions(-) diff --git a/BinaryObjectScanner/Handler.cs b/BinaryObjectScanner/Handler.cs index 13bf9c79..beec6f48 100644 --- a/BinaryObjectScanner/Handler.cs +++ b/BinaryObjectScanner/Handler.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Reflection; using BinaryObjectScanner.Interfaces; -using SabreTools.Serialization.Wrappers; namespace BinaryObjectScanner { @@ -80,236 +79,6 @@ namespace BinaryObjectScanner return ProcessProtectionString(protection); } - /// - /// Handle 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 - /// Scanner object to use on extractable contents - /// Set of protections in file, null on error - public static ProtectionDictionary? HandleExtractable(IExtractable impl, string fileName, Stream? stream, Scanner scanner) - { - // If the extractable file itself fails - try - { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = impl.Extract(stream, fileName, tempPath, scanner.IncludeDebug); - - // Collect and format all found protections - ProtectionDictionary? subProtections = null; - if (extracted) - subProtections = scanner.GetProtections(tempPath); - - // If temp directory cleanup fails - try - { - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(fileName); - return subProtections; - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - return null; - } - - /// - /// Handle files based on an IExtractableMSDOSExecutable implementation - /// - /// IExtractableMSDOSExecutable class representing the file type - /// Name of the source file of the stream, for tracking - /// MSDOS to scan the contents of - /// Scanner object to use on extractable contents - /// Set of protections in file, null on error - public static ProtectionDictionary? HandleExtractable(IExtractableMSDOSExecutable impl, string fileName, MSDOS mz, Scanner scanner) - { - // If the extractable file itself fails - try - { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = impl.Extract(fileName, mz, tempPath, scanner.IncludeDebug); - - // Collect and format all found protections - ProtectionDictionary? subProtections = null; - if (extracted) - subProtections = scanner.GetProtections(tempPath); - - // If temp directory cleanup fails - try - { - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(fileName); - return subProtections; - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - return null; - } - - /// - /// Handle files based on an IExtractableLinearExecutable implementation - /// - /// IExtractableLinearExecutable class representing the file type - /// Name of the source file of the stream, for tracking - /// LinearExecutable to scan the contents of - /// Scanner object to use on extractable contents - /// Set of protections in file, null on error - public static ProtectionDictionary? HandleExtractable(IExtractableLinearExecutable impl, string fileName, LinearExecutable lex, Scanner scanner) - { - // If the extractable file itself fails - try - { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = impl.Extract(fileName, lex, tempPath, scanner.IncludeDebug); - - // Collect and format all found protections - ProtectionDictionary? subProtections = null; - if (extracted) - subProtections = scanner.GetProtections(tempPath); - - // If temp directory cleanup fails - try - { - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(fileName); - return subProtections; - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - return null; - } - - /// - /// Handle files based on an IExtractableNewExecutable implementation - /// - /// IExtractableNewExecutable class representing the file type - /// Name of the source file of the stream, for tracking - /// NewExecutable to scan the contents of - /// Scanner object to use on extractable contents - /// Set of protections in file, null on error - public static ProtectionDictionary? HandleExtractable(IExtractableNewExecutable impl, string fileName, NewExecutable nex, Scanner scanner) - { - // If the extractable file itself fails - try - { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = impl.Extract(fileName, nex, tempPath, scanner.IncludeDebug); - - // Collect and format all found protections - ProtectionDictionary? subProtections = null; - if (extracted) - subProtections = scanner.GetProtections(tempPath); - - // If temp directory cleanup fails - try - { - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(fileName); - return subProtections; - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - return null; - } - - /// - /// Handle files based on an IExtractablePortableExecutable implementation - /// - /// IExtractablePortableExecutable class representing the file type - /// Name of the source file of the stream, for tracking - /// PortableExecutable to scan the contents of - /// Scanner object to use on extractable contents - /// Set of protections in file, null on error - public static ProtectionDictionary? HandleExtractable(IExtractablePortableExecutable impl, string fileName, PortableExecutable pex, Scanner scanner) - { - // If the extractable file itself fails - try - { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = impl.Extract(fileName, pex, tempPath, scanner.IncludeDebug); - - // Collect and format all found protections - ProtectionDictionary? subProtections = null; - if (extracted) - subProtections = scanner.GetProtections(tempPath); - - // If temp directory cleanup fails - try - { - if (Directory.Exists(tempPath)) - Directory.Delete(tempPath, true); - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(fileName); - return subProtections; - } - catch (Exception ex) - { - if (scanner.IncludeDebug) Console.WriteLine(ex); - } - - return null; - } - /// /// Handle files based on an IPathCheck implementation /// diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 37e8b17e..75beb9f2 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -14,9 +14,6 @@ namespace BinaryObjectScanner { #region Options - /// - public bool IncludeDebug => _options.IncludeDebug; - /// /// Options object for configuration /// @@ -39,7 +36,13 @@ namespace BinaryObjectScanner /// Enable including path detections in output /// Enable including debug information /// Optional progress callback - public Scanner(bool scanArchives, bool scanContents, bool scanGameEngines, bool scanPackers, bool scanPaths, bool includeDebug, IProgress? fileProgress = null) + public Scanner(bool scanArchives, + bool scanContents, + bool scanGameEngines, + bool scanPackers, + bool scanPaths, + bool includeDebug, + IProgress? fileProgress = null) { _options = new Options { @@ -308,9 +311,39 @@ namespace BinaryObjectScanner // If we're scanning archives if (extractable != null && _options.ScanArchives) { - var subProtections = Handler.HandleExtractable(extractable, fileName, stream, this); - if (subProtections != null) - protections.Append(subProtections); + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = extractable.Extract(stream, fileName, tempPath, _options.IncludeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(fileName); + if (subProtections != null) + protections.Append(subProtections); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } } #endregion @@ -435,7 +468,9 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// MSDOS to scan the contents of /// Set of protections found from extraction, null on error - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, MSDOS mz) + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, + string fileName, + MSDOS mz) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -454,10 +489,39 @@ namespace BinaryObjectScanner if (extractable == null) return; - // Get the protection for the class, if possible - var extractedProtections = Handler.HandleExtractable(extractable, fileName, mz, this); - if (extractedProtections != null) - protections.Append(extractedProtections); + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = extractable.Extract(fileName, mz, tempPath, _options.IncludeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(fileName); + if (subProtections != null) + protections.Append(subProtections); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } }); return protections; @@ -470,7 +534,9 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// LinearExecutable to scan the contents of /// Set of protections found from extraction, null on error - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, LinearExecutable lex) + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, + string fileName, + LinearExecutable lex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -489,10 +555,39 @@ namespace BinaryObjectScanner if (extractable == null) return; - // Get the protection for the class, if possible - var extractedProtections = Handler.HandleExtractable(extractable, fileName, lex, this); - if (extractedProtections != null) - protections.Append(extractedProtections); + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = extractable.Extract(fileName, lex, tempPath, _options.IncludeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(fileName); + if (subProtections != null) + protections.Append(subProtections); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } }); return protections; @@ -505,7 +600,9 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// NewExecutable to scan the contents of /// Set of protections found from extraction, null on error - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, NewExecutable nex) + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, + string fileName, + NewExecutable nex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -524,10 +621,39 @@ namespace BinaryObjectScanner if (extractable == null) return; - // Get the protection for the class, if possible - var extractedProtections = Handler.HandleExtractable(extractable, fileName, nex, this); - if (extractedProtections != null) - protections.Append(extractedProtections); + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = extractable.Extract(fileName, nex, tempPath, _options.IncludeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(fileName); + if (subProtections != null) + protections.Append(subProtections); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } }); return protections; @@ -540,7 +666,9 @@ namespace BinaryObjectScanner /// Name of the source file of the stream, for tracking /// PortableExecutable to scan the contents of /// Set of protections found from extraction, null on error - private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, string fileName, PortableExecutable pex) + private ProtectionDictionary? HandleExtractableProtections(IEnumerable? classes, + string fileName, + PortableExecutable pex) { // If we have an invalid set of classes if (classes == null || !classes.Any()) @@ -559,10 +687,39 @@ namespace BinaryObjectScanner if (extractable == null) return; - // Get the protection for the class, if possible - var extractedProtections = Handler.HandleExtractable(extractable, fileName, pex, this); - if (extractedProtections != null) - protections.Append(extractedProtections); + // If the extractable file itself fails + try + { + // Extract and get the output path + string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + bool extracted = extractable.Extract(fileName, pex, tempPath, _options.IncludeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = GetProtections(tempPath); + + // If temp directory cleanup fails + try + { + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } + + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(fileName); + if (subProtections != null) + protections.Append(subProtections); + } + catch (Exception ex) + { + if (_options.IncludeDebug) Console.WriteLine(ex); + } }); return protections;