Move resource helpers to PortableExecutable

This commit is contained in:
Matt Nadareski
2022-04-02 15:54:51 -07:00
parent 32695ee6dd
commit 61c09e3c97
27 changed files with 335 additions and 352 deletions

View File

@@ -1,9 +1,13 @@
using System;
using System.IO;
using System.Linq;
using System.Xml;
using BurnOutSharp.ExecutableType.Microsoft.MZ.Headers;
using BurnOutSharp.ExecutableType.Microsoft.PE.Entries;
using BurnOutSharp.ExecutableType.Microsoft.PE.Headers;
using BurnOutSharp.ExecutableType.Microsoft.PE.Sections;
using BurnOutSharp.ExecutableType.Microsoft.PE.Tables;
using BurnOutSharp.ExecutableType.Microsoft.Resources;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ExecutableType.Microsoft.PE
@@ -426,7 +430,287 @@ namespace BurnOutSharp.ExecutableType.Microsoft.PE
#endregion
#region Helpers
// TODO: This entire section needs to have caching
#region Resource Helpers
/// <summary>
/// Find resource data in a ResourceSection, if possible
/// </summary>
/// <param name="dataStart">String to use if checking for data starting with a string</param>
/// <param name="dataContains">String to use if checking for data contains a string</param>
/// <param name="dataEnd">String to use if checking for data ending with a string</param>
/// <returns>Full encoded resource data, null on error</returns>
public ResourceDataEntry FindResource(string dataStart = null, string dataContains = null, string dataEnd = null)
{
if (this.ResourceSection == null)
return null;
return FindResourceInTable(this.ResourceSection.ResourceDirectoryTable, dataStart, dataContains, dataEnd);
}
/// <summary>
/// Get the company name as reported by the resources
/// </summary>
/// <returns>Company name string, null on error</returns>
public string GetCompanyName() => GetResourceString("CompanyName");
/// <summary>
/// Get the file description as reported by the resources
/// </summary>
/// <returns>Description string, null on error</returns>
public string GetFileDescription() => GetResourceString("FileDescription");
/// <summary>
/// Get the file version as reported by the resources
/// </summary>
/// <returns>File version string, null on error</returns>
public string GetFileVersion() => GetResourceString("FileVersion")?.Replace(", ", ".");
/// <summary>
/// Get the internal name as reported by the resources
/// </summary>
/// <returns>Internal name string, null on error</returns>
public string GetInternalName() => GetResourceString("InternalName");
/// <summary>
/// Get the legal copyright as reported by the resources
/// </summary>
/// <returns>Legal copyright string, null on error</returns>
public string GetLegalCopyright() => GetResourceString("LegalCopyright");
/// <summary>
/// Get the assembly version as determined by an embedded assembly manifest
/// </summary>
/// <returns>Description string, null on error</returns>
public string GetManifestDescription()
{
// If we don't have a complete PE executable, just return null
if (this.ResourceSection == null)
return null;
// Read in the manifest to a string
string manifestString = FindAssemblyManifest();
if (string.IsNullOrWhiteSpace(manifestString))
return null;
// Try to read the XML in from the string
try
{
// Try to read the assembly
var assemblyNode = GetAssemblyNode(manifestString);
if (assemblyNode == null)
return null;
// Return the content of the description node, if possible
var descriptionNode = assemblyNode["description"];
if (descriptionNode == null)
return null;
return descriptionNode.InnerXml;
}
catch
{
return null;
}
}
/// <summary>
/// Get the assembly version as determined by an embedded assembly manifest
/// </summary>
/// <returns>Version string, null on error</returns>
public string GetManifestVersion()
{
// If we don't have a complete PE executable, just return null
if (this.ResourceSection == null)
return null;
// Read in the manifest to a string
string manifestString = FindAssemblyManifest();
if (string.IsNullOrWhiteSpace(manifestString))
return null;
// Try to read the XML in from the string
try
{
// Try to read the assembly
var assemblyNode = GetAssemblyNode(manifestString);
if (assemblyNode == null)
return null;
// Try to read the assemblyIdentity
var assemblyIdentityNode = assemblyNode["assemblyIdentity"];
if (assemblyIdentityNode == null)
return null;
// Return the version attribute, if possible
return assemblyIdentityNode.GetAttribute("version");
}
catch
{
return null;
}
}
/// <summary>
/// Get the original filename as reported by the resources
/// </summary>
/// <returns>Original filename string, null on error</returns>
public string GetOriginalFileName() => GetResourceString("OriginalFileName");
/// <summary>
/// Get the product name as reported by the resources
/// </summary>
/// <returns>Product name string, null on error</returns>
public string GetProductName() => GetResourceString("ProductName");
/// <summary>
/// Get the product name as reported by the resources
/// </summary>
/// <returns>Product version string, null on error</returns>
public string GetProductVersion() => GetResourceString("ProductVersion")?.Replace(", ", ".");
/// <summary>
/// Get the assembly identity node from an embedded manifest
/// </summary>
/// <param name="manifestString">String representing the XML document</param>
/// <returns>Assembly identity node, if possible</returns>
private XmlElement GetAssemblyNode(string manifestString)
{
// An invalid string means we can't read it
if (string.IsNullOrWhiteSpace(manifestString))
return null;
try
{
// Load the XML string as a document
var manifestDoc = new XmlDocument();
manifestDoc.LoadXml(manifestString);
// If the XML has no children, it's invalid
if (!manifestDoc.HasChildNodes)
return null;
// Try to read the assembly node
return manifestDoc["assembly"];
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// Find the assembly manifest from a resource section, if possible
/// </summary>
/// <returns>Full assembly manifest, null on error</returns>
private string FindAssemblyManifest() => FindResource(dataContains: "<assembly")?.DataAsUTF8String;
/// <summary>
/// Find resource data in a ResourceDirectoryTable, if possible
/// </summary>
/// <param name="rdt">ResourceDirectoryTable representing a layer</param>
/// <param name="dataStart">String to use if checking for data starting with a string</param>
/// <param name="dataContains">String to use if checking for data contains a string</param>
/// <param name="dataEnd">String to use if checking for data ending with a string</param>
/// <returns>Full encoded resource data, null on error</returns>
private ResourceDataEntry FindResourceInTable(ResourceDirectoryTable rdt, string dataStart, string dataContains, string dataEnd)
{
if (rdt == null)
return null;
try
{
foreach (var rdte in rdt.NamedEntries)
{
if (rdte.IsResourceDataEntry() && rdte.DataEntry != null)
{
if (dataStart != null && rdte.DataEntry.DataAsUTF8String.StartsWith(dataStart))
return rdte.DataEntry;
else if (dataContains != null && rdte.DataEntry.DataAsUTF8String.Contains(dataContains))
return rdte.DataEntry;
else if (dataEnd != null && rdte.DataEntry.DataAsUTF8String.EndsWith(dataStart))
return rdte.DataEntry;
}
else
{
var manifest = FindResourceInTable(rdte.Subdirectory, dataStart, dataContains, dataEnd);
if (manifest != null)
return manifest;
}
}
foreach (var rdte in rdt.IdEntries)
{
if (rdte.IsResourceDataEntry() && rdte.DataEntry != null)
{
if (dataStart != null && rdte.DataEntry.DataAsUTF8String.StartsWith(dataStart))
return rdte.DataEntry;
else if (dataContains != null && rdte.DataEntry.DataAsUTF8String.Contains(dataContains))
return rdte.DataEntry;
else if (dataEnd != null && rdte.DataEntry.DataAsUTF8String.EndsWith(dataStart))
return rdte.DataEntry;
}
else
{
var manifest = FindResourceInTable(rdte.Subdirectory, dataStart, dataContains, dataEnd);
if (manifest != null)
return manifest;
}
}
}
catch { }
return null;
}
/// <summary>
/// Get a resource string from the version info
/// </summary>
/// <returns>Original filename string, null on error</returns>
private string GetResourceString(string key)
{
var resourceStrings = GetVersionInfo()?.ChildrenStringFileInfo?.Children?.Children;
if (resourceStrings == null)
return null;
var value = resourceStrings.FirstOrDefault(s => s.Key == key);
if (!string.IsNullOrWhiteSpace(value?.Value))
return value.Value.Trim(' ', '\0');
return null;
}
/// <summary>
/// Get the version info object related to file contents, if possible
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>VersionInfo object on success, null on error</returns>
private VersionInfo GetVersionInfo()
{
// If we don't have a complete PE executable, just return null
if (this.ResourceSection == null)
return null;
// Try to get the matching resource
var resource = FindResource(dataContains: "V\0S\0_\0V\0E\0R\0S\0I\0O\0N\0_\0I\0N\0F\0O\0");
if (resource?.Data == null)
return null;
try
{
int index = 0;
return VersionInfo.Deserialize(resource.Data, ref index);
}
catch (Exception ex)
{
// Console.WriteLine(ex);
return null;
}
}
#endregion
#region Section Helpers
/// <summary>
/// Determine if a section is contained within the section table

View File

@@ -22,7 +22,7 @@ namespace BurnOutSharp.PackerType
return null;
// Known to detect versions 5.0.0.3 - 8.1.0.0
string name = Utilities.GetProductName(pex);
string name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("AutoPlay Media Studio", StringComparison.OrdinalIgnoreCase))
return $"AutoPlay Media Studio {GetVersion(pex)}";
@@ -57,7 +57,7 @@ namespace BurnOutSharp.PackerType
private string GetVersion(PortableExecutable pex)
{
// Check the product version explicitly
string version = Utilities.GetProductVersion(pex);
string version = pex.GetProductVersion();
if (!string.IsNullOrEmpty(version))
return version;

View File

@@ -19,11 +19,11 @@ namespace BurnOutSharp.PackerType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("InstallAnywhere Self Extractor", StringComparison.OrdinalIgnoreCase))
return $"InstallAnywhere {GetVersion(pex)}";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("InstallAnywhere", StringComparison.OrdinalIgnoreCase))
return $"InstallAnywhere {GetVersion(pex)}";

