Compare commits

...

9 Commits
3.4.3 ... 3.4.5

Author SHA1 Message Date
Matt Nadareski
f1afb6d60b Bump version 2025-10-07 16:00:04 -04:00
Matt Nadareski
5e812daf01 Fix debug flag use 2025-10-07 14:56:53 -04:00
Matt Nadareski
ab508eebe8 Use main feature pattern 2025-10-07 13:44:27 -04:00
Matt Nadareski
31792fab48 Update packages 2025-10-07 13:33:55 -04:00
Matt Nadareski
010f792b1a Add minor identifier for NSIS 2025-10-07 09:45:50 -04:00
Matt Nadareski
a3118cee68 Use CommandLine library for executable 2025-10-06 09:43:15 -04:00
Matt Nadareski
daa72eb970 Bump version 2025-10-05 17:32:27 -04:00
Matt Nadareski
a42328ef60 Update Serialization to 2.0.1 2025-10-05 17:26:01 -04:00
Matt Nadareski
7f2dd26d9b Require exact versions for build 2025-09-30 11:12:41 -04:00
8 changed files with 255 additions and 256 deletions

View File

@@ -17,7 +17,7 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.14.1" />
<PackageReference Include="SabreTools.Serialization" Version="2.0.0" />
<PackageReference Include="SabreTools.Serialization" Version="[2.0.2]" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.4">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

View File

@@ -13,7 +13,8 @@
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>3.4.3</Version>
<Version>3.4.5</Version>
<WarningsNotAsErrors>NU5104</WarningsNotAsErrors>
<!-- Package Properties -->
<Authors>Matt Nadareski</Authors>
@@ -45,11 +46,11 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="GrindCore.SharpCompress" Version="0.40.4-alpha" Condition="!$(TargetFramework.StartsWith(`net2`)) AND !$(TargetFramework.StartsWith(`net3`)) AND !$(TargetFramework.StartsWith(`net40`)) AND !$(TargetFramework.StartsWith(`net452`))" />
<PackageReference Include="MinThreadingBridge" Version="0.11.4" Condition="$(TargetFramework.StartsWith(`net2`)) OR $(TargetFramework.StartsWith(`net3`)) OR $(TargetFramework.StartsWith(`net40`))" />
<PackageReference Include="SabreTools.Hashing" Version="1.5.0" />
<PackageReference Include="SabreTools.IO" Version="1.7.5" />
<PackageReference Include="SabreTools.Serialization" Version="2.0.0" />
<PackageReference Include="SharpCompress" Version="0.40.0" Condition="!$(TargetFramework.StartsWith(`net2`)) AND !$(TargetFramework.StartsWith(`net3`)) AND !$(TargetFramework.StartsWith(`net40`)) AND !$(TargetFramework.StartsWith(`net452`))" />
<PackageReference Include="SabreTools.Hashing" Version="[1.5.1]" />
<PackageReference Include="SabreTools.IO" Version="[1.7.6]" />
<PackageReference Include="SabreTools.Serialization" Version="[2.0.2]" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="9.0.9" Condition="!$(TargetFramework.StartsWith(`net2`)) AND !$(TargetFramework.StartsWith(`net3`)) AND !$(TargetFramework.StartsWith(`net40`)) AND !$(TargetFramework.StartsWith(`net452`))" />
</ItemGroup>

View File

