For the complete documentation index, see llms.txt. This page is also available as Markdown.

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

Loading text files into Oracle tables can be done in a variety of ways. For example we can use utl_file:

Alternatively, we can use Oracle external tables:

Earlier versions of PostgreSQL did not accept absolute paths in pg_read_file or pg_ls_dir. Newer versions (as of this commit) will allow reading any file/filepath for super users or users in the default_role_read_server_files group.

Aleternatilvly, we may use the COPY command

The LOAD_FILE function can be used to read files.

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:

Write files via Oracle Dataabse can be done in a variety of ways. For example we can use utl_file:

We may use the COPY command

Or, using lo* commands

We may use the OUTFILE keyword to write data into a 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.

Oracle Java

We may execute command on OracleSQL using Oracle Java. First, check if you have enought privileges:

Now we can execute commands:

Oracle Java Store Procedure

Alternatively, a very popular way to execute your command on the server is to write a java stored procedure. This is done in three stages. First, create a Java class called ‘oraexec’. To do this, connect via ‘sqlplus’ terminal and write:

Next, write a PL/SQL wrapper for this class:

That’s it. Now, to execute a command, all you need is just to send the following query:

Note that when using the above procedure, we cannot see the results of executed command, however, you can redirect the output to a file and read it. You can find the full code of the shell that allows to read and write files. Here is a more sophisticated script that handles the command output

Oracle DBMS_SCHEDULER

The next method, which will help us if there is no Java virtual machine, is to use ‘dbmsscheduler’, the built-in task scheduler of Oracle

Execution of programs is not allowed using DBMS_SCHEDULER. However, there is a bug that allows this restriction to be bypassed. By embedding shell meta-characters such as the ampersand (&) or pipes (||) in the name of the program to be run, it's possible to execute programs

Alternatively, here’s a code sample that implements the entry of ‘0wned’ string into a text file in the root of the C: drive:

This will create and run a job for executing your command. And here’s an option for calling the Scheduler from another procedure – ‘SYS.KUPP$PROC.CREATE_MASTER_PROCESS’, which is of interest to us, primarily, because it allows you to embed multi-statement queries, that is, those consisting of multiple sub-queries. Theoretically, you can run such query even in case of injection into a web application.

Oracle External Tables

As the last method for achieving the execution of OS commands, we can use Oracle External Tables. This method will need the following privileges:

  • UTL_FILE;

  • CREATE TABLE;

  • a directory reserved for the user.

Let’s remember that the access to ‘UTL_FILE’ package is by default provided to all accounts with ‘CONNECT’ role. Step one: Check the issued directories with the following query:

Step two: Create an executable batch file with desired command:

Step three: Prepare the external table ‘EXTT’, you will need it to run the file:

Now, just call your batch file with the following command:

The terminal will start to display error messages that the system cannot match the table and invoked file but, in this case, it is not important, as the main objective was to open the executable file, which you have achieved.

CVE-2019–9193 allow us to run system commands on Linux or Windows using the PROGRAM parameter.

First, we have to be super user or member of the pg_execute_server_program group:

Run arbitrary commands:

Resources

Last updated