ADCS ESC2: Find and Fix Certificate Templates That Work for Any Purpose

ADCS ESC2 is a certificate template whose certificates work for anything: the Any Purpose EKU, or no EKU at all. If any domain user can enroll, they can potentially use that certificate to request certificates for other users, sign code your machines trust or, with no EKU, issue certificates of their own. This post shows you how to find these templates, check what they already issued and narrow them without breaking the app that needed them.

Privileged Access & Permissions Critical 10 min read
PriorityCritical. A certificate that works for anything can potentially be used to request certificates as a domain admin
What decides itA template with the Any Purpose EKU or no EKU, no approval and broad enroll rights
Who's affectedForests with an enterprise CA and a custom template someone made "work everywhere", or a subordinate CA template opened to users
FixReplace Any Purpose with the EKUs the app needs, narrow who can enroll or require manager approval. Then revoke anything suspicious it issued

Check it now: do any published templates work for any purpose?

PowerShell
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
$published = Get-ADObject -SearchBase "CN=Enrollment Services,$pks" -LDAPFilter '(objectClass=pKIEnrollmentService)' -Properties certificateTemplates |
    ForEach-Object { $_.certificateTemplates }
Get-ADObject -SearchBase "CN=Certificate Templates,$pks" -LDAPFilter '(objectClass=pKICertificateTemplate)' -Properties pKIExtendedKeyUsage, msPKI-Enrollment-Flag, msPKI-RA-Signature |
    Where-Object {
        $_.Name -in $published -and
        (-not $_.pKIExtendedKeyUsage -or $_.pKIExtendedKeyUsage -contains '2.5.29.37.0') -and
        -not ($_.'msPKI-Enrollment-Flag' -band 2) -and
        -not $_.'msPKI-RA-Signature'
    } | Select-Object Name

Runs as any domain user with the ActiveDirectory module. It lists templates that are published on at least one CA, carry the Any Purpose EKU or no EKU and need no approval or authorized signature. No output means none of your published templates match. It doesn't check who can enroll yet. The script further down does.

You checked one setting. The free scan checks the rest of your on-prem AD in a few minutes.

Nothing gets installed on your domain controllers. Only HTTPS leaves your network.

What ADCS ESC2 is

Every certificate template lists extended key usages (EKUs). They say what its certificates are for: Client Authentication for sign-in, Server Authentication for TLS, Code Signing and so on. Two settings remove that limit. The Any Purpose EKU (OID 2.5.29.37.0) means the certificate is meant for any use. A template with no EKU at all issues certificates with no limit either, which is how subordinate CA certificates look. SpecterOps named this ESC2 in their 2021 Certified Pre-Owned research.

A template is ESC2 when all of these are true:

  • The EKU is unlimited. The template has Any Purpose, or no EKU.
  • Nothing checks the request. CA certificate manager approval is off and no authorized signatures are required.
  • Broad groups can enroll. Domain Users, Domain Computers, Authenticated Users or Everyone holds Enroll or a right that includes it.
  • A CA publishes it and lets those users enroll, which is the default for an enterprise CA.

However, unlike ESC1, the requester doesn't pick whose name goes on the certificate. The certificate is in their own name. Instead, the danger is everything else it can be used for. These templates are usually custom. For example, someone may have removed the EKUs or picked Any Purpose to make a certificate work in one more place.

Why ADCS ESC2 is dangerous

It can request certificates for other users

Some templates let an enrollment agent request a certificate on behalf of someone else. Normally only a certificate with the Certificate Request Agent EKU can do that. However, an Any Purpose certificate can stand in for it. SpecterOps's ESC2 documentation says "it is possible to use it as a Certificate Request Agent." So any domain user with one can potentially request a certificate as a domain admin from a template that accepts agent requests, unless the CA restricts enrollment agents. Our check library describes the same path. MITRE ATT&CK tracks this as T1649, Steal or Forge Authentication Certificates.

It works anywhere your PKI is trusted

The same certificate also works for server authentication and code signing. In other words, a regular user could potentially sign a script or program that your machines trust as coming from your own PKI. Microsoft's Defender for Identity guidance says certificates from such templates "can be used maliciously by an adversary, compromising trust."

With no EKU, it can issue certificates

