hacktricks/windows-hardening/active-directory-methodology/ad-certificates/domain-persistence.md

94 lines
8 KiB
Markdown
Raw Normal View History

2022-09-03 00:18:00 +00:00
# AD CS Domain Persistence
2022-09-01 21:06:19 +00:00
<details>
2024-01-09 11:45:12 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-09-01 21:06:19 +00:00
2024-01-09 11:45:12 +00:00
Other ways to support HackTricks:
2022-09-01 21:06:19 +00:00
2024-01-09 11:45:12 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-09-01 21:06:19 +00:00
</details>
2024-02-08 03:06:37 +00:00
**This is a summary of the domain persistence techniques shared in [https://www.specterops.io/assets/resources/Certified\_Pre-Owned.pdf](https://www.specterops.io/assets/resources/Certified\_Pre-Owned.pdf)**. Check it for further details.
2024-02-05 02:29:11 +00:00
2022-09-01 22:02:18 +00:00
## Forging Certificates with Stolen CA Certificates - DPERSIST1
2022-09-01 21:06:19 +00:00
2022-09-01 22:02:18 +00:00
How can you tell that a certificate is a CA certificate?
2022-09-01 21:06:19 +00:00
2024-02-05 02:29:11 +00:00
It can be determined that a certificate is a CA certificate if several conditions are met:
- The certificate is stored on the CA server, with its private key secured by the machine's DPAPI, or by hardware such as a TPM/HSM if the operating system supports it.
- Both the Issuer and Subject fields of the certificate match the distinguished name of the CA.
- A "CA Version" extension is present in the CA certificates exclusively.
- The certificate lacks Extended Key Usage (EKU) fields.
2022-09-01 21:06:19 +00:00
2024-02-05 02:29:11 +00:00
To extract the private key of this certificate, the `certsrv.msc` tool on the CA server is the supported method via the built-in GUI. Nonetheless, this certificate does not differ from others stored within the system; thus, methods such as the [THEFT2 technique](certificate-theft.md#user-certificate-theft-via-dpapi-theft2) can be applied for extraction.
2022-09-01 21:06:19 +00:00
2024-02-05 02:29:11 +00:00
The certificate and private key can also be obtained using Certipy with the following command:
2022-09-02 13:32:02 +00:00
```bash
certipy ca 'corp.local/administrator@ca.corp.local' -hashes :123123.. -backup
```
2024-02-05 02:29:11 +00:00
Upon acquiring the CA certificate and its private key in `.pfx` format, tools like [ForgeCert](https://github.com/GhostPack/ForgeCert) can be utilized to generate valid certificates:
2022-09-01 21:06:19 +00:00
2022-09-01 22:02:18 +00:00
```bash
2024-02-05 02:29:11 +00:00
# Generating a new certificate with ForgeCert
2022-09-01 22:02:18 +00:00
ForgeCert.exe --CaCertPath ca.pfx --CaCertPassword Password123! --Subject "CN=User" --SubjectAltName localadmin@theshire.local --NewCertPath localadmin.pfx --NewCertPassword Password123!
2024-02-05 02:29:11 +00:00
# Generating a new certificate with certipy
2022-09-02 13:32:02 +00:00
certipy forge -ca-pfx CORP-DC-CA.pfx -upn administrator@corp.local -subject 'CN=Administrator,CN=Users,DC=CORP,DC=LOCAL'
2024-02-05 02:29:11 +00:00
# Authenticating using the new certificate with Rubeus
2022-09-01 22:02:18 +00:00
Rubeus.exe asktgt /user:localdomain /certificate:C:\ForgeCert\localadmin.pfx /password:Password123!
2022-09-02 13:32:02 +00:00
2024-02-05 02:29:11 +00:00
# Authenticating using the new certificate with certipy
2022-09-02 13:32:02 +00:00
certipy auth -pfx administrator_forged.pfx -dc-ip 172.16.126.128
2022-09-01 22:02:18 +00:00
```
{% hint style="warning" %}
2024-02-05 02:29:11 +00:00
The user targeted for certificate forgery must be active and capable of authenticating in Active Directory for the process to succeed. Forging a certificate for special accounts like krbtgt is ineffective.
2022-09-01 22:02:18 +00:00
{% endhint %}
This forged certificate will be **valid** until the end date specified and as **long as the root CA certificate is valid** (usually from 5 to **10+ years**). It's also valid for **machines**, so combined with **S4U2Self**, an attacker can **maintain persistence on any domain machine** for as long as the CA certificate is valid.\
Moreover, the **certificates generated** with this method **cannot be revoked** as CA is not aware of them.
## Trusting Rogue CA Certificates - DPERSIST2
2024-02-05 02:29:11 +00:00
The `NTAuthCertificates` object is defined to contain one or more **CA certificates** within its `cacertificate` attribute, which Active Directory (AD) utilizes. The verification process by the **domain controller** involves checking the `NTAuthCertificates` object for an entry matching the **CA specified** in the Issuer field of the authenticating **certificate**. Authentication proceeds if a match is found.
2022-09-01 22:02:18 +00:00
2024-02-05 02:29:11 +00:00
A self-signed CA certificate can be added to the `NTAuthCertificates` object by an attacker, provided they have control over this AD object. Normally, only members of the **Enterprise Admin** group, along with **Domain Admins** or **Administrators** in the **forest roots domain**, are granted permission to modify this object. They can edit the `NTAuthCertificates` object using `certutil.exe` with the command `certutil.exe -dspublish -f C:\Temp\CERT.crt NTAuthCA126`, or by employing the [**PKI Health Tool**](https://docs.microsoft.com/en-us/troubleshoot/windows-server/windows-security/import-third-party-ca-to-enterprise-ntauth-store#method-1---import-a-certificate-by-using-the-pki-health-tool).
2022-09-01 22:02:18 +00:00
2024-02-05 02:29:11 +00:00
This capability is especially relevant when used in conjunction with a previously outlined method involving ForgeCert to dynamically generate certificates.
2022-09-01 22:02:18 +00:00
## Malicious Misconfiguration - DPERSIST3
2024-02-05 02:29:11 +00:00
Opportunities for **persistence** through **security descriptor modifications of AD CS** components are plentiful. Modifications described in the "[Domain Escalation](domain-escalation.md)" section can be maliciously implemented by an attacker with elevated access. This includes the addition of "control rights" (e.g., WriteOwner/WriteDACL/etc.) to sensitive components such as:
2022-09-01 22:02:18 +00:00
2024-02-05 02:29:11 +00:00
- The **CA servers AD computer** object
- The **CA servers RPC/DCOM server**
- Any **descendant AD object or container** in **`CN=Public Key Services,CN=Services,CN=Configuration,DC=<DOMAIN>,DC=<COM>`** (for instance, the Certificate Templates container, Certification Authorities container, the NTAuthCertificates object, etc.)
- **AD groups delegated rights to control AD CS** by default or by the organization (such as the built-in Cert Publishers group and any of its members)
2022-09-01 22:02:18 +00:00
2024-02-05 02:29:11 +00:00
An example of malicious implementation would involve an attacker, who has **elevated permissions** in the domain, adding the **`WriteOwner`** permission to the default **`User`** certificate template, with the attacker being the principal for the right. To exploit this, the attacker would first change the ownership of the **`User`** template to themselves. Following this, the **`mspki-certificate-name-flag`** would be set to **1** on the template to enable **`ENROLLEE_SUPPLIES_SUBJECT`**, allowing a user to provide a Subject Alternative Name in the request. Subsequently, the attacker could **enroll** using the **template**, choosing a **domain administrator** name as an alternative name, and utilize the acquired certificate for authentication as the DA.
2022-09-01 22:02:18 +00:00
2022-09-01 21:06:19 +00:00
<details>
2024-01-09 11:45:12 +00:00
<summary><strong>Learn AWS hacking from zero to hero with</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-09-01 21:06:19 +00:00
2024-01-09 11:45:12 +00:00
Other ways to support HackTricks:
2022-09-01 21:06:19 +00:00
2024-01-09 11:45:12 +00:00
* If you want to see your **company advertised in HackTricks** or **download HackTricks in PDF** Check the [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Get the [**official PEASS & HackTricks swag**](https://peass.creator-spring.com)
* Discover [**The PEASS Family**](https://opensea.io/collection/the-peass-family), our collection of exclusive [**NFTs**](https://opensea.io/collection/the-peass-family)
* **Join the** 💬 [**Discord group**](https://discord.gg/hRep4RUj7f) or the [**telegram group**](https://t.me/peass) or **follow** me on **Twitter** 🐦 [**@carlospolopm**](https://twitter.com/carlospolopm)**.**
* **Share your hacking tricks by submitting PRs to the** [**HackTricks**](https://github.com/carlospolop/hacktricks) and [**HackTricks Cloud**](https://github.com/carlospolop/hacktricks-cloud) github repos.
2022-09-01 21:06:19 +00:00
</details>