Extract loop into new method; fix build

This commit is contained in:
Matt Nadareski
2024-11-05 01:24:14 -05:00
parent f04cf25fa9
commit 3822cc41f2
3 changed files with 62 additions and 38 deletions

View File

@@ -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)

View File

@@ -58,7 +58,7 @@ namespace BinaryObjectScanner.FileType
protectionList.AddRange(protections[key]);
}
return string.Join(";", [.. protections]);
return string.Join(";", [.. protectionList]);
}
/// <inheritdoc cref="IDetectable.Detect(Stream, string, bool)"/>
@@ -239,9 +239,13 @@ namespace BinaryObjectScanner.FileType
/// <param name="scanner">Scanner for handling recursive protections</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections found from extraction, empty on error</returns>
private ProtectionDictionary HandleExtractableProtections<T, U>(string file, T exe, IEnumerable<U> checks, Scanner? scanner, bool includeDebug)
where T : WrapperBase
where U : IExecutableCheck<T>
private static ProtectionDictionary HandleExtractableProtections<T, U>(string file,
T exe,
IEnumerable<U> checks,
Scanner? scanner,
bool includeDebug)
where T : WrapperBase
where U : IExecutableCheck<T>
{
// Create the output dictionary
var protections = new ProtectionDictionary();
@@ -256,46 +260,66 @@ namespace BinaryObjectScanner.FileType
.Select(c => c as IExtractableExecutable<T>);
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;
}
/// <summary>
/// Handle files based on an IExtractableExecutable implementation
/// </summary>
/// <param name="file">Name of the source file of the stream, for tracking</param>
/// <param name="exe">Executable to scan the contents of</param>
/// <param name="impl">IExtractableExecutable class representing the file type</param>
/// <param name="scanner">Scanner for handling recursive protections</param>
/// <param name="includeDebug">True to include debug data, false otherwise</param>
/// <returns>Set of protections in path, empty on error</returns>
private static ProtectionDictionary PerformExtractableCheck<T>(IExtractableExecutable<T> 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

View File

@@ -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
/// <param name="impl">IPathCheck class representing the file type</param>
/// <param name="path">Path of the file or directory to check</param>
/// <returns>Set of protections in path, empty on error</returns>
private static List<string> PerformCheck(IPathCheck impl, string? path, IEnumerable<string>? files)
private static List<string> PerformPathCheck(IPathCheck impl, string? path, IEnumerable<string>? files)
{
// If we have an invalid path
if (string.IsNullOrEmpty(path))