Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
function ConvertTo-MtAdcsEnrollmentPrerequisiteState {
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory)]
[AllowNull()]
[PSCustomObject] $Configuration
)

$clientAuthenticationOids = @(
'1.3.6.1.5.5.7.3.2',
'1.3.6.1.4.1.311.20.2.2',
'1.3.6.1.5.2.3.4',
'2.5.29.37.0'
)
$enrolleeSuppliesSubjectFlag = 0x00000001
$pendAllRequestsFlag = 0x00000002

if ($null -eq $Configuration) {
return [PSCustomObject]@{
EvidenceAvailable = $false
Disposition = 'Inconclusive'
Templates = @()
EligibleCount = 0
PotentiallyEligibleCount = 0
InconclusiveCount = 0
}
}

$enterpriseCas = @($Configuration.EnterpriseCAs)
$templates = @($Configuration.CertificateTemplates)
if ($null -eq $Configuration.EnterpriseCAs -or $null -eq $Configuration.CertificateTemplates) {
return [PSCustomObject]@{
EvidenceAvailable = $false
Disposition = 'Inconclusive'
Templates = @()
EligibleCount = 0
PotentiallyEligibleCount = 0
InconclusiveCount = 0
}
}

$publishedBy = @{}
foreach ($ca in $enterpriseCas) {
foreach ($templateName in @($ca.certificateTemplates)) {
$name = [string](ConvertFrom-MtLdapValue $templateName)
if ([string]::IsNullOrWhiteSpace($name)) { continue }
if (-not $publishedBy.ContainsKey($name)) { $publishedBy[$name] = [System.Collections.Generic.List[string]]::new() }
$caName = [string](ConvertFrom-MtLdapValue $ca.Name)
if ([string]::IsNullOrWhiteSpace($caName)) { $caName = [string](ConvertFrom-MtLdapValue $ca.DistinguishedName) }
$publishedBy[$name].Add($caName)
}
}

$facts = foreach ($template in $templates) {
$name = [string](ConvertFrom-MtLdapValue $template.Name)
$ekuValues = @($template.'pKIExtendedKeyUsage' | ForEach-Object { [string](ConvertFrom-MtLdapValue $_) })
$enrollmentFlagRaw = ConvertFrom-MtLdapValue $template.'msPKI-Enrollment-Flag'
$nameFlagRaw = ConvertFrom-MtLdapValue $template.'msPKI-Certificate-Name-Flag'
$enrollmentFlag = if ($null -eq $enrollmentFlagRaw) { $null } else { [int64]$enrollmentFlagRaw }
$nameFlag = if ($null -eq $nameFlagRaw) { $null } else { [int64]$nameFlagRaw }
$published = $publishedBy.ContainsKey($name)
$authenticationCapable = @($ekuValues | Where-Object { $_ -in $clientAuthenticationOids }).Count -gt 0
$suppliesSubject = $null -ne $nameFlag -and (($nameFlag -band $enrolleeSuppliesSubjectFlag) -ne 0)
$managerApprovalRequired = $null -ne $enrollmentFlag -and (($enrollmentFlag -band $pendAllRequestsFlag) -ne 0)
$securityDescriptorAvailable = $null -ne $template.nTSecurityDescriptor

$disposition = if (-not $published -or -not $authenticationCapable) {
'PrerequisitesNotSatisfied'
}
elseif ($null -ne $nameFlag -and -not $suppliesSubject) {
'PrerequisitesNotSatisfied'
}
elseif ($null -ne $enrollmentFlag -and $managerApprovalRequired) {
'PrerequisitesNotSatisfied'
}
elseif ($null -eq $nameFlag -or $null -eq $enrollmentFlag) {
'Inconclusive'
}
elseif (-not $securityDescriptorAvailable) {
'Inconclusive'
}
else {
'PotentiallyEligible'
}

[PSCustomObject]@{
Name = $name
Published = $published
PublishedBy = if ($published) { @($publishedBy[$name]) } else { @() }
AuthenticationCapable = $authenticationCapable
EnrolleeSuppliesSubject = $suppliesSubject
ManagerApprovalRequired = $managerApprovalRequired
SecurityDescriptorAvailable = $securityDescriptorAvailable
Disposition = $disposition
}
}

