Vulnerable Application
The auxiliary/gather/ldap_esc_vulnerable_cert_finder
module allows users to query a LDAP server for vulnerable certificate templates and will print these certificates out in a table along with which attack they are vulnerable to and the SIDs that can be used to enroll in that certificate template.
Additionally the module will also print out a list of known certificate servers along with info about which vulnerable certificate templates the certificate server allows enrollment in and which SIDs are authorized to use that certificate server to perform this enrollment operation.
Currently the module is capable of checking for certificates that are vulnerable to ESC1, ESC2, ESC3, ESC13, and ESC15. The module is limited to checking for these techniques due to them being identifiable remotely from a normal user account by analyzing the objects in LDAP.
Installing AD CS
- Install AD CS on either a new or existing domain controller
- Open the Server Manager
- Select Add roles and features
- Select “Active Directory Certificate Services” under the “Server Roles” section
- When prompted add all of the features and management tools
- On the AD CS “Role Services” tab, leave the default selection of only “Certificate Authority”
- Completion the installation and reboot the server
- Reopen the Server Manager
- Go to the AD CS tab and where it says “Configuration Required”, hit “More” then “Configure Active Directory Certificate…”
- Select “Certificate Authority” in the Role Services tab
- Keep all of the default settings, noting the “Common name for this CA” value on the “CA Name” tab.
- Accept the rest of the default settings and complete the configuration
Setting up a ESC1 Vulnerable Certificate Template
- Open up the run prompt and type in
certsrv
. - In the window that appears you should see your list of certification authorities under
Certification Authority (Local)
. - Right click on the folder in the drop down marked
Certificate Templates
and then clickManage
. - Scroll down to the
User
certificate. Right click on it and selectDuplicate Template
. - From here you can refer to https://github.com/RayRRT/Active-Directory-Certificate-Services-abuse/blob/3da1d59f1b66dd0e381b2371b8fb42d87e2c9f82/ADCS.md for screenshots.
- Select the
General
tab and rename this to something meaningful likeESC1-Template
, then click theApply
button. - In the
Subject Name
tab, selectSupply in the request
and clickOk
on the security warning that appears. - Click the
Apply
button. - Scroll to the
Extensions
tab. - Under
Application Policies
ensure thatClient Authentication
,Server Authentication
,KDC Authentication
, orSmart Card Logon
is listed. - Click the
Apply
button. - Under the
Security
tab make sure thatDomain Users
group listed and theEnroll
permissions is marked as allowed for this group. - Under
Issuance Requirements
tab, ensure that underRequire the following for enrollment
that theCA certificate manager approval
box is unticked, as is theThis number of authorized signatures
box. - Click
Apply
and thenOk
- Go back to the
certsrv
screen and right click on theCertificate Templates
folder. Then clickNew
followed byCertificate Template to Issue
. - Scroll down and select the
ESC1-Template
certificate, or whatever you named the ESC1 template you created, and selectOK
. The certificate should now be available to be issued by the CA server.
Setting up a ESC2 Vulnerable Certificate Template
- Open up
certsrv
- Scroll down to
Certificate Templates
folder, right click on it and selectManage
. - Find the
ESC1
certificate template you created earlier and right click on that, then selectDuplicate Template
. - Select the
General
tab, and then name the templateESC2-Template
. Then clickApply
. - Go to the
Subject Name
tab and selectBuild from this Active Directory Information
and selectFully distinguished name
under theSubject Name Format
. The main idea of setting this option is to prevent being able to supply the subject name in the request as this is more what makes the certificate vulnerable to ESC1. The specific options here I don’t think will matter so much so long as theSupply in the request
option isn’t ticked. Then clickApply
. - Go the to
Extensions
tab and click onApplication Policies
. Then click onEdit
. - Delete all the existing application policies by clicking on them one by one and clicking the
Remove
button. - Click the
Add
button and selectAny Purpose
from the list that appears. Then click theOK
button. - Click the
Apply
button, and thenOK
. The certificate should now be created. - Go back to the
certsrv
screen and right click on theCertificate Templates
folder. Then clickNew
followed byCertificate Template to Issue
. - Scroll down and select the
ESC2-Template
certificate, or whatever you named the ESC2 template you created, and selectOK
. The certificate should now be available to be issued by the CA server.
Setting up a ESC3 Template 1 Vulnerable Certificate Template
- Follow the instructions above to duplicate the ESC2 template and name it
ESC3-Template1
, then clickApply
. - Go to the
Extensions
tab, click the Application Policies entry, click theEdit
button, and remove theAny Purpose
policy and replace it withCertificate Request Agent
, then clickOK
. - Click
Apply
. - Go to
Issuance Requirements
tab and double check that bothCA certificate manager approval
andThis number of authorized signatures
are unchecked. - Click
Apply
if any changes were made or the button is not grey’d out, then clickOK
to create the certificate. - Go back to the
certsrv
screen and right click on theCertificate Templates
folder. Then clickNew
followed byCertificate Template to Issue
. - Scroll down and select the
ESC3-Template1
certificate, or whatever you named the ESC3 template number 1 template you just created, and selectOK
. The certificate should now be available to be issued by the CA server.
Setting up a ESC3 Template 2 Vulnerable Certificate Template
- Follow the instructions above to duplicate the ESC2 template and name it
ESC3-Template2
, then clickApply
. - Go to the
Extensions
tab, click the Application Policies entry, click theEdit
button, and remove theAny Purpose
policy and replace it withClient Authentication
, then clickOK
. - Click
Apply
. - Go to
Issuance Requirements
tab and double check that bothCA certificate manager approval
is unchecked. - Check the
This number of authorized signatures
checkbox and ensure the value specified is 1, and that thePolicy type required in signature
is set toApplication Policy
, and that theApplication policy
value isCertificate Request Agent
. - Click
Apply
and then clickOK
to issue the certificate. - Go back to the
certsrv
screen and right click on theCertificate Templates
folder. - Click
New
followed byCertificate Template to Issue
. - Scroll down and select the
ESC3-Template2
certificate, and selectOK
. - The certificate should now be available to be issued by the CA server.
Setting up a ESC4 Vulnerable Certificate Template
- Follow the instructions above to duplicate the ESC2 template and name it
ESC4-Template
, then clickApply
. - Go to the
Security
tab. - Under
Groups or usernames
selectAuthenticated Users
- Under
Permissions for Authenticated Users
selectWrite
->Allow
. - Click
Apply
and then clickOK
to issue the certificate. - Go back to the
certsrv
screen and right click on theCertificate Templates
folder. - Click
New
followed byCertificate Template to Issue
. - Scroll down and select the
ESC3-Template2
certificate, and selectOK
. - The certificate should now be available to be issued by the CA server.
Setting up a ESC9 Vulnerable Certificate Template
- Open up the run prompt and type in
certsrv
. - In the window that appears you should see your list of certification authorities under
Certification Authority (Local)
. - Right click on the folder in the drop down marked
Certificate Templates
and then clickManage
. - Scroll down to the
User
certificate. Right click on it and selectDuplicate Template
. - The
User
certificate already has theClient Authentication
EKU enabled so we can use this as a base template. - Select the Subject Name tab and select
Build from this Active Directory Information
, under theSubject Name Format
section selectUser Principal Name (UPN)
(orDNS Name
depending on what scenario you’re attempting to exploit). - Under the
Subject Name Format
also be sure to unselectInclude e-mail name in subject name
andE-mail name
. - Select the
General
tab and rename this to something meaningful likeESC9-Template
, then click theApply
button. - Select the Security tab and click the
Add
button. - Enter
user2
(or whatever user’s UPN you will be changing for this attack). Click OK. - Under Permissions for
user2
selectAllow
forEnroll
andRead
. - Click
Apply
and thenOK
. - Open Active Directory Users and Computers, expand the domain on the left hand side.
- Enable advanced features to access the security tab by checking “View” > “Advanced Features”
- Right click
Users
and navigateuser2
and selectProperties
. - In the security tab, select
Add
and enteruser1
(or whatever user you will be using to perform the attack). Click OK. - Under Permissions for
user1
selectAllow
forRead
andWrite
(or selectAllow
forFull Control
). - Open a Powershell prompt as Administrator and run the following (change
kerberos.issue
to your domain name):$template = [ADSI]"LDAP://CN=ESC9-Template,CN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,DC=kerberos,DC=issue" $template.Put("msPKI-Enrollment-Flag", 0x80000) $template.SetInfo()
Configuring Windows to be Vulnerable to ESC9
- The template should now be reported as
Potentially Vulnerable
by the module. - In order to be able to exploit this template run the following Powershell command and ensure
StrongCertificateBindingEnforcement
is not set to2
(it should be 1, or 0):Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement -Value 1 Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement
Setting up a ESC10 Vulnerable Certificate Template
- Follow the first 15 steps
Setting up a ESC9 Vulnerable Certificate Template
to create theESC10-Template
. - The template should now be reported as
Potentially Vulnerable
by the module.ESC10 Case1:
- In order to be able to exploit this template run the following Powershell command and ensure
StrongCertificateBindingEnforcement
is set to0
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement -Value 0 Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement
ESC10 Case2:
- In order to be able to exploit this template run the following Powershell command and ensure
CertificateMappingMethods
is set to0x4
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\" -Name CertificateMappingMethods -Value 4 Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\" -Name CertificateMappingMethods
Setting up a ESC13 Vulnerable Certificate Template
- Follow the instructions above to duplicate the ESC2 template and name it
ESC13
, then clickApply
. - Go to the
Extensions
tab, click the Issuance Policies entry, click theAdd
button, click theNew...
button. - Name the new issuance policy
ESC13-Issuance-Policy
. - Copy the Object Identifier as this will be needed later (ex: 11.3.6.1.4.1.311.21.8.12682474.6065318.6963902.6406785.3291287.83.1172775.12545198`).
- Leave the CPS location field blank.
- Click
Apply
. - Open Active Directory Users and Computers, expand the domain on the left hand side.
- Right click
Users
and navigate to New -> Group. - Enter
ESC13-Group
for the Group Name. - Select
Universal
for Group scope andSecurity
for Group type. - Click
Apply
. - Open ADSI Edit.
- In the left hand side right click
ADSI Edit
and selectConnect to...
. - Under
Select a well known naming context
selectDefault naming context
. - Select the newly established connection, select the domain, select
CN=User
. - On the right hand side find the recently created security group
CN=ESC13-Group
, right click select properties. - Copy the value of the
distinguishedName
attribute, save this as we’ll need it later. - Back on the left hand side establish another connection, right click
ADSI Edit
and selectConnect to...
. - This time under
Select a well known naming context
selectConfiguration
. - Select the newly established connection, select the domain, select
CN=Services
->CN=Public Key Services
->CN=OID
. - In the right hand side find the object that corresponds to the Object Identifier saved earlier.
- The OID saved earlier ended in
12545198
, the object on the right will start withCN=12545198.
followed by 34 hex characters. ex:CN=12545198.7BCA239924D9515E63EA6B6F00748837
). - Once located right click -> properties, select
msDS-OIDToGroupLink
. - Paste the
distingushedName
of the security group saved above (ex:CN=ESC13-Group,CN=Users,DC=demo,DC=lab
). - Click
Apply
. - Go back to the
certsrv
screen and right click on theCertificate Templates
folder. - Click
New
followed byCertificate Template to Issue
. - Scroll down and select the
ESC13-Template
certificate, and selectOK
. - The certificate should now be available to be issued by the CA server.
Setting up a ESC15 Vulnerable Certificate Template
- ESC15 depends on the schema version of the template being version 1 - which can no longer be created so we will edit an existing template that is schema version 1.
- Right click the
WebServer
template, select properties. - Go to the Security Tab.
- Under
Groups or usernames
selectAuthenticated Users
. - Under
Permissions for Authenticated Users
selectEnroll
->Allow
. - Click Apply.
- Go back to the
certsrv
screen and right click on theCertificate Templates
folder and ensureWebServer
is listed, if it’s not, add it. - The certificate should now be available to be issued by the CA server.
Setting up a ESC16 Vulnerable Certificate Template
Configuring Windows to be Vulnerable to ESC16
- There are two ECS16 scenarios and both depend on the CA having the OID:
1.3.6.1.4.1.311.25.2
being present in itspolicy\DisableExtensionList
- Run the following Powershell snippet to add the OID to the
DisableExtensionList
if it is not already present: ```powershell $activePolicyName = Get-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration*\PolicyModules” -Name “Active” | Select-Object -ExpandProperty Active $disableExtensionList = Get-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration*\PolicyModules$activePolicyName” -Name “DisableExtensionList” | Select-Object -ExpandProperty DisableExtensionList
if (-not ($disableExtensionList -contains “1.3.6.1.4.1.311.25.2”)) { $updatedList = $disableExtensionList + @(“1.3.6.1.4.1.311.25.2”) Set-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration*\PolicyModules$activePolicyName” -Name “DisableExtensionList” -Value $updatedList Write-Output “OID 1.3.6.1.4.1.311.25.2 has been added to the DisableExtensionList.” } else { Write-Output “OID 1.3.6.1.4.1.311.25.2 is already present in the DisableExtensionList.” }
#### ESC16 Scenario 1
When a CA has the OID `1.3.6.1.4.1.311.25.2` added to its `policy\DisableExtensionList` registry setting every certificate issued by this CA will lack this SID security extension.
This effectively makes all templates published by this CA behave as if they were individually configured with the `CT_FLAG_NO_SECURITY_EXTENSION` flag (as seen in ESC9).
So if `StrongCertificateBindingEnforcement` is not set to `2` we can exploit this weak mapping.
In order to create a template vulnerable to ESC16 scenario 1, follow the first 15 steps in `Setting up a ESC9 Vulnerable Certificate Template`,
which is all the steps up to and excluding the `msPKI-Enrollment-Flag", 0x80000` powershell step which is how you set the `CT_FLAG_NO_SECURITY_EXTENSION`.
Ensure that `StrongCertificateBindingEnforcement` is set to `0` or `1` (not `2`) by running the following command listed in `Configuring Windows to be Vulnerable to ESC9`
### ESC16 Scenario 2
When a CA has the OID `1.3.6.1.4.1.311.25.2` added to its `policy\DisableExtensionList` and `StrongCertificateBindingEnforcement` is set to `2`, there is still a way to exploit the template.
If the policy module's `EditFlags` has the `EDITF_ATTRIBUTESUBJECTALTNAME2` flag set (which is essentially ESC6), then the template is vulnerable to ESC16 scenario 2.
Ensure the `EDITF_ATTRIBUTESUBJECTALTNAME2` flag is set by running following PowerShell command:
```powershell
$EDITF_ATTRIBUTESUBJECTALTNAME2 = 0x00040000
$activePolicyName = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules" -Name "Active").Active
$editFlagsPath = "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules\$activePolicyName"
$editFlags = (Get-ItemProperty -Path $editFlagsPath -Name "EditFlags").EditFlags
if ($editFlags -band $EDITF_ATTRIBUTESUBJECTALTNAME2) {
Write-Output "The EDITF_ATTRIBUTESUBJECTALTNAME2 flag is already enabled."
} else {
# Enable the flag by setting it in the EditFlags value
$newEditFlags = $editFlags -bor $EDITF_ATTRIBUTESUBJECTALTNAME2
Set-ItemProperty -Path $editFlagsPath -Name "EditFlags" -Value $newEditFlags
Write-Output "The EDITF_ATTRIBUTESUBJECTALTNAME2 flag has been enabled."
}
Module usage
- Do: Start msfconsole
- Do:
use auxiliary/gather/ldap_esc_vulnerable_cert_finder
- Do:
set BIND_DN <DOMAIN>\\<USERNAME to log in as>
- Do:
set BIND_PW <PASSWORD FOR USER>
- Do:
set RHOSTS <target IP(s)>
- Optional:
set RPORT <target port>
if target port is non-default. - Optional:
set SSL true
if the target port is SSL enabled. - Do:
run
Options
REPORT
What templates to report (applies filtering to results).
- all - Report all certificate templates.
- published - Report certificate templates that are published by at least one CA server.
- enrollable - Same as above, but omits templates that the user does not have permissions to enroll in.
- vulnerable - Report certificate templates where at least one misconfiguration is appears to be present.
- vulnerable-and-published - Same as above, but omits templates that are not published by at least one CA server.
- vulnerable-and-enrollable - Same as above, but omits templates that the user does not have permissions to enroll in.
Scenarios
Windows Server 2022 with AD CS
msf auxiliary(gather/ldap_esc_vulnerable_cert_finder) > run
[*] Running module against 192.168.159.10
[*] Discovering base DN automatically
[!] Couldn't find any vulnerable ESC13 templates!
[+] Template: ESC1-Test
[*] Distinguished Name: CN=ESC1-Test,CN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,DC=msflab,DC=local
[*] Manager Approval: Disabled
[*] Required Signatures: 0
[+] Vulnerable to: ESC1
[*] Notes: ESC1: Request can specify a subjectAltName (msPKI-Certificate-Name-Flag)
[*] Certificate Template Enrollment SIDs:
[*] * S-1-5-21-3978004297-3499718965-4169012971-512 (Domain Admins)
[*] * S-1-5-21-3978004297-3499718965-4169012971-513 (Domain Users)
[*] * S-1-5-21-3978004297-3499718965-4169012971-519 (Enterprise Admins)
[+] Issuing CA: msflab-DC-CA (DC.msflab.local)
[*] Enrollment SIDs:
[*] * S-1-5-11 (Authenticated Users)
[*] * S-1-5-21-3978004297-3499718965-4169012971-519 (Enterprise Admins)
[*] * S-1-5-21-3978004297-3499718965-4169012971-512 (Domain Admins)
[+] Template: ESC2-Test
[*] Distinguished Name: CN=ESC2-Test,CN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,DC=msflab,DC=local
[*] Manager Approval: Disabled
[*] Required Signatures: 0
[+] Vulnerable to: ESC2
[*] Notes: ESC2: Template defines the Any Purpose OID or no EKUs (PkiExtendedKeyUsage)
[*] Certificate Template Enrollment SIDs:
[*] * S-1-5-21-3978004297-3499718965-4169012971-512 (Domain Admins)
[*] * S-1-5-21-3978004297-3499718965-4169012971-513 (Domain Users)
[*] * S-1-5-21-3978004297-3499718965-4169012971-519 (Enterprise Admins)
[+] Issuing CA: msflab-DC-CA (DC.msflab.local)
[*] Enrollment SIDs:
[*] * S-1-5-11 (Authenticated Users)
[*] * S-1-5-21-3978004297-3499718965-4169012971-519 (Enterprise Admins)
[*] * S-1-5-21-3978004297-3499718965-4169012971-512 (Domain Admins)
[*] Auxiliary module execution completed