MSSQL
xp_cmdshell
is an extended stored procedure provided by Microsoft SQL Server that allows system administrators to execute operating system commands directly from within an SQL query. It is disabled by default due to its powerful and potentially dangerous capabilities, but if enabled, it can be leveraged to execute arbitrary commands on the server's operating system.
To use xp_cmdshell
for executing operating system commands, an attacker or a privileged user must first ensure it is enabled. Hereβs a brief outline of how it can be enabled and used:
Enable
xp_cmdshell
:Execute Commands: Once enabled, you can execute commands directly from an SQL query:
By enabling and using xp_cmdshell
, users can perform file operations, access network information, and even connect to external systems from the SQL Server environment. However, because of its potential for misuse, it should be handled with extreme caution and ideally, its usage should be restricted and closely monitored.
Reverse Shell Examples
After enabling xp_cmdshell
, you can download and execute Netcat on the target machine. Here are several examples illustrating this process:
Example 1:
Download and execute a Netcat executable:
Example 2:
Execute a PowerShell script to establish a reverse shell, which is generally more stealthy and effective:
This method leaves fewer traces and works most of the time.
Example 3:
Chain SQL and PowerShell commands to fetch and execute Netcat, facilitating a reverse shell connection:
These examples showcase various techniques for executing remote shells through SQL injection vulnerabilities, demonstrating the critical importance of securing SQL servers against unauthorized command execution.
Last updated