| 1 |
#!powershell |
| 2 |
|
| 3 |
#AnsibleRequires -CSharpUtil Ansible.Basic |
| 4 |
|
| 5 |
$spec = @{ |
| 6 |
options = @{ |
| 7 |
remote_tmp_dir = @{ |
| 8 |
type = 'path' |
| 9 |
required = $true |
| 10 |
} |
| 11 |
} |
| 12 |
} |
| 13 |
$module = [Ansible.Basic.AnsibleModule]::Create($args, $spec) |
| 14 |
|
| 15 |
$tmpPath = $module.Params.remote_tmp_dir |
| 16 |
$module.Result.changed = $true |
| 17 |
|
| 18 |
$policyPath = Join-Path $tmpPath policy.xml |
| 19 |
$certPath = Join-Path $tmpPath signing.cer |
| 20 |
# Using $tmpPath has this step fail |
| 21 |
$policyBinPath = "$env:windir\System32\CodeIntegrity\SiPolicy.p7b" |
| 22 |
$policyName = 'Ansible_AppControl_Test' |
| 23 |
|
| 24 |
Copy-Item -LiteralPath "$env:windir\schemas\CodeIntegrity\ExamplePolicies\DefaultWindows_Enforced.xml" $policyPath |
| 25 |
|
| 26 |
$signScript = { |
| 27 |
[CmdletBinding()] |
| 28 |
param ( |
| 29 |
[Parameter(Mandatory)] |
| 30 |
[string] |
| 31 |
$PolicyName, |
| 32 |
|
| 33 |
[Parameter(Mandatory)] |
| 34 |
[string] |
| 35 |
$PolicyPath, |
| 36 |
|
| 37 |
[Parameter(Mandatory)] |
| 38 |
[string] |
| 39 |
$PolicyBinPath, |
| 40 |
|
| 41 |
[Parameter(Mandatory)] |
| 42 |
[string[]] |
| 43 |
$CertPath |
| 44 |
) |
| 45 |
|
| 46 |
$ErrorActionPreference = 'Stop' |
| 47 |
|
| 48 |
Set-CIPolicyIdInfo -FilePath $PolicyPath -PolicyName $PolicyName -PolicyId (New-Guid) |
| 49 |
Set-CIPolicyVersion -FilePath $PolicyPath -Version "1.0.0.0" |
| 50 |
|
| 51 |
$CertPath | ForEach-Object -Process { |
| 52 |
Add-SignerRule -FilePath $PolicyPath -CertificatePath $_ -User |
| 53 |
} |
| 54 |
Set-RuleOption -FilePath $PolicyPath -Option 0 # Enabled:UMCI |
| 55 |
Set-RuleOption -FilePath $PolicyPath -Option 3 -Delete # Enabled:Audit Mode |
| 56 |
Set-RuleOption -FilePath $PolicyPath -Option 11 -Delete # Disabled:Script Enforcement |
| 57 |
Set-RuleOption -FilePath $PolicyPath -Option 19 # Enabled:Dynamic Code Security |
| 58 |
|
| 59 |
$null = ConvertFrom-CIPolicy -XmlFilePath $PolicyPath -BinaryFilePath $PolicyBinPath |
| 60 |
} |
| 61 |
|
| 62 |
if ($IsCoreCLR) { |
| 63 |
# WDAC cmdlets only work in WinPS so we need to run it in a sub session |
| 64 |
# running as WinPS. We also need to trust the signing certs for pwsh.exe |
| 65 |
# and related DLLs so we export those and pass them to the job. |
| 66 |
$psSigningCerts = Get-ChildItem -LiteralPath $PSHome | |
| 67 |
Where-Object Extension -in @('.dll', '.exe') | |
| 68 |
Get-AuthenticodeSignature | |
| 69 |
Select-Object -ExpandProperty SignerCertificate -Unique | |
| 70 |
ForEach-Object -Process { |
| 71 |
$newCertPath = Join-Path -Path $tmpPath -ChildPath "$($_.Thumbprint).cer" |
| 72 |
[System.IO.File]::WriteAllBytes($newCertPath, $_.Export('Cert')) |
| 73 |
$newCertPath |
| 74 |
} |
| 75 |
|
| 76 |
Start-Job -ScriptBlock $signScript -ArgumentList @( |
| 77 |
$policyName, |
| 78 |
$policyPath, |
| 79 |
$policyBinPath, |
| 80 |
@($certPath; $psSigningCerts) |
| 81 |
) -PSVersion 5.1 | Receive-Job -Wait -AutoRemoveJob |
| 82 |
} |
| 83 |
else { |
| 84 |
& $signScript -PolicyName $policyName -PolicyPath $policyPath -PolicyBinPath $policyBinPath -CertPath $certPath |
| 85 |
} |
| 86 |
|
| 87 |
$ciTool = Get-Command -Name CiTool.exe -ErrorAction SilentlyContinue |
| 88 |
$policyId = $null |
| 89 |
if ($ciTool) { |
| 90 |
$setInfo = & $ciTool --update-policy $policyBinPath *>&1 |
| 91 |
if ($LASTEXITCODE) { |
| 92 |
throw "citool.exe --update-policy failed ${LASTEXITCODE}: $setInfo" |
| 93 |
} |
| 94 |
|
| 95 |
$policyId = & $ciTool --list-policies --json | |
| 96 |
ConvertFrom-Json | |
| 97 |
Select-Object -ExpandProperty Policies | |
| 98 |
Where-Object FriendlyName -eq $policyName | |
| 99 |
Select-Object -ExpandProperty PolicyID |
| 100 |
} |
| 101 |
else { |
| 102 |
$rc = Invoke-CimMethod -Namespace root\Microsoft\Windows\CI -ClassName PS_UpdateAndCompareCIPolicy -MethodName Update -Arguments @{ |
| 103 |
FilePath = $policyBinPath |
| 104 |
} |
| 105 |
if ($rc.ReturnValue) { |
| 106 |
$module.FailJson("PS_UpdateAndCompareCIPolicy Update failed $($rc.ReturnValue)") |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
$module.Result.policy_id = $policyId |
| 111 |
$module.Result.path = $policyBinPath |
| 112 |
$module.ExitJson() |