From SQL Injection to Remote Code Execution: A Full Exploitation
A SQL injection in an 'id' parameter allowed database user enumeration and, because the database user had FILE privileges, writing a PHP web shell to the web root. The attacker then executed commands and established a reverse shell, achieving remote code execution.
Published on medium.com
In this blog, I will share my journey of discovering and exploiting a SQL Injection (SQLi) vulnerability that escalated to full remote code execution (RCE) on a server. This walkthrough highlights the process, payloads used, and how one vulnerability can cascade into a critical compromise.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application built SQL queries by concatenating the 'id' parameter without parameterisation, and the database user had excessive FILE privileges.
Impact
An attacker could read and write files on the server and execute arbitrary commands, leading to full system compromise.
What the attacker needed
SQL injection in the id parameter; Database user with FILE privilege; Web root writable by database user
The fix
The application should use parameterised queries and the database user should have minimal privileges, especially no FILE privilege.
How it was found
- Inject a single quote into the id parameter and observe a 200 error.
- Inject a double quote and observe 200 OK, confirming unusual behaviour.
- Use a UNION SELECT payload to enumerate the database user.
- Query information_schema.user_privileges to check for FILE privilege.
- Write a PHP web shell to the web root using INTO OUTFILE.
- Access the shell and execute commands, then establish a reverse shell.
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
The website put user input directly into a database query without checking it, so an attacker could add their own database commands. The database account was allowed to write files on the server, so the attacker saved a small program that let them run commands. This turned a data leak into full control of the server.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.