Add separate ProtectionScan executable

This commit is contained in:
Matt Nadareski
2024-11-04 14:38:23 -05:00
parent f998a578cc
commit 3de56083d8
7 changed files with 448 additions and 1 deletions

138
ProtectionScan/Options.cs Normal file
View File

@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
namespace ProtectionScan
{
/// <summary>
/// Set of options for the test executable
/// </summary>
internal sealed class Options
{
#region Properties
/// <summary>
/// Enable debug output for relevant operations
/// </summary>
public bool Debug { get; private set; } = false;
/// <summary>
/// Set of input paths to use for operations
/// </summary>
public List<string> InputPaths { get; private set; } = [];
/// <summary>
/// Scan archives during protection scanning
/// </summary>
public bool ScanArchives { get; private set; } = true;
/// <summary>
/// Scan file contents during protection scanning
/// </summary>
public bool ScanContents { get; private set; } = true;
/// <summary>
/// Scan game engines during protection scanning
/// </summary>
public bool ScanGameEngines { get; private set; } = true;
/// <summary>
/// Scan packers during protection scanning
/// </summary>
public bool ScanPackers { get; private set; } = true;
/// <summary>
/// Scan file paths during protection scanning
/// </summary>
public bool ScanPaths { get; private set; } = true;
#endregion
/// <summary>
/// Parse commandline arguments into an Options object
/// </summary>
public static Options? ParseOptions(string[] args)
{
// If we have invalid arguments
if (args == null || args.Length == 0)
return null;
// Create an Options object
var options = new Options();
// Parse the options and paths
for (int index = 0; index < args.Length; index++)
{
string arg = args[index];
switch (arg)
{
case "-?":
case "-h":
case "--help":
return null;
case "-d":
case "--debug":
options.Debug = true;
break;
case "-na":
case "--no-archives":
options.ScanArchives = false;
break;
case "-nc":
case "--no-contents":
options.ScanContents = false;
break;
case "-ng":
case "--no-game-engines":
options.ScanGameEngines = false;
break;
case "-np":
case "--no-packers":
options.ScanPackers = false;
break;
case "-ns":
case "--no-paths":
options.ScanPaths = false;
break;
default:
options.InputPaths.Add(arg);
break;
}
}
// Validate we have any input paths to work on
if (options.InputPaths.Count == 0)
{
Console.WriteLine("At least one path is required!");
return null;
}
return options;
}
/// <summary>
/// Display help text
/// </summary>
public static void DisplayHelp()
{
Console.WriteLine("Protection Scanner");
Console.WriteLine();
Console.WriteLine("ProtectionScan.exe <options> file|directory ...");
Console.WriteLine();
Console.WriteLine("Options:");
Console.WriteLine("-?, -h, --help Display this help text and quit");
Console.WriteLine("-d, --debug Enable debug mode");
Console.WriteLine("-nc, --no-contents Disable scanning for content checks");
Console.WriteLine("-na, --no-archives Disable scanning archives");
Console.WriteLine("-ng, --no-game-engines Disable scanning for game engines");
Console.WriteLine("-np, --no-packers Disable scanning for packers");
Console.WriteLine("-ns, --no-paths Disable scanning for path checks");
}
}
}

108
ProtectionScan/Program.cs Normal file
View File

@@ -0,0 +1,108 @@
using System;
using System.IO;
using System.Linq;
using BinaryObjectScanner;
namespace ProtectionScan
{
class Program
{
static void Main(string[] args)
{
#if NET462_OR_GREATER || NETCOREAPP
// Register the codepages
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
// Create progress indicator
var fileProgress = new Progress<ProtectionProgress>();
fileProgress.ProgressChanged += Changed;
// Get the options from the arguments
var options = Options.ParseOptions(args);
// If we have an invalid state
if (options == null)
{
Options.DisplayHelp();
return;
}
// Create scanner for all paths
var scanner = new Scanner(
options.ScanArchives,
options.ScanContents,
options.ScanGameEngines,
options.ScanPackers,
options.ScanPaths,
options.Debug,
fileProgress);
// Loop through the input paths
foreach (string inputPath in options.InputPaths)
{
GetAndWriteProtections(scanner, inputPath);
}
}
/// <summary>
/// Wrapper to get and log protections for a single path
/// </summary>
/// <param name="scanner">Scanner object to use</param>
/// <param name="path">File or directory path</param>
private static void GetAndWriteProtections(Scanner scanner, string path)
{
// An invalid path can't be scanned
if (!Directory.Exists(path) && !File.Exists(path))
{
Console.WriteLine($"{path} does not exist, skipping...");
return;
}
try
{
var protections = scanner.GetProtections(path);
WriteProtectionResultFile(path, protections);
}
catch (Exception ex)
{
using var sw = new StreamWriter(File.OpenWrite($"exception-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
sw.WriteLine(ex);
}
}
/// <summary>
/// Write the protection results from a single path to file, if possible
/// </summary>
/// <param name="path">File or directory path</param>
/// <param name="protections">Dictionary of protections found, if any</param>
private static void WriteProtectionResultFile(string path, ProtectionDictionary? protections)
{
if (protections == null)
{
Console.WriteLine($"No protections found for {path}");
return;
}
using var sw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
foreach (string key in protections.Keys.OrderBy(k => k))
{
// Skip over files with no protection
if (protections[key] == null || !protections[key].Any())
continue;
string line = $"{key}: {string.Join(", ", [.. protections[key].OrderBy(p => p)])}";
Console.WriteLine(line);
sw.WriteLine(line);
}
}
/// <summary>
/// Protection progress changed handler
/// </summary>
private static void Changed(object? source, ProtectionProgress value)
{
Console.WriteLine($"{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}");
}
}
}

View File

@@ -0,0 +1,37 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net20;net35;net40;net452;net462;net472;net48;netcoreapp3.1;net5.0;net6.0;net7.0;net8.0</TargetFrameworks>
<OutputType>Exe</OutputType>
<CheckEolTargetFramework>false</CheckEolTargetFramework>
<IncludeSourceRevisionInInformationalVersion>false</IncludeSourceRevisionInInformationalVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<!-- Support All Frameworks -->
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net4`))">
<RuntimeIdentifiers>win-x86;win-x64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith(`netcoreapp`)) OR $(TargetFramework.StartsWith(`net5`))">
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(TargetFramework.StartsWith(`net6`)) OR $(TargetFramework.StartsWith(`net7`)) OR $(TargetFramework.StartsWith(`net8`))">
<RuntimeIdentifiers>win-x86;win-x64;win-arm64;linux-x64;linux-arm64;osx-x64;osx-arm64</RuntimeIdentifiers>
</PropertyGroup>
<PropertyGroup Condition="$(RuntimeIdentifier.StartsWith(`osx-arm`))">
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
</PropertyGroup>
<!-- Set a build flag for Windows specifically -->
<PropertyGroup Condition="'$(RuntimeIdentifier)'=='win-x86'">
<DefineConstants>$(DefineConstants);WIN</DefineConstants>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\BinaryObjectScanner\BinaryObjectScanner.csproj" />
</ItemGroup>
</Project>