2022-05-01 21:02:59 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using BurnOutSharp.ExecutableType.Microsoft.PE;
|
2022-05-01 17:41:50 -07:00
|
|
|
|
using BurnOutSharp.Interfaces;
|
2021-08-27 14:28:17 -07:00
|
|
|
|
|
2021-03-20 22:29:19 -06:00
|
|
|
|
namespace BurnOutSharp.PackerType
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2022-07-13 12:52:20 -07:00
|
|
|
|
// TODO: Better version detection - https://raw.githubusercontent.com/wolfram77web/app-peid/master/userdb.txt
|
2022-05-01 21:02:59 -07:00
|
|
|
|
// TODO: Add extraction
|
|
|
|
|
|
public class PECompact : IPortableExecutableCheck, IScannable
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2022-05-01 21:02:59 -07:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public bool ShouldScan(byte[] magic) => true;
|
|
|
|
|
|
|
2021-08-27 14:28:17 -07:00
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-08-27 14:28:17 -07:00
|
|
|
|
// Get the sections from the executable, if possible
|
|
|
|
|
|
var sections = pex?.SectionTable;
|
|
|
|
|
|
if (sections == null)
|
|
|
|
|
|
return null;
|
2021-09-11 00:32:48 -07:00
|
|
|
|
|
|
|
|
|
|
// 0x4F434550 is "PECO"
|
|
|
|
|
|
if (pex.ImageFileHeader.PointerToSymbolTable == 0x4F434550)
|
|
|
|
|
|
return "PE Compact v1.x";
|
2021-03-20 22:29:19 -06:00
|
|
|
|
|
2021-08-27 14:28:17 -07:00
|
|
|
|
// TODO: Get more granular version detection. PiD is somehow able to detect version ranges based
|
|
|
|
|
|
// on the data in the file. This may be related to information in other fields
|
2021-03-20 22:29:19 -06:00
|
|
|
|
|
2021-08-27 14:28:17 -07:00
|
|
|
|
// Get the pec1 section, if it exists
|
2021-09-11 21:03:36 -07:00
|
|
|
|
bool pec1Section = pex.ContainsSection("pec1", exact: true);
|
|
|
|
|
|
if (pec1Section)
|
2021-08-27 14:28:17 -07:00
|
|
|
|
return "PE Compact v1.x";
|
2021-03-20 22:29:19 -06:00
|
|
|
|
|
2021-09-11 16:47:25 -07:00
|
|
|
|
// Get the PEC2 section, if it exists -- TODO: Verify this comment since it's pulling the .text section
|
2021-09-11 21:03:36 -07:00
|
|
|
|
var textSection = pex.GetFirstSection(".text", exact: true);
|
2021-08-27 14:28:17 -07:00
|
|
|
|
if (textSection != null && textSection.PointerToRelocations == 0x32434550)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (textSection.PointerToLinenumbers != 0)
|
|
|
|
|
|
return $"PE Compact v{textSection.PointerToLinenumbers} (internal version)";
|
|
|
|
|
|
|
|
|
|
|
|
return "PE Compact v2.x (or newer)";
|
|
|
|
|
|
}
|
2019-09-27 23:52:24 -07:00
|
|
|
|
|
2021-08-27 14:28:17 -07:00
|
|
|
|
return null;
|
2019-09-27 23:52:24 -07:00
|
|
|
|
}
|
2022-05-01 21:02:59 -07:00
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
|
|
|
|
|
|
{
|
|
|
|
|
|
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)
|
|
|
|
|
|
{
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2019-09-27 23:52:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|