From SQLi to Java RCE: Exploiting H2 with Multipart Payloads (HT
A Java Spring endpoint built SQL queries by directly inserting an unsanitised 'name' parameter, allowing SQL injection. The attacker exploited the H2 database's CREATE ALIAS feature to define a Java function that executed shell commands, achieving remote code execution.
Published on medium.com
This was my first Hack The Box (HTB) challenge, and it turned out to be more than just another SQLi case.
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
The application used string formatting to build SQL queries with user input instead of using parameterised queries (prepared statements).
Impact
An authenticated attacker could execute arbitrary shell commands on the server, leading to full remote code execution.
What the attacker needed
A valid session (authenticated user); ability to send POST requests with multipart/form-data
The fix
The application should use parameterised queries (prepared statements) instead of concatenating user input into SQL strings.
How it was found
- Send a POST request to /api/note with a multipart/form-data body containing the name parameter.
- Inject a UNION SELECT to confirm SQL injection and enumerate the database.
- Use CREATE ALIAS to define a Java method that executes system commands.
- Call the created alias with EXEC('ls ..') to list the parent directory.
- Call EXEC('cat /<filename>.txt') to read the flag file.
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 web app took user input and pasted it straight into a database command without checking it. An attacker could add extra database instructions, and because the database (H2) lets you create custom functions that run Java code, the attacker could make the server run system commands.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.