π° Originally published on SecurityElites β the canonical, fully-updated version of this article.
π§ͺ DVWA LAB SERIES
FREE
Part of the DVWA Lab Series β 30 Labs
Lab 23 of 30 Β· 76.7% complete
β οΈ Authorised Lab Use Only: This lab demonstrates SQL injection escalation to OS remote code execution. Practice exclusively on DVWA running in your own local environment (VirtualBox, VMware, Docker, XAMPP). Never attempt these techniques against any system you do not own. The SELECT INTO OUTFILE technique and webshell deployment demonstrated here are criminal offences when used without explicit authorisation.
Twenty minutes. Thatβs how long it took me on a real engagement to go from finding a SQL injection in a report field to having a shell on the application server. The client called it an βinternal toolβ β network-accessible only from the corporate LAN, not exposed to the internet. They assumed internal meant safe. It didnβt.
The SQLi to OS shell chain is why SQL injection is rated Critical, not just High. The headline finding isnβt βattacker reads database records.β Itβs βattacker runs OS commands as the web server process.β From there itβs a short path to reading application config files, finding credential reuse, and elevating further. The chain is three technical conditions. Understand each one and you understand why this attack path is so devastating β and exactly what breaks it.
Lab 23 walks the complete sequence: injection confirmation β FILE privilege verification β server file read β webshell write β RCE. Run it once end to end and youβll never document SQL injection as anything less than Critical when the conditions are met.
π― What Youβll Master in Lab 23
Verify MySQL FILE privilege via SQL injection β the prerequisite for file write
Use LOAD_FILE() to read server files and confirm web root path
Deploy a PHP webshell via SELECT INTO OUTFILE through SQL injection
Execute OS commands through the deployed webshell for full RCE
Understand secure_file_priv and the three-layer defence that breaks this chain
β±οΈ 45 min Β· 3 Kali Terminal exercises Β· DVWA required #### How far have you escalated SQL injection before? Data extraction only β SELECT from tables File read with LOAD_FILE β not file write Full OS shell via SQLi β done it before This is my first time seeing this attack chain
π DVWA SQLi to OS Shell β Lab 23 Contents
- Prerequisites β Three Conditions Required
- Step 1 β Confirm SQL Injection and FILE Privilege
- Step 2 β Read Server Files with LOAD_FILE()
- Step 3 β Write Webshell via SELECT INTO OUTFILE
- Step 4 β Execute OS Commands via Webshell
- Defence β Breaking Each Link in the Chain
DVWA SQLi to OS Shell β Three Conditions Required
Before you run the first query, know what youβre working toward. The SQLi to OS shell chain requires three conditions simultaneously. Break any one of them and the chain stops. Understanding each condition also reveals where defenders should place controls.
Condition 1 β Exploitable SQL injection. You need an actual injectable parameter with no parameterisation. In DVWA Low security, the User ID parameter is directly concatenated into the SQL query β the foundation of this lab.
Condition 2 β MySQL FILE privilege. The database user running the queries needs FILE privilege. This is what enables LOAD_FILE() file reads and SELECT INTO OUTFILE using the permissions of the mysql OS process.
Condition 3 β MySQL write access to the web root. The mysql OS process needs write permission to the document root. In DVWAβs default configuration, this is intentionally misconfigured to allow the lab to function. In production, this should never be the case.
securityelites.com
SQLi to OS Shell β Attack Chain Overview
ENTRY
SQL Injection β UNION SELECT injection in user ID parameter
β requires: FILE privilege on MySQL user
ESCALATION
LOAD_FILE() reads /etc/passwd, Apache config β confirms web root path
β requires: write permission to /var/www/html/
PERSISTENCE
SELECT INTO OUTFILE writes PHP webshell to /var/www/html/dvwa/shell.php
β
RCE
shell.php?cmd=whoami β www-data Β· Full OS command execution
πΈ SQLi to OS shell attack chain with prerequisites at each step. The chain illustrates why defence in depth matters: three separate controls must all fail simultaneously for this attack to succeed. Parameterised queries eliminate the SQLi entry point. Removing FILE privilege stops escalation even if SQLi exists. Filesystem permissions break the webshell write even if FILE privilege is present. All three controls are required β patching only one leaves the others exposed.
Step 1 β Confirm SQL Injection and FILE Privilege
Donβt jump to the FILE operations yet. Confirm two things first: that the injection is UNION-based with the right column count, and that the MySQL user actually has FILE privilege. FILE privilege verification requires querying the MySQL user table β something only possible through SQL injection if the application database user has access to mysql.user, which in DVWAβs default configuration it does.
STEP 1 β INJECTION CONFIRMATION AND FILE PRIVILEGE CHECKCopy
DVWA URL: http://localhost/dvwa/vulnerabilities/sqli/
Security level: LOW
1a. Confirm basic injection
?id=1β² OR β1β=β1&Submit=Submit
Returns all users β injection confirmed
π Read the complete guide on SecurityElites
This article continues with deeper technical detail, screenshots, code samples, and an interactive lab walk-through. Read the full article on SecurityElites β
This article was originally written and published by the SecurityElites team. For more cybersecurity tutorials, ethical hacking guides, and CTF walk-throughs, visit SecurityElites.

Top comments (0)