@@ -78,7 +78,7 @@ namespace BinaryObjectScanner.Packer
{
embeddedTypes.Add("Embedded UHARC Archive");
}
else if (overlaySample.StartsWith(SabreTools.Data.Models.XZ.Constants.SignatureBytes))
else if (overlaySample.StartsWith(SabreTools.Data.Models.XZ.Constants.HeaderSignatureBytes))
{
embeddedTypes.Add("Embedded XZ Archive");
}
@@ -152,7 +152,7 @@ namespace BinaryObjectScanner.Packer
embeddedTypes.Add("Embedded RAR Archive");
else if (resourceSample.StartsWith([0x55, 0x48, 0x41, 0x06]))
embeddedTypes.Add("Embedded UHARC Archive");
else if (resourceSample.StartsWith(SabreTools.Data.Models.XZ.Constants.SignatureBytes))
else if (resourceSample.StartsWith(SabreTools.Data.Models.XZ.Constants.HeaderSignatureBytes))
embeddedTypes.Add("Embedded XZ Archive");
else if (resourceSample.StartsWith(SabreTools.Data.Models.MSDOS.Constants.SignatureBytes))
embeddedTypes.Add("Embedded Executable");
@@ -229,7 +229,7 @@ namespace BinaryObjectScanner.Packer
{
embeddedTypes.Add("Embedded Executable");
}
else if (overlaySample.StartsWith(SabreTools.Data.Models.XZ.Constants.SignatureBytes))
else if (overlaySample.StartsWith(SabreTools.Data.Models.XZ.Constants.HeaderSignatureBytes))
{
// 7-zip SFX script -- ";!@Install" to ";!@InstallEnd@!"
overlayOffset = exe.OverlayData.FirstPosition([0x3B, 0x21, 0x40, 0x49, 0x6E, 0x73, 0x74, 0x61, 0x6C, 0x6C, 0x45, 0x6E, 0x64, 0x40, 0x21]);

View File

@@ -19,8 +19,13 @@ namespace BinaryObjectScanner.Packer
if (name.OptionalStartsWith("Nullsoft Install System"))
return $"NSIS {name!.Substring("Nullsoft Install System".Length).Trim()}";
name = exe.AssemblyName;
if (name.OptionalStartsWith("Nullsoft.NSIS"))
return "NSIS";
// Get the .data/DATA section strings, if they exist
var strs = exe.GetFirstSectionStrings(".data") ?? exe.GetFirstSectionStrings("DATA");
var strs = exe.GetFirstSectionStrings(".data") ?? exe.GetFirstSectionStrings("DATA");
if (strs != null)
{
if (strs.Exists(s => s.Contains("NullsoftInst")))

View File

@@ -0,0 +1,185 @@
using System;
using System.Collections.Generic;
using System.IO;
using BinaryObjectScanner;
using SabreTools.CommandLine;
using SabreTools.CommandLine.Inputs;
namespace ProtectionScan.Features
{
internal sealed class MainFeature : Feature
{
#region Feature Definition
public const string DisplayName = "main";
/// <remarks>Flags are unused</remarks>
private static readonly string[] _flags = [];
/// <remarks>Description is unused</remarks>
private const string _description = "";
#endregion
#region Inputs
private const string _debugName = "debug";
internal readonly FlagInput DebugInput = new(_debugName, ["-d", "--debug"], "Enable debug mode");
private const string _noArchivesName = "no-archives";
internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives");
private const string _noContentsName = "no-contents";
internal readonly FlagInput NoContentsInput = new(_noContentsName, ["-nc", "--no-contents"], "Disable scanning for content checks");
private const string _noPathsName = "no-paths";
internal readonly FlagInput NoPathsInput = new(_noPathsName, ["-np", "--no-paths"], "Disable scanning for path checks");
private const string _noSubdirsName = "no-subdirs";
internal readonly FlagInput NoSubdirsInput = new(_noSubdirsName, ["-ns", "--no-subdirs"], "Disable scanning subdirectories");
#endregion
public MainFeature()
: base(DisplayName, _flags, _description)
{
RequiresInputs = true;
Add(DebugInput);
Add(NoContentsInput);
Add(NoArchivesInput);
Add(NoPathsInput);
Add(NoSubdirsInput);
}
/// <inheritdoc/>
public override bool Execute()
{
// Create progress indicator
var fileProgress = new Progress<ProtectionProgress>();
fileProgress.ProgressChanged += Changed;
// Create scanner for all paths
var scanner = new Scanner(
!GetBoolean(_noArchivesName),
!GetBoolean(_noContentsName),
!GetBoolean(_noPathsName),
!GetBoolean(_noSubdirsName),
GetBoolean(_debugName),
fileProgress);
// Loop through the input paths
for (int i = 0; i < Inputs.Count; i++)
{
string arg = Inputs[i];
GetAndWriteProtections(scanner, arg);
}
return true;
}
/// <inheritdoc/>
public override bool VerifyInputs() => Inputs.Count > 0;
/// <summary>
/// Protection progress changed handler
/// </summary>
private static void Changed(object? source, ProtectionProgress value)
{
string prefix = string.Empty;
for (int i = 0; i < value.Depth; i++)
{
prefix += "--> ";
}
Console.WriteLine($"{prefix}{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}");
}
/// <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)
{
// Normalize by getting the full path
path = Path.GetFullPath(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)
{
try
{
using var sw = new StreamWriter(File.OpenWrite($"exception-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
sw.WriteLine(ex);
}
catch
{
Console.WriteLine("Could not open exception log file for writing. See original message below:");
Console.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, Dictionary<string, List<string>> protections)
{
if (protections == null)
{
Console.WriteLine($"No protections found for {path}");
return;
}
// Attempt to open a protection file for writing
StreamWriter? sw = null;
try
{
sw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
}
catch
{
Console.WriteLine("Could not open protection log file for writing. Only a console log will be provided.");
}
// Sort the keys for consistent output
string[] keys = [.. protections.Keys];
Array.Sort(keys);
// Loop over all keys
foreach (string key in keys)
{
// Skip over files with no protection
var value = protections[key];
if (value.Count == 0)
continue;
// Sort the detected protections for consistent output
string[] fileProtections = [.. value];
Array.Sort(fileProtections);
// Format and output the line
string line = $"{key}: {string.Join(", ", fileProtections)}";
Console.WriteLine(line);
sw?.WriteLine(line);
}
// Dispose of the writer
sw?.Dispose();
}
}
}

View File

@@ -1,127 +0,0 @@
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 file paths during protection scanning
/// </summary>
public bool ScanPaths { get; private set; } = true;
/// <summary>
/// Scan subdirectories during protection scanning
/// </summary>
public bool ScanSubdirectories { get; 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 "-np":
case "--no-paths":
options.ScanPaths = false;
break;
case "-ns":
case "--no-subdirs":
options.ScanSubdirectories = 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 a basic help text
/// </summary>
public static void DisplayHelp()
{
Console.WriteLine("Protection Scanner");
Console.WriteLine();
Console.WriteLine("ProtectionScan <options> <path> ...");
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("-np, --no-paths Disable scanning for path checks");
Console.WriteLine("-ns, --no-subdirs Disable scanning subdirectories");
}
}
}

View File

@@ -1,148 +1,82 @@
using System;
using System.Collections.Generic;
using System.IO;
using BinaryObjectScanner;
using ProtectionScan.Features;
using SabreTools.CommandLine;
using SabreTools.CommandLine.Features;
namespace ProtectionScan
{
class Program
public static class Program
{
static void Main(string[] args)
public static void Main(string[] args)
{
#if NET462_OR_GREATER || NETCOREAPP || NETSTANDARD2_0_OR_GREATER
// Register the codepages
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance);
#endif
// Create progress indicator
var fileProgress = new Progress<ProtectionProgress>();
fileProgress.ProgressChanged += Changed;
// Create the command set
var mainFeature = new MainFeature();
var commandSet = CreateCommands(mainFeature);
// Get the options from the arguments
var options = Options.ParseOptions(args);
// If we have an invalid state
if (options == null)
// If we have no args, show the help and quit
if (args == null || args.Length == 0)
{
Options.DisplayHelp();
commandSet.OutputAllHelp();
return;
}
// Create scanner for all paths
var scanner = new Scanner(
options.ScanArchives,
options.ScanContents,
options.ScanPaths,
options.ScanSubdirectories,
options.Debug,
fileProgress);
// Cache the first argument and starting index
string featureName = args[0];
// Loop through the input paths
foreach (string inputPath in options.InputPaths)
// Try processing the standalone arguments
var topLevel = commandSet.GetTopLevel(featureName);
switch (topLevel)
{
GetAndWriteProtections(scanner, inputPath);
// Standalone Options
case Help help: help.ProcessArgs(args, 0, commandSet); return;
// Default Behavior
default:
if (!mainFeature.ProcessArgs(args, 0))
{
commandSet.OutputAllHelp();
return;
}
else if (!mainFeature.VerifyInputs())
{
Console.Error.WriteLine("At least one input is required");
commandSet.OutputAllHelp();
return;
}
mainFeature.Execute();
break;
}
}
/// <summary>
/// Wrapper to get and log protections for a single path
/// Create the command set for the program
/// </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)
private static CommandSet CreateCommands(MainFeature mainFeature)
{
// Normalize by getting the full path
path = Path.GetFullPath(path);
List<string> header = [
"Protection Scanner",
string.Empty,
"ProtectionScan <options> file|directory ...",
string.Empty,
];
// An invalid path can't be scanned
if (!Directory.Exists(path) && !File.Exists(path))
{
Console.WriteLine($"{path} does not exist, skipping...");
return;
}
var commandSet = new CommandSet(header);
try
{
var protections = scanner.GetProtections(path);
WriteProtectionResultFile(path, protections);
}
catch (Exception ex)
{
try
{
using var sw = new StreamWriter(File.OpenWrite($"exception-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
sw.WriteLine(ex);
}
catch
{
Console.WriteLine("Could not open exception log file for writing. See original message below:");
Console.WriteLine(ex);
}
}
}
commandSet.Add(new Help(["-?", "-h", "--help"]));
commandSet.Add(mainFeature.DebugInput);
commandSet.Add(mainFeature.NoContentsInput);
commandSet.Add(mainFeature.NoArchivesInput);
commandSet.Add(mainFeature.NoPathsInput);
commandSet.Add(mainFeature.NoSubdirsInput);
/// <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, Dictionary<string, List<string>> protections)
{
if (protections == null)
{
Console.WriteLine($"No protections found for {path}");
return;
}
// Attempt to open a protection file for writing
StreamWriter? sw = null;
try
{
sw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.txt"));
}
catch
{
Console.WriteLine("Could not open protection log file for writing. Only a console log will be provided.");
}
// Sort the keys for consistent output
string[] keys = [.. protections.Keys];
Array.Sort(keys);
// Loop over all keys
foreach (string key in keys)
{
// Skip over files with no protection
var value = protections[key];
if (value.Count == 0)
continue;
// Sort the detected protections for consistent output
string[] fileProtections = [.. value];
Array.Sort(fileProtections);
// Format and output the line
string line = $"{key}: {string.Join(", ", fileProtections)}";
Console.WriteLine(line);
sw?.WriteLine(line);
}
// Dispose of the writer
sw?.Dispose();
}
/// <summary>
/// Protection progress changed handler
/// </summary>
private static void Changed(object? source, ProtectionProgress value)
{
string prefix = string.Empty;
for (int i = 0; i < value.Depth; i++)
{
prefix += "--> ";
}
Console.WriteLine($"{prefix}{value.Percentage * 100:N2}%: {value.Filename} - {value.Protection}");
return commandSet;
}
}
}

View File

@@ -10,7 +10,7 @@
<Nullable>enable</Nullable>
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<Version>3.4.3</Version>
<Version>3.4.5</Version>
</PropertyGroup>
<!-- Support All Frameworks -->
@@ -32,7 +32,8 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="SabreTools.Serialization" Version="2.0.0" />
<PackageReference Include="SabreTools.CommandLine" Version="[1.3.2]" />
<PackageReference Include="SabreTools.Serialization" Version="[2.0.2]" />
</ItemGroup>
</Project>