|
| 1 | +function Find-ESC7 { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + This script finds Active Directory Certificate Services (AD CS) Certificate Authorities (CA) that have the ESC7 vulnerability. |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + The script takes an array of AD CS objects as input and filters them based on objects that have the objectClass |
| 8 | + 'pKIEnrollmentService'. If the CA objects have non-standard/unsafe principals as administrators or managers, an issue is created. |
| 9 | +
|
| 10 | + .PARAMETER ADCSObjects |
| 11 | + Specifies the array of AD CS objects to be processed. This parameter is mandatory. |
| 12 | +
|
| 13 | + .PARAMETER UnsafeUsers |
| 14 | + Principals that should never be granted control of a CA. |
| 15 | +
|
| 16 | + .PARAMETER SafeUsers |
| 17 | + Principals that are generally recognized as safe to control a CA. |
| 18 | +
|
| 19 | + .PARAMETER SkipRisk |
| 20 | + Switch used when processing second-order risks. |
| 21 | +
|
| 22 | + .OUTPUTS |
| 23 | + The script outputs an array of custom objects representing the matching AD CS objects and their associated information. |
| 24 | +
|
| 25 | + #> |
| 26 | + [CmdletBinding()] |
| 27 | + param( |
| 28 | + [Parameter(Mandatory)] |
| 29 | + [Microsoft.ActiveDirectory.Management.ADEntity[]]$ADCSObjects, |
| 30 | + [Parameter(Mandatory)] |
| 31 | + [string]$UnsafeUsers, |
| 32 | + [Parameter(Mandatory)] |
| 33 | + [string]$SafeUsers, |
| 34 | + [switch]$SkipRisk |
| 35 | + ) |
| 36 | + process { |
| 37 | + Write-Output $ADCSObjects -PipelineVariable object | Where-Object { |
| 38 | + ($object.objectClass -eq 'pKIEnrollmentService') -and $object.CAHostDistinguishedName -and |
| 39 | + ( ($object.CAAdministrator) -or ($object.CertificateManager) ) |
| 40 | + } | ForEach-Object { |
| 41 | + Write-Output $object.CAAdministrator -PipelineVariable admin | ForEach-Object { |
| 42 | + $SID = Convert-IdentityReferenceToSid -Object $admin |
| 43 | + if ($SID -notmatch $SafeUsers) { |
| 44 | + $Issue = [pscustomobject]@{ |
| 45 | + Forest = $object.CanonicalName.split('/')[0] |
| 46 | + Name = $object.Name |
| 47 | + DistinguishedName = $object.DistinguishedName |
| 48 | + IdentityReference = $admin |
| 49 | + IdentityReferenceSID = $SID |
| 50 | + Right = 'CA Administrator' |
| 51 | + Issue = @" |
| 52 | +$admin has been granted CA Administrator rights on this Certification Authority (CA). |
| 53 | +
|
| 54 | +$admin has full control over this CA. |
| 55 | +
|
| 56 | +More info: |
| 57 | + - https://posts.specterops.io/certified-pre-owned-d95910965cd2 |
| 58 | +
|
| 59 | +"@ |
| 60 | + Fix = "Revoke CA Administrator rights from ${admin}." |
| 61 | + Revert = "Restore CA Administrator rights to ${admin}." |
| 62 | + Technique = 'ESC7' |
| 63 | + } |
| 64 | + |
| 65 | + if ($SkipRisk -eq $false) { |
| 66 | + Set-RiskRating -ADCSObjects $ADCSObjects -Issue $Issue -SafeUsers $SafeUsers -UnsafeUsers $UnsafeUsers |
| 67 | + } |
| 68 | + |
| 69 | + if ( $Mode -in @(1, 3, 4) ) { |
| 70 | + Update-ESC7Remediation -Issue $Issue |
| 71 | + } |
| 72 | + |
| 73 | + $Issue |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + Write-Output $object.CertificateManager -PipelineVariable admin | ForEach-Object { |
| 78 | + $SID = Convert-IdentityReferenceToSid -Object $admin |
| 79 | + if ($SID -notmatch $SafeUsers) { |
| 80 | + $Issue = [pscustomobject]@{ |
| 81 | + Forest = $object.CanonicalName.split('/')[0] |
| 82 | + Name = $object.Name |
| 83 | + DistinguishedName = $object.DistinguishedName |
| 84 | + IdentityReference = $admin |
| 85 | + IdentityReferenceSID = $SID |
| 86 | + Right = 'Certificate Manager' |
| 87 | + Issue = @" |
| 88 | +$admin has been granted Certificate Manager rights on this Certification Authority (CA). |
| 89 | +
|
| 90 | +$admin can approve pending certificate requests on this CA. |
| 91 | +
|
| 92 | +More info: |
| 93 | + - https://posts.specterops.io/certified-pre-owned-d95910965cd2 |
| 94 | +
|
| 95 | +"@ |
| 96 | + Fix = "Revoke Certificate Manager rights from ${admin}." |
| 97 | + Revert = "Restore Certificate Manager rights to ${admin}." |
| 98 | + Technique = 'ESC7' |
| 99 | + } |
| 100 | + |
| 101 | + if ($SkipRisk -eq $false) { |
| 102 | + Set-RiskRating -ADCSObjects $ADCSObjects -Issue $Issue -SafeUsers $SafeUsers -UnsafeUsers $UnsafeUsers |
| 103 | + } |
| 104 | + |
| 105 | + if ( $Mode -in @(1, 3, 4) ) { |
| 106 | + Update-ESC7Remediation -Issue $Issue |
| 107 | + } |
| 108 | + |
| 109 | + $Issue |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments