Files
BinaryObjectScanner/BurnOutSharp/FileType/Executable.cs

194 lines
7.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Concurrent;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using BurnOutSharp.ExecutableType.Microsoft.NE;
using BurnOutSharp.ExecutableType.Microsoft.PE;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
2021-08-25 15:09:42 -07:00
using BurnOutSharp.Tools;
namespace BurnOutSharp.FileType
{
public class Executable : IScannable
{
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic)
{
// DOS MZ executable file format (and descendants)
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0x4d, 0x5a }))
return true;
// Executable and Linkable Format
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0x7f, 0x45, 0x4c, 0x46 }))
return true;
// Mach-O binary (32-bit)
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0xfe, 0xed, 0xfa, 0xce }))
return true;
// Mach-O binary (32-bit, reverse byte ordering scheme)
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0xce, 0xfa, 0xed, 0xfe }))
return true;
// Mach-O binary (64-bit)
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0xfe, 0xed, 0xfa, 0xcf }))
return true;
// Mach-O binary (64-bit, reverse byte ordering scheme)
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0xcf, 0xfa, 0xed, 0xfe }))
return true;
// Prefrred Executable File Format
2021-03-20 20:47:56 -07:00
if (magic.StartsWith(new byte?[] { 0x4a, 0x6f, 0x79, 0x21, 0x70, 0x65, 0x66, 0x66 }))
return true;
return false;
}
2021-02-26 09:26:23 -08:00
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
2021-02-26 09:26:23 -08:00
{
if (!File.Exists(file))
return null;
using (var fs = File.OpenRead(file))
{
return Scan(scanner, fs, file);
}
}
/// <inheritdoc/>
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
{
// Files can be protected in multiple ways
var protections = new ConcurrentDictionary<string, ConcurrentQueue<string>>();
2022-03-15 12:39:22 -07:00
// Load the current file content for debug only
byte[] fileContent = null;
2022-03-15 12:39:22 -07:00
if (scanner.IncludeDebug)
{
2022-03-15 12:39:22 -07:00
try
{
2022-03-15 12:39:22 -07:00
using (BinaryReader br = new BinaryReader(stream, Encoding.Default, true))
{
fileContent = br.ReadBytes((int)stream.Length);
}
}
2022-05-15 20:58:27 -07:00
catch (Exception ex)
2022-03-15 12:39:22 -07:00
{
2022-05-15 20:58:27 -07:00
if (scanner.IncludeDebug) Console.WriteLine(ex);
2022-03-15 12:39:22 -07:00
Utilities.AppendToDictionary(protections, file, "[Out of memory attempting to open]");
return protections;
}
}
2021-09-10 16:15:20 -07:00
// Create PortableExecutable and NewExecutable objects for use in the checks
2021-09-10 21:45:34 -07:00
stream.Seek(0, SeekOrigin.Begin);
2022-03-15 12:39:22 -07:00
PortableExecutable pex = new PortableExecutable(stream);
stream.Seek(0, SeekOrigin.Begin);
NewExecutable nex = new NewExecutable(stream);
stream.Seek(0, SeekOrigin.Begin);
2021-09-10 21:45:34 -07:00
2022-03-14 10:49:02 -07:00
// Iterate through all generic content checks
if (fileContent != null)
2021-02-26 09:26:23 -08:00
{
2022-05-01 14:46:01 -07:00
Parallel.ForEach(ScanningClasses.ContentCheckClasses, contentCheckClass =>
2021-02-26 09:26:23 -08:00
{
2022-05-01 17:17:15 -07:00
string protection = contentCheckClass.CheckContents(file, fileContent, scanner.IncludeDebug);
2022-05-01 14:27:04 -07:00
if (ShouldAddProtection(contentCheckClass, scanner.ScanPackers, protection))
2022-03-15 12:39:22 -07:00
Utilities.AppendToDictionary(protections, file, protection);
// If we have an IScannable implementation
if (contentCheckClass is IScannable scannable)
{
2022-03-15 12:39:22 -07:00
if (file != null && !string.IsNullOrEmpty(protection))
{
var subProtections = scannable.Scan(scanner, null, file);
Utilities.PrependToKeys(subProtections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
}
});
}
2021-02-26 09:26:23 -08:00
2022-03-14 10:49:02 -07:00
// If we have a NE executable, iterate through all NE content checks
2022-03-15 12:39:22 -07:00
if (nex?.Initialized == true)
2022-03-14 10:49:02 -07:00
{
2022-05-01 17:17:15 -07:00
Parallel.ForEach(ScanningClasses.NewExecutableCheckClasses, contentCheckClass =>
2022-03-14 10:49:02 -07:00
{
// Check using custom content checks first
2022-05-01 17:17:15 -07:00
string protection = contentCheckClass.CheckNewExecutable(file, nex, scanner.IncludeDebug);
2022-05-01 14:27:04 -07:00
if (ShouldAddProtection(contentCheckClass, scanner.ScanPackers, protection))
2022-03-14 10:49:02 -07:00
Utilities.AppendToDictionary(protections, file, protection);
// If we have an IScannable implementation
if (contentCheckClass is IScannable scannable)
{
if (file != null && !string.IsNullOrEmpty(protection))
{
var subProtections = scannable.Scan(scanner, null, file);
Utilities.PrependToKeys(subProtections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
}
});
}
// If we have a PE executable, iterate through all PE content checks
2022-03-15 12:39:22 -07:00
if (pex?.Initialized == true)
2022-03-14 10:49:02 -07:00
{
2022-07-11 16:10:52 -07:00
// Print the section table for debug
if (scanner.IncludeDebug && pex.SectionTable != null)
pex.PrintAllSections();
2022-05-01 17:17:15 -07:00
Parallel.ForEach(ScanningClasses.PortableExecutableCheckClasses, contentCheckClass =>
2022-03-14 10:49:02 -07:00
{
// Check using custom content checks first
2022-05-01 17:17:15 -07:00
string protection = contentCheckClass.CheckPortableExecutable(file, pex, scanner.IncludeDebug);
2022-05-01 14:27:04 -07:00
if (ShouldAddProtection(contentCheckClass, scanner.ScanPackers, protection))
2022-03-14 10:49:02 -07:00
Utilities.AppendToDictionary(protections, file, protection);
// If we have an IScannable implementation
if (contentCheckClass is IScannable scannable)
{
if (file != null && !string.IsNullOrEmpty(protection))
{
var subProtections = scannable.Scan(scanner, null, file);
Utilities.PrependToKeys(subProtections, file);
Utilities.AppendToDictionary(protections, subProtections);
}
}
});
}
return protections;
}
2021-03-23 10:04:09 -07:00
2022-03-14 10:49:02 -07:00
#region Helpers
/// <summary>
/// Check to see if a protection should be added or not
/// </summary>
2022-05-01 14:24:46 -07:00
/// <param name="checkClass">Class that was last used to check</param>
2022-05-01 14:27:04 -07:00
/// <param name="scanPackers">Determines if packers should be included in the output</param>
/// <param name="protection">The protection result to be checked</param>
2022-05-01 14:27:04 -07:00
private bool ShouldAddProtection(object checkClass, bool scanPackers, string protection)
{
2022-05-01 14:28:28 -07:00
// If we have an invalid protection
if (string.IsNullOrWhiteSpace(protection))
return false;
// If we have a valid content check based on settings
2022-05-01 14:27:04 -07:00
if (scanPackers || !checkClass.GetType().Namespace.ToLowerInvariant().Contains("packertype"))
2022-05-01 14:28:28 -07:00
return true;
2022-03-14 10:49:02 -07:00
2022-05-01 14:28:28 -07:00
// Everything else fails
2022-03-14 10:49:02 -07:00
return false;
}
#endregion
}
}