A certificate with no EKU acts like a subordinate CA certificate. SpecterOps notes the holder could use it to sign new certificates with whatever fields and EKUs they choose. Domain controllers won't accept those for sign-in by default, because the new CA isn't in the NTAuthCertificates store. Still, anything that trusts your root for TLS, code signing or federation may trust them. Also, a password reset doesn't touch any of these certificates. They work until they expire or are revoked.

How ADCS ESC2 works: any user enrolls in an Any Purpose template and uses it to request certificates for others
How an Any Purpose template can turn a domain user into a domain admin. Tap to enlarge.

Find ESC2 templates in your forest

Templates and CAs live in the forest's Configuration partition, so one run covers the whole forest. The first script runs as any domain user with the AD module. The other two run on the CA.

List ADCS ESC2 templates and who can enroll

This lists every template with Any Purpose or no EKU that needs no approval. For each one, it shows the EKU, which CAs publish it and every account or group that can enroll.

PowerShell
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
# Everyone, NETWORK, INTERACTIVE, Anonymous, Authenticated Users, This Organization, Users
$low = 'S-1-1-0', 'S-1-5-2', 'S-1-5-4', 'S-1-5-7', 'S-1-5-11', 'S-1-5-15', 'S-1-5-32-545'
# Domain Users, Domain Guests and Domain Computers of any domain
$lowRid = '^S-1-5-21-\d+-\d+-\d+-51[345]$'
$enroll = [guid]'0e10c968-78fb-11d2-90d4-00c04f79dc55'
$cas = Get-ADObject -SearchBase "CN=Enrollment Services,$pks" -LDAPFilter '(objectClass=pKIEnrollmentService)' -Properties certificateTemplates
Get-ADObject -SearchBase "CN=Certificate Templates,$pks" -LDAPFilter '(objectClass=pKICertificateTemplate)' -Properties displayName, pKIExtendedKeyUsage, msPKI-Enrollment-Flag, msPKI-RA-Signature, nTSecurityDescriptor |
    ForEach-Object {
        $t = $_
        $ekus = @($t.pKIExtendedKeyUsage)
        if ($ekus.Count -gt 0 -and $ekus -notcontains '2.5.29.37.0') { return }
        if (($t.'msPKI-Enrollment-Flag' -band 2) -or $t.'msPKI-RA-Signature') { return }
        $enrollers = $t.nTSecurityDescriptor.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier]) |
            Where-Object {
                $_.AccessControlType -eq 'Allow' -and (
                    (($_.ActiveDirectoryRights -band [System.DirectoryServices.ActiveDirectoryRights]::GenericAll) -eq [System.DirectoryServices.ActiveDirectoryRights]::GenericAll) -or
                    (($_.ActiveDirectoryRights -band [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight) -and
                     ($_.ObjectType -eq $enroll -or $_.ObjectType -eq [guid]::Empty)))
            } |
            ForEach-Object {
                $sid = $_.IdentityReference
                try { $name = $sid.Translate([System.Security.Principal.NTAccount]).Value } catch { $name = $sid.Value }
                if ($sid.Value -in $low -or $sid.Value -match $lowRid) { "$name (low privilege)" } else { $name }
            }
        [pscustomobject]@{
            Template    = $t.Name
            DisplayName = $t.displayName
            EKU         = if ($ekus.Count) { 'Any Purpose' } else { 'None (subordinate CA)' }
            PublishedOn = ($cas | Where-Object { $_.certificateTemplates -contains $t.Name }).Name -join ', '
            Enrollers   = ($enrollers | Sort-Object -Unique) -join '; '
        }
    }

What to look for: rows where PublishedOn isn't empty and Enrollers includes an entry marked (low privilege). Those are exploitable today. For example, a built-in SubCA template that only Domain Admins and Enterprise Admins can enroll in is expected. Also check custom groups in Enrollers, since one may hold most of your users. Rows with an empty PublishedOn can't be requested right now. However, anyone who can publish templates could turn them back on.

Check who holds certificates from it

Fixing the template doesn't touch certificates it already issued. This lists every certificate the CA issued from one ESC2 template.