View File

@@ -15,7 +15,7 @@ namespace BurnOutSharp.PackerType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name)
&& (name.Equals("Intel(R) Installation Framework", StringComparison.OrdinalIgnoreCase)
|| name.Equals("Intel Installation Framework", StringComparison.OrdinalIgnoreCase)))
@@ -23,7 +23,7 @@ namespace BurnOutSharp.PackerType
return $"Intel Installation Framework {Utilities.GetInternalVersion(pex)}";
}
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name)
&& (name.Equals("Intel(R) Installation Framework", StringComparison.OrdinalIgnoreCase)
|| name.Equals("Intel Installation Framework", StringComparison.OrdinalIgnoreCase)))

View File

@@ -22,11 +22,11 @@ namespace BurnOutSharp.PackerType
if (sections == null)
return null;
string name = Utilities.GetInternalName(pex);
string name = pex.GetInternalName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("Wextract", StringComparison.OrdinalIgnoreCase))
return $"Microsoft CAB SFX {GetVersion(pex)}";
name = Utilities.GetOriginalFileName(pex);
name = pex.GetOriginalFileName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("WEXTRACT.EXE", StringComparison.OrdinalIgnoreCase))
return $"Microsoft CAB SFX {GetVersion(pex)}";

View File

@@ -1,7 +1,6 @@
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.PackerType
{
@@ -15,7 +14,7 @@ namespace BurnOutSharp.PackerType
if (sections == null)
return null;
string description = Utilities.GetManifestDescription(pex);
string description = pex.GetManifestDescription();
if (!string.IsNullOrWhiteSpace(description) && description.StartsWith("Nullsoft Install System"))
return $"NSIS {description.Substring("Nullsoft Install System".Length).Trim()}";

View File

@@ -20,16 +20,16 @@ namespace BurnOutSharp.PackerType
return null;
// Known to detect versions 7.0.5.1 - 9.1.0.0
string name = Utilities.GetLegalCopyright(pex);
string name = pex.GetLegalCopyright();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Setup Engine", StringComparison.OrdinalIgnoreCase))
return $"Setup Factory {GetVersion(pex)}";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Setup Factory", StringComparison.OrdinalIgnoreCase))
return $"Setup Factory {GetVersion(pex)}";
// Known to detect version 5.0.1 - 6.0.1.3
name = Utilities.GetFileDescription(pex);
name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Setup Factory", StringComparison.OrdinalIgnoreCase))
return $"Setup Factory {GetVersion(pex)}";
@@ -62,7 +62,7 @@ namespace BurnOutSharp.PackerType
private string GetVersion(PortableExecutable pex)
{
// Check the product version explicitly
string version = Utilities.GetProductVersion(pex);
string version = pex.GetProductVersion();
if (!string.IsNullOrEmpty(version))
return version;

View File

@@ -187,8 +187,8 @@ namespace BurnOutSharp.PackerType
private static string GetAdjustedManifestVersion(PortableExecutable pex)
{
// Get the manifest information, if possible
string description = Utilities.GetManifestDescription(pex);
string version = Utilities.GetManifestVersion(pex);
string description = pex.GetManifestDescription();
string version = pex.GetManifestVersion();
// Either an incorrect description or empty version mean we can't match
if (description != "WinZip Self-Extractor")

View File

@@ -1,6 +1,5 @@
using System;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -14,7 +13,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetInternalName(pex);
string name = pex.GetInternalName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("CDKey", StringComparison.OrdinalIgnoreCase))
return "CD-Key / Serial";

View File

@@ -22,7 +22,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("EReg MFC Application"))
return $"EA CdKey Registration Module {Utilities.GetInternalVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.Contains("Registration code installer program"))
@@ -30,11 +30,11 @@ namespace BurnOutSharp.ProtectionType
else if (!string.IsNullOrWhiteSpace(name) && name.Equals("EA DRM Helper", StringComparison.OrdinalIgnoreCase))
return $"EA DRM Protection {Utilities.GetInternalVersion(pex)}";
name = Utilities.GetInternalName(pex);
name = pex.GetInternalName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("CDCode", StringComparison.Ordinal))
return $"EA CdKey Registration Module {Utilities.GetInternalVersion(pex)}";
var resource = Utilities.FindResourceInSection(pex.ResourceSection, dataContains: "A\0b\0o\0u\0t\0 \0C\0D\0K\0e\0y");
var resource = pex.FindResource(dataContains: "A\0b\0o\0u\0t\0 \0C\0D\0K\0e\0y");
if (resource != null)
return $"EA CdKey Registration Module {Utilities.GetInternalVersion(pex)}";

