Files
BinaryObjectScanner/BinaryObjectScanner.Packer/EmbeddedExecutable.cs

96 lines
3.4 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-03-07 16:59:14 -05:00
using BinaryObjectScanner.Matching;
using BinaryObjectScanner.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/>
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
{
// Get the sections from the executable, if possible
var sections = pex?.SectionTable;
if (sections == null)
return null;
// Get the resources that have an executable signature
2023-03-07 16:59:14 -05:00
if (pex.ResourceData?.Any(kvp => kvp.Value is byte[] ba && ba.StartsWith(BinaryObjectScanner.Models.MSDOS.Constants.SignatureBytes)) == true)
2022-12-30 09:35:35 -08:00
return "Embedded Executable";
return null;
}
/// <inheritdoc/>
2023-03-09 17:16:39 -05:00
public string Extract(string file, bool includeDebug)
{
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/>
2023-03-09 17:16:39 -05:00
public string Extract(Stream stream, string file, bool includeDebug)
{
2023-03-09 17:16:39 -05:00
try
{
// Parse into an executable again for easier extraction
PortableExecutable pex = PortableExecutable.Create(stream);
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[])
.Where(kvp => (kvp.Value as byte[]).StartsWith(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
var resource = resources[i];
byte[] data = resource.Value as byte[];
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
using (Stream tempStream = File.Open(tempFile, FileMode.Create, FileAccess.Write, FileShare.ReadWrite))
{
tempStream.Write(data, 0, data.Length);
}
}
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
}
}