Skip to content

Commit 3822cc4

Browse files
committed
Extract loop into new method; fix build
1 parent f04cf25 commit 3822cc4

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

BinaryObjectScanner/Data/StaticChecks.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public static List<IExecutableCheck<PortableExecutable>> PortableExecutableCheck
144144
}
145145

146146
// Get information from the type param
147-
string interfaceName = typeof(T)!.FullName;
147+
string interfaceName = typeof(T)!.FullName!;
148148

149149
// Loop through all types
150150
foreach (Type type in assemblyTypes)

BinaryObjectScanner/FileType/Executable.cs

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public class Executable : IDetectable
5858
protectionList.AddRange(protections[key]);
5959
}
6060

61-
return string.Join(";", [.. protections]);
61+
return string.Join(";", [.. protectionList]);
6262
}
6363

6464
/// <inheritdoc cref="IDetectable.Detect(Stream, string, bool)"/>
@@ -239,9 +239,13 @@ public IDictionary<U, string> RunExecutableChecks<T, U>(string file, T exe, List
239239
/// <param name="scanner">Scanner for handling recursive protections</param>
240240
/// <param name="includeDebug">True to include debug data, false otherwise</param>
241241
/// <returns>Set of protections found from extraction, empty on error</returns>
242-
private ProtectionDictionary HandleExtractableProtections<T, U>(string file, T exe, IEnumerable<U> checks, Scanner? scanner, bool includeDebug)
243-
where T : WrapperBase
244-
where U : IExecutableCheck<T>
242+
private static ProtectionDictionary HandleExtractableProtections<T, U>(string file,
243+
T exe,
244+
IEnumerable<U> checks,
245+
Scanner? scanner,
246+
bool includeDebug)
247+
where T : WrapperBase
248+
where U : IExecutableCheck<T>
245249
{
246250
// Create the output dictionary
247251
var protections = new ProtectionDictionary();
@@ -256,46 +260,66 @@ private ProtectionDictionary HandleExtractableProtections<T, U>(string file, T e
256260
.Select(c => c as IExtractableExecutable<T>);
257261
extractables.IterateWithAction(extractable =>
258262
{
259-
// If we have an invalid extractable somehow
260-
if (extractable == null)
261-
return;
263+
var subProtections = PerformExtractableCheck(extractable!, file, exe, scanner, includeDebug);
264+
protections.Append(subProtections);
265+
});
266+
267+
return protections;
268+
}
269+
270+
/// <summary>
271+
/// Handle files based on an IExtractableExecutable implementation
272+
/// </summary>
273+
/// <param name="file">Name of the source file of the stream, for tracking</param>
274+
/// <param name="exe">Executable to scan the contents of</param>
275+
/// <param name="impl">IExtractableExecutable class representing the file type</param>
276+
/// <param name="scanner">Scanner for handling recursive protections</param>
277+
/// <param name="includeDebug">True to include debug data, false otherwise</param>
278+
/// <returns>Set of protections in path, empty on error</returns>
279+
private static ProtectionDictionary PerformExtractableCheck<T>(IExtractableExecutable<T> impl,
280+
string file,
281+
T exe,
282+
Scanner? scanner,
283+
bool includeDebug)
284+
where T : WrapperBase
285+
{
286+
// If we have an invalid extractable somehow
287+
if (impl == null)
288+
return [];
262289

263-
// If the extractable file itself fails
290+
// If the extractable file itself fails
291+
try
292+
{
293+
// Extract and get the output path
294+
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
295+
bool extracted = impl.Extract(file, exe, tempPath, includeDebug);
296+
297+
// Collect and format all found protections
298+
ProtectionDictionary? subProtections = null;
299+
if (extracted)
300+
subProtections = scanner?.GetProtections(tempPath);
301+
302+
// If temp directory cleanup fails
264303
try
265304
{
266-
// Extract and get the output path
267-
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
268-
bool extracted = extractable.Extract(file, exe, tempPath, includeDebug);
269-
270-
// Collect and format all found protections
271-
ProtectionDictionary? subProtections = null;
272-
if (extracted)
273-
subProtections = scanner?.GetProtections(tempPath);
274-
275-
// If temp directory cleanup fails
276-
try
277-
{
278-
if (Directory.Exists(tempPath))
279-
Directory.Delete(tempPath, true);
280-
}
281-
catch (Exception ex)
282-
{
283-
if (includeDebug) Console.WriteLine(ex);
284-
}
285-
286-
// Prepare the returned protections
287-
subProtections?.StripFromKeys(tempPath);
288-
subProtections?.PrependToKeys(file);
289-
if (subProtections != null)
290-
protections.Append(subProtections);
305+
if (Directory.Exists(tempPath))
306+
Directory.Delete(tempPath, true);
291307
}
292308
catch (Exception ex)
293309
{
294310
if (includeDebug) Console.WriteLine(ex);
295311
}
296-
});
297312

298-
return protections;
313+
// Prepare the returned protections
314+
subProtections?.StripFromKeys(tempPath);
315+
subProtections?.PrependToKeys(file);
316+
return subProtections ?? [];
317+
}
318+
catch (Exception ex)
319+
{
320+
if (includeDebug) Console.WriteLine(ex);
321+
return [];
322+
}
299323
}
300324

301325
#endregion

BinaryObjectScanner/Scanner.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ private static ProtectionDictionary HandlePathChecks(string path, IEnumerable<st
367367
// Iterate through all checks
368368
StaticChecks.PathCheckClasses.IterateWithAction(checkClass =>
369369
{
370-
var subProtections = PerformCheck(checkClass, path, files);
370+
var subProtections = PerformPathCheck(checkClass, path, files);
371371
protections.Append(path, subProtections);
372372
});
373373

@@ -380,7 +380,7 @@ private static ProtectionDictionary HandlePathChecks(string path, IEnumerable<st
380380
/// <param name="impl">IPathCheck class representing the file type</param>
381381
/// <param name="path">Path of the file or directory to check</param>
382382
/// <returns>Set of protections in path, empty on error</returns>
383-
private static List<string> PerformCheck(IPathCheck impl, string? path, IEnumerable<string>? files)
383+
private static List<string> PerformPathCheck(IPathCheck impl, string? path, IEnumerable<string>? files)
384384
{
385385
// If we have an invalid path
386386
if (string.IsNullOrEmpty(path))

0 commit comments

Comments
 (0)