AD CS Domain Escalation
Last updated
Last updated
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)
This is a summary of escalation technique sections of the posts:
Enrolment rights are granted to low-privileged users by the Enterprise CA.
Manager approval is not required.
No signatures from authorized personnel are needed.
Security descriptors on certificate templates are overly permissive, allowing low-privileged users to obtain enrolment rights.
Certificate templates are configured to define EKUs that facilitate authentication:
Extended Key Usage (EKU) identifiers such as Client Authentication (OID 1.3.6.1.5.5.7.3.2), PKINIT Client Authentication (1.3.6.1.5.2.3.4), Smart Card Logon (OID 1.3.6.1.4.1.311.20.2.2), Any Purpose (OID 2.5.29.37.0), or no EKU (SubCA) are included.
The ability for requesters to include a subjectAltName in the Certificate Signing Request (CSR) is allowed by the template:
The Active Directory (AD) prioritizes the subjectAltName (SAN) in a certificate for identity verification if present. This means that by specifying the SAN in a CSR, a certificate can be requested to impersonate any user (e.g., a domain administrator). Whether a SAN can be specified by the requester is indicated in the certificate template's AD object through the mspki-certificate-name-flag
property. This property is a bitmask, and the presence of the CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT
flag permits the specification of the SAN by the requester.
The configuration outlined permits low-privileged users to request certificates with any SAN of choice, enabling authentication as any domain principal through Kerberos or SChannel.
This feature is sometimes enabled to support the on-the-fly generation of HTTPS or host certificates by products or deployment services, or due to a lack of understanding.
It is noted that creating a certificate with this option triggers a warning, which is not the case when an existing certificate template (such as the WebServer
template, which has CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT
enabled) is duplicated and then modified to include an authentication OID.
To find vulnerable certificate templates you can run:
To abuse this vulnerability to impersonate an administrator one could run:
Then you can transform the generated certificate to .pfx
format and use it to authenticate using Rubeus or certipy again:
The Windows binaries "Certreq.exe" & "Certutil.exe" can be used to generate the PFX: https://gist.github.com/b4cktr4ck2/95a9b908e57460d9958e8238f85ef8ee
The enumeration of certificate templates within the AD Forest's configuration schema, specifically those not necessitating approval or signatures, possessing a Client Authentication or Smart Card Logon EKU, and with the CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT
flag enabled, can be performed by running the following LDAP query:
The second abuse scenario is a variation of the first one:
Enrollment rights are granted to low-privileged users by the Enterprise CA.
The requirement for manager approval is disabled.
The need for authorized signatures is omitted.
An overly permissive security descriptor on the certificate template grants certificate enrollment rights to low-privileged users.
The certificate template is defined to include the Any Purpose EKU or no EKU.
The Any Purpose EKU permits a certificate to be obtained by an attacker for any purpose, including client authentication, server authentication, code signing, etc. The same technique used for ESC3 can be employed to exploit this scenario.
Certificates with no EKUs, which act as subordinate CA certificates, can be exploited for any purpose and can also be used to sign new certificates. Hence, an attacker could specify arbitrary EKUs or fields in the new certificates by utilizing a subordinate CA certificate.
However, new certificates created for domain authentication will not function if the subordinate CA is not trusted by the NTAuthCertificates
object, which is the default setting. Nonetheless, an attacker can still create new certificates with any EKU and arbitrary certificate values. These could be potentially abused for a wide range of purposes (e.g., code signing, server authentication, etc.) and could have significant implications for other applications in the network like SAML, AD FS, or IPSec.
To enumerate templates that match this scenario within the AD Forest’s configuration schema, the following LDAP query can be run:
This scenario is like the first and second one but abusing a different EKU (Certificate Request Agent) and 2 different templates (therefore it has 2 sets of requirements),
The Certificate Request Agent EKU (OID 1.3.6.1.4.1.311.20.2.1), known as Enrollment Agent in Microsoft documentation, allows a principal to enroll for a certificate on behalf of another user.
The “enrollment agent” enrolls in such a template and uses the resulting certificate to co-sign a CSR on behalf of the other user. It then sends the co-signed CSR to the CA, enrolling in a template that permits “enroll on behalf of”, and the CA responds with a certificate belong to the “other” user.
Requirements 1:
Enrollment rights are granted to low-privileged users by the Enterprise CA.
The requirement for manager approval is omitted.
No requirement for authorized signatures.
The security descriptor of the certificate template is excessively permissive, granting enrollment rights to low-privileged users.
The certificate template includes the Certificate Request Agent EKU, enabling the request of other certificate templates on behalf of other principals.
Requirements 2:
The Enterprise CA grants enrollment rights to low-privileged users.
Manager approval is bypassed.
The template's schema version is either 1 or exceeds 2, and it specifies an Application Policy Issuance Requirement that necessitates the Certificate Request Agent EKU.
An EKU defined in the certificate template permits domain authentication.
Restrictions for enrollment agents are not applied on the CA.
You can use Certify or Certipy to abuse this scenario:
The users who are allowed to obtain an enrollment agent certificate, the templates in which enrollment agents are permitted to enroll, and the accounts on behalf of which the enrollment agent may act can be constrained by enterprise CAs. This is achieved by opening the certsrc.msc
snap-in, right-clicking on the CA, clicking Properties, and then navigating to the “Enrollment Agents” tab.
However, it is noted that the default setting for CAs is to “Do not restrict enrollment agents.” When the restriction on enrollment agents is enabled by administrators, setting it to “Restrict enrollment agents,” the default configuration remains extremely permissive. It allows Everyone access to enroll in all templates as anyone.
The security descriptor on certificate templates defines the permissions specific AD principals possess concerning the template.
Should an attacker possess the requisite permissions to alter a template and institute any exploitable misconfigurations outlined in prior sections, privilege escalation could be facilitated.
Notable permissions applicable to certificate templates include:
Owner: Grants implicit control over the object, allowing for the modification of any attributes.
FullControl: Enables complete authority over the object, including the capability to alter any attributes.
WriteOwner: Permits the alteration of the object's owner to a principal under the attacker's control.
WriteDacl: Allows for the adjustment of access controls, potentially granting an attacker FullControl.
WriteProperty: Authorizes the editing of any object properties.
An example of a privesc like the previous one:
ESC4 is when a user has write privileges over a certificate template. This can for instance be abused to overwrite the configuration of the certificate template to make the template vulnerable to ESC1.
As we can see in the path above, only JOHNPC
has these privileges, but our user JOHN
has the new AddKeyCredentialLink
edge to JOHNPC
. Since this technique is related to certificates, I have implemented this attack as well, which is known as Shadow Credentials. Here’s a little sneak peak of Certipy’s shadow auto
command to retrieve the NT hash of the victim.
Certipy can overwrite the configuration of a certificate template with a single command. By default, Certipy will overwrite the configuration to make it vulnerable to ESC1. We can also specify the -save-old
parameter to save the old configuration, which will be useful for restoring the configuration after our attack.
The extensive web of interconnected ACL-based relationships, which includes several objects beyond certificate templates and the certificate authority, can impact the security of the entire AD CS system. These objects, which can significantly affect security, encompass:
The AD computer object of the CA server, which may be compromised through mechanisms like S4U2Self or S4U2Proxy.
The RPC/DCOM server of the CA server.
Any descendant AD object or container within the specific container path CN=Public Key Services,CN=Services,CN=Configuration,DC=<DOMAIN>,DC=<COM>
. This path includes, but is not limited to, containers and objects such as the Certificate Templates container, Certification Authorities container, the NTAuthCertificates object, and the Enrollment Services Container.
The security of the PKI system can be compromised if a low-privileged attacker manages to gain control over any of these critical components.
The subject discussed in the CQure Academy post also touches on the EDITF_ATTRIBUTESUBJECTALTNAME2
flag's implications, as outlined by Microsoft. This configuration, when activated on a Certification Authority (CA), permits the inclusion of user-defined values in the subject alternative name for any request, including those constructed from Active Directory®. Consequently, this provision allows an intruder to enroll through any template set up for domain authentication—specifically those open to unprivileged user enrollment, like the standard User template. As a result, a certificate can be secured, enabling the intruder to authenticate as a domain administrator or any other active entity within the domain.
Note: The approach for appending alternative names into a Certificate Signing Request (CSR), through the -attrib "SAN:"
argument in certreq.exe
(referred to as “Name Value Pairs”), presents a contrast from the exploitation strategy of SANs in ESC1. Here, the distinction lies in how account information is encapsulated—within a certificate attribute, rather than an extension.
To verify whether the setting is activated, organizations can utilize the following command with certutil.exe
:
This operation essentially employs remote registry access, hence, an alternative approach might be:
Tools like Certify and Certipy are capable of detecting this misconfiguration and exploiting it:
To alter these settings, assuming one possesses domain administrative rights or equivalent, the following command can be executed from any workstation:
To disable this configuration in your environment, the flag can be removed with:
Post the May 2022 security updates, newly issued certificates will contain a security extension that incorporates the requester's objectSid
property. For ESC1, this SID is derived from the specified SAN. However, for ESC6, the SID mirrors the requester's objectSid
, not the SAN.
To exploit ESC6, it is essential for the system to be susceptible to ESC10 (Weak Certificate Mappings), which prioritizes the SAN over the new security extension.
Access control for a certificate authority is maintained through a set of permissions that govern CA actions. These permissions can be viewed by accessing certsrv.msc
, right-clicking a CA, selecting properties, and then navigating to the Security tab. Additionally, permissions can be enumerated using the PSPKI module with commands such as:
This provides insights into the primary rights, namely ManageCA
and ManageCertificates
, correlating to the roles of “CA administrator” and “Certificate Manager” respectively.
Having ManageCA
rights on a certificate authority enables the principal to manipulate settings remotely using PSPKI. This includes toggling the EDITF_ATTRIBUTESUBJECTALTNAME2
flag to permit SAN specification in any template, a critical aspect of domain escalation.
Simplification of this process is achievable through the use of PSPKI’s Enable-PolicyModuleFlag cmdlet, allowing modifications without direct GUI interaction.
Possession of ManageCertificates
rights facilitates the approval of pending requests, effectively circumventing the "CA certificate manager approval" safeguard.
A combination of Certify and PSPKI modules can be utilized to request, approve, and download a certificate:
In the previous attack Manage CA
permissions were used to enable the EDITF_ATTRIBUTESUBJECTALTNAME2 flag to perform the ESC6 attack, but this will not have any effect until the CA service (CertSvc
) is restarted. When a user has the Manage CA
access right, the user is also allowed to restart the service. However, it does not mean that the user can restart the service remotely. Furthermore, ESC6 might not work out of the box in most patched environments due to the May 2022 security updates.
Therefore, another attack is presented here.
Perquisites:
Only ManageCA
permission
Manage Certificates
permission (can be granted from ManageCA
)
Certificate template SubCA
must be enabled (can be enabled from ManageCA
)
The technique relies on the fact that users with the Manage CA
and Manage Certificates
access right can issue failed certificate requests. The SubCA
certificate template is vulnerable to ESC1, but only administrators can enroll in the template. Thus, a user can request to enroll in the SubCA
- which will be denied - but then issued by the manager afterwards.
You can grant yourself the Manage Certificates
access right by adding your user as a new officer.
The SubCA
template can be enabled on the CA with the -enable-template
parameter. By default, the SubCA
template is enabled.
If we have fulfilled the prerequisites for this attack, we can start by requesting a certificate based on the SubCA
template.
This request will be denied, but we will save the private key and note down the request ID.
With our Manage CA
and Manage Certificates
, we can then issue the failed certificate request with the ca
command and the -issue-request <request ID>
parameter.
And finally, we can retrieve the issued certificate with the req
command and the -retrieve <request ID>
parameter.
In environments where AD CS is installed, if a web enrollment endpoint vulnerable exists and at least one certificate template is published that permits domain computer enrollment and client authentication (such as the default Machine
template), it becomes possible for any computer with the spooler service active to be compromised by an attacker!
Several HTTP-based enrollment methods are supported by AD CS, made available through additional server roles that administrators may install. These interfaces for HTTP-based certificate enrollment are susceptible to NTLM relay attacks. An attacker, from a compromised machine, can impersonate any AD account that authenticates via inbound NTLM. While impersonating the victim account, these web interfaces can be accessed by an attacker to request a client authentication certificate using the User
or Machine
certificate templates.
The web enrollment interface (an older ASP application available at http://<caserver>/certsrv/
), defaults to HTTP only, which does not offer protection against NTLM relay attacks. Additionally, it explicitly permits only NTLM authentication through its Authorization HTTP header, rendering more secure authentication methods like Kerberos inapplicable.
The Certificate Enrollment Service (CES), Certificate Enrollment Policy (CEP) Web Service, and Network Device Enrollment Service (NDES) by default support negotiate authentication via their Authorization HTTP header. Negotiate authentication supports both Kerberos and NTLM, allowing an attacker to downgrade to NTLM authentication during relay attacks. Although these web services enable HTTPS by default, HTTPS alone does not safeguard against NTLM relay attacks. Protection from NTLM relay attacks for HTTPS services is only possible when HTTPS is combined with channel binding. Regrettably, AD CS does not activate Extended Protection for Authentication on IIS, which is required for channel binding.
A common issue with NTLM relay attacks is the short duration of NTLM sessions and the inability of the attacker to interact with services that require NTLM signing.
Nevertheless, this limitation is overcome by exploiting an NTLM relay attack to acquire a certificate for the user, as the certificate's validity period dictates the session's duration, and the certificate can be employed with services that mandate NTLM signing. For instructions on utilizing a stolen certificate, refer to:
AD CS Account PersistenceAnother limitation of NTLM relay attacks is that an attacker-controlled machine must be authenticated to by a victim account. The attacker could either wait or attempt to force this authentication:
Force NTLM Privileged AuthenticationCertify’s cas
enumerates enabled HTTP AD CS endpoints:
The msPKI-Enrollment-Servers
property is used by enterprise Certificate Authorities (CAs) to store Certificate Enrollment Service (CES) endpoints. These endpoints can be parsed and listed by utilizing the tool Certutil.exe:
The request for a certificate is made by Certipy by default based on the template Machine
or User
, determined by whether the account name being relayed ends in $
. The specification of an alternative template can be achieved through the use of the -template
parameter.
A technique like PetitPotam can then be employed to coerce authentication. When dealing with domain controllers, the specification of -template DomainController
is required.
The new value CT_FLAG_NO_SECURITY_EXTENSION
(0x80000
) for msPKI-Enrollment-Flag
, referred to as ESC9, prevents the embedding of the new szOID_NTDS_CA_SECURITY_EXT
security extension in a certificate. This flag becomes relevant when StrongCertificateBindingEnforcement
is set to 1
(the default setting), which contrasts with a setting of 2
. Its relevance is heightened in scenarios where a weaker certificate mapping for Kerberos or Schannel might be exploited (as in ESC10), given that the absence of ESC9 would not alter the requirements.
The conditions under which this flag's setting becomes significant include:
StrongCertificateBindingEnforcement
is not adjusted to 2
(with the default being 1
), or CertificateMappingMethods
includes the UPN
flag.
The certificate is marked with the CT_FLAG_NO_SECURITY_EXTENSION
flag within the msPKI-Enrollment-Flag
setting.
Any client authentication EKU is specified by the certificate.
GenericWrite
permissions are available over any account to compromise another.
Suppose John@corp.local
holds GenericWrite
permissions over Jane@corp.local
, with the goal to compromise Administrator@corp.local
. The ESC9
certificate template, which Jane@corp.local
is permitted to enroll in, is configured with the CT_FLAG_NO_SECURITY_EXTENSION
flag in its msPKI-Enrollment-Flag
setting.
Initially, Jane
's hash is acquired using Shadow Credentials, thanks to John
's GenericWrite
:
Subsequently, Jane
's userPrincipalName
is modified to Administrator
, purposely omitting the @corp.local
domain part:
This modification does not violate constraints, given that Administrator@corp.local
remains distinct as Administrator
's userPrincipalName
.
Following this, the ESC9
certificate template, marked vulnerable, is requested as Jane
:
It's noted that the certificate's userPrincipalName
reflects Administrator
, devoid of any “object SID”.
Jane
's userPrincipalName
is then reverted to her original, Jane@corp.local
:
Attempting authentication with the issued certificate now yields the NT hash of Administrator@corp.local
. The command must include -domain <domain>
due to the certificate's lack of domain specification:
Two registry key values on the domain controller are referred to by ESC10:
The default value for CertificateMappingMethods
under HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\SecurityProviders\Schannel
is 0x18
(0x8 | 0x10
), previously set to 0x1F
.
The default setting for StrongCertificateBindingEnforcement
under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Kdc
is 1
, previously 0
.
Case 1
When StrongCertificateBindingEnforcement
is configured as 0
.
Case 2
If CertificateMappingMethods
includes the UPN
bit (0x4
).
With StrongCertificateBindingEnforcement
configured as 0
, an account A with GenericWrite
permissions can be exploited to compromise any account B.
For instance, having GenericWrite
permissions over Jane@corp.local
, an attacker aims to compromise Administrator@corp.local
. The procedure mirrors ESC9, allowing any certificate template to be utilized.
Initially, Jane
's hash is retrieved using Shadow Credentials, exploiting the GenericWrite
.
Subsequently, Jane
's userPrincipalName
is altered to Administrator
, deliberately omitting the @corp.local
portion to avoid a constraint violation.
Following this, a certificate enabling client authentication is requested as Jane
, using the default User
template.
Jane
's userPrincipalName
is then reverted to its original, Jane@corp.local
.
Authenticating with the obtained certificate will yield the NT hash of Administrator@corp.local
, necessitating the specification of the domain in the command due to the absence of domain details in the certificate.
With the CertificateMappingMethods
containing the UPN
bit flag (0x4
), an account A with GenericWrite
permissions can compromise any account B lacking a userPrincipalName
property, including machine accounts and the built-in domain administrator Administrator
.
Here, the goal is to compromise DC$@corp.local
, starting with obtaining Jane
's hash through Shadow Credentials, leveraging the GenericWrite
.
Jane
's userPrincipalName
is then set to DC$@corp.local
.
A certificate for client authentication is requested as Jane
using the default User
template.
Jane
's userPrincipalName
is reverted to its original after this process.
To authenticate via Schannel, Certipy’s -ldap-shell
option is utilized, indicating authentication success as u:CORP\DC$
.
Through the LDAP shell, commands such as set_rbcd
enable Resource-Based Constrained Delegation (RBCD) attacks, potentially compromising the domain controller.
This vulnerability also extends to any user account lacking a userPrincipalName
or where it does not match the sAMAccountName
, with the default Administrator@corp.local
being a prime target due to its elevated LDAP privileges and the absence of a userPrincipalName
by default.
If CA Server Do not configured with IF_ENFORCEENCRYPTICERTREQUEST
, it can be makes NTLM relay attacks without signing via RPC service. Reference in here.
You can use certipy
to enumerate if Enforce Encryption for Requests
is Disabled and certipy will show ESC11
Vulnerabilities.
It need to setup a relay server:
Note: For domain controllers, we must specify -template
in DomainController.
Or using sploutchy's fork of impacket :
Administrators can set up the Certificate Authority to store it on an external device like the "Yubico YubiHSM2".
If USB device connected to the CA server via a USB port, or a USB device server in case of the CA server is a virtual machine, an authentication key (sometimes referred to as a "password") is required for the Key Storage Provider to generate and utilize keys in the YubiHSM.
This key/password is stored in the registry under HKEY_LOCAL_MACHINE\SOFTWARE\Yubico\YubiHSM\AuthKeysetPassword
in cleartext.
Reference in here.
If the CA's private key stored on a physical USB device when you got a shell access, it is possible to recover the key.
In first, you need to obtain the CA certificate (this is public) and then:
Finally, use the certutil -sign
command to forge a new arbitrary certificate using the CA certificate and its private key.
The msPKI-Certificate-Policy
attribute allows the issuance policy to be added to the certificate template. The msPKI-Enterprise-Oid
objects that are responsible for issuing policies can be discovered in the Configuration Naming Context (CN=OID,CN=Public Key Services,CN=Services) of the PKI OID container. A policy can be linked to an AD group using this object's msDS-OIDToGroupLink
attribute, enabling a system to authorize a user who presents the certificate as though he were a member of the group. Reference in here.
In other words, when a user has permission to enroll a certificate and the certificate is link to an OID group, the user can inherit the privileges of this group.
Use Check-ADCSESC13.ps1 to find OIDToGroupLink:
Find a user permission it can use certipy find
or Certify.exe find /showAllPermissions
.
If John
have have permission to enroll VulnerableTemplate
, the user can inherit the privileges of VulnerableGroup
group.
All it need to do just specify the template, it will get a certificate with OIDToGroupLink rights.
The configuration for cross-forest enrollment is made relatively straightforward. The root CA certificate from the resource forest is published to the account forests by administrators, and the enterprise CA certificates from the resource forest are added to the NTAuthCertificates
and AIA containers in each account forest. To clarify, this arrangement grants the CA in the resource forest complete control over all other forests for which it manages PKI. Should this CA be compromised by attackers, certificates for all users in both the resource and account forests could be forged by them, thereby breaking the security boundary of the forest.
In multi-forest environments, caution is required concerning Enterprise CAs that publish certificate templates which allow Authenticated Users or foreign principals (users/groups external to the forest to which the Enterprise CA belongs) enrollment and edit rights. Upon authentication across a trust, the Authenticated Users SID is added to the user’s token by AD. Thus, if a domain possesses an Enterprise CA with a template that allows Authenticated Users enrollment rights, a template could potentially be enrolled in by a user from a different forest. Likewise, if enrollment rights are explicitly granted to a foreign principal by a template, a cross-forest access-control relationship is thereby created, enabling a principal from one forest to enroll in a template from another forest.
Both scenarios lead to an increase in the attack surface from one forest to another. The settings of the certificate template could be exploited by an attacker to obtain additional privileges in a foreign domain.
Learn & practice AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE) Learn & practice GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE)