Skip to content

Commit

Permalink
Significantly more conversion to C#
Browse files Browse the repository at this point in the history
Significantly more conversion to C#
  • Loading branch information
HotCakeX committed Oct 1, 2024
1 parent e98eda4 commit 611e536
Show file tree
Hide file tree
Showing 76 changed files with 4,568 additions and 1,740 deletions.
6 changes: 2 additions & 4 deletions WDACConfig/Program.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@

#pragma warning disable

namespace WDACConfig
{
public class Program
{
public static void Main()
{
// Some test
// List<WDACConfig.AllCertificatesGrabber.AllFileSigners> Certificates = WDACConfig.AllCertificatesGrabber.WinTrust.GetAllFileSigners(@"");

}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#nullable enable

#pragma warning disable CA2000

// The following functions and methods use the Windows APIs to grab all of the certificates from a signed file

namespace WDACConfig
Expand Down Expand Up @@ -205,7 +207,7 @@ public static List<AllFileSigners> GetAllFileSigners(string FilePath)
// Call WinVerifyTrust to verify trust on the file
WinTrust.WinVerifyTrustResult verifyTrustResult = WinTrust.WinVerifyTrust(
IntPtr.Zero,
WinTrust.GenericWinTrustVerifyActionGuid,
WinTrust.GenericWinTrustVerifyActionGuid,
winTrustDataPointer
);

Expand Down Expand Up @@ -279,14 +281,13 @@ public static List<AllFileSigners> GetAllFileSigners(string FilePath)
signerCertificate.Decode(numArray);

// Initialize X509Chain object based on signer's certificate chain context
X509Chain certificateChain;

// Check if csSigners is less than or equal to 0
if (providerData.csSigners <= 0U)
{
// If csSigners is 0 or negative, create a new X509Chain without parameters
using X509Chain certificateChain = new();

// Add signer's certificate and certificate chain to AllFileSigners list
AllFileSigners.Add(new AllFileSigners(signerCertificate, certificateChain));
certificateChain = new X509Chain();
}
else
{
Expand All @@ -295,20 +296,17 @@ public static List<AllFileSigners> GetAllFileSigners(string FilePath)
CryptProviderSigner signer = Marshal.PtrToStructure<CryptProviderSigner>(providerData.pasSigners);

// Initialize X509Chain with the pChainContext from the signer structure
using X509Chain certificateChain = new(signer.pChainContext);

// Add signer's certificate and certificate chain to AllFileSigners list
AllFileSigners.Add(new AllFileSigners(signerCertificate, certificateChain));
certificateChain = new X509Chain(signer.pChainContext);
}

// Add signer's certificate and certificate chain to AllFileSigners list
AllFileSigners.Add(new AllFileSigners(signerCertificate, certificateChain));
}
}
}
}
finally
{

#pragma warning disable CA1508 // Avoid dead conditional code

if (TrustedData != null)
{
// Set StateAction to close the WinTrustData structure
Expand All @@ -319,8 +317,6 @@ public static List<AllFileSigners> GetAllFileSigners(string FilePath)
_ = WinTrust.WinVerifyTrust(IntPtr.Zero, WinTrust.GenericWinTrustVerifyActionGuid, winTrustDataPointer);
}

#pragma warning restore CA1508 // Avoid dead conditional code

// Free memory allocated to winTrustDataPointer
Marshal.FreeHGlobal(winTrustDataPointer);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Collections.Generic;
using System.Xml;

#nullable enable

namespace WDACConfig
{

public class CheckPolicyDeploymentStatus
{

/// <summary>
/// Check if a policy is deployed on the system
/// </summary>
/// <param name="policyXMLFile"></param>
/// <returns></returns>
public static bool IsDeployed(string policyXMLFile)
{

// Create a new HashSet with case-insensitive string comparison
var currentPolicyIDs = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);

// Get all of the deployed policies on the system
var policies = CiToolHelper.GetPolicies(false, true, true);

// Loop through each policy and add its ID to the HashSet
foreach (WDACConfig.CiPolicyInfo item in policies)
{
_ = currentPolicyIDs.Add(item.PolicyID!);
}

// Load XML document
XmlDocument xmlDoc = new();
xmlDoc.Load(policyXMLFile);

// Create namespace manager and add the default namespace with a prefix
XmlNamespaceManager namespaceManager = new(xmlDoc.NameTable);
namespaceManager.AddNamespace("ns", "urn:schemas-microsoft-com:sipolicy");

// Retrieve BasePolicyID and PolicyID
// XmlNode? basePolicyNode = xmlDoc.SelectSingleNode("//ns:BasePolicyID", namespaceManager);
XmlNode? policyNode = xmlDoc.SelectSingleNode("//ns:PolicyID", namespaceManager);

if (policyNode is not null)
{
// string basePolicyID = basePolicyNode.InnerText;

string policyID = policyNode.InnerText;

// Make sure the ID is in correct comparable format
policyID = policyID.Trim('"', '"');
policyID = policyID.Trim('{', '}');
policyID = policyID.Trim('"', '"');
policyID = $"{policyID}";
policyID = policyID.ToLowerInvariant();

// If the PolicyID of the currently selected XML is in the HashSet of the deployed policy IDs, then it is deployed
return currentPolicyIDs.Contains(policyID);
}
else
{
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;

#nullable enable

Expand All @@ -24,22 +22,11 @@ public static class DirectorySelector

do
{
using FolderBrowserDialog dialog = new();
dialog.Description = "To stop selecting directories, press ESC or select Cancel.";
dialog.ShowNewFolderButton = false;
dialog.RootFolder = Environment.SpecialFolder.MyComputer;
string? SelectedFolderPath = FileSystemPicker.ShowDirectoryPicker("Select a folder | To stop selecting folders, press ESC or select Cancel.");

// Use ShowDialog and set top most by using Win32 API
// This method is much better than the ShowDialog overload that takes a parent form
// This makes the opened File/Folder picker top most without the ability to go behind the window that initiated it
// Which is the experience that other native Windows applications have
// Also after picking a directory, the next time the picker GUI opens up will be in the same directory as the last time instead of opening at C drive or some other default location
IntPtr hwnd = GetForegroundWindow();
DialogResult result = dialog.ShowDialog(new WindowWrapper(hwnd));

if (result == DialogResult.OK)
if (SelectedFolderPath is not null)
{
_ = programsPaths.Add(new DirectoryInfo(dialog.SelectedPath));
_ = programsPaths.Add(new DirectoryInfo(SelectedFolderPath));
}
else
{
Expand Down Expand Up @@ -78,21 +65,5 @@ public int GetHashCode(DirectoryInfo obj)
return StringComparer.OrdinalIgnoreCase.GetHashCode(obj.FullName);
}
}


// P/Invoke declarations
[DllImport("user32.dll")]
// Get the handle of the foreground window
// https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getforegroundwindow
private static extern IntPtr GetForegroundWindow();

// Wrapper class to satisfy IWin32Window interface
public class WindowWrapper(IntPtr handle) : IWin32Window
{
private IntPtr _hwnd = handle;

// Property to satisfy IWin32Window interface
public IntPtr Handle => _hwnd;
}
}
}

This file was deleted.

Loading

0 comments on commit 611e536

Please sign in to comment.