PowerShell
# Run on the CA as a CA administrator or certificate manager, with the AD module installed.
# Replace AnyPurposeCustom with the template name from the script above.
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
$t = Get-ADObject -SearchBase "CN=Certificate Templates,$pks" -LDAPFilter '(cn=AnyPurposeCustom)' -Properties msPKI-Cert-Template-OID
if (-not $t) { throw 'Template not found.' }
certutil -view -restrict "CertificateTemplate=$($t.'msPKI-Cert-Template-OID'),Disposition=20" -out 'RequestID,RequesterName,CommonName,NotAfter,SerialNumber' csv

What to look for: requesters who have no reason to hold a certificate that works for anything. Note the SerialNumber of each one you can't explain. If nothing comes back, try the template's name instead of the OID in the restriction.

Check for requests made on behalf of someone else

This lists issued certificates where the account that sent the request (Request.CallerName) differs from the name in the request (Request.RequesterName), as Microsoft's [MS-CSRA] specification defines them.

PowerShell
# Run on the CA as a CA administrator or certificate manager.
# Lists issued certificates where the account that sent the request differs from the account it was issued for.
$rows = certutil -view -restrict 'Disposition=20' -out 'RequestID,CallerName,RequesterName,CertificateTemplate,NotAfter,SerialNumber' csv |
    ConvertFrom-Csv -Header RequestID, Caller, Requester, Template, NotAfter, Serial |
    Select-Object -Skip 1
if (-not $rows) { throw 'No rows returned. Check you are on the CA and have permission.' }
$rows | Where-Object { $_.Serial -and $_.Caller -ne $_.Requester }

What to look for: a regular user as Caller with an admin as Requester. Of course, expected enrollment agents, such as a smart card management system or an NDES server, will show up too. Anything else needs an explanation.

Check ADCS ESC2 with Insight Recon

The free scan includes Unrestricted certificate template with any purpose in ADCS (ESC2), a Critical check. It lists every published template with Any Purpose or no EKU that needs no approval or authorized signature and where a broad group holds Enroll, all extended rights or full control. Broad groups here are Everyone, Anonymous, Authenticated Users, NETWORK, INTERACTIVE, This Organization and Users. Domain Users, Domain Guests and Domain Computers from any domain count too. It also honors deny entries on the template. Owners and accounts that can only edit the template show up under ESC4 instead. For each template, the finding shows the groups and the rights they hold. It also comes with the Hacker Insight, remediation steps and compliance mappings.

Know what the ADCS ESC2 check doesn't decide

Some things are still yours to check. The check skips templates no CA publishes. If it can't read a CA's template list, it treats every template as published, so it may list one nobody can request right now. When unpublished templates would match, a note on the result says how many. They show up in the script above with an empty PublishedOn. It also doesn't count custom groups with many members. Also, it reads templates only. It doesn't look at certificates already issued or at the CA's enrollment agent restrictions. The scripts above cover the groups and the certificates.

Related ADCS checks beyond ESC2

These related checks cover the rest of this area:

  • User-controlled certificate template subject in ADCS (ESC1), templates where the requester picks whose name goes on the certificate. Our ADCS ESC1 post covers it. A template can be both.
  • Misconfigured enrollment agent templates in ADCS (ESC3), templates that hand out the Certificate Request Agent EKU itself. Our ADCS ESC3 post covers it.
  • Unrestricted certificate template modification rights in ADCS (ESC4), templates that broad groups can rewrite into an ESC2. Our ADCS ESC4 post covers it.
  • Schema V1 Certificate Template Allows Arbitrary Application Policy in ADCS (ESC15), older templates where the requester can add usages of their own. Our ADCS ESC15 post covers it.

You can see what the findings look like in our sample report. The full list is in our check library.

Find every template like this in your forest, plus the other ADCS attack paths.

It runs from any Windows machine that can reach your DCs, with a standard domain account. Every core AD check is free for one domain.

Before you change an ESC2 template

Find out what the certificates are used for before you change the template. Then pick the fix that keeps that working.

Find the owner of each ADCS ESC2 template

The template's name and the requesters in the issued list usually point to the app. Then ask the owner what the certificate does. The answer is almost always one or two specific uses, such as Client Authentication or Server Authentication. If nothing was issued from it in the last year, it's probably safe to unpublish.

Pick the fix that fits

  • Unpublish it if nothing uses it.
  • Replace Any Purpose with the specific EKUs the app needs. SpecterOps's guidance is to avoid Any Purpose and subordinate CA templates for end-user certificates.
  • Narrow who can enroll to the accounts that need it. Microsoft's ESC2 guidance starts here.
  • Require CA certificate manager approval or an authorized signature if the template has to stay as it is. Microsoft lists both as extra mitigations.

If you replace the EKU with Client Authentication, check the Subject Name tab too. With "Supply in the request" selected, the template becomes an ESC1 instead. With Server Authentication and "Supply in the request", it becomes an ESC17.

Save the current settings

Note the template's current settings and permissions so you can put them back. The enroll script below saves the permissions to a file before it changes anything.

Fix ADCS ESC2 safely

ADCS ESC2 fix order: check what was issued, narrow the template, revoke suspicious certificates, watch the CA
The order for fixing an ESC2 template. Tap to enlarge.

1. Narrow the template

To replace the EKU, use the Certificate Templates console (certtmpl.msc). On the Extensions tab, edit Application Policies, remove Any Purpose and add only what the app needs. The scripts below change one template each. Each is a dry run with -WhatIf. Drop that to apply. They need Enterprise Admins, or rights delegated on the template or CA object. Change AnyPurposeCustom and Contoso-CA to your names.

Narrow who can enroll. This removes Enroll and Autoenroll for the broad groups and keeps their Read access. It warns about entries that combine Enroll with other rights, which you fix on the template's Security tab. Before you apply it, give Enroll to the group that needs the template.

PowerShell
# Dry run. Drop -WhatIf to apply.
# Needs Enterprise Admins, or permission to change the template's security.
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
$t = Get-ADObject -SearchBase "CN=Certificate Templates,$pks" -LDAPFilter '(cn=AnyPurposeCustom)'
if (-not $t) { throw 'Template not found.' }
$low = 'S-1-1-0', 'S-1-5-2', 'S-1-5-4', 'S-1-5-7', 'S-1-5-11', 'S-1-5-15', 'S-1-5-32-545'
$lowRid = '^S-1-5-21-\d+-\d+-\d+-51[345]$'
$enrollRights = [guid]'0e10c968-78fb-11d2-90d4-00c04f79dc55', [guid]'a05b8cc2-17bc-4802-a710-e7c15ab866a2', [guid]::Empty
$path = "AD:$($t.DistinguishedName)"
$acl = Get-Acl -Path $path -ErrorAction Stop
# Saves the current permissions so you can put them back
$acl.Sddl | Set-Content -Path ".\$($t.Name)-acl.sddl"
foreach ($ace in $acl.GetAccessRules($true, $false, [System.Security.Principal.SecurityIdentifier])) {
    if ($ace.AccessControlType -ne 'Allow' -or ($ace.IdentityReference.Value -notin $low -and $ace.IdentityReference.Value -notmatch $lowRid)) { continue }
    if ($ace.ActiveDirectoryRights -eq 'ExtendedRight' -and $ace.ObjectType -in $enrollRights) {
        [void]$acl.RemoveAccessRuleSpecific($ace)
    } elseif ($ace.ActiveDirectoryRights -band [System.DirectoryServices.ActiveDirectoryRights]::ExtendedRight) {
        Write-Warning "$($ace.IdentityReference) holds Enroll combined with other rights. Fix it on the template's Security tab."
    }
}
Set-Acl -Path $path -AclObject $acl -ErrorAction Stop -WhatIf

Require approval. This turns on CA certificate manager approval. Every request then waits for a person. To undo it, set the flag back to the value it had before.

PowerShell
# Dry run. Drop -WhatIf to apply.
# Needs Enterprise Admins, or write rights on the template object.
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
$t = Get-ADObject -SearchBase "CN=Certificate Templates,$pks" -LDAPFilter '(cn=AnyPurposeCustom)' -Properties msPKI-Enrollment-Flag
if (-not $t) { throw 'Template not found.' }
# 0x2 is CT_FLAG_PEND_ALL_REQUESTS: every request waits for a certificate manager
Set-ADObject -Identity $t -Replace @{ 'msPKI-Enrollment-Flag' = ($t.'msPKI-Enrollment-Flag' -bor 2) } -ErrorAction Stop -WhatIf

