@@ -58,7 +58,7 @@ public class Executable : IDetectable
58
58
protectionList . AddRange ( protections [ key ] ) ;
59
59
}
60
60
61
- return string . Join ( ";" , [ .. protections ] ) ;
61
+ return string . Join ( ";" , [ .. protectionList ] ) ;
62
62
}
63
63
64
64
/// <inheritdoc cref="IDetectable.Detect(Stream, string, bool)"/>
@@ -239,9 +239,13 @@ public IDictionary<U, string> RunExecutableChecks<T, U>(string file, T exe, List
239
239
/// <param name="scanner">Scanner for handling recursive protections</param>
240
240
/// <param name="includeDebug">True to include debug data, false otherwise</param>
241
241
/// <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 >
245
249
{
246
250
// Create the output dictionary
247
251
var protections = new ProtectionDictionary ( ) ;
@@ -256,46 +260,66 @@ private ProtectionDictionary HandleExtractableProtections<T, U>(string file, T e
256
260
. Select ( c => c as IExtractableExecutable < T > ) ;
257
261
extractables . IterateWithAction ( extractable =>
258
262
{
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 [ ] ;
262
289
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
264
303
try
265
304
{
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 ) ;
291
307
}
292
308
catch ( Exception ex )
293
309
{
294
310
if ( includeDebug ) Console . WriteLine ( ex ) ;
295
311
}
296
- } ) ;
297
312
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
+ }
299
323
}
300
324
301
325
#endregion
0 commit comments