Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to file scoped namespace and other minor improvements #465

Merged
merged 4 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions Harden-Windows-Security Module/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,21 @@
spelling_exclusion_path = .\exclusion.dic

# Miscellaneous settings
# https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties
trim_trailing_whitespace = true
end_of_line = crlf
indent_style = tab
insert_final_newline = true

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/dotnet-formatting-options
dotnet_sort_system_directives_first = true

# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options
csharp_using_directive_placement = outside_namespace:error
csharp_prefer_simple_using_statement = true:error
csharp_new_line_before_members_in_object_initializers = true:error
csharp_space_after_dot = false:error
csharp_space_before_comma = false:error

# CA1304: Specify CultureInfo
dotnet_diagnostic.CA1304.severity = error
Expand Down Expand Up @@ -252,6 +266,9 @@ dotnet_diagnostic.IDE0058.severity = error
# CA2201: Do not raise reserved exception types
dotnet_diagnostic.CA2201.severity = error

# IDE0240: Remove redundant nullable directive
dotnet_diagnostic.IDE0240.severity = error

# IDE0040: Add accessibility modifiers
dotnet_diagnostic.IDE0040.severity = error

Expand Down Expand Up @@ -1006,8 +1023,19 @@ dotnet_diagnostic.IDE0084.severity = error
# IDE0079: Remove unnecessary suppression
dotnet_diagnostic.IDE0079.severity = error

# CA1047: Do not declare protected members in sealed types
dotnet_diagnostic.CA1047.severity = error

# IDE0054: Use compound assignment
dotnet_diagnostic.IDE0054.severity = error

# IDE0161: Use file-scoped namespace - enforces file-scoped namespace style with error severity
# https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-10.0/file-scoped-namespaces
dotnet_diagnostic.IDE0161.severity = error
csharp_style_namespace_declarations = file_scoped:error

# CA1002: Do not expose generic lists
dotnet_diagnostic.CA1002.severity = error

# SYSLIB1054: Use 'LibraryImportAttribute' instead of 'DllImportAttribute' to generate P/Invoke marshalling code at compile time
dotnet_diagnostic.SYSLIB1054.severity = silent

# IDE0240: Remove redundant nullable directive
dotnet_diagnostic.IDE0240.severity = error

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -2,124 +2,123 @@
using System.Globalization;
using System.Management;

namespace HardenWindowsSecurity
namespace HardenWindowsSecurity;

