From 78947616b1b6056ba2b49a58f6cd686c222dba83 Mon Sep 17 00:00:00 2001
From: HeroponRikiBestest
<50224630+HeroponRikiBestest@users.noreply.github.com>
Date: Fri, 7 Nov 2025 22:07:08 -0500
Subject: [PATCH] Add json output for protectionscan (#390)
* actual first pr
* Initial fixes
* Second round of fixes
* Final fix
---
ProtectionScan/Features/MainFeature.cs | 61 ++++++++++++++++++++++++++
1 file changed, 61 insertions(+)
diff --git a/ProtectionScan/Features/MainFeature.cs b/ProtectionScan/Features/MainFeature.cs
index 7c104bd6..6288f533 100644
--- a/ProtectionScan/Features/MainFeature.cs
+++ b/ProtectionScan/Features/MainFeature.cs
@@ -29,6 +29,11 @@ namespace ProtectionScan.Features
private const string _fileOnlyName = "file-only";
internal readonly FlagInput FileOnlyInput = new(_fileOnlyName, ["-f", "--file"], "Print to file only");
+#if NETCOREAPP
+ private const string _jsonName = "json";
+ internal readonly FlagInput JsonInput = new(_jsonName, ["-j", "--json"], "Output to json file");
+#endif
+
private const string _noArchivesName = "no-archives";
internal readonly FlagInput NoArchivesInput = new(_noArchivesName, ["-na", "--no-archives"], "Disable scanning archives");
@@ -47,6 +52,11 @@ namespace ProtectionScan.Features
/// Output information to file only, skip printing to console
///
public bool FileOnly { get; private set; }
+
+ ///
+ /// Output information to json
+ ///
+ public bool JsonFlag { get; private set; }
public MainFeature()
: base(DisplayName, _flags, _description)
@@ -55,6 +65,9 @@ namespace ProtectionScan.Features
Add(DebugInput);
Add(FileOnlyInput);
+#if NETCOREAPP
+ Add(JsonInput);
+#endif
Add(NoContentsInput);
Add(NoArchivesInput);
Add(NoPathsInput);
@@ -70,6 +83,9 @@ namespace ProtectionScan.Features
// Get the options from the arguments
FileOnly = GetBoolean(_fileOnlyName);
+#if NETCOREAPP
+ JsonFlag = GetBoolean(_jsonName);
+#endif
// Create scanner for all paths
var scanner = new Scanner(
@@ -127,7 +143,12 @@ namespace ProtectionScan.Features
try
{
var protections = scanner.GetProtections(path);
+
WriteProtectionResultFile(path, protections);
+
+#if NETCOREAPP
+ WriteProtectionResultJson(path, protections);
+#endif
}
catch (Exception ex)
{
@@ -199,5 +220,45 @@ namespace ProtectionScan.Features
// Dispose of the writer
sw?.Dispose();
}
+
+#if NETCOREAPP
+ ///
+ /// Write the protection results from a single path to a json file, if possible
+ ///
+ /// File or directory path
+ /// Dictionary of protections found, if any
+ private static void WriteProtectionResultJson(string path, Dictionary> protections)
+ {
+ if (protections == null)
+ {
+ Console.WriteLine($"No protections found for {path}");
+ return;
+ }
+
+ // Attempt to open a protection file for writing
+ StreamWriter? jsw = null;
+ try
+ {
+ jsw = new StreamWriter(File.OpenWrite($"protection-{DateTime.Now:yyyy-MM-dd_HHmmss.ffff}.json"));
+ // Create the output data
+ string serializedData = System.Text.Json.JsonSerializer.Serialize(protections, JsonSerializerOptions);
+
+ // Write the output data
+ // TODO: this prints plus symbols wrong, probably some other things too
+ jsw?.WriteLine(serializedData);
+ jsw?.Flush();
+
+ // Dispose of the writer
+ jsw?.Dispose();
+ }
+ catch { }
+ }
+
+ ///
+ /// JSON serializer options for output printing
+ ///
+ private static System.Text.Json.JsonSerializerOptions JsonSerializerOptions
+ => new System.Text.Json.JsonSerializerOptions { WriteIndented = true };
+#endif
}
}