View File

@@ -17,7 +17,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Games for Windows - LIVE Zero Day Piracy Protection", StringComparison.OrdinalIgnoreCase))
return $"Games for Windows LIVE - Zero Day Piracy Protection Module {Utilities.GetInternalVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Games for Windows", StringComparison.OrdinalIgnoreCase))

View File

@@ -18,15 +18,15 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("ImpulseReactor Dynamic Link Library"))
return $"Impulse Reactor Core Module {Utilities.GetInternalVersion(pex)}";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("ImpulseReactor Dynamic Link Library"))
return $"Impulse Reactor Core Module {Utilities.GetInternalVersion(pex)}";
name = Utilities.GetOriginalFileName(pex);
name = pex.GetOriginalFileName();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("ReactorActivate.exe"))
return $"Stardock Product Activation {Utilities.GetInternalVersion(pex)}";

View File

@@ -1,5 +1,4 @@
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -29,7 +28,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
var fileNameResource = Utilities.FindResourceInSection(pex.ResourceSection, dataContains: $"NO NESTED PRMS SUPPORTED");
var fileNameResource = pex.FindResource(dataContains: $"NO NESTED PRMS SUPPORTED");
if (fileNameResource != null)
return "ITENIUM Trial & Buy Protection";

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -16,7 +15,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("SDKHM (KEEP)"))
return "key2AudioXS";
else if (!string.IsNullOrWhiteSpace(name) && name.Contains("SDKHM (KEPT)"))

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -16,7 +15,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
var resource = Utilities.FindResourceInSection(pex.ResourceSection, dataContains: "Cd3Ctl");
var resource = pex.FindResource(dataContains: "Cd3Ctl");
if (resource != null)
return $"MediaMax CD-3";

