|
| 1 | +function Set-LinkedGroupOIDPolicy { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Adds HasLinkedGroupOIDPolicy and LinkedGroupOIDPolicies properties to AD CS certificate |
| 5 | + template objects. |
| 6 | +
|
| 7 | + .DESCRIPTION |
| 8 | + Examines the CertificatePolicy property (msPKI-Certificate-Policy) of each certificate |
| 9 | + template against the set of msPKI-Enterprise-Oid objects in the AdcsObjectStore. |
| 10 | +
|
| 11 | + An msPKI-Enterprise-Oid object that has msDS-OIDToGroupLink set links its OID value to |
| 12 | + a universal group. When a certificate template lists such an OID as an application |
| 13 | + policy and supports Client Authentication, any principal who enrolls and uses the issued |
| 14 | + certificate gains the rights of the linked group while that group's membership appears |
| 15 | + empty — the ESC13 attack path. |
| 16 | +
|
| 17 | + This function adds two synthetic properties to each certificate template: |
| 18 | + 1. HasLinkedGroupOIDPolicy: Boolean indicating whether at least one policy OID on the |
| 19 | + template is linked to a group via msDS-OIDToGroupLink. |
| 20 | + 2. LinkedGroupOIDPolicies: Array of group DNs linked to the template's policy OIDs. |
| 21 | +
|
| 22 | + IMPORTANT: This function requires $script:AdcsObjectStore to be fully populated before |
| 23 | + it is called. It must run after Get-AdcsObject has completed so that all |
| 24 | + msPKI-Enterprise-Oid objects are present in the store. |
| 25 | +
|
| 26 | + .PARAMETER AdcsObject |
| 27 | + One or more LS2AdcsObject instances representing AD CS certificate templates. |
| 28 | + Non-template objects are passed through unmodified. |
| 29 | +
|
| 30 | + .INPUTS |
| 31 | + LS2AdcsObject[] |
| 32 | +
|
| 33 | + .OUTPUTS |
| 34 | + LS2AdcsObject[] |
| 35 | + Returns the input objects. Templates have HasLinkedGroupOIDPolicy and |
| 36 | + LinkedGroupOIDPolicies set. Non-templates are passed through unchanged. |
| 37 | +
|
| 38 | + .EXAMPLE |
| 39 | + $templates | Set-LinkedGroupOIDPolicy |
| 40 | + Processes all certificate templates and adds the linked group OID properties. |
| 41 | +
|
| 42 | + .EXAMPLE |
| 43 | + Get-AdcsObject | Where-Object { $_.IsCertificateTemplate() } | Set-LinkedGroupOIDPolicy |
| 44 | + Retrieves all templates and evaluates which ones have group-linked application policy OIDs. |
| 45 | +
|
| 46 | + .NOTES |
| 47 | + Author: Jake Hildreth (@jakehildreth) |
| 48 | + Module: Locksmith2 |
| 49 | + Requires: PowerShell 5.1+ |
| 50 | +
|
| 51 | + Requires script-scope variable set by Initialize-AdcsObjectStore: |
| 52 | + - $script:AdcsObjectStore: Cache of all AD CS objects (must include msPKI-Enterprise-Oid objects) |
| 53 | +
|
| 54 | + Used by ESC13 detection in Find-LS2VulnerableTemplate. |
| 55 | +
|
| 56 | + Reference: https://posts.specterops.io/adcs-esc13-abuse-technique-fda4272fbd53 |
| 57 | +
|
| 58 | + .LINK |
| 59 | + Set-AuthenticationEKUExist |
| 60 | +
|
| 61 | + .LINK |
| 62 | + Find-LS2VulnerableTemplate |
| 63 | + #> |
| 64 | + [CmdletBinding()] |
| 65 | + param( |
| 66 | + [Parameter(Mandatory, ValueFromPipeline)] |
| 67 | + [LS2AdcsObject[]]$AdcsObject |
| 68 | + ) |
| 69 | + |
| 70 | + begin { |
| 71 | + Write-Verbose 'Building OID-to-group lookup map from AdcsObjectStore...' |
| 72 | + |
| 73 | + # Build a map of CertTemplateOID -> OIDToGroupLink for all OID objects that have a group link. |
| 74 | + # msPKI-Enterprise-Oid objects live in CN=OID,CN=Public Key Services,... and are collected |
| 75 | + # by Get-AdcsObject as part of its full subtree search. |
| 76 | + $oidGroupMap = @{} |
| 77 | + if ($script:AdcsObjectStore) { |
| 78 | + $script:AdcsObjectStore.Values | Where-Object { $_.OIDToGroupLink } | ForEach-Object { |
| 79 | + if ($_.CertTemplateOID) { |
| 80 | + $oidGroupMap[$_.CertTemplateOID] = $_.OIDToGroupLink |
| 81 | + Write-Verbose " OID '$($_.CertTemplateOID)' -> group '$($_.OIDToGroupLink)'" |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + Write-Verbose "OID-to-group map has $($oidGroupMap.Count) entry/entries" |
| 86 | + } |
| 87 | + |
| 88 | + process { |
| 89 | + foreach ($obj in $AdcsObject) { |
| 90 | + if ($obj.SchemaClassName -ne 'pKICertificateTemplate') { |
| 91 | + $obj |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + try { |
| 96 | + $linkedGroups = [System.Collections.Generic.List[string]]::new() |
| 97 | + |
| 98 | + if ($oidGroupMap.Count -gt 0 -and $obj.CertificatePolicy -and $obj.CertificatePolicy.Count -gt 0) { |
| 99 | + foreach ($oid in $obj.CertificatePolicy) { |
| 100 | + if ($oidGroupMap.ContainsKey($oid)) { |
| 101 | + $linkedGroups.Add($oidGroupMap[$oid]) |
| 102 | + Write-Verbose " Template '$($obj.Name)': policy OID '$oid' links to group '$($oidGroupMap[$oid])'" |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + $obj.LinkedGroupOIDPolicies = $linkedGroups.ToArray() |
| 108 | + $obj.HasLinkedGroupOIDPolicy = ($linkedGroups.Count -gt 0) |
| 109 | + |
| 110 | + Write-Verbose " Template '$($obj.Name)': HasLinkedGroupOIDPolicy=$($obj.HasLinkedGroupOIDPolicy)" |
| 111 | + } catch { |
| 112 | + $errorRecord = [System.Management.Automation.ErrorRecord]::new( |
| 113 | + $_.Exception, |
| 114 | + 'SetLinkedGroupOIDPolicyFailed', |
| 115 | + [System.Management.Automation.ErrorCategory]::NotSpecified, |
| 116 | + $obj.distinguishedName |
| 117 | + ) |
| 118 | + $PSCmdlet.WriteError($errorRecord) |
| 119 | + } |
| 120 | + |
| 121 | + $obj |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments