(AD CS) is Microsoft’s PKI implementation that integrates with existing Active Directory forests; • While AD CS is not installed by default for Active Directory environments, in facts it is widely deployed. It can be used: • User Authentication; • HTTPS certificates; • VPN certificates; • Digital Signatures; • Code Signing • … • Propper AD CS configuration is extremely complex task! • So, there are a lot of AD CS deployments with different misconfigurations; • AD CS misconfigurations can lead to whole domain compromise! Microsoft PKI
Initial Authentication in Kerberos (PKINIT) protocol enables the use of public key cryptography in the initial authentication exchange of the Kerberos protocol. Instead of sharing a secret key between the client and KDC, the client possesses a public key pair that is signed by a trusted Certification Authority. When PKINIT is enabled, it is possible to: • Perform Kerberos authentication using X.509 certificate and obtain a TGT • Create a Schannel Security Context using X.509 certificate for LDAP over SSL (LDAPS) • Recover NTLM from TGT requested using X.509 certificate (UnPAC the hash)
Services has a lot of attack potential • In June 2021, Will Schroeder and Lee Christensen from SpecterOps published a research named ”Certified Pre-Owned”, that demonstrates how an adversary can utilize and abuse the AD CS environment to elevate privileges, get a strong foothold and persistence within a network • ”Of note, nearly every environment with AD CS that we’ve examined for domain escalation misconfigurations has been vulnerable. It’s hard for us to overstate what a big deal these issues are” – SpecterOps Team
= 64 Audit Filter = 16 Audit Filter = 4 Audit Filter = 8 Audit Filter = 32 Audit Filter = 1 To configure Certification Service audit, you must enable ”Audit Certification Services” subcategory of advanced audit policy, and at the level of the CA server, additionally determine which event categories should be logged. It is recommended to select all events to audit!
Search for modifications of the audit filter: EventID:4885 Search for changing of the related registry value: EventID:13 AND TargetObject:("*\\Services\\CertSvc\\Configuration\\*" AND "\\AuditFilter")
4898 event is not suitable for real-time detection of template creation/modification. This event doesn’t fire each time certificate template created, modified or used to issue the certificate. By default, 4898 is triggered in the following cases: o at the time of the first enrollment since CA service start; o at the time of the first enrollment since certificate template modification. • Thus, this means that until the certificate is issued using the corresponding template for the first time after starting the CA service or modification the template, there will be no 4898 event for template. • It is possible to increase the frequency of 4898 events by setting flag EDITF_AUDITCERTTEMPLATELOAD for EditFlags parameter, using certutil or via registry modification. With this setting, event 4898, in addition to the situations already described, will also be generated after CA service start for each template published for enrollment Old Value of the EditFlags mask doesn’t contain EDITF_AUDITCERTTEMPLATE LOAD flag New Value of the EditFlags mask contains EDITF_AUDITCERTTEMPLATE LOAD flag
receives a certificate request. 4887 is logged when a certificate is issued as a result of either: • An administrator or certificate manager issues a pending request; • The CA automatically approves the request based on the CA's policy and that of the certificate template associated with the request. • There is no Certificate Template name in the event and it’s parameters :( • There is no Certificate Request parameters :( • Thus, these events are practically useless from detection point of view! Certifiсate requests/issuing monitoring Events 4886/4887 (the worst ones)
a SAN • Subject Alternative Name (SAN) is an extension to X.509 that allows various identities to be bound to a certificate beyond the subject; • By default during certificate-based authentication, certificates are mapped to Active Directory accounts based on a user principal name (UPN) specified in the SAN; • So, when a certificate template allows requester to specify a SAN, it is possible to request a certificate for another user; • It can be used for privileges escalation if the certificate template defines EKUs that enable domain authentication and can be enrolled by non- privileged user without manager approval. ”simple” user in the Subject “dadmin” user in the SAN
events Unfortunately, there is no simple way to monitor requesting the certificates with an arbitrary SAN. But it is possible to find vulnerable templates, using 4898 event: Requester can specify the SAN in a CSR Manager approval is disabled (no flag CT_FLAG_PEND_A LL_REQUESTS) No authorized signatures are required ”Client Authentication” EKU allows authentication Grants certificate enrollment right to the ”Domain Users” group Grants certificate enrollment right to the ”Domain Users” group
hunt it! Search for certificate templates that met the following conditions: • an overly permissive certificate template security descriptor grants certificate enrollment rights to low- privileged users; • the certificate template allows requesters to specify a SAN in the CSR; • no authorized signatures are required; • manager approval is disabled; • the certificate template defines EKUs that enable authentication – Client Authentication (1.3.6.1.5.5.7.3.2), PKINIT Client Authentication (1.3.6.1.5.2.3.4), Smart Card Logon (1.3.6.1.4.1.311.20.2.2), Any Purpose (2.5.29.37.0) or SubCA (no EKUs). EventID:4898 AND SecurityDescriptor:(";0e10c968-78fb-11d2-90d4-00c04f79dc55;;DU" OR ";0e10c968-78fb- 11d2-90d4-00c04f79dc55;;AU" OR ";0e10c968-78fb-11d2-90d4-00c04f79dc55;;WD") AND TemplateContent:"CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT" AND TemplateContent:"msPKI-RA-Signature = 0" AND -TemplateContent:"CT_FLAG_PEND_ALL_REQUESTS" AND (TemplateContent:("1.3.6.1.5.5.7.3.2" OR "1.3.6.1.5.2.3.4" OR "1.3.6.1.4.1.311.20.2.2" OR "2.5.29.37.0") OR TemplateContent:"pKIExtendedKeyUsage = ")
hunt it! Grants certificate enrollment right to the ”Domain Users” group No authorized signatures are required ”Client Authentication” EKU allows authentication Manager approval is disabled (there is no flag CT_FLAG_PEND_ALL_RE QUESTS) Requester can specify the SAN in a CSR
no EKU (Subordinate CA) • Extended Key Usage (EKU) describes how the certificate can be used (Client Authentication, Smart Card Logon, etc.); • When a certificate template specifies the Any Purpose EKU, or no EKU at all, the certificate can be used for anything; • If the requester can specify a SAN, ESC2 vulnerable certificate can be abused like ESC1; • It can be abused like ESC3 – the ESC2 vulnerable certificate can be used to request another one on behalf of any other user; • There also can be more exotic ways to abuse ESC2 – code signing, server authentication, etc.
events (Any Purpose EKU) Use 4898 event to find vulnerable templates: Grants certificate enrollment right to the ”Domain Users” group Manager approval is disabled (no flag CT_FLAG_PEND_ALL _REQUESTS) No authorized signatures are required Any Purpose EKU
events (no EKU) Manager approval is disabled (no flag CT_FLAG_PEND_ALL _REQUESTS) No authorized signatures are required SubCA Template (no EKUs) Use 4898 event to find vulnerable templates: Grants certificate enrollment right to the ”Domain Users” group
hunt it! Search for certificate templates that met the following conditions: • an overly permissive certificate template security descriptor grants certificate enrollment rights to low-privileged users; • no authorized signatures are required; • manager approval is disabled; • the certificate template defines Any Purpose EKUs (“2.5.29.37.0”) or no EKUs. EventID:4898 AND SecurityDescriptor:(";0e10c968-78fb-11d2-90d4-00c04f79dc55;;DU" OR ";0e10c968- 78fb-11d2-90d4-00c04f79dc55;;AU" OR ";0e10c968-78fb-11d2-90d4-00c04f79dc55;;WD") AND - TemplateContent:"CT_FLAG_PEND_ALL_REQUESTS" AND TemplateContent:"msPKI-RA-Signature = 0" AND (TemplateContent:"2.5.29.37.0" OR TemplateContent:"pKIExtendedKeyUsage = ")
hunt it! Grants certificate enrollment right to the ”Domain Users” group No authorized signatures are required Any Purpose EKU SubCA (no EKUs) Manager approval is disabled (there is no flag CT_FLAG_PEND_ALL_RE QUESTS)
– users who are able to enroll for a certificate on behalf of another user; • There is special EKU “Certificate Request” (1.3.6.1.4.1.311.20.2.1) for Enrollment Agents certificates; • So, if there is a template with “Certificate Request” EKU, that can be enrolled without approval by non-privileged user and there are no any enrollment restrictions – it can be abused for privilege escalation ; • The issued certificate from ESC3 vulnerable template allows to request another certificate on behalf of any user (so, It means that it is possible to impersonate almost any user).
events Use 4898 event to find vulnerable templates: Manager approval is disabled (no flag CT_FLAG_PEND_ALL _REQUESTS) No authorized signatures are required Template defines the Certificate Request Agent EKU Grants certificate enrollment right to the ”Domain Users” group
hunt it! Search for certificate templates that met the following conditions: • an overly permissive certificate template security descriptor grants certificate enrollment rights to low- privileged users; • no authorized signatures are required; • manager approval is disabled; • the certificate template defines the Certificate Request Agent EKU (“1.3.6.1.4.1.311.20.2.1”). EventID:4898 AND SecurityDescriptor:(";0e10c968-78fb-11d2-90d4-00c04f79dc55;;DU" OR ";0e10c968-78fb- 11d2-90d4-00c04f79dc55;;AU" OR ";0e10c968-78fb-11d2-90d4-00c04f79dc55;;WD") AND TemplateContent:"msPKI-RA-Signature = 0" AND -TemplateContent:"CT_FLAG_PEND_ALL_REQUESTS" AND TemplateContent:"1.3.6.1.4.1.311.20.2.1"
hunt it! Grants certificate enrollment right to the ”Domain Users” group No authorized signatures are required Manager approval is disabled (there is no flag CT_FLAG_PEND_ALL_RE QUESTS) Template defines the Certificate Request Agent EKU
events When somebody requests certificate on Behalf of Other User requester and subject are differing in the related 4887 event simpleuser requests certificate on Behalf of dadmin user simpleuser/DC$ requests certificate themselves Requester and Subject are different users Requester and Subject are the same user Requester and Subject are the same user
templates are AD objects, so they have security descriptor, that defines which permissions AD principals have over the template; • Weak permissions (Excessive access rights) can allow non-privileged users to edit sensitive security settings in the template (defines EKUs, allows SAN, disable manager approval), thereby making its vulnerable to the ECS1-3 technique; • The rights we care about are: Right Descrription Owner Implicit full control of the object, can edit any properties FullControl Full control of the object, can edit any properties. WriteOwner Can modify the owner to an attacker-controlled principal WriteDacl Can modify access control to grant an attacker FullControl WriteProperty Can edit any properties
hunt it! Search for certificate templates with weak permissions: EventID:4898 AND SecurityDescriptor:(";CCDCLCSWRPWPDTLOCRSDRCWDWO;;;DU" OR ";CCDCLCSWRPWPDTLOCRSDRCWDWO;;;AU" OR ";CCDCLCSWRPWPDTLOCRSDRCWDWO;;;WD" OR ";WPWDWO;;;DU" OR ";WPWDWO;;;AU" OR ";WPWDWO;;;WD")
to monitor the modifications of the critical Certificate Templates attributes (pKIExtendedKeyUsage, msPKI-Certificate-Name-Flag, msPKI-Enrollment-Flag). This event also generated when Certificate Template is created: https://bit.ly/3NctSVZ https://bit.ly/3LfjkE0
to monitor the modifications of the critical templates attributes. Unfortunately, this event is not suitable for real- time detection of modifications. 4899 is triggered once when the template is changed, and the first enrollment is occurred after this modification. It is also worth noting that the already mentioned event 4898 is also triggered at the same time with 4899
Let’s hunt it! Search for addition of the CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT flag to msPKI-Certificate-Name-Flag attribute (5136 events): EventID:"5136" AND ObjectClass:"pKICertificateTemplate" AND OperationType:"%%14674" AND AttributeLDAPDisplayName:"msPKI- Certificate-Name-Flag" AND AttributeValue_list:"CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT” Search for addition of the CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT flag to msPKI-Certificate-Name-Flag attribute (4899/4900 events): EventID:("4899" OR "4900") AND -OldTemplateContent:"CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT" AND NewTemplateContent:"CT_FLAG_ENROLLEE_SUPPLIES_SUBJECT" Value Added
Search for deletion of the CT_FLAG_PEND_ALL_REQUESTS flag from msPKI-Enrollment-Flag attribute (5136 events): EventID:"5136" AND ObjectClass:"pKICertificateTemplate" AND OperationType:"%%14674" AND AttributeLDAPDisplayName:"msPKI-Enrollment-Flag" AND -AttributeValue_list:*CT_FLAG_PEND_ALL_REQUESTS* Search for deletion of the CT_FLAG_PEND_ALL_REQUESTS flag from msPKI-Enrollment-Flag attribute (4899/4900 events): EventID:("4899" OR "4900") AND OldTemplateContent:"CT_FLAG_PEND_ALL_REQUESTS" AND - NewTemplateContent:"CT_FLAG_PEND_ALL_REQUESTS" There is no CT_FLAG_PEND_ALL_REQUESTS flag Value Added There is no CT_FLAG_PEND_ALL_REQUESTS flag
AttributeValue field contains dangerous EKUs (Any Purpose EKU or Certificate Request Agent EKU): EventID:"5136" AND ObjectClass:"pKICertificateTemplate" AND OperationType:"%%14674" AND AttributeLDAPDisplayName:"pKIExtendedKeyUsage" AND AttributeValue:("2.5.29.37.0" OR "1.3.6.1.4.1.311.20.2.1") Setting dangerous EKUs Let’s hunt it (using 5136 event)! Certificate Request Agent Any Purpose Value Added
4900 events)! Search for 4899/4900 events, where NewTemplateContent field contains dangerous EKUs (Any Purpose EKU or Certificate Request Agent EKU) or no EKUs (SubCA Template): EventID:("4899" OR "4900") AND ( (-OldTemplateContent:"1.3.6.1.4.1.311.20.2.1" AND NewTemplateContent:"1.3.6.1.4.1.311.20.2.1") OR (-OldTemplateContent:"2.5.29.37.0" AND NewTemplateContent:"2.5.29.37.0") OR NewTemplateContent:"pKIExtendedKeyUsage = msPKI-Certificate-Application-Policy") SubCA (no EKUs)
objects outside of certificate templates and the certificate authority itself can have a security impact on the entire AD CS system: • The CA server’s AD computer object • The CA server’s RPC/DCOM server • Any descendant AD object or container in the container CN=Public Key Services, CN=Services, CN=Configuration, DC=demo, DC=local (e.g., the Certificate Templates container, Certification Authorities container, the NTAuthCertificates object, the Enrollment Services Container, etc…) If a low-privileged attacker can gain control over any of these, the attack can likely compromise the PKI system.
authentication, the domain controller checks if NTAuthCertificates object contains an entry for the CA specified in the authenticating certificate’s Issuer field. • If it is, authentication proceeds. If the certificate is not in the NTAuthCertificates object, authentication fails. • An alternative path to forgery is to generate a self-signed CA certificate and add it to the NTAuthCertificates object. Attackers can do this if they have control over the NTAuthCertificates AD object.
for modifications of the NTAuthCertificates object attributes: EventID:5136 AND ObjectDN:"CN=NTAuthCertificates,CN=Public Key Services,CN=Services,CN=Configuration" AND OperationType:"%%14674" The same approach (auditing modifications of the critical objects attributes, using 5136 event) can be used for any other PKI AD Object: • Certificate Templates Container • Certification Authorities • Enrollment Services Container • KRA (Key Recovery Agents) Container • CDP Container • The CA server’s RPC/DCOM server • The CA server’s AD computer object … Value Added <Binary>
If EDITF_ATTRIBUTESUBJECTALTNAME2 flag is enabled on an enterprise CA, alternative names are allowed for any certificate templates, regardless of templates' restrictions itself; • Microsoft strongly not to enable this flag on an Enterprise CA; • This misconfiguration can be abused by adversary for issuing the certificate with an alternative name that would allow them to impersonate another user (like in case of ESC1).
list of enabled EditFlags: CommandLine:(*reg* OR *powershell* OR *certutil*) AND (CommandLine:*EditFlags* OR CommandLine:("*\\Services\\CertSvc\\Configuration\\*" AND "*\\PolicyModules\\*") )
authority itself has permissions that secure various CA actions; • From the security perspective it is necessary to care about the Manage CA (aka “CA Administrator”) and Manage Certificates (aka “Certificate Officer”) permissions; • If an attacker gains control over a principal that has the Manage CA right over the CA, he can remotely change CA configuration, includes flipping the EDITF_ATTRIBUTESUBJECTALTNAME2 to allow SAN specification in any template and thereby making them vulnerable to the ESC6 technique; • If an attacker gains control over a principal that has the Manage Certificates right over the CA, he can remotely approve pending certificate requests, subvertnig the "CA certificate manager approval" protection.
the EDITIF_ATTRIBUTESUBJECTNAME2. Useful events There is no EDITIF_ATTRIBUTES UBJECTNAME2 flag Use Taralogic Certify fork to remotely enable EDITIF_ATTRIBUTESUBJECTNAME2 flag
the EDITIF_ATTRIBUTESUBJECTNAME2. Let’s hunt it! Search for changing of the related registry value: EventID:13 AND TargetObject:("*\\Services\\CertSvc\\Configuration\\*" AND "*\\PolicyModules\\*" AND "\\EditFlags") AND EditFlags:*EDITF_ATTRIBUTESUBJECTALTNAME2* Search for changing of the EditFlags configuration entry, where EDITF_ATTRIBUTESUBJECTALTNAME2 flag is enabled: EventID:4891 AND Node:PolicyModules* AND Entry:EditFlags AND Value_list:*EDITF_ATTRIBUTESUBJECTALTNAME2*
for any 4882 event (it shouldn't happen often), pay attention where SecuritySettings attribute contains insecure permissions (CA Administrator/Certificate Manager for unprivileged users/groups): EventID:4882 AND SecuritySettings:(Everyone OR "Domain Users" OR Authenticated)
• AD CS supports several HTTP-based enrollment methods if additional AD CS server roles are installed. • These HTTP-based certificate enrollment interfaces are all vulnerable NTLM relay attacks. • Using NTLM relay attacker can relay any inbound NTLM authenticating to the AD CS HTTP-based interface and request a certificate for the impersonated user\machine account. http://ca.demo.local/certsrv/
events 58 • Machine accounts are not supposed to use the manual way of requesting certificates via HTTP-based interface, so it doesn’t make sense to render them on the Certificate Authority Web Enrollment service. • However it is possible to request certificate using any published template. To enroll certificate it is needed to send specifically crafted HTTP request to the CA Web Enrollment service. • Detect NTLM Relay to AD CS HTTP Endpoints by collecting AD CS IIS log files and hunt for a cs-username are having $ sign. C:\inetpub\logs\LogFiles\W3SVC1\u_ex*.log
hunt! 59 Any NTLM-relay with forced authentication attack (Printer Bug, PetitPotam, etc…) will lead to NTLM authentication on target host (CA in this case) from victim machine (DC in this case). This behavior is very suspicious, search for suspicious logon events (EventID 4624) on the CA servers from machine accounts that were made using NTLM: EventID:4624 AND TargetUserName.keyword:/.*\$/ AND AuthenticationPackageName:"NTLM" AND Hostname:("ca.demo.local" OR "subca.demo.local")
for TGT requests (EventID 4768) from non computer accounts that were made using PKINIT except of the user’s whitelist: EventID:4768 AND CertIssuerName:* AND -TargetUserName.keyword:/.*\$/ AND -TargetUserName:(" known_user_with_smartcard1 " OR " known_user_with_smartcard2") Search for TGT requests (EventID 4768) from computer accounts that were made using PKINIT: EventID:4768 AND CertIssuerName:* AND TargetUserName.keyword:/.*\$/
any process create event with command line field to find execution of the Certify/ForgeCert tools (or any other tool that may appear in the future) by specific command line arguments, process name (some dummy "hackers" may use tools even without renaming) or OriginalFileName attribute from the VERSIONINFO
unique Certify tool default process name/OriginalFileName or command line arguments: CommandLine:(*certify* OR *pkiobjects* OR (*enrollcert* AND *onbehalfof*) OR (*find* AND *clientauth*) OR (*find* AND *enrolleeSuppliesSubject*) OR (*find* AND *vulnerable*) OR (*find* AND *showAllPermissions*) OR (*find* AND *json* AND *outfile*) OR (*request* AND *altname*)) OR CommandLine.keyword:(/.* \/ca\:.*/ AND /.* \/template\:.*/) OR CommandLine.keyword:(/.* \/ca\:.*/ AND /.* \/id:.*/) OR (CommandLine:*download* AND CommandLine.keyword:(/.* \/ca\:.*/ AND /.* \id\:.*/)) OR OriginalFileName:"Certify.exe" Search for unique ForgeCert tool default process name/ OriginalFileName or command line arguments: CommandLine:(*ForgeCert* OR (*CaCertPath* AND *SubjectAltName*) OR (*NewCertPassword* AND *NewCertPath*) OR (*CaCertPath* AND *CaCertPassword*)) OR OriginalFileName:"ForgeCert.exe"
a replacement of traditional password based authentication with a key based trust model. The public key is stored in the msDS-KeyCredentialLink and private one in the TPM or other certificate store. • When trying to pre-authenticate with PKINIT, the KDC will check that the authenticating user has knowledge of the matching private key, and a TGT will be sent if there is a match. • There are multiple scenarios where an attacker can have control over an account that has the ability to edit the msDS-KeyCredentialLink attribute of other objects (e.g. member of a Key Admins or Enterprise Key Admins domain groups, has overly ACEs, etc.). http://ca.demo.local/certsrv/
AND OperationType:"%%14674" AND AttributeLDAPDisplayName:"msDS-KeyCredentialLink" Shadow Credentials. Let’s hunt it! Search for operations that were made on an user or machine account’s object: EventID:4662 AND AccessList:"%%7685" AND Properties:"{5b47d60f-6090-40b2-9f37-2a4de88f3063}" Value Added Write Property ms-DS-Key-Credential-Link GUID
account or the creation of a new machine with the specified dNSHostName as a DCs’: EventID:(4741 OR 4742) AND DnsHostName:("dc.demo.local" OR "dc2.demo.local") CVE-2022-26923 vulnerability. Let’s hunt it! Also use other previously discussed hunts for detect usage of usage DCs’ account for domain authentication via PKINIT.
DLL that receive requests from the Certificate Services, evaluate those requests, and specify optional properties of the certificates that are built to fill these requests. • A policy module may view existing certificate properties and extensions, and it may also view request attributes and properties. In addition, a policy module may set or modify certificate extensions and some other properties. • Exit modules are DLL that receive notifications from the CA when operations such as the issuance of a certificate occur. A typical operation for an exit module is to publish a completed certificate in a specified location. • An exit module may view existing certificate properties and extensions, and it may also view request attributes and properties. An exit module cannot, however, modify any properties.
modification of certificate authority modules registry keys: EventID:13 AND TargetObject.keyword:/HKLM\\System\\CurrentControlSet\\Services\\CertSvc\\Configuration\\.*/ AND TargetObject.keyword:/.*\\(ExitModules|PolicyModules)\\Active.*/ Search for modification of certificate authority modules registry keys: EventID:4891 AND Node:("ExitModules" OR "PolicyModules")