Unpublish it. This stops one CA from issuing the template. To undo it, run the same command with -Add. Repeat it for each CA in PublishedOn.

PowerShell
# Dry run. Drop -WhatIf to apply.
# Needs Enterprise Admins, or write rights on the CA's enrollment services object.
Import-Module ActiveDirectory -ErrorAction Stop
$pks = "CN=Public Key Services,CN=Services,$((Get-ADRootDSE).configurationNamingContext)"
$ca = Get-ADObject -SearchBase "CN=Enrollment Services,$pks" -LDAPFilter '(cn=Contoso-CA)'
if (-not $ca) { throw 'CA not found.' }
Set-ADObject -Identity $ca -Remove @{ certificateTemplates = 'AnyPurposeCustom' } -ErrorAction Stop -WhatIf

2. Revoke certificates you can't explain

For each serial number you noted, revoke the certificate on the CA with certutil -revoke, then publish a new CRL with certutil -crl. Domain controllers and other systems may keep using a cached CRL until it expires, so publish the new one right away and run certutil -setreg chain\ChainCacheResyncFiletime @now on each domain controller. Also revoke any certificate with no EKU held by someone who isn't a CA administrator. If the on-behalf-of list shows a certificate for a privileged account that nobody can explain, treat it as a possible domain compromise.

3. Re-run the find script

The template should no longer appear, or should show only the groups you meant to allow.

Keep ADCS ESC2 closed

  • Log what the CA issues. Turn on the CA's audit filter and the Audit Certification Services policy on the CA. Events 4886 and 4887 then record each request and each certificate issued.
  • Watch template changes. Audit changes to the Certificate Templates container with Audit Directory Service Changes (event 5136). A template that loses its EKUs or gains broad Enroll rights is how ESC2 comes back.
  • Restrict enrollment agents on the CA. Limit which agents can request certificates for which users. That limits what an Any Purpose certificate can do if one slips through.
  • Re-run the scan. It flags the template again if it reopens.

ADCS ESC2 checklist

  • Is every published template with Any Purpose or no EKU limited to the accounts that need it?
  • Do Domain Users, Domain Computers, Authenticated Users and Everyone lack Enroll on those templates?
  • Is every subordinate CA template limited to CA administrators?
  • Did you review who holds certificates from each ESC2 template?
  • Did you check for certificates requested on behalf of privileged accounts?
  • Did you revoke the ones you can't explain and publish a new CRL?
  • Are CA issuance and template changes logged and watched?

A "no" on the first two questions means any domain user can potentially get a certificate that works for anything.

Compliance mapping

FrameworkReferenceWhy it applies
MITRE ATT&CKT1649Steal or Forge Authentication Certificates. An Any Purpose certificate can be used to get certificates for other accounts.
CIS Controls v8Control 3, Control 6Data protection and access control management. Certificates that work for anything should only go to the accounts that need them.
NIST CSF 2.0PR.AA-01, PR.AA-02, PR.AA-05Credentials are managed and bound to the right identity. Access follows least privilege. ESC2 hands out credentials with no limit on use.
DISA STIGV-269098Active Directory Forest STIG. Requires CA manager approval on risky certificate templates, which is also one of the fixes for ESC2.

ADCS ESC2 FAQ

What is ADCS ESC2?

A certificate template with the Any Purpose EKU or no EKU that needs no approval and lets broad groups enroll. Anyone who can enroll gets a certificate that works for anything.

How is ESC2 different from ESC1?

In ESC1, the requester picks whose name goes on the certificate. In ESC2, the certificate is in their own name but can be used for anything, including requesting certificates for other users.

Is the built-in SubCA template a problem?

Not by default. Only Domain Admins and Enterprise Admins can enroll in it. It becomes ESC2 if broad groups get Enroll.

Does fixing the template revoke existing certificates?

No. Certificates already issued keep working until they expire or you revoke them.

Does Insight Recon check for ADCS ESC2?

Yes. The free scan includes Unrestricted certificate template with any purpose in ADCS (ESC2), along with the other ADCS checks.

See whether any user in your domain can get a certificate that works for anything.

It runs from any Windows machine that can reach your DCs, with a standard domain account. Every core AD check is free for one domain.

Not ready to scan?

AD Signal is our monthly look at what's changing in AD security and how real domains score. Read the latest report.

References