SQL injection to remote code execution
A SQL injection flaw exposes the database, then the database's own features — command execution or file write — are leveraged into a shell on the server.
Scenario
An authorized web application test. You have found a parameter vulnerable to SQL injection. The objective is to show how a data-layer flaw escalates from information disclosure to code execution on the host.
- 1
Initial Access / Discovery
Confirm and characterise the injection
Verify the injection point and determine the DBMS, privileges and whether stacked queries are allowed — this decides which escalation paths are even possible.
DetectionSQL errors, UNION patterns and tautologies in query logs and WAF alerts flag injection attempts.
MitigationUse parameterised queries/prepared statements, least-privilege DB accounts, and a WAF as defence in depth.
- 2
Collection
Demonstrate data exposure
Read a limited, agreed sample from the database (schema, a few rows) to prove impact — never bulk-exfiltrating real customer data.
DetectionUnusual high-volume reads, access to tables the app never touches, and information_schema queries are visible in DB audit logs.
MitigationDatabase activity monitoring, query allow-listing, and encryption of sensitive columns at rest.
References sqlinjection - 3
Execution
Turn database access into command execution
Where the DB account permits it, use the database's own features — such as command-execution procedures, user-defined functions, or writing a file to a web-served directory — to run code on the host. Report each capability as a privilege finding.
DetectionThe database process spawning a shell, or new files appearing in web roots, are high-fidelity host alerts.
MitigationDisable dangerous DB features (e.g. command-shell procedures), run the DB as a low-privileged service account, and make web roots non-writable by the DB.
- 4
Persistence
Establish a foothold
If a file write to a web-accessible path is possible, demonstrate a minimal web shell to confirm code execution, then document and remove it as part of clean-up.
DetectionNew executable files in web directories and web requests to unknown endpoints are detectable with file-integrity monitoring and web logs.
MitigationFile-integrity monitoring on web roots, disallow script execution in upload/writable paths, and review for unexpected files.
Key takeaways
- SQL injection is not only a data-disclosure bug — with a privileged DB account it becomes code execution.
- Parameterised queries stop the injection; a least-privilege DB account limits how far it can go if one slips through.
- Disable command-execution DB features and keep web roots unwritable so data access cannot become a shell.