Repository navigation
Skip the AppLocker policy test files when no script policy is configured #28374
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
Open
Alexandre Zollinger Chohfi (azchohfi)
wants to merge
3
commits into
PowerShell:master
Choose a base branch
from
azchohfi:skip-applocker-probe-without-policy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT License. | ||
|
|
||
| Describe "AppLocker policy test files at startup" -Tags 'CI' { | ||
| BeforeAll { | ||
| $skipTest = -not $IsWindows | ||
| if (-not $skipTest) { | ||
| Import-Module HelpersCommon | ||
|
|
||
| $systemPolicy = [System.Management.Automation.Security.SystemPolicy] | ||
| $flags = [System.Reflection.BindingFlags]'NonPublic, Static' | ||
| $hasScriptPolicy = $systemPolicy.GetMethod('HasAppLockerOrSaferScriptPolicy', $flags).Invoke($null, $null) | ||
|
|
||
| # When WDAC enforces lockdown, the AppLocker check is never reached. | ||
| $null = $systemPolicy::GetSystemLockdownPolicy() | ||
| $wldpPolicy = $systemPolicy.GetField('s_cachedWldpSystemPolicy', $flags).GetValue($null) | ||
| $expectTestFiles = $hasScriptPolicy -and ($wldpPolicy -ne [System.Management.Automation.Security.SystemEnforcementMode]::Enforce) | ||
| } | ||
| } | ||
|
|
||
| It "Writes __PSScriptPolicyTest files only when a script policy may exist" -Skip:$skipTest { | ||
| $tempDir = Join-Path $TestDrive 'temp' | ||
| $null = New-Item -ItemType Directory -Path $tempDir | ||
| $watcher = [System.IO.FileSystemWatcher]::new($tempDir, '__PSScriptPolicyTest_*') | ||
| $sourceId = 'AppLockerProbe_' + [guid]::NewGuid() | ||
| $null = Register-ObjectEvent -InputObject $watcher -EventName Created -SourceIdentifier $sourceId | ||
| $watcher.EnableRaisingEvents = $true | ||
|
|
||
| $oldTemp = $env:TEMP | ||
| $oldTmp = $env:TMP | ||
| try { | ||
| $env:TEMP = $tempDir | ||
| $env:TMP = $tempDir | ||
| Start-NativeExecution { & "$PSHOME/pwsh" -NoProfile -NonInteractive -Command exit } | ||
| Start-Sleep -Milliseconds 500 | ||
| $created = @(Get-Event -SourceIdentifier $sourceId -ErrorAction Ignore) | ||
| } | ||
| finally { | ||
| $env:TEMP = $oldTemp | ||
| $env:TMP = $oldTmp | ||
| Unregister-Event -SourceIdentifier $sourceId | ||
| Remove-Event -SourceIdentifier $sourceId -ErrorAction Ignore | ||
| $watcher.Dispose() | ||
| } | ||
|
|
||
| if ($expectTestFiles) { | ||
| $created.Count | Should -BeGreaterThan 0 | ||
| } | ||
| else { | ||
| $created.Count | Should -Be 0 | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| #if !UNIX | ||
| using System; | ||
| using System.IO; | ||
| using System.Management.Automation.Security; | ||
| using System.Runtime.Versioning; | ||
| using Microsoft.Win32; | ||
| using Xunit; | ||
|
|
||
| namespace PSTests.Parallel | ||
| { | ||
| [SupportedOSPlatform("windows")] | ||
| public static class SystemPolicyTests | ||
| { | ||
| [Fact] | ||
| public static void TestIsPolicyKeyEmpty() | ||
| { | ||
| string testKeyPath = @"Software\PowerShellTests\SystemPolicy_" + Guid.NewGuid().ToString("N"); | ||
| string codeIdentifiers = testKeyPath + @"\CodeIdentifiers"; | ||
| try | ||
| { | ||
| // Key does not exist. | ||
| Assert.True(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, "AuthenticodeEnabled")); | ||
|
|
||
| using (RegistryKey key = Registry.CurrentUser.CreateSubKey(codeIdentifiers)) | ||
| { | ||
| // Key exists without values or subkeys. | ||
| Assert.True(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, "AuthenticodeEnabled")); | ||
|
|
||
| // Only the ignored value. | ||
| key.SetValue("authenticodeenabled", 0, RegistryValueKind.DWord); | ||
| Assert.True(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, "AuthenticodeEnabled")); | ||
| Assert.False(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, ignoredValueName: null)); | ||
|
|
||
| // Any other value means a policy is configured. | ||
| key.SetValue("DefaultLevel", 0x40000, RegistryValueKind.DWord); | ||
| Assert.False(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, "AuthenticodeEnabled")); | ||
| key.DeleteValue("DefaultLevel"); | ||
|
|
||
| // So does any subkey (rules). | ||
| key.CreateSubKey(@"262144\Paths").Dispose(); | ||
| Assert.False(SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, codeIdentifiers, "AuthenticodeEnabled")); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| Registry.CurrentUser.DeleteSubKeyTree(testKeyPath, throwOnMissingSubKey: false); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void TestPolicyFileMayExist() | ||
| { | ||
| string directory = Path.Combine(Path.GetTempPath(), "SystemPolicy_" + Guid.NewGuid().ToString("N")); | ||
| string file = Path.Combine(directory, "Script.AppLocker"); | ||
|
|
||
| // Directory does not exist. | ||
| Assert.False(SystemPolicy.PolicyFileMayExist(file)); | ||
|
|
||
| Directory.CreateDirectory(directory); | ||
| try | ||
| { | ||
| Assert.False(SystemPolicy.PolicyFileMayExist(file)); | ||
|
|
||
| File.WriteAllText(file, string.Empty); | ||
| Assert.True(SystemPolicy.PolicyFileMayExist(file)); | ||
| } | ||
| finally | ||
| { | ||
| Directory.Delete(directory, recursive: true); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void TestRegistryKeyMayExist() | ||
| { | ||
| string testKeyPath = @"Software\PowerShellTests\SystemPolicy_" + Guid.NewGuid().ToString("N"); | ||
| string script = testKeyPath + @"\SrpV2\Script"; | ||
| try | ||
| { | ||
| Assert.False(SystemPolicy.RegistryKeyMayExist(Registry.CurrentUser, script)); | ||
|
|
||
| // An existing AppLocker key counts even when it is empty. | ||
| Registry.CurrentUser.CreateSubKey(script).Dispose(); | ||
| Assert.True(SystemPolicy.RegistryKeyMayExist(Registry.CurrentUser, script)); | ||
| } | ||
| finally | ||
| { | ||
| Registry.CurrentUser.DeleteSubKeyTree(testKeyPath, throwOnMissingSubKey: false); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public static void TestHasAppLockerOrSaferScriptPolicyMatchesMachine() | ||
| { | ||
| bool expected = (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess) | ||
| || SystemPolicy.PolicyFileMayExist(Path.Combine(Environment.SystemDirectory, "AppLocker", "Script.AppLocker")) | ||
| || SystemPolicy.RegistryKeyMayExist(Registry.LocalMachine, @"SOFTWARE\Policies\Microsoft\Windows\SrpV2\Script") | ||
| || !SystemPolicy.IsPolicyKeyEmpty(Registry.LocalMachine, @"SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers", "AuthenticodeEnabled") | ||
| || !SystemPolicy.IsPolicyKeyEmpty(Registry.CurrentUser, @"SOFTWARE\Policies\Microsoft\Windows\Safer\CodeIdentifiers", "AuthenticodeEnabled"); | ||
|
|
||
| Assert.Equal(expected, SystemPolicy.HasAppLockerOrSaferScriptPolicy()); | ||
| } | ||
| } | ||
| } | ||
| #endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.