View File

@@ -15,7 +15,7 @@ namespace BurnOutSharp.ProtectionType
return null;
// TODO: Is this too broad in general?
string name = Utilities.GetInternalName(pex);
string name = pex.GetInternalName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("EReg", StringComparison.OrdinalIgnoreCase))
return $"Executable-Based Online Registration {Utilities.GetInternalVersion(pex)}";

View File

@@ -3,7 +3,6 @@ using System.Collections.Concurrent;
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -17,11 +16,11 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("Origin", StringComparison.OrdinalIgnoreCase))
return "Origin";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("Origin", StringComparison.OrdinalIgnoreCase))
return "Origin";

View File

@@ -54,7 +54,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("SafeCast2", StringComparison.OrdinalIgnoreCase))
return $"SafeCast";

View File

@@ -20,7 +20,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("SecuROM PA"))
return $"SecuROM PA v{Utilities.GetInternalVersion(pex)}";

View File

@@ -20,7 +20,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("DVM Library", StringComparison.OrdinalIgnoreCase))
return $"SolidShield {Utilities.GetInternalVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Solidshield Activation Library", StringComparison.OrdinalIgnoreCase))
@@ -28,7 +28,7 @@ namespace BurnOutSharp.ProtectionType
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Activation Manager", StringComparison.OrdinalIgnoreCase))
return $"SolidShield Activation Manager Module {GetInternalVersion(pex)}";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Solidshield Activation Library", StringComparison.OrdinalIgnoreCase))
return $"SolidShield Core.dll {Utilities.GetInternalVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Solidshield Library", StringComparison.OrdinalIgnoreCase))
@@ -58,7 +58,7 @@ namespace BurnOutSharp.ProtectionType
}
// Get the wrapper resource, if it exists
var resource = Utilities.FindResourceInSection(pex.ResourceSection, dataContains: "B\0I\0N\0" + (char)0x07 + "\0I\0D\0R\0_\0S\0G\0T\0");
var resource = pex.FindResource(dataContains: "B\0I\0N\0" + (char)0x07 + "\0I\0D\0R\0_\0S\0G\0T\0");
if (resource != null)
return "SolidShield EXE Wrapper v1";
@@ -190,7 +190,7 @@ namespace BurnOutSharp.ProtectionType
private static string GetInternalVersion(PortableExecutable pex)
{
string companyName = Utilities.GetCompanyName(pex)?.ToLowerInvariant();
string companyName = pex.GetCompanyName()?.ToLowerInvariant();
if (!string.IsNullOrWhiteSpace(companyName) && (companyName.Contains("solidshield") || companyName.Contains("tages")))
return Utilities.GetInternalVersion(pex);

View File

@@ -18,18 +18,18 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetLegalCopyright(pex);
string name = pex.GetLegalCopyright();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("Protection Technology")) // Protection Technology (StarForce)?
return $"StarForce {Utilities.GetInternalVersion(pex)}";
name = Utilities.GetInternalName(pex);
name = pex.GetInternalName();
if (!string.IsNullOrWhiteSpace(name) && name.Equals("CORE.EXE", StringComparison.Ordinal))
return $"StarForce {Utilities.GetInternalVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.Equals("protect.exe", StringComparison.Ordinal))
return $"StarForce {Utilities.GetInternalVersion(pex)}";
// TODO: Find what fvinfo field actually maps to this
name = Utilities.GetFileDescription(pex);
name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.Contains("Protected Module"))
return $"StarForce 5";

