Files
BinaryObjectScanner/BurnOutSharp/PackerType/MicrosoftCABSFX.cs

100 lines
3.7 KiB
C#
Raw Normal View History

using System;
2021-08-24 14:29:30 -07:00
using System.Collections.Concurrent;
using System.Collections.Generic;
2021-08-24 14:29:30 -07:00
using System.IO;
using BurnOutSharp.ExecutableType.Microsoft.PE;
2022-05-01 17:41:50 -07:00
using BurnOutSharp.Interfaces;
using BurnOutSharp.Matching;
2021-08-25 15:09:42 -07:00
using BurnOutSharp.Tools;
namespace BurnOutSharp.PackerType
{
// TODO: Add extraction, which should be possible with LibMSPackN, but it refuses to extract due to SFX files lacking the typical CAB identifiers.
2022-05-01 17:17:15 -07:00
public class MicrosoftCABSFX : IPortableExecutableCheck, IScannable
{
2021-08-24 14:29:30 -07:00
/// <inheritdoc/>
public bool ShouldScan(byte[] magic) => true;
/// <inheritdoc/>
2022-05-01 17:17:15 -07:00
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
{
2021-08-30 12:08:17 -07:00
// Get the sections from the executable, if possible
var sections = pex?.SectionTable;
if (sections == null)
return null;
2022-04-02 16:12:23 -07:00
string name = pex.InternalName;
if (!string.IsNullOrWhiteSpace(name) && name.Equals("Wextract", StringComparison.OrdinalIgnoreCase))
2021-09-11 21:41:17 -07:00
return $"Microsoft CAB SFX {GetVersion(pex)}";
2022-04-02 16:12:23 -07:00
name = pex.OriginalFileName;
if (!string.IsNullOrWhiteSpace(name) && name.Equals("WEXTRACT.EXE", StringComparison.OrdinalIgnoreCase))
2021-09-11 21:41:17 -07:00
return $"Microsoft CAB SFX {GetVersion(pex)}";
2021-08-30 12:08:17 -07:00
// Get the .data section, if it exists
if (pex.DataSectionRaw != null)
2021-08-30 12:08:17 -07:00
{
var matchers = new List<ContentMatchSet>
{
// wextract_cleanup
new ContentMatchSet(new byte?[]
{
0x77, 0x65, 0x78, 0x74, 0x72, 0x61, 0x63, 0x74,
0x5F, 0x63, 0x6C, 0x65, 0x61, 0x6E, 0x75, 0x70,
2021-09-11 16:58:05 -07:00
}, "Microsoft CAB SFX"),
2021-08-30 12:08:17 -07:00
};
string match = MatchUtil.GetFirstMatch(file, pex.DataSectionRaw, matchers, includeDebug);
2021-08-30 12:08:17 -07:00
if (!string.IsNullOrWhiteSpace(match))
2021-09-11 21:41:17 -07:00
return $"Microsoft CAB SFX {GetVersion(pex)}";
2021-08-30 12:08:17 -07:00
}
// Get the .text section, if it exists
if (pex.TextSectionRaw != null)
2021-08-30 12:08:17 -07:00
{
var matchers = new List<ContentMatchSet>
{
/* This detects a different but similar type of SFX that uses Microsoft CAB files.
Further research is needed to see if it's just a different version or entirely separate. */
// MSCFu
2021-09-11 16:58:05 -07:00
new ContentMatchSet(new byte?[] { 0x4D, 0x53, 0x43, 0x46, 0x75 }, "Microsoft CAB SFX"),
2021-08-30 12:08:17 -07:00
};
string match = MatchUtil.GetFirstMatch(file, pex.TextSectionRaw, matchers, includeDebug);
2021-08-30 12:08:17 -07:00
if (!string.IsNullOrWhiteSpace(match))
2021-09-11 21:41:17 -07:00
return $"Microsoft CAB SFX {GetVersion(pex)}";
2021-08-30 12:08:17 -07:00
}
return null;
}
2021-08-24 14:29:30 -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;
}
2021-09-11 21:41:17 -07:00
private string GetVersion(PortableExecutable pex)
{
2022-04-01 10:16:31 -07:00
// Check the internal versions
string version = Utilities.GetInternalVersion(pex);
2021-09-11 21:41:17 -07:00
if (!string.IsNullOrWhiteSpace(version))
return $"v{version}";
return string.Empty;
}
}
}