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

EXEC master.sys.xp_dirtree 'C:\',1,1
xp_dirtree 'C:\'
EXEC master.dbo.xp_dirtree 'C:\'
EXEC master..xp_subdirs 'C:\'
EXEC master..xp_fileexist 'C:\'

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:

SELECT * FROM OPENROWSET(BULK N'C:/Windows/System32/drivers/etc/hosts', SINGLE_CLOB) AS Contents

However, the BULK option requires the ADMINISTER BULK OPERATIONS or the ADMINISTER DATABASE BULK OPERATIONS permission.

# Check if you have it
SELECT * FROM fn_my_permissions(NULL, 'SERVER') WHERE permission_name='ADMINISTER BULK OPERATIONS' OR permission_name='ADMINISTER DATABASE BULK OPERATIONS';

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).

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).

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).

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?