View File

@@ -16,7 +16,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrEmpty(name) && name.Contains("Steam Autorun Setup"))
return "Steam";
else if (!string.IsNullOrEmpty(name) && name.Contains("Steam Client API"))
@@ -26,7 +26,7 @@ namespace BurnOutSharp.ProtectionType
else if (!string.IsNullOrEmpty(name) && name.Contains("Steam Client Service"))
return "Steam";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrEmpty(name) && name.Contains("Steam Autorun Setup"))
return "Steam";
else if (!string.IsNullOrEmpty(name) && name.Contains("Steam Client API"))

View File

@@ -28,13 +28,13 @@ namespace BurnOutSharp.ProtectionType
// - TagesClient.exe
// - TagesClient.dat (Does not always exist)
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("TagesSetup", StringComparison.OrdinalIgnoreCase))
return $"TAGES Driver Setup {GetVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Tagès activation client", StringComparison.OrdinalIgnoreCase))
return $"TAGES Activation Client {GetVersion(pex)}";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("Application TagesSetup", StringComparison.OrdinalIgnoreCase))
return $"TAGES Driver Setup {GetVersion(pex)}";
else if (!string.IsNullOrWhiteSpace(name) && name.StartsWith("T@GES", StringComparison.OrdinalIgnoreCase))

