Read/Write/Execute
Theory
When exploiting SQL injection vulnerabilities, or if you have a direct access to the DBMS, you may be able to read/write files on the operating system. In some case you will be able to execute arbitrary command
Practice
Some queries on this page can be used with different techniques as UNION or Blind based attacks
Read
Directories
We may list directories on MSSQL using following commands
You can check who (apart sysadmins) has permissions to run those MSSQL functions with:
Files
By default, MSSQL
allows file read on any file in the operating system to which the account has read access. We can use the following SQL query:
However, the BULK
option requires the ADMINISTER BULK OPERATIONS
or the ADMINISTER DATABASE BULK OPERATIONS
permission.
Registry
Microsoft SQL Server provides multiple extended stored procedures that allow you to interact with not only the network but also the file system and even the Windows Registry:
Regular
Instance-Aware
sys.xp_regread
sys.xp_instance_regread
sys.xp_regenumvalues
sys.xp_instance_regenumvalues
sys.xp_regenumkeys
sys.xp_instance_regenumkeys
sys.xp_regwrite
sys.xp_instance_regwrite
sys.xp_regdeletevalue
sys.xp_instance_regdeletevalue
sys.xp_regdeletekey
sys.xp_instance_regdeletekey
sys.xp_regaddmultistring
sys.xp_instance_regaddmultistring
sys.xp_regremovemultistring
sys.xp_instance_regremovemultistring
Write
To write files using MSSQL
, we need to enable Ole Automatin Procedures, which requires admin privileges, and then execute some stored procedures to create the file:
Command Execution
XP_CMDSHELL
We can execute code on MSSQL instances using the xp_cmdshell
function. To enable it, we need admin privileges (sysadmin
privileges).
xp_cmdshell is disabled by default since SQL Server 2005
Alternatively, we may use PowerUpSQL to acheive the same goal.
Python Scripts
We can execute python scipts as follows (needs sysadmin
privileges).
Commands will runs with privileges of a dynamically created Windows user account (member of the SQLRUserGroup
).
Python was introduced in SQL Server 2017. Runtime environments must be installed as a prerequisite (wich is not by default).
Alternatively, we may use PowerUpSQL to acheive the same goal.
OLE Automation Procedure
We can execute commands through a stored procedure utilizing Object Linking and Embedding (OLE) technology. (needs sysadmin
privileges).
Commands will be executed with the privileges of SQL Server service account.
Alternatively, we may use PowerUpSQL to acheive the same goal.
Custom CLR Assemblies
SQL Server CLR integration allows writing stored procedures. We can create a CLR UDF (Common Language Runtime User Defined Function) in any .NET language and compil it into a DLL. It can then be loaded within MSSQL for executing custom functions (needs sysadmin
privileges).
The TRUSTWORTHY
property should be set to allow the use of the CREATE ASSEMBLY
statement.
By default, only the msdb
database has this property enabled
First lets write and compile the following DLL
We should know enable CLR:
Convert the compiled DLL (.NET Assembly) to hexadecimal:
We can now load the assembly into MSSQL and execute it as follows:
Alternatively, we may use PowerUpSQL to acheive the same goal.
Resources
Last updated
Was this helpful?