From ae5bdcc97a55f6d1d008e9924933700ad37c1b7b Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 6 Sep 2021 13:58:16 -0700 Subject: [PATCH] Convert Sisiphus to section based --- BurnOutSharp/ProtectionType/Sysiphus.cs | 85 ++++++++++++++++--------- 1 file changed, 56 insertions(+), 29 deletions(-) diff --git a/BurnOutSharp/ProtectionType/Sysiphus.cs b/BurnOutSharp/ProtectionType/Sysiphus.cs index 27535d6b..bb9cb2f6 100644 --- a/BurnOutSharp/ProtectionType/Sysiphus.cs +++ b/BurnOutSharp/ProtectionType/Sysiphus.cs @@ -1,4 +1,8 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using BurnOutSharp.ExecutableType.Microsoft; using BurnOutSharp.Matching; namespace BurnOutSharp.ProtectionType @@ -6,41 +10,64 @@ namespace BurnOutSharp.ProtectionType public class Sysiphus : IContentCheck { /// - public List GetContentMatchSets() - { - return new List - { - // V SUHPISYSDVD - new ContentMatchSet(new byte?[] - { - 0x56, 0x20, 0x53, 0x55, 0x48, 0x50, 0x49, 0x53, - 0x59, 0x53, 0x44, 0x56, 0x44 - }, GetVersion, "Sysiphus DVD"), - - // V SUHPISYSDVD - new ContentMatchSet(new byte?[] - { - 0x56, 0x20, 0x53, 0x55, 0x48, 0x50, 0x49, 0x53, - 0x59, 0x53 - }, GetVersion, "Sysiphus"), - }; - } + public List GetContentMatchSets() => null; /// - public string CheckContents(string file, byte[] fileContent, bool includeDebug = false) => null; + public string CheckContents(string file, byte[] fileContent, bool includeDebug = false) + { + // Get the sections from the executable, if possible + PortableExecutable pex = PortableExecutable.Deserialize(fileContent, 0); + var sections = pex?.SectionTable; + if (sections == null) + return null; + + // Get the .data section, if it exists + var dataSection = sections.FirstOrDefault(s => Encoding.ASCII.GetString(s.Name).StartsWith(".data")); + if (dataSection != null) + { + int sectionAddr = (int)dataSection.PointerToRawData; + int sectionEnd = sectionAddr + (int)dataSection.VirtualSize; + var matchers = new List + { + // V SUHPISYS + new ContentMatchSet( + new ContentMatch(new byte?[] + { + 0x56, 0x20, 0x53, 0x55, 0x48, 0x50, 0x49, 0x53, + 0x59, 0x53 + }, start: sectionAddr, end: sectionEnd), + GetVersion, "Sysiphus"), + }; + + string match = MatchUtil.GetFirstMatch(file, fileContent, matchers, includeDebug); + if (!string.IsNullOrWhiteSpace(match)) + return match; + } + + return null; + } public static string GetVersion(string file, byte[] fileContent, List positions) { - int index = positions[0] - 3; - char subVersion = (char)fileContent[index]; - index++; - index++; - char version = (char)fileContent[index]; + // The version is reversed + string version = new string( + new ArraySegment(fileContent, positions[0] - 4, 4) + .Reverse() + .Select(b => (char)b) + .ToArray()) + .Trim(); - if (char.IsNumber(version) && char.IsNumber(subVersion)) - return $"{version}.{subVersion}"; + // Check for the DVD extra string + string extra = new string( + new ArraySegment(fileContent, positions[0] + "V SUHPISYS".Length, 3) + .Select(b => (char)b) + .ToArray()); + bool isDVD = extra == "DVD"; - return string.Empty; + if (char.IsNumber(version[0]) && char.IsNumber(version[2])) + return isDVD ? $"DVD {version}" : version; + + return isDVD ? "DVD" : string.Empty; } } }