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

Code refactor and improved resource file usage #601

Merged
merged 3 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion AppControl Manager/AppControl Manager.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
<PackageReference Include="CommunityToolkit.WinUI.UI.Controls.DataGrid" Version="7.1.2" />
<PackageReference Include="Microsoft.Graphics.Win2D" Version="1.3.2" />
<PackageReference Include="Microsoft.Identity.Client" Version="4.68.0" />
<PackageReference Include="Microsoft.IdentityModel.Abstractions" Version="8.4.0" />
<PackageReference Include="Microsoft.IdentityModel.Abstractions" Version="8.5.0" />
<PackageReference Include="Microsoft.Web.WebView2" Version="1.0.3065.39" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.183">
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ private async void VerifyButton_Click(object sender, RoutedEventArgs e)
await Task.Run(() =>
{
// Instantiate the selected XML policy file
policyObject = SiPolicy.Management.Initialize(policyPathFromUI);
policyObject = SiPolicy.Management.Initialize(policyPathFromUI, null);

// See if the deployed base policy IDs contain the ID of the policy being removed
// Only checking among base policies because supplemental policies can be removed normally whether they're signed or not
Expand Down
13 changes: 2 additions & 11 deletions AppControl Manager/IntelGathering/PrepareEmptyPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,21 @@
using System.IO;
using AppControlManager.Others;
using AppControlManager.XMLOps;

namespace AppControlManager.IntelGathering;

public static class PrepareEmptyPolicy
{

/// <summary>
/// Copies one of the template Code Integrity policies to the directory it receives, empties it and returns its path
/// Copies the empty policy in app resources to the defined directory and returns its new path
/// </summary>
/// <param name="directory"></param>
/// <returns></returns>
public static string Prepare(string directory)
{

string pathToReturn = Path.Combine(directory, "EmptyPolicyFile.xml");

Logger.Write("Copying the template policy to the staging area");

File.Copy(@"C:\Windows\schemas\CodeIntegrity\ExamplePolicies\AllowAll.xml", pathToReturn, true);

Logger.Write("Emptying the policy file in preparation for the new data insertion");
ClearCiPolicySemantic.Clear(pathToReturn);
File.Copy(GlobalVars.EmptyPolicyPath, pathToReturn, true);

return pathToReturn;

}
}
17 changes: 8 additions & 9 deletions AppControl Manager/Main/BasePolicyCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,15 @@ internal static void GetDriversBlockRules(string StagingArea)
driverBlockRulesXML.LoadXml(xmlContent);

// Instantiate the policy
CodeIntegrityPolicy codeIntegrityPolicy = new(null, driverBlockRulesXML);
SiPolicy.SiPolicy policyObj = SiPolicy.Management.Initialize(null, driverBlockRulesXML);

// Generate the path for the XML file
string xmlPath = Path.Combine(StagingArea, $"{name}.xml");

// Save the XML content to a file
CodeIntegrityPolicy.Save(codeIntegrityPolicy.XmlDocument, xmlPath);
SiPolicy.Management.SavePolicyToFile(policyObj, xmlPath);

CiRuleOptions.Set(filePath: xmlPath, rulesToRemove: [CiRuleOptions.PolicyRuleOptions.EnabledAuditMode]);
CiRuleOptions.Set(filePath: xmlPath, rulesToRemove: [SiPolicy.OptionType.EnabledAuditMode]);

// The final path where the XML policy file will be located
string savePathLocation = Path.Combine(GlobalVars.UserConfigDir, $"{name}.xml");
Expand Down Expand Up @@ -580,22 +580,21 @@ internal static void GetBlockRules(string StagingArea, bool deploy)
throw new InvalidOperationException("No XML content found on the Microsoft GitHub source for Microsoft Recommended User Mode Block Rules.");
}


// Load the XML content into an XmlDocument
XmlDocument userModeBlockRulesXML = new();
userModeBlockRulesXML.LoadXml(xmlContent);

// Instantiate the policy
CodeIntegrityPolicy codeIntegrityPolicy = new(null, userModeBlockRulesXML);
SiPolicy.SiPolicy policyObj = SiPolicy.Management.Initialize(null, userModeBlockRulesXML);

// Paths only used during staging area processing
string tempPolicyPath = Path.Combine(StagingArea, $"{policyName}.xml");
string tempPolicyCIPPath = Path.Combine(StagingArea, $"{policyName}.cip");

// Save the XML content to a file
CodeIntegrityPolicy.Save(codeIntegrityPolicy.XmlDocument, tempPolicyPath);
SiPolicy.Management.SavePolicyToFile(policyObj, tempPolicyPath);

CiRuleOptions.Set(filePath: tempPolicyPath, rulesToAdd: [CiRuleOptions.PolicyRuleOptions.EnabledUpdatePolicyNoReboot, CiRuleOptions.PolicyRuleOptions.DisabledScriptEnforcement], rulesToRemove: [CiRuleOptions.PolicyRuleOptions.EnabledAuditMode, CiRuleOptions.PolicyRuleOptions.EnabledAdvancedBootOptionsMenu]);
CiRuleOptions.Set(filePath: tempPolicyPath, rulesToAdd: [SiPolicy.OptionType.EnabledUpdatePolicyNoReboot, SiPolicy.OptionType.DisabledScriptEnforcement], rulesToRemove: [SiPolicy.OptionType.EnabledAuditMode, SiPolicy.OptionType.EnabledAdvancedBootOptionsMenu]);

Logger.Write("Assigning policy name and resetting policy ID");

Expand All @@ -621,7 +620,7 @@ internal static void GetBlockRules(string StagingArea, bool deploy)
Logger.Write($"{policyName} policy is already deployed, updating it using the same GUID which is {CurrentlyDeployedBlockRulesGUID}.");

// Swap the policyID in the current policy XML file with the one from the deployed policy
PolicyEditor.EditGuids(CurrentlyDeployedBlockRulesGUID, new FileInfo(tempPolicyPath));
PolicyEditor.EditGuids(CurrentlyDeployedBlockRulesGUID, tempPolicyPath);
}
else
{
Expand Down Expand Up @@ -761,7 +760,7 @@ internal static void BuildStrictKernelMode(string StagingArea, bool IsAudit, boo
if (IsAudit)
{
// Add the audit mode rule option to the policy
CiRuleOptions.Set(filePath: policyPath, rulesToAdd: [CiRuleOptions.PolicyRuleOptions.EnabledAuditMode]);
CiRuleOptions.Set(filePath: policyPath, rulesToAdd: [SiPolicy.OptionType.EnabledAuditMode]);
}

string policyID;
Expand Down
Loading