internal partial class BitLocker
{
internal partial class BitLocker
{

/// <summary>
/// Decrypts a BitLocker encrypted drive
/// If the drive is OS drive, it will check if it has auto-unlock keys that belong to other data drives.
/// </summary>
/// <param name="DriveLetter"></param>
internal static void Disable(string DriveLetter)
{
/// <summary>
/// Decrypts a BitLocker encrypted drive
/// If the drive is OS drive, it will check if it has auto-unlock keys that belong to other data drives.
/// </summary>
/// <param name="DriveLetter"></param>
internal static void Disable(string DriveLetter)
{

// Get the volume info based on the drive letter
ManagementObject VolumeInfo = GetVolumeFromLetter(DriveLetter);
// Get the volume info based on the drive letter
ManagementObject VolumeInfo = GetVolumeFromLetter(DriveLetter);

if (HasErrorsOccurred) { return; }
if (HasErrorsOccurred) { return; }

// Get the extended volume info based on the drive letter
BitLockerVolume VolumeInfoExtended = GetEncryptedVolumeInfo(DriveLetter);
// Get the extended volume info based on the drive letter
BitLockerVolume VolumeInfoExtended = GetEncryptedVolumeInfo(DriveLetter);

if (HasErrorsOccurred) { return; }
if (HasErrorsOccurred) { return; }


if (VolumeInfoExtended.ConversionStatus is ConversionStatus.FullyDecrypted)
{
Logger.LogMessage($"The drive {DriveLetter} is already decrypted", LogTypeIntel.InformationInteractionRequired);
return;
}
if (VolumeInfoExtended.ConversionStatus is ConversionStatus.FullyDecrypted)
{
Logger.LogMessage($"The drive {DriveLetter} is already decrypted", LogTypeIntel.InformationInteractionRequired);
return;
}

if (VolumeInfoExtended.ConversionStatus is ConversionStatus.DecryptionInProgress)
{
Logger.LogMessage($"The drive {DriveLetter} is being decrypted, please wait.", LogTypeIntel.InformationInteractionRequired);
return;
}
if (VolumeInfoExtended.ConversionStatus is ConversionStatus.DecryptionInProgress)
{
Logger.LogMessage($"The drive {DriveLetter} is being decrypted, please wait.", LogTypeIntel.InformationInteractionRequired);
return;
}


if (VolumeInfoExtended.VolumeType is VolumeType.OperationSystem)
{
if (VolumeInfoExtended.VolumeType is VolumeType.OperationSystem)
{

Logger.LogMessage($"Operation system drive detected during BitLocker disablement", LogTypeIntel.Information);
Logger.LogMessage($"Operation system drive detected during BitLocker disablement", LogTypeIntel.Information);

Logger.LogMessage("Checking whether The Operation System drive has auto-unlock keys that belong to other data drives.", LogTypeIntel.Information);
Logger.LogMessage("Checking whether The Operation System drive has auto-unlock keys that belong to other data drives.", LogTypeIntel.Information);

// https://learn.microsoft.com/en-us/windows/win32/secprov/isautounlockkeystored-win32-encryptablevolume
// https://learn.microsoft.com/en-us/windows/win32/secprov/isautounlockkeystored-win32-encryptablevolume

// Get the method parameters for IsAutoUnlockKeyStored (even if they are empty)
ManagementBaseObject IsAutoUnlockKeyStoredArgs = VolumeInfo.GetMethodParameters("IsAutoUnlockKeyStored");
// Get the method parameters for IsAutoUnlockKeyStored (even if they are empty)
ManagementBaseObject IsAutoUnlockKeyStoredArgs = VolumeInfo.GetMethodParameters("IsAutoUnlockKeyStored");

// Invoke the method with an empty argument object
ManagementBaseObject IsAutoUnlockKeyStoredMethodInvocationResult = VolumeInfo.InvokeMethod("IsAutoUnlockKeyStored", IsAutoUnlockKeyStoredArgs, null);
// Invoke the method with an empty argument object
ManagementBaseObject IsAutoUnlockKeyStoredMethodInvocationResult = VolumeInfo.InvokeMethod("IsAutoUnlockKeyStored", IsAutoUnlockKeyStoredArgs, null);


#region Output handling
uint? MethodInvocationResultCode = null;
#region Output handling
uint? MethodInvocationResultCode = null;

if (IsAutoUnlockKeyStoredMethodInvocationResult is not null)
{
MethodInvocationResultCode = Convert.ToUInt32(IsAutoUnlockKeyStoredMethodInvocationResult["ReturnValue"], CultureInfo.InvariantCulture);
}
if (IsAutoUnlockKeyStoredMethodInvocationResult is not null)
{
MethodInvocationResultCode = Convert.ToUInt32(IsAutoUnlockKeyStoredMethodInvocationResult["ReturnValue"], CultureInfo.InvariantCulture);
}

if (MethodInvocationResultCode is not null && MethodInvocationResultCode == 0)
{
Logger.LogMessage("Successfully checked the OS Drive for any stored auto-unlock keys.", LogTypeIntel.Information);
// Will move forward to the next step
}
else
{
HResultHelper.HandleHresultAndLog(MethodInvocationResultCode);
return;
}
#endregion
if (MethodInvocationResultCode is not null && MethodInvocationResultCode == 0)
{
Logger.LogMessage("Successfully checked the OS Drive for any stored auto-unlock keys.", LogTypeIntel.Information);
// Will move forward to the next step
}
else
{
HResultHelper.HandleHresultAndLog(MethodInvocationResultCode);
return;
}
#endregion

if (IsAutoUnlockKeyStoredMethodInvocationResult!["IsAutoUnlockKeyStored"] is true)
{
// https://learn.microsoft.com/en-us/windows/win32/secprov/decrypt-win32-encryptablevolume#return-value
HResultHelper.HandleHresultAndLog(2150694953);
return;
}
if (IsAutoUnlockKeyStoredMethodInvocationResult!["IsAutoUnlockKeyStored"] is true)
{
// https://learn.microsoft.com/en-us/windows/win32/secprov/decrypt-win32-encryptablevolume#return-value
HResultHelper.HandleHresultAndLog(2150694953);
return;
}



// Get the volume info based on the drive letter again (Just in case if up to date info is required)
VolumeInfo = GetVolumeFromLetter(DriveLetter);
// Get the volume info based on the drive letter again (Just in case if up to date info is required)
VolumeInfo = GetVolumeFromLetter(DriveLetter);

if (HasErrorsOccurred) { return; }
}
if (HasErrorsOccurred) { return; }
}

// The following sections happen regardless of whether the DriveLetter belongs to an OS Drive or not
// The following sections happen regardless of whether the DriveLetter belongs to an OS Drive or not

// https://learn.microsoft.com/en-us/windows/win32/secprov/decrypt-win32-encryptablevolume
// Get the method parameters for Decrypt (even if they are empty)
ManagementBaseObject DecryptArgs = VolumeInfo.GetMethodParameters("Decrypt");
// https://learn.microsoft.com/en-us/windows/win32/secprov/decrypt-win32-encryptablevolume
// Get the method parameters for Decrypt (even if they are empty)
ManagementBaseObject DecryptArgs = VolumeInfo.GetMethodParameters("Decrypt");

// Invoke the method with an empty argument object
ManagementBaseObject DecryptMethodInvocationResult = VolumeInfo.InvokeMethod("Decrypt", DecryptArgs, null);
// Invoke the method with an empty argument object
ManagementBaseObject DecryptMethodInvocationResult = VolumeInfo.InvokeMethod("Decrypt", DecryptArgs, null);


#region Output handling
uint? DecryptMethodInvocationResultCode = null;
#region Output handling
uint? DecryptMethodInvocationResultCode = null;

if (DecryptMethodInvocationResult is not null)
{
DecryptMethodInvocationResultCode = Convert.ToUInt32(DecryptMethodInvocationResult["ReturnValue"], CultureInfo.InvariantCulture);
}
if (DecryptMethodInvocationResult is not null)
{
DecryptMethodInvocationResultCode = Convert.ToUInt32(DecryptMethodInvocationResult["ReturnValue"], CultureInfo.InvariantCulture);
}

if (DecryptMethodInvocationResultCode is not null && DecryptMethodInvocationResultCode == 0)
{
Logger.LogMessage($"Successfully Decrypted the drive {DriveLetter}", LogTypeIntel.InformationInteractionRequired);
}
else
{
HResultHelper.HandleHresultAndLog(DecryptMethodInvocationResultCode);
return;
}
#endregion
if (DecryptMethodInvocationResultCode is not null && DecryptMethodInvocationResultCode == 0)
{
Logger.LogMessage($"Successfully Decrypted the drive {DriveLetter}", LogTypeIntel.InformationInteractionRequired);
}
else
{
HResultHelper.HandleHresultAndLog(DecryptMethodInvocationResultCode);
return;
}
#endregion

}
}

}
}
Loading
Loading