SQL To RCE
SQL Injection (SQLi) vulnerabilities can potentially escalate to Remote Code Execution (RCE) if certain conditions are met, depending on the target database management system (DBMS). Here's how SQLi can be escalated to RCE across three common databases: MySQL, MSSQL, and PostgreSQL, along with the methods used.
MySQL: SQLi to RCE
Method 1: INTO OUTFILE for Web Shell Upload
Condition: The MySQL user running the query has file write permissions (e.g.,
FILE
privilege), and the web server is serving files from a directory writable by the database.How it works: By exploiting the
INTO OUTFILE
feature, an attacker can write arbitrary content (like a PHP web shell) to a directory served by the web server.
Steps:
Write a web shell (or any code) to the file system using
INTO OUTFILE
:Access the uploaded file (
shell.php
) via a web browser and execute system commands by passing them as parameters:
Outcome: RCE via the uploaded shell file.
Method 2: LOAD_FILE() Function
Condition: The attacker needs to have read access to the file system.
How it works: The
LOAD_FILE()
function can be used to read sensitive files from the server. If sensitive credentials or system files (e.g.,/etc/passwd
) are exposed, these can aid in escalating to RCE.
Steps:
While this doesn’t directly lead to RCE, it can expose critical system information that aids in escalation.
Microsoft SQL Server (MSSQL): SQLi to RCE
Method 1: xp_cmdshell Command Execution
Condition: The
xp_cmdshell
stored procedure is enabled (it is disabled by default but can be re-enabled if the attacker has administrative privileges).How it works:
xp_cmdshell
allows running arbitrary OS commands from SQL Server. An attacker can exploit SQLi to execute system commands.
Steps:
Enable
xp_cmdshell
if it's disabled:Use
xp_cmdshell
to execute OS commands:The output will give the user running the SQL service, and further commands can be executed for RCE.
Method 2: sp_OACreate COM Objects
Condition: The attacker has sufficient privileges to execute OLE Automation Procedures.
How it works: The
sp_OACreate
procedure allows creating COM objects that can execute system commands.
Steps:
Outcome: RCE via system command execution.
Method 3: Linked Servers for Command Execution
Condition: SQL Server is configured with linked servers, allowing connections to remote systems.
How it works: An attacker can exploit the
OPENROWSET
function to execute commands on a linked server, potentially leading to RCE on that system.
Steps:
Outcome: RCE on the linked server.
PostgreSQL: SQLi to RCE
Method 1: COPY TO/FROM for Web Shell Upload
Condition: The attacker has write permissions on the file system, and PostgreSQL has access to a directory served by a web server.
How it works: The
COPY
command can write query results to a file on the server, which can be used to upload a web shell.
Steps:
Use
COPY
to write a web shell:Access the web shell via the browser:
Method 2: PostgreSQL User-Defined Functions (UDF)
Condition: The attacker can create and execute User-Defined Functions in languages like
C
orPL/pgSQL
.How it works: PostgreSQL allows creating UDFs in various languages. An attacker can create a UDF in C that executes system commands, leading to RCE.
Steps:
Create a C-based UDF to execute system commands:
Execute the command:
Method 3: libpq COPY PROGRAM
for Command Execution
COPY PROGRAM
for Command ExecutionCondition: The attacker needs to be able to leverage the
COPY PROGRAM
feature of the PostgreSQLlibpq
library.How it works: The
COPY PROGRAM
allows executing commands directly on the server when copying data to or from external files.
Steps:
Outcome: Direct command execution leading to RCE.
Summary of SQLi to RCE Escalation:
Database
Method
Conditions
RCE Methodology
MySQL
INTO OUTFILE
File write permissions (FILE
privilege)
Upload web shell or arbitrary file.
LOAD_FILE()
File read permissions
Read sensitive files to escalate further.
MSSQL
xp_cmdshell
xp_cmdshell
enabled
Execute system commands directly.
sp_OACreate
Admin privileges, OLE Automation enabled
Execute commands through COM objects.
Linked Servers
Linked server configured
Execute commands on remote linked servers.
PostgreSQL
COPY TO/FROM
Write permissions on the file system
Upload web shell or arbitrary file.
User-Defined Functions (UDF)
Ability to create UDFs in C
or PL/pgSQL
Create a function that executes system commands.
COPY PROGRAM
Access to libpq
with COPY PROGRAM
Execute system commands directly through COPY PROGRAM
.
Last updated