$eligible = @($facts | Where-Object Disposition -eq 'Eligible').Count
$potential = @($facts | Where-Object Disposition -eq 'PotentiallyEligible').Count
$inconclusive = @($facts | Where-Object Disposition -eq 'Inconclusive').Count
[PSCustomObject]@{
EvidenceAvailable = $true
Disposition = if ($potential -gt 0) { 'PotentiallyEligible' } elseif ($inconclusive -gt 0) { 'Inconclusive' } else { 'PrerequisitesNotSatisfied' }
Templates = @($facts)
EligibleCount = $eligible
PotentiallyEligibleCount = $potential
InconclusiveCount = $inconclusive
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
BeforeAll {
$repoRoot = Resolve-Path (Join-Path $PSScriptRoot '../../../..')
. (Join-Path $repoRoot 'powershell/internal/services/ad/ConvertTo-MtAdcsEnrollmentPrerequisiteState.ps1')
function ConvertFrom-MtLdapValue { param($Value) return $Value }
}

Describe 'ConvertTo-MtAdcsEnrollmentPrerequisiteState' {
It 'returns inconclusive when configuration evidence is unavailable' {
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $null
$result.EvidenceAvailable | Should -BeFalse
$result.Disposition | Should -Be 'Inconclusive'
}

It 'correlates publication and template prerequisites' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @([PSCustomObject]@{ Name='CA01'; certificateTemplates=@('UserAuth') })
CertificateTemplates = @([PSCustomObject]@{
Name='UserAuth'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=0
'msPKI-Certificate-Name-Flag'=1
nTSecurityDescriptor=[PSCustomObject]@{}
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.PotentiallyEligibleCount | Should -Be 1
$result.Templates[0].PublishedBy | Should -Contain 'CA01'
}

It 'does not classify an unpublished template as potentially eligible' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @()
CertificateTemplates = @([PSCustomObject]@{
Name='Unpublished'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=0
'msPKI-Certificate-Name-Flag'=1
nTSecurityDescriptor=[PSCustomObject]@{}
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.Templates[0].Disposition | Should -Be 'PrerequisitesNotSatisfied'
}

It 'does not classify a template requiring manager approval as potentially eligible' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @([PSCustomObject]@{ Name='CA01'; certificateTemplates=@('Approval') })
CertificateTemplates = @([PSCustomObject]@{
Name='Approval'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=2
'msPKI-Certificate-Name-Flag'=1
nTSecurityDescriptor=[PSCustomObject]@{}
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.Templates[0].ManagerApprovalRequired | Should -BeTrue
$result.Templates[0].Disposition | Should -Be 'PrerequisitesNotSatisfied'
}

It 'keeps missing security descriptor evidence inconclusive' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @([PSCustomObject]@{ Name='CA01'; certificateTemplates=@('UnknownAcl') })
CertificateTemplates = @([PSCustomObject]@{
Name='UnknownAcl'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=0
'msPKI-Certificate-Name-Flag'=1
nTSecurityDescriptor=$null
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.Templates[0].Disposition | Should -Be 'Inconclusive'
}
It 'keeps missing certificate-name flag evidence inconclusive' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @([PSCustomObject]@{ Name='CA01'; certificateTemplates=@('UnknownNameFlag') })
CertificateTemplates = @([PSCustomObject]@{
Name='UnknownNameFlag'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=0
'msPKI-Certificate-Name-Flag'=$null
nTSecurityDescriptor=[PSCustomObject]@{}
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.Templates[0].Disposition | Should -Be 'Inconclusive'
$result.PotentiallyEligibleCount | Should -Be 0
$result.InconclusiveCount | Should -Be 1
}

It 'keeps missing enrollment flag evidence inconclusive' {
$configuration = [PSCustomObject]@{
EnterpriseCAs = @([PSCustomObject]@{ Name='CA01'; certificateTemplates=@('UnknownEnrollmentFlag') })
CertificateTemplates = @([PSCustomObject]@{
Name='UnknownEnrollmentFlag'
'pKIExtendedKeyUsage'=@('1.3.6.1.5.5.7.3.2')
'msPKI-Enrollment-Flag'=$null
'msPKI-Certificate-Name-Flag'=1
nTSecurityDescriptor=[PSCustomObject]@{}
})
}
$result = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $configuration
$result.Templates[0].Disposition | Should -Be 'Inconclusive'
$result.PotentiallyEligibleCount | Should -Be 0
$result.InconclusiveCount | Should -Be 1
}}
28 changes: 28 additions & 0 deletions tests/ad/security/Test.AD-CS-01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Evaluates whether an enterprise CA publishes certificate templates whose collected configuration matches prerequisites commonly associated with certificate-based identity abuse.

#### Control Type

**Operational**

#### Security Recommendation

- Review every published template that supports client authentication and lets the requester supply subject information.
- Require manager approval or authorized signatures where business workflows permit.
- Restrict Enroll and Autoenroll rights to explicitly approved principals.
- Remove unused templates from each enterprise CA.
- Treat a candidate as a configuration lead, not proof of successful enrollment or authentication.

#### How the Test Works

The test correlates enterprise CA `certificateTemplates` publication with each template's EKUs, enrollment flags, certificate-name flags, and security-descriptor availability. A template is reported for investigation when it is published, authentication-capable, allows enrollee-supplied subject information, does not require manager approval, and has authorization evidence available or unresolved.

The test deliberately distinguishes observed prerequisites from exploitability. It does not enroll a certificate, generate or export a private key, perform PKINIT, or claim that an attack path was executed.

#### Result Interpretation

- **PrerequisitesNotSatisfied**: at least one modeled condition is disproven.
- **PotentiallyEligible**: dangerous template and publication conditions exist, with ACL evidence available for follow-up.
- **Inconclusive**: required configuration or authorization evidence is unavailable.

<!--- Results --->
%TestResult%
47 changes: 47 additions & 0 deletions tests/ad/security/Test.AD-CS-01.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
function Test-MtAdcsPublishedTemplatePrerequisites {
[MaesterTest(
Id = 'AD-CS-01',
Title = 'Published AD CS templates should not expose dangerous enrollment prerequisites',
Product = 'ad',
Feature = 'certificate-services',
Area = 'infrastructure',
ResourceType = 'CertificateAuthority',
Severity = 'High',
UserImpact = 'Low',
ImplementationCost = 'Medium',
Mitre = 'T1649',
Service = 'ActiveDirectory',
Permission = ('ActiveDirectory:Ldap:Configuration/*:ReadProperty'),
Author = 'Mario Contestabile'
)]
[CmdletBinding()]
[OutputType([bool])]
param()

$adState = Get-MtADDomainState -Categories @('Configuration')
if ($null -eq $adState -or $null -eq $adState.Configuration) {
Add-MtTestResultDetail -SkippedBecause NotConnectedActiveDirectory
return $null
}

$state = ConvertTo-MtAdcsEnrollmentPrerequisiteState -Configuration $adState.Configuration
if (-not $state.EvidenceAvailable) {
Add-MtTestResultDetail -Result 'Required AD CS publication or template evidence was unavailable.' -Investigate
return $null
}

$candidates = @($state.Templates | Where-Object Disposition -in @('PotentiallyEligible','Eligible','Inconclusive'))
$result = "| Template | Published | Authentication capable | Supplies subject | Manager approval | ACL evidence | Disposition |`n"
$result += "| --- | --- | --- | --- | --- | --- | --- |`n"
foreach ($candidate in $candidates) {
$result += "| $($candidate.Name) | $($candidate.Published) | $($candidate.AuthenticationCapable) | $($candidate.EnrolleeSuppliesSubject) | $($candidate.ManagerApprovalRequired) | $($candidate.SecurityDescriptorAvailable) | $($candidate.Disposition) |`n"
}

if ($candidates.Count -eq 0) {
Add-MtTestResultDetail -Result 'No published certificate template matched the modeled dangerous enrollment prerequisites.'
return $true
}

Add-MtTestResultDetail -Result "Published template candidates require authorization review. Configuration prerequisites do not prove exploitation.`n`n$result" -Investigate
return $null
}