Files
BinaryObjectScanner/BinaryObjectScanner.Packer/EmbeddedExecutable.cs

111 lines
3.8 KiB
C#
Raw Normal View History

2022-12-30 09:35:35 -08:00
using System;
using System.IO;
using System.Linq;
using BinaryObjectScanner.Interfaces;
2023-09-16 22:08:18 -04:00
using SabreTools.Matching;
2023-09-16 02:04:47 -04:00
using SabreTools.Serialization.Wrappers;
2022-12-30 09:35:35 -08:00
2023-03-09 23:52:58 -05:00
namespace BinaryObjectScanner.Packer
2022-12-30 09:35:35 -08:00
{
/// <summary>
/// Though not technically a packer, this detection is for any executables that include
/// others in their resources in some uncompressed manner to be used at runtime.
/// </summary>
2023-03-09 16:02:51 -05:00
public class EmbeddedExecutable : IExtractable, IPortableExecutableCheck
2022-12-30 09:35:35 -08:00
{
/// <inheritdoc/>
2023-09-17 22:37:01 -04:00
#if NET48
2022-12-30 09:35:35 -08:00
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
2023-09-17 22:37:01 -04:00
#else
public string? CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
#endif
2022-12-30 09:35:35 -08:00
{
// Get the sections from the executable, if possible
2023-09-17 22:37:01 -04:00
var sections = pex.Model.SectionTable;
2022-12-30 09:35:35 -08:00
if (sections == null)
return null;
// Get the resources that have an executable signature
2023-09-04 23:44:45 -04:00
if (pex.ResourceData?.Any(kvp => kvp.Value is byte[] ba && ba.StartsWith(SabreTools.Models.MSDOS.Constants.SignatureBytes)) == true)
2022-12-30 09:35:35 -08:00
return "Embedded Executable";
return null;
}
/// <inheritdoc/>
#if NET48
2023-03-09 17:16:39 -05:00
public string Extract(string file, bool includeDebug)
#else
public string? Extract(string file, bool includeDebug)
#endif
{
if (!File.Exists(file))
return null;
using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read))
{
2023-03-09 17:16:39 -05:00
return Extract(fs, file, includeDebug);
}
}
/// <inheritdoc/>
#if NET48
2023-03-09 17:16:39 -05:00
public string Extract(Stream stream, string file, bool includeDebug)
#else
public string? Extract(Stream stream, string file, bool includeDebug)
#endif
{
2023-03-09 17:16:39 -05:00
try
{
// Parse into an executable again for easier extraction
2023-09-17 22:37:01 -04:00
var pex = PortableExecutable.Create(stream);
2023-03-09 17:16:39 -05:00
if (pex?.ResourceData == null)
return null;
2023-03-09 15:46:48 -05:00
2023-03-09 17:16:39 -05:00
// Get the resources that have an executable signature
var resources = pex.ResourceData
.Where(kvp => kvp.Value != null && kvp.Value is byte[])
2023-09-17 22:37:01 -04:00
.Select(kvp => kvp.Value as byte[])
.Where(b => b != null && b.StartsWith(SabreTools.Models.MSDOS.Constants.SignatureBytes))
2023-03-09 17:16:39 -05:00
.ToList();
2023-03-09 15:46:48 -05:00
2023-03-09 17:16:39 -05:00
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
Directory.CreateDirectory(tempPath);
2023-03-09 15:46:48 -05:00
2023-03-09 17:16:39 -05:00
for (int i = 0; i < resources.Count; i++)
{
try
{
// Get the resource data
2023-09-17 22:37:01 -04:00
var data = resources[i];
if (data == null)
continue;
2023-03-09 15:46:48 -05:00
2023-03-09 17:16:39 -05:00
// Create the temp filename
string tempFile = $"embedded_resource_{i}.bin";
tempFile = Path.Combine(tempPath, tempFile);
2023-03-09 15:46:48 -05:00
2023-03-09 17:16:39 -05:00
// Write the resource data to a temp file
2023-09-17 22:37:01 -04:00
using (var tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
2023-03-09 17:16:39 -05:00
{
2023-09-17 22:37:01 -04:00
if (tempStream != null)
tempStream.Write(data, 0, data.Length);
2023-03-09 17:16:39 -05:00
}
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
}
2023-03-09 15:46:48 -05:00
}
2023-03-09 17:16:39 -05:00
return tempPath;
}
catch (Exception ex)
{
if (includeDebug) Console.WriteLine(ex);
return null;
}
}
2022-12-30 09:35:35 -08:00
}
}