2019-09-27 23:52:24 -07:00
|
|
|
|
using System;
|
2021-07-18 09:44:23 -07:00
|
|
|
|
using System.Collections.Concurrent;
|
2019-09-27 23:52:24 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
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 22:19:38 -07:00
|
|
|
|
using BurnOutSharp.Matching;
|
2019-09-27 23:52:24 -07:00
|
|
|
|
|
|
|
|
|
|
namespace BurnOutSharp.ProtectionType
|
|
|
|
|
|
{
|
2022-05-01 17:23:00 -07:00
|
|
|
|
public class CDDVDCops : IContentCheck, INewExecutableCheck, IPathCheck, IPortableExecutableCheck
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-08-25 19:37:32 -07:00
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckContents(string file, byte[] fileContent, bool includeDebug)
|
2021-08-29 21:38:19 -07:00
|
|
|
|
{
|
2021-09-14 11:33:53 -07:00
|
|
|
|
// TODO: Obtain a sample to find where this string is in a typical executable
|
2022-03-02 08:56:26 -08:00
|
|
|
|
if (includeDebug)
|
2021-09-14 11:33:53 -07:00
|
|
|
|
{
|
2022-03-02 08:56:26 -08:00
|
|
|
|
var contentMatchSets = new List<ContentMatchSet>
|
2021-09-14 11:33:53 -07:00
|
|
|
|
{
|
2022-03-02 08:56:26 -08:00
|
|
|
|
// TODO: Remove from here once it's confirmed that no PE executables contain this string
|
|
|
|
|
|
// CD-Cops, ver.
|
|
|
|
|
|
new ContentMatchSet(new byte?[]
|
|
|
|
|
|
{
|
|
|
|
|
|
0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C,
|
|
|
|
|
|
0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20
|
|
|
|
|
|
}, GetVersion, "CD-Cops"),
|
|
|
|
|
|
|
|
|
|
|
|
// // DVD-Cops, ver.
|
|
|
|
|
|
new ContentMatchSet(new byte?[]
|
|
|
|
|
|
{
|
|
|
|
|
|
0x44, 0x56, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73,
|
|
|
|
|
|
0x2C, 0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20
|
|
|
|
|
|
}, GetVersion, "DVD-Cops"),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return MatchUtil.GetFirstMatch(file, fileContent, contentMatchSets, includeDebug);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
2021-08-29 21:38:19 -07:00
|
|
|
|
}
|
2019-09-27 23:52:24 -07:00
|
|
|
|
|
2022-03-14 11:20:11 -07:00
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckNewExecutable(string file, NewExecutable nex, bool includeDebug)
|
2022-03-14 11:20:11 -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:20:11 -07:00
|
|
|
|
// TODO: Do something with these strings in the NE header(?)
|
|
|
|
|
|
// - CDCOPS
|
|
|
|
|
|
// - CDcops assembly-language DLL
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Figure out what NE section this lives in
|
|
|
|
|
|
var neMatchSets = new List<ContentMatchSet>
|
|
|
|
|
|
{
|
|
|
|
|
|
// CD-Cops, ver.
|
|
|
|
|
|
new ContentMatchSet(new byte?[]
|
|
|
|
|
|
{
|
|
|
|
|
|
0x43, 0x44, 0x2D, 0x43, 0x6F, 0x70, 0x73, 0x2C,
|
|
|
|
|
|
0x20, 0x20, 0x76, 0x65, 0x72, 0x2E, 0x20
|
|
|
|
|
|
}, GetVersion, "CD-Cops"),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2022-03-15 11:18:53 -07:00
|
|
|
|
return MatchUtil.GetFirstMatch(file, data, neMatchSets, includeDebug);
|
2022-03-14 11:20:11 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
2022-05-01 17:17:15 -07:00
|
|
|
|
public string CheckPortableExecutable(string file, PortableExecutable pex, bool includeDebug)
|
2022-03-14 11:20:11 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Get the sections from the executable, if possible
|
|
|
|
|
|
var sections = pex?.SectionTable;
|
|
|
|
|
|
if (sections == null)
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
|
|
// Get the .grand section, if it exists -- TODO: Confirm is this is in DVD-Cops as well
|
|
|
|
|
|
bool grandSection = pex.ContainsSection(".grand", exact: true);
|
|
|
|
|
|
if (grandSection)
|
|
|
|
|
|
return "CD-Cops";
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-02-26 00:32:09 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-07-18 09:44:23 -07:00
|
|
|
|
public ConcurrentQueue<string> CheckDirectoryPath(string path, IEnumerable<string> files)
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-03-22 23:02:01 -07:00
|
|
|
|
// TODO: Original had "CDCOPS.DLL" required and all the rest in a combined OR
|
|
|
|
|
|
var matchers = new List<PathMatchSet>
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-03-22 23:02:01 -07:00
|
|
|
|
new PathMatchSet(new PathMatch("CDCOPS.DLL", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".GZ_", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".W_X", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".Qz", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".QZ_", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
};
|
2021-03-19 15:41:49 -07:00
|
|
|
|
|
2021-03-23 13:35:12 -07:00
|
|
|
|
return MatchUtil.GetAllMatches(files, matchers, any: true);
|
2021-03-19 15:41:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public string CheckFilePath(string path)
|
|
|
|
|
|
{
|
2021-03-22 23:02:01 -07:00
|
|
|
|
var matchers = new List<PathMatchSet>
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-03-22 23:02:01 -07:00
|
|
|
|
new PathMatchSet(new PathMatch("CDCOPS.DLL", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".GZ_", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".W_X", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".Qz", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
new PathMatchSet(new PathMatch(".QZ_", useEndsWith: true), "CD-Cops"),
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return MatchUtil.GetFirstMatch(path, matchers, any: true);
|
2019-09-27 23:52:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-03-22 11:43:51 -07:00
|
|
|
|
public static string GetVersion(string file, byte[] fileContent, List<int> positions)
|
2019-09-27 23:52:24 -07:00
|
|
|
|
{
|
2021-03-22 11:43:51 -07:00
|
|
|
|
char[] version = new ArraySegment<byte>(fileContent, positions[0] + 15, 4).Select(b => (char)b).ToArray();
|
2020-09-10 21:10:32 -07:00
|
|
|
|
if (version[0] == 0x00)
|
2021-03-22 16:25:40 -07:00
|
|
|
|
return string.Empty;
|
2019-09-30 11:08:44 -07:00
|
|
|
|
|
2020-09-10 21:10:32 -07:00
|
|
|
|
return new string(version);
|
2019-09-27 23:52:24 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|