HTB [Medium Lab] - Unclaimed
As usual, I started by running an Nmap scan against the target. The scan revealed several open ports, including 80, 139, 445, and one particularly interesting port: 5985, which is commonly used by Windows Remote Management (WinRM). Based on these initial findings, I could tentatively assume that the target was a Windows machine.
I proceeded to investigate port 80 and was redirected to a domain named unclaimed.htb. After adding the domain to my /etc/hosts file, I accessed the web application and discovered that it was a chatbot designed to look up records of lost or delayed baggage. When I tried submitting a query, Tracerbot returned an error that revealed the backend was SQL-based. Given that the target was running Windows, I suspected that the chatbot was backed by Microsoft SQL Server (MSSQL).
At this point, I figured that I would need to manipulate the chatbot into executing SQL queries. However, when I tested an initial payload, my request was blocked. This suggested that the application might have implemented a filtering mechanism to prevent prompt injection attempts. Therefore, I needed to find another approach to bypass these filters and continue exploring potential attack vectors.
I proceeded to test a payload designed for Prompt Disclosure. Interestingly, the chatbot responded by revealing a support override code. According to the chatbot, this code could be used to execute arbitrary SQL queries. If this functionality worked as described, it could potentially give me direct control over the backend SQL Server.
And boom! I was now able to execute SQL queries directly through the chatbot. I also discovered that the current database user was svc_tracer_ro, where ro likely stands for read-only, suggesting that my access was quite limited. With that in mind, I needed to explore potential attack vectors that could allow me to escalate my capabilities and ultimately achieve remote code execution (RCE).
First, I decided to check whether I could impersonate any other users within the SQL Server environment. I discovered that I was able to impersonate another user named svc_reports.
First, I decided to check whether I could impersonate any other users within the SQL Server environment. I discovered that I was able to impersonate another user named svc_reports.
However, I needed to verify the privileges assigned to svc_reports and determine whether it had the sysadmin role. If so, I could potentially execute system commands through xp_cmdshell.
After confirming that svc_reports has the sysadmin role, the next step was to verify whether I could execute operating system commands through xp_cmdshell. To test this, I decided to execute the whoami command and check whether the chatbot successfully returned its output.
Boom! I was now able to execute system commands successfully. Although I could use PowerShell directly to establish a reverse shell, I prefer working with Metasploit. Therefore, I decided to use msfvenom to generate a payload and configure a Metasploit handler to catch the incoming session.
I won’t include screenshots of every step, as all you need to do is place the appropriate command into the SQL query. I’ll generate a Meterpreter payload using msfvenom and upload it to the target machine. Since I already have RCE through the chatbot, downloading and uploading files is relatively straightforward. Once the payload was uploaded, I executed it on the target machine and successfully obtained a reverse shell through my Metasploit handler.
Since I had obtained the sysadmin role through the svc_reports account, I decided to investigate whether there were any credentials stored on the database server. This would be one of the quickest ways to discover additional credentials and potentially decrypt them for further use. While checking the running processes using tasklist, I noticed that SQLAGENT.EXE was running. This caught my attention because it could indicate the presence of SQL Server Agent jobs configured with proxies or credentials.
The reason this is interesting is that SQL Server Agent proxies allow jobs to run under a different Windows security context, rather than using the SQL Server Agent service account directly. This means there could potentially be Windows account credentials stored within SQL Server. My next step was to investigate whether I could retrieve and decrypt those credentials.
Unlike SQL login passwords, which are stored as one-way hashes, credentials used by SQL Server Agent proxies must be recoverable because SQL Server needs the actual password to authenticate as the corresponding Windows account. These credential secrets are encrypted using the Service Master Key (SMK). With sysadmin privileges and access to the SQL Server service context, I could potentially leverage the key hierarchy to decrypt and recover the stored credentials.
After inspecting sys.credentials, I discovered an interesting configuration:
WTArchiveProxy → WTArchiveCred → svc_wtbackup
This indicated that the administrator had configured SQL Server Agent to run backup jobs under the svc_wtbackup account. To make this possible, the password for svc_wtbackup must be stored within SQL Server in an encrypted, recoverable format.
Use the query below to verify that the proxy account is being used by the Agent. The query will also list all other Agent jobs that are configured to run using proxy accounts.
To save time, I’ll use an existing publicly available tool for this task: Get-MSSQLCredentialPasswords.psm1. It automates the entire process by retrieving the Service Master Key (SMK) and entropy, performing DPAPI decryption, and then using AES decryption to recover the stored credential secrets. I uploaded the tool to the target machine, imported the module into PowerShell, and executed the script. Fortunately, it worked as expected, successfully decrypting the stored credential and revealing the password in plaintext.
To save time, I’ll use an existing publicly available tool for this task: Get-MSSQLCredentialPasswords.psm1. It automates the entire process by retrieving the Service Master Key (SMK) and entropy, performing DPAPI decryption, and then using AES decryption to recover the stored credential secrets. I uploaded the tool to the target machine, imported the module into PowerShell, and executed the script. Fortunately, it worked as expected, successfully decrypting the stored credential and revealing the password in plaintext.
Since WinRM was exposed on the target, I decided to try connecting using Evil-WinRM. However, the authentication attempt failed, and I received an error indicating that access was not authorized.
I used CrackMapExec to validate the credentials, but the authentication attempt was still unsuccessful. This made me suspect that the password I had decrypted using the PowerShell script was either incorrect or incomplete. I was already a bit skeptical of the result. In a real-world environment, I might have been more inclined to trust it, but given that this is a lab, I suspected that W0rldTr@cer_ was only part of the password and that something else needed to be appended to it.
I decided to take another look at the PowerShell script and noticed that the author was using LEN() to determine the ciphertext length. The problem is that LEN() stops counting when it encounters a null byte (0x00). Unfortunately, this particular credential’s ciphertext contains a 0x00 byte, causing the data to be truncated before the entire ciphertext could be processed.
To fix this, I modified line 99, replacing LEN(imageval) with DATALENGTH(imageval). Unlike LEN(), DATALENGTH() returns the actual length in bytes, regardless of the byte values, including null bytes.
After applying the fix, I ran the script again and successfully recovered the complete password in plaintext.
I used CrackMapExec to validate the credentials again, and this time, the credentials were successfully authenticated.
Now I’ll use Evil-WinRM to connect to the target machine using the svc_wtbackup account.
After checking the user’s privileges, I discovered that svc_wtbackup had two interesting privileges: SeBackupPrivilege and SeRestorePrivilege. Although this account is not a member of the Administrators group, these privileges are particularly powerful. SeBackupPrivilege allows users to read protected files by bypassing normal file system ACL checks through backup semantics. Meanwhile, SeRestorePrivilege provides similar capabilities for restoring or writing files, even when normal permissions would otherwise prevent access.
The quickest way to escalate privileges to Administrator on this machine is to dump the SAM and SYSTEM registry hives. I can then use Impacket to extract the Administrator’s NTLM hash and perform a Pass-the-Hash (PtH) attack to authenticate as the Administrator.
After successfully dumping the SAM and SYSTEM registry hives, I’ll download both files to my attack machine and use Impacket’s secretsdump.py to extract the NTLM hashes.
After obtaining the Administrator’s NTLM hash, I used Evil-WinRM with a Pass-the-Hash (PtH) attack to authenticate as the Administrator and successfully gain an Administrator session. With that access, all that remained was to retrieve the root flag and complete the machine.