-
Notifications
You must be signed in to change notification settings - Fork 34
/
BuildAndDeployPolicies.psm1
674 lines (495 loc) · 29.5 KB
/
BuildAndDeployPolicies.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
function ConvertTo-WDACPolicyRuleValue {
<#
.SYNOPSIS
A helper function to convert Windows Defender Application Control (WDAC) policy rule options to numeric values which are required for the Set-RuleOption cmdlet.
.DESCRIPTION
ConvertTo-WDACPolicyRuleValue converts human-readable WDAC policy rule options to numeric values for use by the Set-RuleOption cmdlet. Due to the poor design of Set-RuleOption, you cannot supply these string values and must supply integeter values which is not intuitive or user-friendly. This function makes calling Set-RuleOption more intuitive.
This function is not designed to be exposed to users. Functions with parameters that support policy rules options should support tab-completion for the human-readble values.
Author: Matthew Graeber
.PARAMETER PolicyOptionStrings
Specifies an array of human-readble policy rule options that want to be set.
.EXAMPLE
ConvertTo-WDACPolicyRuleValue -PolicyOptionStrings 'Enabled:UMCI', 'Enabled:Boot Menu Protection', 'Enabled:Audit Mode'
Returns an array of integer values representing the requested policy rule options. These values can then be more easily supplied to a subsequent call to Set-RuleOption.
#>
[OutputType([Int[]])]
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0)]
[ValidateSet('Enabled:UMCI',
'Enabled:Boot Menu Protection',
'Required:WHQL',
'Enabled:Audit Mode',
'Disabled:Flight Signing',
'Enabled:Inherit Default Policy',
'Enabled:Unsigned System Integrity Policy',
'Allowed:Debug Policy Augmented',
'Required:EV Signers',
'Enabled:Advanced Boot Options Menu',
'Enabled:Boot Audit On Failure',
'Disabled:Script Enforcement',
'Required:Enforce Store Applications',
'Enabled:Managed Installer',
'Enabled:Intelligent Security Graph Authorization',
'Enabled:Invalidate EAs on Reboot',
'Enabled:Update Policy No Reboot',
'Enabled:Allow Supplemental Policies',
'Disabled:Runtime FilePath Rule Protection',
'Enabled:Dynamic Code Security',
'Enabled:Revoked Expired As Unsigned')]
[String[]] $PolicyOptionStrings
)
$RuleOptionMapping = @{
'Enabled:UMCI' = 0
'Enabled:Boot Menu Protection' = 1
'Required:WHQL' = 2
'Enabled:Audit Mode' = 3
'Disabled:Flight Signing' = 4
'Enabled:Inherit Default Policy' = 5
'Enabled:Unsigned System Integrity Policy' = 6
'Allowed:Debug Policy Augmented' = 7
'Required:EV Signers' = 8
'Enabled:Advanced Boot Options Menu' = 9
'Enabled:Boot Audit On Failure' = 10
'Disabled:Script Enforcement' = 11
'Required:Enforce Store Applications' = 12
'Enabled:Managed Installer' = 13
'Enabled:Intelligent Security Graph Authorization' = 14
'Enabled:Invalidate EAs on Reboot' = 15
'Enabled:Update Policy No Reboot' = 16
'Enabled:Allow Supplemental Policies' = 17
'Disabled:Runtime FilePath Rule Protection' = 18
'Enabled:Dynamic Code Security' = 19
'Enabled:Revoked Expired As Unsigned' = 20
}
foreach ($PolicyOptionString in $PolicyOptionStrings) {
$RuleOptionMapping[$PolicyOptionString]
}
}
function New-WDACPolicyConfiguration {
<#
.SYNOPSIS
A helper function used to specify a code integrity policy configuration.
.DESCRIPTION
New-WDACPolicyConfiguration is used as a helper function to generate code integrity policy configuration options and to supply them to the Invoke-WDACCodeIntegrityPolicyBuild function.
The objects output by New-WDACPolicyConfiguration are intended to be supplied as arguments to the following parameters in Invoke-WDACCodeIntegrityPolicyBuild: -BasePolicyConfiguration, -SupplementalPolicyConfiguration, -MergedPolicyConfiguration
Author: Matthew Graeber
.PARAMETER BasePolicy
Specifies that a base policy is to be configured and built. Base policies must reside in the "BasePolicies" directory.
When this switch is specified, the object output is supplied to the -BasePolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
.PARAMETER SupplementalPolicy
Specifies that a supplemental policy is to be configured and built. Supplemental policies must reside in the "SupplementalPolicies" directory.
When this switch is specified, the object output is supplied to the -SupplementalPolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
.PARAMETER MergedPolicy
Specifies that a supplemental policy is to be configured and built by merging multiple policies together. Supplemental policies designed for merging must reside in the "AppSpecificPolicies" directory.
When this switch is specified, the object output is supplied to the -MergedPolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
.PARAMETER FileName
Specifies the filename of the XML policy file. This parameter only applies when the -BasePolicy or -SupplementalPolicy switches are supplied.
.PARAMETER PolicyName
Specifies the name of the policy. Upon deployment of your policy, this policy name will be surfaced in the event log.
.PARAMETER PolicyRuleOptions
Specifies the policy rule options that you want to supply to the specified policy. This parameter supports tab-completion.
.PARAMETER BasePolicyToSupplement
Specifies the base policy that the merged, application-specific policy is a supplement to. This parameter is mandatory when -MergedPolicy is specified.
.EXAMPLE
$BaseDriverPolicy = New-WDACPolicyConfiguration -BasePolicy -FileName 'BaseDriverPolicy.xml' -PolicyName 'BaseDriverRuleset' -PolicyRuleOptions 'Enabled:Audit Mode'
Specifies configuration options for a base policy. The object output would then be supplied to the -BasePolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
.EXAMPLE
$SupplementalSurfacePolicy = New-WDACPolicyConfiguration -SupplementalPolicy -FileName 'MicrosoftSurfaceDriverPolicy.xml' -PolicyName '3rdPartyDriverRuleset' -PolicyRuleOptions 'Enabled:Audit Mode'
Specifies configuration options for a supplemental policy. The object output would then be supplied to the -SupplementalPolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
.EXAMPLE
$MergedPolicyConfiguration = New-WDACPolicyConfiguration -MergedPolicy -PolicyName 'Merged3rdPartySoftwareRuleset' -BasePolicyToSupplement 'BaseUserModeRuleset' -PolicyRuleOptions 'Enabled:Audit Mode'
Specifies configuration options for a merged supplemental policy (i.e. the policies that reside in the "AppSpecificPolicies" directory). The object output would then be supplied to the -MergedPolicyConfiguration parameter in Invoke-WDACCodeIntegrityPolicyBuild.
#>
[CmdletBinding(DefaultParameterSetName = 'Base')]
param (
[Parameter(Mandatory, ParameterSetName = 'Base')]
[Switch]
$BasePolicy,
[Parameter(Mandatory, ParameterSetName = 'Supplemental')]
[Switch]
$SupplementalPolicy,
[Parameter(Mandatory, ParameterSetName = 'Merged')]
[Switch]
$MergedPolicy,
[Parameter(Mandatory, ParameterSetName = 'Merged')]
[String]
[ValidateNotNullOrEmpty()]
$BasePolicyToSupplement,
[Parameter(Mandatory, ParameterSetName = 'Base')]
[Parameter(Mandatory, ParameterSetName = 'Supplemental')]
[String]
$FileName,
[Parameter(Mandatory)]
[String]
$PolicyName,
[String[]]
[ValidateSet('Enabled:UMCI',
'Enabled:Boot Menu Protection',
'Required:WHQL',
'Enabled:Audit Mode',
'Disabled:Flight Signing',
'Enabled:Inherit Default Policy',
'Enabled:Unsigned System Integrity Policy',
'Allowed:Debug Policy Augmented',
'Required:EV Signers',
'Enabled:Advanced Boot Options Menu',
'Enabled:Boot Audit On Failure',
'Disabled:Script Enforcement',
'Required:Enforce Store Applications',
'Enabled:Managed Installer',
'Enabled:Intelligent Security Graph Authorization',
'Enabled:Invalidate EAs on Reboot',
'Enabled:Update Policy No Reboot',
'Enabled:Allow Supplemental Policies',
'Disabled:Runtime FilePath Rule Protection',
'Enabled:Dynamic Code Security',
'Enabled:Revoked Expired As Unsigned')]
$PolicyRuleOptions
)
if ($BasePolicy) {
$ConfigurationProperties = [Ordered] @{
PSTypeName = 'WDACBasePolicyConfiguration'
FileName = $FileName
PolicyName = $PolicyName
PolicyRuleOptions = $PolicyRuleOptions
}
}
if ($SupplementalPolicy) {
$ConfigurationProperties = [Ordered] @{
PSTypeName = 'WDACSupplementalPolicyConfiguration'
FileName = $FileName
PolicyName = $PolicyName
PolicyRuleOptions = $PolicyRuleOptions
}
}
if ($MergedPolicy) {
$ConfigurationProperties = [Ordered] @{
PSTypeName = 'WDACMergedPolicyConfiguration'
PolicyName = $PolicyName
PolicyRuleOptions = $PolicyRuleOptions
BasePolicyToSupplement = $BasePolicyToSupplement
}
}
New-Object -TypeName PSObject -Property $ConfigurationProperties
}
function Invoke-WDACCodeIntegrityPolicyBuild {
<#
.SYNOPSIS
Facilitates building and deploying multiple base and supplemental code integrity policies.
.DESCRIPTION
Invoke-WDACCodeIntegrityPolicyBuild builds and, optionally, deploys and refreshes code integrity policies locally.
Author: Matthew Graeber
.PARAMETER CommonBasePolicyRuleOptions
Specifies a set of policy rule options to apply to all generated policy files. This parameter was designed to facilitate consistency
.PARAMETER BasePolicyConfiguration
Specifies one or more base policy configurations that were generated by New-WDACPolicyConfiguration.
.PARAMETER SupplementalPolicyConfiguration
Specifies one or more supplemental policy configurations that were generated by New-WDACPolicyConfiguration.
.PARAMETER MergedPolicyConfiguration
Specifies a merged supplemental policy configuration that was generated by New-WDACPolicyConfiguration.
.PARAMETER ArtifactPath
By default, generated artifacts (code integrity policy XML and binary code integrity policy .cip files) are written to the "BuildArtifacts" directory. This parameter allows you to specify an alternate build artifact directory. The directory must already exist.
.PARAMETER Deploy
Copies generated binary policy files to %windir%\System32\CodeIntegrity\CiPolicies\Active. If this option is selected, the policy won't be updated until the next reboot.
.PARAMETER DeployAndUpdate
Copies generated binary policy files to %windir%\System32\CodeIntegrity\CiPolicies\Active and refreshes the rules so that policy changes take effect immediately.
.EXAMPLE
$CommonBasePolicyRuleOptions = @(
'Enabled:Unsigned System Integrity Policy',
'Enabled:Advanced Boot Options Menu',
'Enabled:Update Policy No Reboot',
'Enabled:Allow Supplemental Policies',
'Disabled:Flight Signing',
'Required:WHQL',
'Enabled:Boot Audit On Failure'
)
$BasePolicyConfigurations = @(
(New-WDACPolicyConfiguration -BasePolicy -FileName 'BaseDriverPolicy.xml' -PolicyName 'BaseDriverRuleset' -PolicyRuleOptions 'Enabled:Audit Mode'),
(New-WDACPolicyConfiguration -BasePolicy -FileName 'BaseUserPolicy.xml' -PolicyName 'BaseUserModeRuleset' -PolicyRuleOptions 'Disabled:Script Enforcement', 'Enabled:UMCI', 'Enabled:Audit Mode'),
(New-WDACPolicyConfiguration -BasePolicy -FileName 'MicrosoftRecommendedBlockRules.xml' -PolicyName 'MicrosoftRecommendedBlockRuleset' -PolicyRuleOptions 'Disabled:Script Enforcement', 'Enabled:UMCI', 'Enabled:Audit Mode')
)
$SupplementalPolicyConfigurations = @(
(New-WDACPolicyConfiguration -SupplementalPolicy -FileName 'MicrosoftSurfaceDriverPolicy.xml' -PolicyName '3rdPartyDriverRuleset' -PolicyRuleOptions 'Enabled:Audit Mode')
)
$MergedPolicyConfiguration = New-WDACPolicyConfiguration -MergedPolicy -PolicyName 'Merged3rdPartySoftwareRuleset' -BasePolicyToSupplement 'BaseUserModeRuleset' -PolicyRuleOptions 'Enabled:Audit Mode'
$CodeIntegrityPoliciesArgs = @{
CommonBasePolicyRuleOptions = $CommonBasePolicyRuleOptions
BasePolicyConfiguration = $BasePolicyConfigurations
SupplementalPolicyConfiguration = $SupplementalPolicyConfigurations
MergedPolicyConfiguration = $MergedPolicyConfiguration
}
Invoke-WDACCodeIntegrityPolicyBuild @CodeIntegrityPoliciesArgs
This code specifies several policy configurations, converts them to binary form, and saves the resulting binary policy files to the "BuildArtifacts" directory.
#>
[CmdletBinding(DefaultParameterSetName = 'Deploy')]
param (
[Parameter()]
[ValidateSet('Enabled:UMCI',
'Enabled:Boot Menu Protection',
'Required:WHQL',
'Enabled:Audit Mode',
'Disabled:Flight Signing',
'Enabled:Inherit Default Policy',
'Enabled:Unsigned System Integrity Policy',
'Allowed:Debug Policy Augmented',
'Required:EV Signers',
'Enabled:Advanced Boot Options Menu',
'Enabled:Boot Audit On Failure',
'Disabled:Script Enforcement',
'Required:Enforce Store Applications',
'Enabled:Managed Installer',
'Enabled:Intelligent Security Graph Authorization',
'Enabled:Invalidate EAs on Reboot',
'Enabled:Update Policy No Reboot',
'Enabled:Allow Supplemental Policies',
'Disabled:Runtime FilePath Rule Protection',
'Enabled:Dynamic Code Security',
'Enabled:Revoked Expired As Unsigned')]
[String[]]
$CommonBasePolicyRuleOptions,
[Parameter(Mandatory)]
[PSTypeName('WDACBasePolicyConfiguration')]
[PSObject[]]
$BasePolicyConfiguration,
[PSTypeName('WDACSupplementalPolicyConfiguration')]
[PSObject[]]
$SupplementalPolicyConfiguration,
[PSTypeName('WDACMergedPolicyConfiguration')]
[PSObject]
$MergedPolicyConfiguration,
[String]
[ValidateScript({Test-Path -Path $_ -PathType Container})]
$ArtifactPath,
[Parameter(ParameterSetName = 'Deploy')]
[Switch]
$Deploy,
[Parameter(ParameterSetName = 'DeployAndUpdate')]
[Switch]
$DeployAndUpdate
)
# Policy ID will be set to the day's date.
$DateString = (Get-Date).ToString('MM_dd_yyyy')
$CommonBasePolicyRuleOptionValues = ConvertTo-WDACPolicyRuleValue -PolicyOptionStrings $CommonBasePolicyRuleOptions
$ArtifactBasePath = "$PSScriptRoot\BuildArtifacts"
$MergedPolicy = $null
if ($ArtifactPath) {
$ArtifactBasePath = $ArtifactPath
}
# Configure and build base policies
$BasePolicies = foreach ($BaseConfig in $BasePolicyConfiguration) {
$BasePolicyPath = "$PSScriptRoot\BasePolicies\$($BaseConfig.FileName)"
# Each base template policy will be copied to this location prior to configuration.
$BasePolicyBuildPath = "$ArtifactBasePath\$($BaseConfig.FileName)"
[Xml] $PolicyXml = Get-Content -Path $BasePolicyPath -Raw
$PolicyID = $PolicyXml.SiPolicy.PolicyID
# $BasePolicyID = $PolicyXml.SiPolicy.BasePolicyID
Copy-Item -Path $BasePolicyPath -Destination $BasePolicyBuildPath
if ($CommonBasePolicyRuleOptionValues) {
foreach ($CommonBaseRuleOptionValue in $CommonBasePolicyRuleOptionValues) {
Set-RuleOption -FilePath $BasePolicyBuildPath -Option $CommonBaseRuleOptionValue
}
}
$BaseConfigurationPolicyRuleOptionValues = $null
if ($BaseConfig.PolicyRuleOptions) {
$BaseConfigurationPolicyRuleOptionValues = ConvertTo-WDACPolicyRuleValue -PolicyOptionStrings $BaseConfig.PolicyRuleOptions
}
foreach ($RuleOption in $BaseConfigurationPolicyRuleOptionValues) {
Set-RuleOption -FilePath $BasePolicyBuildPath -Option $RuleOption
}
Set-CIPolicyIdInfo -FilePath $BasePolicyBuildPath -PolicyName $BaseConfig.PolicyName -PolicyId $DateString -WarningAction SilentlyContinue
[Xml] $BuiltPolicyXml = Get-Content -Path $BasePolicyBuildPath -Raw
ConvertFrom-CIPolicy -XmlFilePath $BasePolicyBuildPath -BinaryFilePath "$ArtifactBasePath\$PolicyID.cip" | ForEach-Object {
# ConvertFrom-CIPolicy returns a string consisting of the binary CI policy file. Resolve the path.
$FileInfo = Get-Item -Path $_
$XmlFileInfo = Get-Item -Path $BasePolicyBuildPath
$PolicyProperties = [Ordered] @{
PolicyType = 'Base'
XmlFileInfo = $XmlFileInfo
BinaryFileInfo = $FileInfo
PolicyID = $BuiltPolicyXml.SiPolicy.PolicyID
BasePolicyID = $BuiltPolicyXml.SiPolicy.BasePolicyID
PolicyInfoName = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Name' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
PolicyInfoID = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Id' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
}
New-Object -TypeName PSObject -Property $PolicyProperties
}
}
# Configure and build supplemental policies
if ($SupplementalPolicyConfiguration)
{
$SupplementalPolicies = foreach ($SupplementalConfig in $SupplementalPolicyConfiguration) {
$SupplementalPolicyPath = "$PSScriptRoot\SupplementalPolicies\$($SupplementalConfig.FileName)"
# Each base template policy will be copied to this location prior to configuration.
$SupplementalPolicyBuildPath = "$ArtifactBasePath\$($SupplementalConfig.FileName)"
[Xml] $PolicyXml = Get-Content -Path $SupplementalPolicyPath -Raw
$PolicyID = $PolicyXml.SiPolicy.PolicyID
Copy-Item -Path $SupplementalPolicyPath -Destination $SupplementalPolicyBuildPath
if ($CommonBasePolicyRuleOptions) {
foreach ($CommonBaseRuleOption in $CommonBasePolicyRuleOptionValues) {
Set-RuleOption -FilePath $SupplementalPolicyBuildPath -Option $CommonBaseRuleOption
}
}
$SupplementalConfigurationPolicyRuleOptionValues = $null
if ($SupplementalConfig.PolicyRuleOptions) {
$SupplementalConfigurationPolicyRuleOptionValues = ConvertTo-WDACPolicyRuleValue -PolicyOptionStrings $SupplementalConfig.PolicyRuleOptions
}
foreach ($RuleOption in $SupplementalConfigurationPolicyRuleOptionValues) {
Set-RuleOption -FilePath $SupplementalPolicyBuildPath -Option $RuleOption
}
# Delete the "Enabled:Allow Supplemental Policies" if it was specified in the common policy rule option config.
Set-RuleOption -FilePath $SupplementalPolicyBuildPath -Option 17 -Delete
Set-CIPolicyIdInfo -FilePath $SupplementalPolicyBuildPath -PolicyName $SupplementalConfig.PolicyName -PolicyId $DateString -WarningAction SilentlyContinue
[Xml] $BuiltPolicyXml = Get-Content -Path $SupplementalPolicyBuildPath -Raw
ConvertFrom-CIPolicy -XmlFilePath $SupplementalPolicyBuildPath -BinaryFilePath "$ArtifactBasePath\$PolicyID.cip" | ForEach-Object {
# ConvertFrom-CIPolicy returns a string consisting of the binary CI policy file. Resolve the path.
$FileInfo = Get-Item -Path $_
$XmlFileInfo = Get-Item -Path $SupplementalPolicyBuildPath
$PolicyProperties = [Ordered] @{
PolicyType = 'Supplemental'
XmlFileInfo = $XmlFileInfo
BinaryFileInfo = $FileInfo
PolicyID = $BuiltPolicyXml.SiPolicy.PolicyID
BasePolicyID = $BuiltPolicyXml.SiPolicy.BasePolicyID
PolicyInfoName = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Name' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
PolicyInfoID = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Id' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
}
New-Object -TypeName PSObject -Property $PolicyProperties
}
}
}
# Build the app-specific policy
if ($MergedPolicyConfiguration) {
if (-not ($BasePolicies | Where-Object { $_.PolicyInfoName -eq $MergedPolicyConfiguration.BasePolicyToSupplement })) {
Write-Error "The merged, application-specific supplemental policy is expected to supplement the following base policy that was not supplied: $($MergedPolicyConfiguration.BasePolicyToSupplement)"
return
}
$BasePolicyID = $BasePolicies | Where-Object { $_.PolicyInfoName -eq $MergedPolicyConfiguration.BasePolicyToSupplement } | Select-Object -ExpandProperty BasePolicyId
$CopiedAppTemplateDestination = "$ArtifactBasePath\AppSpecificPolicyTemplate.xml"
# Copy the application-specific template policy to the artifacts directory.
# This is done because the BasePolicyID element is going to be updated in the XML.
# Assign the application-specific supplemental policy base policy ID to the base policy name specified.
# I'd love to use the supported cmdlet for this but I really don't like that you can't avoid
# Having the PolicyID reset.
# Set-CIPolicyIdInfo -FilePath $CopiedAppTemplateDestination -SupplementsBasePolicyID $BasePolicyID
$XmlString = Get-Content -Path "$PSScriptRoot\AppSpecificPolicies\AppSpecificPolicyTemplate.xml" -Raw
$BasePolicyIdToReplace = [Xml] $XmlString | Select-Object -ExpandProperty SiPolicy | Select-Object -ExpandProperty BasePolicyId
$ReplacedXmlString = $XmlString -replace "<BasePolicyID>$BasePolicyIdToReplace</BasePolicyID>", "<BasePolicyID>$BasePolicyID</BasePolicyID>"
Out-File -FilePath $CopiedAppTemplateDestination -InputObject $ReplacedXmlString -Encoding utf8 -ErrorAction Stop
# AppSpecificPolicyTemplate.xml is used for maintaining file rule options.
# Note: AppSpecificPolicyTemplate.xml must be the first policy file specified as this is what Merge-CIPolicy takes policy options from.
$AppSpecificPolicyFiles = New-Object -TypeName String[](0)
$AppSpecificPolicyFiles += $CopiedAppTemplateDestination
Get-ChildItem "$PSScriptRoot\AppSpecificPolicies\*.xml" -Exclude 'AppSpecificPolicyTemplate.xml' |
Select-Object -ExpandProperty FullName |
ForEach-Object { $AppSpecificPolicyFiles += $_ }
$MergedPolicyPath = "$ArtifactBasePath\MergedPolicy.xml"
$null = Merge-CIPolicy -OutputFilePath $MergedPolicyPath -PolicyPaths ([String[]] $AppSpecificPolicyFiles)
[Xml] $PolicyXml = Get-Content -Path $MergedPolicyPath -Raw
$PolicyID = $PolicyXml.SiPolicy.PolicyID
if ($CommonBasePolicyRuleOptions)
{
foreach ($CommonBaseRuleOption in $CommonBasePolicyRuleOptionValues) {
Set-RuleOption -FilePath $MergedPolicyPath -Option $CommonBaseRuleOption
}
}
$MergedConfigurationPolicyRuleOptionValues = $null
if ($MergedPolicyConfiguration.PolicyRuleOptions) {
$MergedConfigurationPolicyRuleOptionValues = ConvertTo-WDACPolicyRuleValue -PolicyOptionStrings $MergedPolicyConfiguration.PolicyRuleOptions
}
foreach ($RuleOption in $MergedConfigurationPolicyRuleOptionValues) {
Set-RuleOption -FilePath $MergedPolicyPath -Option $RuleOption
}
# Delete the "Enabled:Allow Supplemental Policies" if it was specified in the common policy rule option config.
Set-RuleOption -FilePath $MergedPolicyPath -Option 17 -Delete
Set-CIPolicyIdInfo -FilePath $MergedPolicyPath -PolicyName $MergedPolicyConfiguration.PolicyName -PolicyId $DateString -WarningAction SilentlyContinue
[Xml] $BuiltPolicyXml = Get-Content -Path $MergedPolicyPath -Raw
ConvertFrom-CIPolicy -XmlFilePath $MergedPolicyPath -BinaryFilePath "$ArtifactBasePath\$PolicyID.cip" | ForEach-Object {
# ConvertFrom-CIPolicy returns a string consisting of the binary CI policy file. Resolve the path.
$FileInfo = Get-Item -Path $_
$XmlFileInfo = Get-Item -Path $MergedPolicyPath
$PolicyProperties = [Ordered] @{
PolicyType = 'MergedSupplemental'
XmlFileInfo = $XmlFileInfo
BinaryFileInfo = $FileInfo
PolicyID = $BuiltPolicyXml.SiPolicy.PolicyID
BasePolicyID = $BuiltPolicyXml.SiPolicy.BasePolicyID
PolicyInfoName = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Name' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
PolicyInfoID = ($BuiltPolicyXml.SiPolicy.Settings.Setting | Where-Object { $_.ValueName -eq 'Id' } | Select-Object -ExpandProperty Value | Select-Object -ExpandProperty String)
}
$MergedPolicy = New-Object -TypeName PSObject -Property $PolicyProperties
}
}
# Build a list of the generated binary policy files so that only those files are deployed
# when -Deploy or -DeployAndUpdate are specified.
$BinaryPolicyFiles = New-Object -TypeName String[](0)
if ($BasePolicies) {
$BasePolicies
$BasePolicies | ForEach-Object { $BinaryPolicyFiles += $_.BinaryFileInfo }
}
if ($SupplementalPolicies) {
$SupplementalPolicies
$SupplementalPolicies | ForEach-Object { $BinaryPolicyFiles += $_.BinaryFileInfo }
}
if ($MergedPolicy) {
$MergedPolicy
$BinaryPolicyFiles += $MergedPolicy.BinaryFileInfo
}
# Copy all binary policy files to the relevant WDAC CI policy directory.
if ($Deploy -or $DeployAndUpdate) {
$BinaryPolicyFiles | Get-ChildItem | ForEach-Object {
$DestinationDir = "$Env:windir\System32\CodeIntegrity\CiPolicies\Active"
Write-Verbose "Copying $($_.FullName) to $DestinationDir."
$_ | Copy-Item -Destination $DestinationDir -PassThru
}
}
# Refresh all active code integrity policies so that the changes can take effect immediately without needing to reboot.
if ($DeployAndUpdate) {
Get-ChildItem -Path "$Env:windir\System32\CodeIntegrity\CiPolicies\Active\*.cip" | ForEach-Object {
Write-Verbose "Applying the following policy: $($_.FullName)"
$Result = Invoke-CimMethod -Namespace root\Microsoft\Windows\CI -ClassName PS_UpdateAndCompareCIPolicy -MethodName Update -Arguments @{ FilePath = $_.FullName }
if ($Result.ReturnValue -ne 0) {
Write-Error "The following policy failed to refresh: $($_.FullName). Return value: $($Result.ReturnValue)"
}
}
}
}
filter Update-WDACBinaryCodeIntegrityPolicy {
<#
.SYNOPSIS
Refreshes a binary code integrity policy without requiring a reboot.
.DESCRIPTION
Update-WDACBinaryCodeIntegrityPolicy is used to explicitly refresh one or more binary code integrity policies without requiring a reboot. In order to support this scenario, your base policy must be configured with the "Enabled:Update Policy No Reboot" option.
Author: Matthew Graeber
.PARAMETER Path
Specifies the path to a binary code integrity file that you would like to explicitly update without rebooting. A binary code integrity policy will have a file extension of .p7b or .cip in a multi-policy scenario.
.EXAMPLE
Update-WDACBinaryPolicy -Path C:\Windows\System32\CodeIntegrity\SIPolicy.p7b
Updating a single policy.
.EXAMPLE
Get-ChildItem -Path "$Env:windir\System32\CodeIntegrity\CiPolicies\Active\*.cip" | Update-WDACBinaryCodeIntegrityPolicy
Updating all policies in a multi-policy scenario.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)]
[String]
[ValidateNotNullOrEmpty()]
[Alias('FullName')]
$Path
)
$FullPath = Resolve-Path $Path
$Result = Invoke-CimMethod -Namespace root\Microsoft\Windows\CI -ClassName PS_UpdateAndCompareCIPolicy -MethodName Update -Arguments @{ FilePath = $FullPath.Path }
if ($Result.ReturnValue -ne 0) {
Write-Error "The following policy failed to refresh: $($FullPath.Path). Return value: $($Result.ReturnValue)"
}
}
function Disable-WDACDeployedPolicy {
<#
.SYNOPSIS
Disables the currently deployed code integrity policy. Note: this does not delete deployed policy files in %windir%\System32\CodeIntegrity. Those should be removed manually, if desired.
.EXAMPLE
Disable-WDACDeployedPolicy
#>
Invoke-CimMethod -Namespace root\Microsoft\Windows\CI -ClassName PS_UpdateAndCompareCIPolicy -MethodName Delete
}