2020-09-10 21:10:32 -07:00
|
|
|
|
using System;
|
2021-07-18 09:44:23 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2022-03-14 10:40:44 -07:00
|
|
|
|
using BurnOutSharp.ExecutableType.Microsoft.NE;
|
|
|
|
|
|
using BurnOutSharp.ExecutableType.Microsoft.PE;
|
2022-05-01 17:41:50 -07:00
|
|
|
|
using BurnOutSharp.Interfaces;
|
2021-03-21 15:24:23 -07:00
|
|
|
|
using BurnOutSharp.Matching;
|
2021-08-25 15:09:42 -07:00
|
|
|
|
using BurnOutSharp.Tools;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
using Wise = WiseUnpacker.WiseUnpacker;
|
|
|
|
|
|
|
2021-02-26 09:34:07 -08:00
|
|
|
|
namespace BurnOutSharp.PackerType
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public class WiseInstaller : INewExecutableCheck, IPortableExecutableCheck, IScannable
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public bool ShouldScan(byte[] magic) => true;
|
|
|
|
|
|
|
2021-08-25 19:37:32 -07:00
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckNewExecutable(string file, NewExecutable nex, bool includeDebug)
|
2021-08-29 20:52:00 -07:00
|
|
|
|
{
|
2022-03-14 11:00:17 -07:00
|
|
|
|
// Get the DOS stub from the executable, if possible
|
|
|
|
|
|
var stub = nex?.DOSStubHeader;
|
|
|
|
|
|
if (stub == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2022-03-15 11:18:53 -07:00
|
|
|
|
// TODO: Don't read entire file
|
|
|
|
|
|
var data = nex.ReadArbitraryRange();
|
|
|
|
|
|
if (data == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
2022-03-14 11:00:17 -07:00
|
|
|
|
// TODO: Keep this around until it can be confirmed with NE checks as well
|
|
|
|
|
|
// TODO: This _may_ actually over-match. See msvbvm50.exe for an example
|
|
|
|
|
|
var neMatchSets = new List<ContentMatchSet>
|
2021-09-10 15:32:37 -07:00
|
|
|
|
{
|
2022-03-14 11:00:17 -07:00
|
|
|
|
// WiseMain
|
|
|
|
|
|
new ContentMatchSet(new byte?[] { 0x57, 0x69, 0x73, 0x65, 0x4D, 0x61, 0x69, 0x6E }, "Wise Installation Wizard Module"),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-03-15 11:18:53 -07:00
|
|
|
|
return MatchUtil.GetFirstMatch(file, data, neMatchSets, includeDebug);
|
2022-03-14 11:00:17 -07:00
|
|
|
|
}
|
2021-09-10 15:32:37 -07:00
|
|
|
|
|
2022-03-14 11:00:17 -07:00
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
|
2022-03-14 11:00:17 -07:00
|
|
|
|
{
|
2022-03-14 11:20:11 -07:00
|
|
|
|
// Get the sections from the executable, if possible
|
2022-03-14 11:00:17 -07:00
|
|
|
|
var sections = pex?.SectionTable;
|
|
|
|
|
|
if (sections == null)
|
2021-08-29 20:52:00 -07:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the .data section, if it exists
|
2021-09-11 16:47:25 -07:00
|
|
|
|
if (pex.DataSectionRaw != null)
|
2021-08-29 20:52:00 -07:00
|
|
|
|
{
|
|
|
|
|
|
var matchers = new List<ContentMatchSet>
|
|
|
|
|
|
{
|
|
|
|
|
|
// WiseMain
|
2021-09-11 16:47:25 -07:00
|
|
|
|
new ContentMatchSet(new byte?[] { 0x57, 0x69, 0x73, 0x65, 0x4D, 0x61, 0x69, 0x6E }, "Wise Installation Wizard Module"),
|
2021-08-29 20:52:00 -07:00
|
|
|
|
};
|
2021-09-11 16:47:25 -07:00
|
|
|
|
|
|
|
|
|
|
string match = MatchUtil.GetFirstMatch(file, pex.DataSectionRaw, matchers, includeDebug);
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(match))
|
|
|
|
|
|
return match;
|
2021-08-29 20:52:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Get the .rdata section, if it exists
|
2021-09-11 16:47:25 -07:00
|
|
|
|
if (pex.ResourceDataSectionRaw != null)
|
2021-08-29 20:52:00 -07:00
|
|
|
|
{
|
|
|
|
|
|
var matchers = new List<ContentMatchSet>
|
|
|
|
|
|
{
|
|
|
|
|
|
// WiseMain
|
2021-09-11 16:47:25 -07:00
|
|
|
|
new ContentMatchSet(new byte?[] { 0x57, 0x69, 0x73, 0x65, 0x4D, 0x61, 0x69, 0x6E }, "Wise Installation Wizard Module"),
|
2021-08-29 20:52:00 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
2021-09-11 16:47:25 -07:00
|
|
|
|
string match = MatchUtil.GetFirstMatch(file, pex.ResourceDataSectionRaw, matchers, includeDebug);
|
2021-08-29 20:52:00 -07:00
|
|
|
|
if (!string.IsNullOrWhiteSpace(match))
|
|
|
|
|
|
return match;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, string file)
|
2021-02-26 09:26:23 -08:00
|
|
|
|
{
|
|
|
|
|
|
if (!File.Exists(file))
|
|
|
|
|
|
return null;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2021-02-26 09:26:23 -08:00
|
|
|
|
using (var fs = File.OpenRead(file))
|
|
|
|
|
|
{
|
|
|
|
|
|
return Scan(scanner, fs, file);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-26 09:26:23 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentDictionary<string, ConcurrentQueue<string>> Scan(Scanner scanner, Stream stream, string file)
|
2020-09-10 21:10:32 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If the installer file itself fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string tempPath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
|
|
|
|
|
|
Directory.CreateDirectory(tempPath);
|
|
|
|
|
|
|
|
|
|
|
|
Wise unpacker = new Wise();
|
|
|
|
|
|
unpacker.ExtractTo(file, tempPath);
|
|
|
|
|
|
|
2020-10-28 12:23:25 -07:00
|
|
|
|
// Collect and format all found protections
|
2020-10-31 14:00:31 -07:00
|
|
|
|
var protections = scanner.GetProtections(tempPath);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2020-10-28 12:23:25 -07:00
|
|
|
|
// If temp directory cleanup fails
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Directory.Delete(tempPath, true);
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-10-31 14:00:31 -07:00
|
|
|
|
|
2020-11-02 15:18:03 -08:00
|
|
|
|
// Remove temporary path references
|
|
|
|
|
|
Utilities.StripFromKeys(protections, tempPath);
|
|
|
|
|
|
|
2020-10-31 14:00:31 -07:00
|
|
|
|
return protections;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
2022-05-15 20:58:27 -07:00
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (scanner.IncludeDebug) Console.WriteLine(ex);
|
|
|
|
|
|
}
|
2020-09-10 21:10:32 -07:00
|
|
|
|
|
2020-10-31 14:00:31 -07:00
|
|
|
|
return null;
|
2020-09-10 21:10:32 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|