From 6452d39de187645c7720326032467684ea15710e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Sun, 29 Aug 2021 21:38:19 -0700 Subject: [PATCH] Partially convert CD-Cops to section based; add note --- BurnOutSharp/ProtectionType/CDCops.cs | 42 ++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/BurnOutSharp/ProtectionType/CDCops.cs b/BurnOutSharp/ProtectionType/CDCops.cs index a578447d..52c2f7f0 100644 --- a/BurnOutSharp/ProtectionType/CDCops.cs +++ b/BurnOutSharp/ProtectionType/CDCops.cs @@ -2,6 +2,8 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Text; +using BurnOutSharp.ExecutableType.Microsoft; using BurnOutSharp.Matching; namespace BurnOutSharp.ProtectionType @@ -11,6 +13,7 @@ namespace BurnOutSharp.ProtectionType /// public List GetContentMatchSets() { + // TODO: Obtain a sample to find where this string is in a typical executable return new List { // CD-Cops, ver. @@ -19,14 +22,45 @@ namespace BurnOutSharp.ProtectionType 0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C, 0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20 }, GetVersion, "CD-Cops"), - - // .grand + (char)0x00 - new ContentMatchSet(new byte?[] { 0x2E, 0x67, 0x72, 0x61, 0x6E, 0x64, 0x00 }, "CD-Cops"), }; } /// - 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 .grand section, if it exists + var grandSection = sections.FirstOrDefault(s => Encoding.ASCII.GetString(s.Name).StartsWith(".grand")); + if (grandSection != null) + { + int sectionAddr = (int)grandSection.PointerToRawData; + int sectionEnd = sectionAddr + (int)grandSection.VirtualSize; + var matchers = new List + { + // CD-Cops, ver. + new ContentMatchSet( + new ContentMatch(new byte?[] + { + 0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C, + 0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20 + }, start: sectionAddr, end: sectionEnd), + GetVersion, "CD-Cops"), + }; + + string match = MatchUtil.GetFirstMatch(file, fileContent, matchers, includeDebug); + if (!string.IsNullOrWhiteSpace(match)) + return match; + + // return "CD-Cops (Unknown Version)"; + } + + return null; + } /// public ConcurrentQueue CheckDirectoryPath(string path, IEnumerable files)