Skip to content

Commit 84e2e6b

Browse files
authored
Merge pull request #20149 from jheysel-r7/esc9_esc10_cert_finder_update
ESC9, ESC10 and ESC16 detection for ldap_esc_vulnerable_cert_finder
2 parents 1361e91 + 13df676 commit 84e2e6b

File tree

6 files changed

+367
-154
lines changed

6 files changed

+367
-154
lines changed

docs/metasploit-framework.wiki/ad-certificates/overview.md

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -52,79 +52,4 @@ Microsoft provides a very useful [training module](https://learn.microsoft.com/e
5252
that covers the fundamentals of AD CS and as well as examples which cover the management of certificate enrollment, certificate revocation and certificate trusts.
5353

5454
## Setting up A Vulnerable AD CS Server
55-
The following steps assume that you have installed an AD CS on either a new or existing domain controller.
56-
### Installing AD CS
57-
1. Open the Server Manager
58-
2. Select Add roles and features
59-
3. Select "Active Directory Certificate Services" under the "Server Roles" section
60-
4. When prompted add all of the features and management tools
61-
5. On the AD CS "Role Services" tab, leave the default selection of only "Certificate Authority"
62-
6. Completion the installation and reboot the server
63-
7. Reopen the Server Manager
64-
8. Go to the AD CS tab and where it says "Configuration Required", hit "More" then "Configure Active Directory Certificate..."
65-
9. Select "Certificate Authority" in the Role Services tab
66-
10. Select "Enterprise CA" in the "Setup Type" tab (the user must be a Domain Administrator for this option to be available)
67-
11. Keep all of the default settings, noting the value of the "Common name for this CA" on the "CA Name" tab (this value corresponds to the `CA` datastore option)
68-
12. Accept the rest of the default settings and complete the configuration
69-
70-
### Setting up a ESC1 Vulnerable Certificate Template
71-
1. Open up the run prompt and type in `certsrv`.
72-
2. 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 click `Manage`.
73-
3. Scroll down to the `User` certificate. Right click on it and select `Duplicate Template`.
74-
4. From here you can refer to the following [Active-Directory-Certificate-Services-abuse](https://github.com/RayRRT/Active-Directory-Certificate-Services-abuse/blob/3da1d59f1b66dd0e381b2371b8fb42d87e2c9f82/ADCS.md) documentation for screenshots.
75-
5. Select the `General` tab and rename this to something meaningful like `ESC1-Template`, then click the `Apply` button.
76-
6. In the `Subject Name` tab, select `Supply in the request` and click `Ok` on the security warning that appears. Then click the `Apply` button.
77-
7. Scroll to the `Extensions` tab and under `Application Policies` ensure that `Client Authentication`, `Server Authentication`, `KDC Authentication`, or `Smart Card Logon` is listed. Then click the `Apply` button.
78-
8. Under the `Security` tab make sure that `Domain Users` group listed and the `Enroll` permissions is marked as allowed for this group.
79-
9. Under `Issuance Requirements` tab, ensure that under `Require the following for enrollment` that the `CA certificate manager approval` box is unticked, as is the `This number of authorized signatures` box.
80-
10. Click `Apply` and then `Ok`
81-
11. Go back to the `certsrv` screen and right click on the `Certificate Templates` folder. Then click `New` followed by `Certificate Template to Issue`.
82-
12. Scroll down and select the `ESC1-Template` certificate, or whatever you named the ESC1 template you created, and select `OK`. The certificate should now be available to be issued by the CA server.
83-
84-
### Setting up a ESC2 Vulnerable Certificate Template
85-
1. Open up `certsrv`
86-
2. Scroll down to `Certificate Templates` folder, right click on it and select `Manage`.
87-
3. Find the `ESC1` certificate template you created earlier and right click on that, then select `Duplicate Template`.
88-
4. Select the `General` tab, and then name the template `ESC2-Template`. Then click `Apply`.
89-
5. Go to the `Subject Name` tab and select `Build from this Active Directory Information` and select `Fully distinguished name` under the `Subject 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 the `Supply in the request` option isn't ticked. Then click `Apply`.
90-
6. Go the to `Extensions` tab and click on `Application Policies`. Then click on `Edit`.
91-
7. Delete all the existing application policies by clicking on them one by one and clicking the `Remove` button.
92-
8. Click the `Add` button and select `Any Purpose` from the list that appears. Then click the `OK` button.
93-
9. Click the `Apply` button, and then `OK`. The certificate should now be created.
94-
10. Go back to the `certsrv` screen and right click on the `Certificate Templates` folder. Then click `New` followed by `Certificate Template to Issue`.
95-
11. Scroll down and select the `ESC2-Template` certificate, or whatever you named the ESC2 template you created, and select `OK`. The certificate should now be available to be issued by the CA server.
96-
97-
### Setting up a ESC3 Template 1 Vulnerable Certificate Template
98-
1. Follow the instructions above to duplicate the ESC2 template and name it `ESC3-Template1`, then click `Apply`.
99-
2. Go to the `Extensions` tab, click the Application Policies entry, click the `Edit` button, and remove the `Any Purpose` policy and replace it with `Certificate Request Agent`, then click `OK`.
100-
3. Click `Apply`.
101-
4. Go to `Issuance Requirements` tab and double check that both `CA certificate manager approval` and `This number of authorized signatures` are unchecked.
102-
5. Click `Apply` if any changes were made or the button is not grey'd out, then click `OK` to create the certificate.
103-
6. Go back to the `certsrv` screen and right click on the `Certificate Templates` folder. Then click `New` followed by `Certificate Template to Issue`.
104-
7. Scroll down and select the `ESC3-Template1` certificate, or whatever you named the ESC3 template number 1 template you just created, and select `OK`. The certificate should now be available to be issued by the CA server.
105-
106-
### Setting up a ESC3 Template 2 Vulnerable Certificate Template
107-
1. Follow the instructions above to duplicate the ESC2 template and name it `ESC3-Template2`, then click `Apply`.
108-
2. Go to the `Extensions` tab, click the Application Policies entry, click the `Edit` button, and remove the `Any Purpose` policy and replace it with `Client Authentication`, then click `OK`.
109-
3. Click `Apply`.
110-
4. Go to `Issuance Requirements` tab and double check that both `CA certificate manager approval` is unchecked.
111-
5. Check the `This number of authorized signatures` checkbox and ensure the value specified is 1, and that the `Policy type required in signature` is set to `Application Policy`, and that the `Application policy` value is `Certificate Request Agent`.
112-
6. Click `Apply` and then click `OK` to issue the certificate.
113-
7. Go back to the `certsrv` screen and right click on the `Certificate Templates` folder. Then click `New` followed by `Certificate Template to Issue`.
114-
8. Scroll down and select the `ESC3-Template2` certificate, or whatever you named the ESC3 template number 2 template you just created, and select `OK`. The certificate should now be available to be issued by the CA server.
115-
116-
### Setting up a ESC8 Vulnerable Host
117-
1. Follow instructions for creating an AD CS enabled server
118-
2. Select Add Roles and Features
119-
3. Under "Select Server Roles" expand Active Directory Certificate Services and add `Certificate Enrollment Policy Web Service`, `Certificate Enrollment Web Service`, and `Certificate Authority Web Enrollment`.
120-
4. For each selection, accept the default for any pop-up.
121-
5. Accept the default features and install.
122-
6. When the installation is complete, click on the warning in the Dashboard for post-deployment configuration.
123-
7. Under Credentials, accept the default
124-
8. Under Role Services, select `Certificate Authority Web Enrollment`, `Certificate Enrollment Web Service`, and `Certificate Enrollment Policy Web Service`
125-
9. In CA for CES, accept the defaults
126-
10. In Authentication Types, accept the default integrated authentication
127-
11. In Service account for CES, select `Use built-in application pool identity`
128-
12. Accept default integrated authentication for CEP
129-
13. Select the domain certificate in Server Certificate (the one that starts with the domain name by default) if more than one appears.
130-
14. Accept the remaining defaults.
55+
The steps for setting up a vulnerable AD CS server are covered in the [[Installing AD CS|./ldap_esc_vulnerable_cert_finder.md]] section.

documentation/modules/auxiliary/gather/ldap_esc_vulnerable_cert_finder.md

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,56 @@ a normal user account by analyzing the objects in LDAP.
9090
1. Scroll down and select the `ESC3-Template2` certificate, and select `OK`.
9191
1. The certificate should now be available to be issued by the CA server.
9292

93+
### Setting up a ESC9 Vulnerable Certificate Template
94+
1. Open up the run prompt and type in `certsrv`.
95+
1. In the window that appears you should see your list of certification authorities under `Certification Authority (Local)`.
96+
1. Right click on the folder in the drop down marked `Certificate Templates` and then click `Manage`.
97+
1. Scroll down to the `User` certificate. Right click on it and select `Duplicate Template`.
98+
1. The `User` certificate already has the `Client Authentication` EKU enabled so we can use this as a base template.
99+
1. Select the Subject Name tab and select `Build from this Active Directory Information`, under the `Subject Name Format` section select `User Principal Name (UPN)` (or `DNS Name` depending on what scenario you're attempting to exploit).
100+
1. Under the `Subject Name Format` also be sure to unselect `Include e-mail name in subject name` and `E-mail name`.
101+
1. Select the `General` tab and rename this to something meaningful like `ESC9-Template`, then click the `Apply` button.
102+
1. Select the Security tab and click the `Add` button.
103+
1. Enter `user2` (or whatever user's UPN you will be changing for this attack). Click OK.
104+
1. Under Permissions for `user2` select `Allow` for `Enroll` and `Read`.
105+
1. Click `Apply` and then `OK`.
106+
1. Open Active Directory Users and Computers, expand the domain on the left hand side.
107+
1. Enable advanced features to access the security tab by checking "View" > "Advanced Features"
108+
1. Right click `Users` and navigate `user2` and select `Properties`.
109+
1. In the security tab, select `Add` and enter `user1` (or whatever user you will be using to perform the attack). Click OK.
110+
1. Under Permissions for `user1` select `Allow` for `Read` and `Write` (or select `Allow` for `Full Control`).
111+
1. Open a Powershell prompt as Administrator and run the following (change `kerberos.issue` to your domain name):
112+
```powershell
113+
$template = [ADSI]"LDAP://CN=ESC9-Template,CN=Certificate Templates,CN=Public Key Services,CN=Services,CN=Configuration,DC=kerberos,DC=issue"
114+
$template.Put("msPKI-Enrollment-Flag", 0x80000)
115+
$template.SetInfo()
116+
```
117+
#### Configuring Windows to be Vulnerable to ESC9
118+
1. The template should now be reported as `Potentially Vulnerable` by the module.
119+
1. In order to be able to exploit this template run the following Powershell command and ensure `StrongCertificateBindingEnforcement` is not set to `2` (it should be 1, or 0):
120+
```powershell
121+
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement -Value 1
122+
Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement
123+
```
124+
125+
### Setting up a ESC10 Vulnerable Certificate Template
126+
1. Follow the first 15 steps `Setting up a ESC9 Vulnerable Certificate Template` to create the `ESC10-Template`.
127+
1. Everything up to and excluding the `msPKI-Enrollment-Flag", 0x80000` powershell step.
128+
#### Configuring Windows to be Vulnerable to ESC10
129+
1. The template should now be reported as `Potentially Vulnerable` by the module.
130+
##### ESC10 Case1:
131+
1. In order to be able to exploit this template run the following Powershell command and ensure `StrongCertificateBindingEnforcement` is set to `0`
132+
```powershell
133+
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement -Value 0
134+
Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Services\Kdc\" -Name StrongCertificateBindingEnforcement
135+
```
136+
##### ESC10 Case2:
137+
1. In order to be able to exploit this template run the following Powershell command and ensure `CertificateMappingMethods` is set to `0x4`
138+
```powershell
139+
Set-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\" -Name CertificateMappingMethods -Value 4
140+
Get-ItemProperty -Path "HKLM:SYSTEM\CurrentControlSet\Control\SecurityProviders\Schannel\" -Name CertificateMappingMethods
141+
```
142+
93143
### Setting up a ESC13 Vulnerable Certificate Template
94144
1. Follow the instructions above to duplicate the ESC2 template and name it `ESC13`, then click `Apply`.
95145
1. Go to the `Extensions` tab, click the Issuance Policies entry, click the `Add` button, click the `New...` button.
@@ -131,6 +181,52 @@ a normal user account by analyzing the objects in LDAP.
131181
1. Go back to the `certsrv` screen and right click on the `Certificate Templates` folder and ensure `WebServer` is listed, if it's not, add it.
132182
1. The certificate should now be available to be issued by the CA server.
133183

184+
### Setting up a ESC16 Vulnerable Certificate Template
185+
#### Configuring Windows to be Vulnerable to ESC16
186+
1. 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 its `policy\DisableExtensionList`
187+
1. Run the following Powershell snippet to add the OID to the `DisableExtensionList` if it is not already present:
188+
```powershell
189+
$activePolicyName = Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules" -Name "Active" | Select-Object -ExpandProperty Active
190+
$disableExtensionList = Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules\$activePolicyName" -Name "DisableExtensionList" | Select-Object -ExpandProperty DisableExtensionList
191+
192+
if (-not ($disableExtensionList -contains "1.3.6.1.4.1.311.25.2")) {
193+
$updatedList = $disableExtensionList + @("1.3.6.1.4.1.311.25.2")
194+
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules\$activePolicyName" -Name "DisableExtensionList" -Value $updatedList
195+
Write-Output "OID 1.3.6.1.4.1.311.25.2 has been added to the DisableExtensionList."
196+
} else {
197+
Write-Output "OID 1.3.6.1.4.1.311.25.2 is already present in the DisableExtensionList."
198+
}
199+
```
200+
#### ESC16 Scenario 1
201+
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.
202+
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).
203+
So if `StrongCertificateBindingEnforcement` is not set to `2` we can exploit this weak mapping.
204+
205+
In order to create a template vulnerable to ESC16 scenario 1, follow the first 15 steps in `Setting up a ESC9 Vulnerable Certificate Template`,
206+
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`.
207+
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`
208+
209+
### ESC16 Scenario 2
210+
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.
211+
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.
212+
213+
Ensure the `EDITF_ATTRIBUTESUBJECTALTNAME2` flag is set by running following PowerShell command:
214+
```powershell
215+
$EDITF_ATTRIBUTESUBJECTALTNAME2 = 0x00040000
216+
$activePolicyName = (Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules" -Name "Active").Active
217+
$editFlagsPath = "HKLM:\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\*\PolicyModules\$activePolicyName"
218+
$editFlags = (Get-ItemProperty -Path $editFlagsPath -Name "EditFlags").EditFlags
219+
220+
if ($editFlags -band $EDITF_ATTRIBUTESUBJECTALTNAME2) {
221+
Write-Output "The EDITF_ATTRIBUTESUBJECTALTNAME2 flag is already enabled."
222+
} else {
223+
# Enable the flag by setting it in the EditFlags value
224+
$newEditFlags = $editFlags -bor $EDITF_ATTRIBUTESUBJECTALTNAME2
225+
Set-ItemProperty -Path $editFlagsPath -Name "EditFlags" -Value $newEditFlags
226+
Write-Output "The EDITF_ATTRIBUTESUBJECTALTNAME2 flag has been enabled."
227+
}
228+
```
229+
134230
## Module usage
135231

136232
1. Do: Start msfconsole

lib/msf/core/exploit/remote/ldap/active_directory/security_descriptor_matcher.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def apply_ace!(ace)
7575
return if ignore_ace?(ace)
7676

7777
@result = ace.header.ace_type
78-
78+
7979
nil
8080
end
8181

lib/rex/proto/crypto_asn1/o_i_ds.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ class OIDs
7979
OID_AES192_CCM = ObjectId.new('2.16.840.1.101.3.4.1.27', name: 'OID_AES192_CCM', label: 'AES192 in CCM mode')
8080
OID_AES256_GCM = ObjectId.new('2.16.840.1.101.3.4.1.46', name: 'OID_AES256_GCM', label: 'AES256 in GCM mode')
8181
OID_AES256_CCM = ObjectId.new('2.16.840.1.101.3.4.1.47', name: 'OID_AES256_CCM', label: 'AES256 in CCM mode')
82+
83+
# https://oidref.com/2.5.29.37.0
84+
OID_ANY_EXTENDED_KEY_USAGE = ObjectId.new('2.5.29.37.0', name: 'OID_ANY_EXTENDED_KEY_USAGE', label: 'Any Extended Key Usage')
8285

8386
def self.name(value)
8487
value = ObjectId.new(value) if value.is_a?(String)

lib/rex/proto/ms_crtd.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ module MsCrtd
3636
CT_FLAG_ALLOW_PREVIOUS_APPROVAL_KEYBASEDRENEWAL_VALIDATE_REENROLLMENT = 0x00010000
3737
CT_FLAG_ISSUANCE_POLICIES_FROM_REQUEST = 0x00020000
3838
CT_FLAG_SKIP_AUTO_RENEWAL = 0x00040000
39+
CT_FLAG_NO_SECURITY_EXTENSION = 0x80000
3940

4041
# [2.27 msPKI-Private-Key-Flag Attribute](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-crtd/f6122d87-b999-4b92-bff8-f465e8949667)
4142
CT_FLAG_REQUIRE_PRIVATE_KEY_ARCHIVAL = 0x00000001

0 commit comments

Comments
 (0)