Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
155 lines (129 loc) · 6.08 KB
/
Copy pathProgram.cs
File metadata and controls
155 lines (129 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// DefenderRuleParser
// Author: Andrea Cristaldi 2025 - https://github.com/andreacristaldi/DefenderRuleParser
// This project is licensed under the Apache 2.0 License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace DefenderRuleParser2
{
internal class Program
{
static void Main(string[] args)
{
Logger.Info("DefenderRuleParser\nhttps://github.com/andreacristaldi/DefenderRuleParser\n\n");
if (args.Length < 1)
{
Logger.Info("Usage:");
Logger.Info(" DefenderRuleParser <fileOrFolder> [--recursive] [--skip-existing] [--debug] [--debug-hex] [--quiet]");
Logger.Info("");
Logger.Info("Options:");
Logger.Info(" --recursive Process folders recursively");
//Logger.Info(" --skip-existing Skip .json if already present");
Logger.Info(" --debug Enable verbose logs");
//Logger.Info(" --quiet Suppress console logs (modules can still check Logger flags)");
return;
}
string inputPath = args[0];
bool recursive = args.Contains("--recursive", StringComparer.OrdinalIgnoreCase);
bool skipExisting = args.Contains("--skip-existing", StringComparer.OrdinalIgnoreCase);
bool debug = args.Contains("--debug", StringComparer.OrdinalIgnoreCase);
bool quiet = args.Contains("--quiet", StringComparer.OrdinalIgnoreCase);
Logger.DebugEnabled = debug;
Logger.Verbose($"[cfg] recursive={recursive} skipExisting={skipExisting} debug={debug} quiet={quiet}");
Logger.Info("[+] Loading threat dictionary from 'defender.csv'...");
string filePath = "defender.csv";
if (!File.Exists(filePath))
{
Logger.Warn("Threat dictionary not found. Attempting to retrieve using PowerShell...");
try
{
var psi = new ProcessStartInfo()
{
FileName = "powershell.exe",
Arguments = "Get-MpThreatCatalog | Export-Csv -Path ./defender.csv -NoTypeInformation",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
};
using (var process = new Process())
{
process.StartInfo = psi;
process.Start();
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
process.WaitForExit();
if (process.ExitCode != 0 || !File.Exists(filePath))
{
Logger.Error("PowerShell execution failed or defender.csv not created.");
Logger.Verbose(error);
Logger.Info("Ensure PowerShell is available, Windows Defender is installed, and you have sufficient permissions.");
return;
}
Logger.Info("Threat catalog successfully retrieved.");
}
}
catch (Exception ex)
{
Logger.Error("Error executing PowerShell: " + ex.Message);
Logger.Info("Make sure PowerShell is installed and accessible, and that script execution is not restricted.");
return;
}
}
ThreatDatabase.Load(filePath);
var binFiles = new List<string>();
if (File.Exists(inputPath) && inputPath.EndsWith(".bin", StringComparison.OrdinalIgnoreCase))
{
binFiles.Add(inputPath);
}
else if (Directory.Exists(inputPath))
{
var searchOption = recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly;
var allBins = Directory.GetFiles(inputPath, "*.bin", searchOption);
foreach (var bin in allBins)
{
string jsonPath = Path.ChangeExtension(bin, ".json");
if (skipExisting && File.Exists(jsonPath))
{
Logger.Info($"[>] Skipping {Path.GetFileName(bin)} (JSON already exists)");
continue;
}
binFiles.Add(bin);
}
}
else
{
Logger.Error("[!] Invalid file or folder path.");
return;
}
foreach (string binPath in binFiles)
{
Logger.Info($"\n[+] Processing: {binPath}");
Logger.OutputFolder = Path.GetFullPath(binPath);
try
{
using (var fs = new FileStream(binPath, FileMode.Open, FileAccess.Read))
using (var reader = new BinaryReader(fs))
{
SignatureReader.ExtractSignatures(reader, binPath);
}
string outputJson = Path.Combine(
Path.GetDirectoryName(binPath),
Path.GetFileNameWithoutExtension(binPath) + ".json"
);
//Exporter.SaveAllThreats(Path.GetDirectoryName(outputJson), Path.GetFileNameWithoutExtension(outputJson));
//Logger.Info($"[V] Exported: {outputJson}");
//File.Delete(binPath);
}
catch (Exception ex)
{
Logger.Error($"[!] Failed to process {binPath}: {ex.Message}");
}
}
Logger.Info("\n[V] Parsing completed. Press any key to exit.");
Console.ReadKey();
}
}
}