From 3822cc41f2845a30996c55f10055c9214c405e0a Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 5 Nov 2024 01:24:14 -0500 Subject: [PATCH] Extract loop into new method; fix build --- BinaryObjectScanner/Data/StaticChecks.cs | 2 +- BinaryObjectScanner/FileType/Executable.cs | 94 ++++++++++++++-------- BinaryObjectScanner/Scanner.cs | 4 +- 3 files changed, 62 insertions(+), 38 deletions(-) diff --git a/BinaryObjectScanner/Data/StaticChecks.cs b/BinaryObjectScanner/Data/StaticChecks.cs index 23045896..719eb5d4 100644 --- a/BinaryObjectScanner/Data/StaticChecks.cs +++ b/BinaryObjectScanner/Data/StaticChecks.cs @@ -144,7 +144,7 @@ namespace BinaryObjectScanner.Data } // Get information from the type param - string interfaceName = typeof(T)!.FullName; + string interfaceName = typeof(T)!.FullName!; // Loop through all types foreach (Type type in assemblyTypes) diff --git a/BinaryObjectScanner/FileType/Executable.cs b/BinaryObjectScanner/FileType/Executable.cs index 4890f08a..b99ad2f6 100644 --- a/BinaryObjectScanner/FileType/Executable.cs +++ b/BinaryObjectScanner/FileType/Executable.cs @@ -58,7 +58,7 @@ namespace BinaryObjectScanner.FileType protectionList.AddRange(protections[key]); } - return string.Join(";", [.. protections]); + return string.Join(";", [.. protectionList]); } /// @@ -239,9 +239,13 @@ namespace BinaryObjectScanner.FileType /// Scanner for handling recursive protections /// True to include debug data, false otherwise /// Set of protections found from extraction, empty on error - private ProtectionDictionary HandleExtractableProtections(string file, T exe, IEnumerable checks, Scanner? scanner, bool includeDebug) - where T : WrapperBase - where U : IExecutableCheck + private static ProtectionDictionary HandleExtractableProtections(string file, + T exe, + IEnumerable checks, + Scanner? scanner, + bool includeDebug) + where T : WrapperBase + where U : IExecutableCheck { // Create the output dictionary var protections = new ProtectionDictionary(); @@ -256,46 +260,66 @@ namespace BinaryObjectScanner.FileType .Select(c => c as IExtractableExecutable); extractables.IterateWithAction(extractable => { - // If we have an invalid extractable somehow - if (extractable == null) - return; + var subProtections = PerformExtractableCheck(extractable!, file, exe, scanner, includeDebug); + protections.Append(subProtections); + }); - // If the extractable file itself fails + return protections; + } + + /// + /// Handle files based on an IExtractableExecutable implementation + /// + /// Name of the source file of the stream, for tracking + /// Executable to scan the contents of + /// IExtractableExecutable class representing the file type + /// Scanner for handling recursive protections + /// True to include debug data, false otherwise + /// Set of protections in path, empty on error + private static ProtectionDictionary PerformExtractableCheck(IExtractableExecutable impl, + string file, + T exe, + Scanner? scanner, + bool includeDebug) + where T : WrapperBase + { + // If we have an invalid extractable somehow + if (impl == null) + return []; + + // 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(file, exe, tempPath, includeDebug); + + // Collect and format all found protections + ProtectionDictionary? subProtections = null; + if (extracted) + subProtections = scanner?.GetProtections(tempPath); + + // If temp directory cleanup fails try { - // Extract and get the output path - string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); - bool extracted = extractable.Extract(file, exe, tempPath, 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 (includeDebug) Console.WriteLine(ex); - } - - // Prepare the returned protections - subProtections?.StripFromKeys(tempPath); - subProtections?.PrependToKeys(file); - if (subProtections != null) - protections.Append(subProtections); + if (Directory.Exists(tempPath)) + Directory.Delete(tempPath, true); } catch (Exception ex) { if (includeDebug) Console.WriteLine(ex); } - }); - return protections; + // Prepare the returned protections + subProtections?.StripFromKeys(tempPath); + subProtections?.PrependToKeys(file); + return subProtections ?? []; + } + catch (Exception ex) + { + if (includeDebug) Console.WriteLine(ex); + return []; + } } #endregion diff --git a/BinaryObjectScanner/Scanner.cs b/BinaryObjectScanner/Scanner.cs index 2a71b4ae..2be74cd8 100644 --- a/BinaryObjectScanner/Scanner.cs +++ b/BinaryObjectScanner/Scanner.cs @@ -367,7 +367,7 @@ namespace BinaryObjectScanner // Iterate through all checks StaticChecks.PathCheckClasses.IterateWithAction(checkClass => { - var subProtections = PerformCheck(checkClass, path, files); + var subProtections = PerformPathCheck(checkClass, path, files); protections.Append(path, subProtections); }); @@ -380,7 +380,7 @@ namespace BinaryObjectScanner /// IPathCheck class representing the file type /// Path of the file or directory to check /// Set of protections in path, empty on error - private static List PerformCheck(IPathCheck impl, string? path, IEnumerable? files) + private static List PerformPathCheck(IPathCheck impl, string? path, IEnumerable? files) { // If we have an invalid path if (string.IsNullOrEmpty(path))