View File

@@ -1,5 +1,4 @@
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -13,7 +12,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
var resource = Utilities.FindResourceInSection(pex.ResourceSection, dataContains: "3\02\01\0S\0t\0u\0d\0i\0o\0s\0 \0A\0c\0t\0i\0v\0a\0t\0i\0o\0n\0");
var resource = pex.FindResource(dataContains: "3\02\01\0S\0t\0u\0d\0i\0o\0s\0 \0A\0c\0t\0i\0v\0a\0t\0i\0o\0n\0");
if (resource != null)
return $"321Studios Online Activation";

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -17,7 +16,7 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrEmpty(name) && name.Contains("Ubisoft Connect Installer"))
return "Uplay / Ubisoft Connect";
else if (!string.IsNullOrEmpty(name) && name.Contains("Ubisoft Connect Service"))
@@ -34,7 +33,7 @@ namespace BurnOutSharp.ProtectionType
return "Uplay / Ubisoft Connect";
// There's also a variant that looks like "Uplay <version> installer"
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrEmpty(name) && name.Contains("Ubisoft Connect"))
return "Uplay / Ubisoft Connect";
else if (!string.IsNullOrEmpty(name) && name.Contains("Uplay"))

View File

@@ -2,7 +2,6 @@
using System.Collections.Generic;
using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.Matching;
using BurnOutSharp.Tools;
namespace BurnOutSharp.ProtectionType
{
@@ -16,11 +15,11 @@ namespace BurnOutSharp.ProtectionType
if (sections == null)
return null;
string name = Utilities.GetFileDescription(pex);
string name = pex.GetFileDescription();
if (!string.IsNullOrEmpty(name) && name.Contains("Copy Protection Viewer"))
return "WTM Protection Viewer";
name = Utilities.GetProductName(pex);
name = pex.GetProductName();
if (!string.IsNullOrEmpty(name) && name.Contains("WTM Copy Protection Viewer"))
return "WTM Protection Viewer";

View File

@@ -9,7 +9,6 @@ using BurnOutSharp.ExecutableType.Microsoft.PE;
using BurnOutSharp.ExecutableType.Microsoft.PE.Entries;
using BurnOutSharp.ExecutableType.Microsoft.PE.Sections;
using BurnOutSharp.ExecutableType.Microsoft.PE.Tables;
using BurnOutSharp.ExecutableType.Microsoft.Resources;
namespace BurnOutSharp.Tools
{
@@ -203,15 +202,15 @@ namespace BurnOutSharp.Tools
/// <returns>Version string, null on error</returns>
public static string GetInternalVersion(PortableExecutable pex)
{
string version = GetFileVersion(pex);
string version = pex.GetFileVersion();
if (!string.IsNullOrWhiteSpace(version))
return version;
version = GetProductVersion(pex);
version = pex.GetProductVersion();
if (!string.IsNullOrWhiteSpace(version))
return version;
version = GetManifestVersion(pex);
version = pex.GetManifestVersion();
if (!string.IsNullOrWhiteSpace(version))
return version;
@@ -238,250 +237,6 @@ namespace BurnOutSharp.Tools
#region Executable Information
/// <summary>
/// Get the company name as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Company name string, null on error</returns>
public static string GetCompanyName(PortableExecutable pex) => GetResourceString(pex, "CompanyName");
/// <summary>
/// Get the file description as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Description string, null on error</returns>
public static string GetFileDescription(PortableExecutable pex) => GetResourceString(pex, "FileDescription");
/// <summary>
/// Get the file version as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>File version string, null on error</returns>
public static string GetFileVersion(PortableExecutable pex) => GetResourceString(pex, "FileVersion")?.Replace(", ", ".");
/// <summary>
/// Get the internal name as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Internal name string, null on error</returns>
public static string GetInternalName(PortableExecutable pex) => GetResourceString(pex, "InternalName");
/// <summary>
/// Get the legal copyright as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Legal copyright string, null on error</returns>
public static string GetLegalCopyright(PortableExecutable pex) => GetResourceString(pex, "LegalCopyright");
/// <summary>
/// Get the assembly version as determined by an embedded assembly manifest
/// </summary>
/// <param name="fileContent">Byte array representing the file contents</param>
/// <returns>Version string, null on error</returns>
public static string GetManifestDescription(PortableExecutable pex)
{
// If we don't have a PE executable, just return null
var resourceSection = pex?.ResourceSection;
if (resourceSection == null)
return null;
// Read in the manifest to a string
string manifestString = FindAssemblyManifest(pex.ResourceSection);
if (string.IsNullOrWhiteSpace(manifestString))
return null;
// Try to read the XML in from the string
try
{
// Try to read the assembly
var assemblyNode = GetAssemblyNode(manifestString);
if (assemblyNode == null)
return null;
// Return the content of the description node, if possible
var descriptionNode = assemblyNode["description"];
if (descriptionNode == null)
return null;
return descriptionNode.InnerXml;
}
catch
{
return null;
}
}
/// <summary>
/// Get the assembly version as determined by an embedded assembly manifest
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Version string, null on error</returns>
public static string GetManifestVersion(PortableExecutable pex)
{
// If we don't have a PE executable, just return null
var resourceSection = pex?.ResourceSection;
if (resourceSection == null)
return null;
// Read in the manifest to a string
string manifestString = FindAssemblyManifest(pex.ResourceSection);
if (string.IsNullOrWhiteSpace(manifestString))
return null;
// Try to read the XML in from the string
try
{
// Try to read the assembly
var assemblyNode = GetAssemblyNode(manifestString);
if (assemblyNode == null)
return null;
// Try to read the assemblyIdentity
var assemblyIdentityNode = assemblyNode["assemblyIdentity"];
if (assemblyIdentityNode == null)
return null;
// Return the version attribute, if possible
return assemblyIdentityNode.GetAttribute("version");
}
catch
{
return null;
}
}
/// <summary>
/// Get the original filename as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Original filename string, null on error</returns>
public static string GetOriginalFileName(PortableExecutable pex) => GetResourceString(pex, "OriginalFileName");
/// <summary>
/// Get the product name as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Product name string, null on error</returns>
public static string GetProductName(PortableExecutable pex) => GetResourceString(pex, "ProductName");
/// <summary>
/// Get the product name as reported by the resources
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Product version string, null on error</returns>
public static string GetProductVersion(PortableExecutable pex) => GetResourceString(pex, "ProductVersion")?.Replace(", ", ".");
/// <summary>
/// Find resource data in a ResourceSection, if possible
/// </summary>
/// <param name="rs">ResourceSection from the executable</param>
/// <param name="dataStart">String to use if checking for data starting with a string</param>
/// <param name="dataContains">String to use if checking for data contains a string</param>
/// <param name="dataEnd">String to use if checking for data ending with a string</param>
/// <returns>Full encoded resource data, null on error</returns>
public static ResourceDataEntry FindResourceInSection(ResourceSection rs, string dataStart = null, string dataContains = null, string dataEnd = null)
{
if (rs == null)
return null;
return FindResourceInTable(rs.ResourceDirectoryTable, dataStart, dataContains, dataEnd);
}
/// <summary>
/// Find resource data in a ResourceDirectoryTable, if possible
/// </summary>
/// <param name="rdt">ResourceDirectoryTable representing a layer</param>
/// <param name="dataStart">String to use if checking for data starting with a string</param>
/// <param name="dataContains">String to use if checking for data contains a string</param>
/// <param name="dataEnd">String to use if checking for data ending with a string</param>
/// <returns>Full encoded resource data, null on error</returns>
private static ResourceDataEntry FindResourceInTable(ResourceDirectoryTable rdt, string dataStart, string dataContains, string dataEnd)
{
if (rdt == null)
return null;
try
{
foreach (var rdte in rdt.NamedEntries)
{
if (rdte.IsResourceDataEntry() && rdte.DataEntry != null)
{
if (dataStart != null && rdte.DataEntry.DataAsUTF8String.StartsWith(dataStart))
return rdte.DataEntry;
else if (dataContains != null && rdte.DataEntry.DataAsUTF8String.Contains(dataContains))
return rdte.DataEntry;
else if (dataEnd != null && rdte.DataEntry.DataAsUTF8String.EndsWith(dataStart))
return rdte.DataEntry;
}
else
{
var manifest = FindResourceInTable(rdte.Subdirectory, dataStart, dataContains, dataEnd);
if (manifest != null)
return manifest;
}
}
foreach (var rdte in rdt.IdEntries)
{
if (rdte.IsResourceDataEntry() && rdte.DataEntry != null)
{
if (dataStart != null && rdte.DataEntry.DataAsUTF8String.StartsWith(dataStart))
return rdte.DataEntry;
else if (dataContains != null && rdte.DataEntry.DataAsUTF8String.Contains(dataContains))
return rdte.DataEntry;
else if (dataEnd != null && rdte.DataEntry.DataAsUTF8String.EndsWith(dataStart))
return rdte.DataEntry;
}
else
{
var manifest = FindResourceInTable(rdte.Subdirectory, dataStart, dataContains, dataEnd);
if (manifest != null)
return manifest;
}
}
}
catch { }
return null;
}
/// <summary>
/// Find the assembly manifest from a resource section, if possible
/// </summary>
/// <param name="rs">ResourceSection from the executable</param>
/// <returns>Full assembly manifest, null on error</returns>
private static string FindAssemblyManifest(ResourceSection rs) => FindResourceInSection(rs, dataContains: "<assembly")?.DataAsUTF8String;
/// <summary>
/// Get the assembly identity node from an embedded manifest
/// </summary>
/// <param name="manifestString">String representing the XML document</param>
/// <returns>Assembly identity node, if possible</returns>
private static XmlElement GetAssemblyNode(string manifestString)
{
// An invalid string means we can't read it
if (string.IsNullOrWhiteSpace(manifestString))
return null;
try
{
// Load the XML string as a document
var manifestDoc = new XmlDocument();
manifestDoc.LoadXml(manifestString);
// If the XML has no children, it's invalid
if (!manifestDoc.HasChildNodes)
return null;
// Try to read the assembly node
return manifestDoc["assembly"];
}
catch (Exception ex)
{
return null;
}
}
/// <summary>
/// Get the file version info object related to a path, if possible
/// </summary>
@@ -502,53 +257,6 @@ namespace BurnOutSharp.Tools
}
}
/// <summary>
/// Get a resource string from the version info
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>Original filename string, null on error</returns>
private static string GetResourceString(PortableExecutable pex, string key)
{
var resourceStrings = GetVersionInfo(pex)?.ChildrenStringFileInfo?.Children?.Children;
if (resourceStrings == null)
return null;
var value = resourceStrings.FirstOrDefault(s => s.Key == key);
if (!string.IsNullOrWhiteSpace(value?.Value))
return value.Value.Trim(' ', '\0');
return null;
}
/// <summary>
/// Get the version info object related to file contents, if possible
/// </summary>
/// <param name="pex">PortableExecutable representing the file contents</param>
/// <returns>VersionInfo object on success, null on error</returns>
private static VersionInfo GetVersionInfo(PortableExecutable pex)
{
// If we don't have a PE executable, just return null
var resourceSection = pex?.ResourceSection;
if (resourceSection == null)
return null;
// Try to get the matching resource
var resource = FindResourceInSection(resourceSection, dataContains: "V\0S\0_\0V\0E\0R\0S\0I\0O\0N\0_\0I\0N\0F\0O\0");
if (resource?.Data == null)
return null;
try
{
int index = 0;
return VersionInfo.Deserialize(resource.Data, ref index);
}
catch (Exception ex)
{
// Console.WriteLine(ex);
return null;
}
}
#endregion
#region Wrappers for Matchers