Skip to content

Commit

Permalink
Merge pull request #314 from HotCakeX/Harden-Windows-Security-v0.5.3
Browse files Browse the repository at this point in the history
Harden Windows Security v0.5.3
  • Loading branch information
HotCakeX authored Aug 2, 2024
2 parents 147b0aa + 2e07fdd commit 16b4d76
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ public static object Get(string taskName, string taskPath, OutputType outputType

// Check if the TaskName matches the provided taskName (if specified)
// and TaskPath matches the provided taskPath (if specified)
bool nameMatches = string.IsNullOrEmpty(taskName) || name == taskName;
bool pathMatches = string.IsNullOrEmpty(taskPath) || path == taskPath;
bool nameMatches = string.IsNullOrEmpty(taskName) || string.Equals(name, taskName, StringComparison.OrdinalIgnoreCase);
bool pathMatches = string.IsNullOrEmpty(taskPath) || string.Equals(path, taskPath, StringComparison.OrdinalIgnoreCase);

// If both TaskName and TaskPath match the provided criteria, add the task to the matchingTasks list
if (nameMatches && pathMatches)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace HardeningModule
{
public class IniFileConverter
{
// a helper method to parse the ini file from the output of the "Secedit /export /cfg .\security_policy.inf"
// A helper method to parse the ini file from the output of the "Secedit /export /cfg .\security_policy.inf"
public static Dictionary<string, Dictionary<string, string>> ConvertFromIniFile(string iniFilePath)
{
var iniObject = new Dictionary<string, Dictionary<string, string>>();
var iniObject = new Dictionary<string, Dictionary<string, string>>(StringComparer.OrdinalIgnoreCase);
string[] lines = File.ReadAllLines(iniFilePath);
string sectionName = string.Empty;

Expand All @@ -21,7 +21,7 @@ public static Dictionary<string, Dictionary<string, string>> ConvertFromIniFile(
if (sectionMatch.Success)
{
sectionName = sectionMatch.Groups[1].Value;
iniObject[sectionName] = new Dictionary<string, string>();
iniObject[sectionName] = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
continue;
}

Expand All @@ -40,7 +40,7 @@ public static Dictionary<string, Dictionary<string, string>> ConvertFromIniFile(
}

// Ignore blank lines or comments
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";") || line.StartsWith("#"))
if (string.IsNullOrWhiteSpace(line) || line.StartsWith(";", StringComparison.Ordinal) || line.StartsWith("#", StringComparison.Ordinal))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ public partial class MDMClassProcessor
{
foreach (var keyValuePair in dictionary)
{
// filter out the items we don't need
if (keyValuePair.Key == "Class" || keyValuePair.Key == "InstanceID" || keyValuePair.Key == "ParentID")
// Filter out the items we don't need using ordinal, case-insensitive comparison
if (String.Equals(keyValuePair.Key, "Class", StringComparison.OrdinalIgnoreCase) ||
String.Equals(keyValuePair.Key, "InstanceID", StringComparison.OrdinalIgnoreCase) ||
String.Equals(keyValuePair.Key, "ParentID", StringComparison.OrdinalIgnoreCase))
{
continue;
}

// Add the date to the list
// Add the data to the list
resultsList.Add(new HardeningModule.MDMClassProcessor(
keyValuePair.Key,
keyValuePair.Value?.ToString(),
Expand Down
11 changes: 6 additions & 5 deletions Harden-Windows-Security Module/Main files/C#/RegistryEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;
using Microsoft.Win32;

namespace HardeningModule
Expand All @@ -8,7 +9,7 @@ public static class RegistryEditor
public static void EditRegistry(string path, string key, string value, string type, string action)
{
// Removing the 'Registry::' prefix from the path
if (path.StartsWith("Registry::"))
if (path.StartsWith("Registry::", StringComparison.OrdinalIgnoreCase))
{
path = path.Substring(10);
}
Expand All @@ -19,7 +20,7 @@ public static void EditRegistry(string path, string key, string value, string ty

RegistryKey baseRegistryKey;

switch (baseKey.ToUpper())
switch (baseKey.ToUpperInvariant())
{
case "HKEY_LOCAL_MACHINE":
{
Expand Down Expand Up @@ -59,7 +60,7 @@ public static void EditRegistry(string path, string key, string value, string ty
RegistryValueKind valueType;
object convertedValue;

switch (type.ToUpper())
switch (type.ToUpperInvariant())
{
case "STRING":
{
Expand All @@ -70,13 +71,13 @@ public static void EditRegistry(string path, string key, string value, string ty
case "DWORD":
{
valueType = RegistryValueKind.DWord;
convertedValue = int.Parse(value);
convertedValue = int.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
break;
}
case "QWORD":
{
valueType = RegistryValueKind.QWord;
convertedValue = long.Parse(value);
convertedValue = long.Parse(value, NumberStyles.Integer, CultureInfo.InvariantCulture);
break;
}
case "BINARY":
Expand Down
33 changes: 33 additions & 0 deletions Harden-Windows-Security Module/Main files/C#/SneakAndPeek.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;

namespace HardeningModule
{
public static class SneakAndPeek
{
/// <summary>
/// Takes a peek into a zip file and returns bool based on whether a file based on the query is found or not
/// </summary>
/// <param name="query"></param>
/// <param name="zipFile"></param>
/// <returns></returns>
public static bool Search(string query, string zipFile)
{
// Convert the query to a regular expression
string regexPattern = "^" + Regex.Escape(query).Replace("\\*", ".*") + "$";
Regex regex = new Regex(regexPattern, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);

// Open the zip file in read mode
using (ZipArchive zipArchive = ZipFile.OpenRead(zipFile))
{
// Make sure the selected zip has the required file
var content = zipArchive.Entries.Where(entry => regex.IsMatch(entry.FullName)).ToList();

// Return true if the number of files found is greater than 0
return content.Count > 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static FeatureStatus CheckWindowsFeatures()
private static Dictionary<string, string> GetOptionalFeatureStates()
{
// Initialize a dictionary to store the states of optional features
var states = new Dictionary<string, string>();
var states = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); // Ensure case-insensitive key comparison

// Create a ManagementObjectSearcher to query Win32_OptionalFeature
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_OptionalFeature"))
Expand Down Expand Up @@ -84,17 +84,20 @@ private static string GetCapabilityState(string capabilityName)
string dismOutput = RunDismCommand($"/Online /Get-CapabilityInfo /CapabilityName:{capabilityName}");

// Check if the output contains "State : Installed"
if (dismOutput.Contains("State : Installed"))
// check if the return value is greater than or equal to 0 indicating that the substring exists in the string
if (dismOutput.IndexOf("State : Installed", StringComparison.Ordinal) >= 0)
{
return "Installed";
}
// Check if the output contains "State : Not Present"
else if (dismOutput.Contains("State : Not Present"))
// check if the return value is greater than or equal to 0 indicating that the substring exists in the string
else if (dismOutput.IndexOf("State : Not Present", StringComparison.Ordinal) >= 0)
{
return "Not Present";
}
// Check if the output contains "State : Staged"
else if (dismOutput.Contains("State : Staged"))
// check if the return value is greater than or equal to 0 indicating that the substring exists in the string
else if (dismOutput.IndexOf("State : Staged", StringComparison.Ordinal) >= 0)
{
return "Staged";
}
Expand Down
3 changes: 2 additions & 1 deletion Harden-Windows-Security Module/Main files/C#/WriteVerbose.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public static void Write(string message)
{
try
{
if (HardeningModule.GlobalVars.VerbosePreference == "Continue" || HardeningModule.GlobalVars.VerbosePreference == "Inquire")
if (string.Equals(HardeningModule.GlobalVars.VerbosePreference, "Continue", StringComparison.OrdinalIgnoreCase) ||
string.Equals(HardeningModule.GlobalVars.VerbosePreference, "Inquire", StringComparison.OrdinalIgnoreCase))
{
HardeningModule.GlobalVars.Host.UI.WriteVerboseLine(message);
}
Expand Down
Loading

0 comments on commit 16b4d76

Please sign in to comment.