SQL Injection and Postgres – An adventure to eventual RCE
An SQL injection in an ORDER BY clause of a Flask app backed by PostgreSQL let an attacker run arbitrary SELECT queries, and because the database user was a superuser, read and write files on the server and eventually execute commands by overwriting the PostgreSQL config file. The write-up walks through error-based extraction, XML helper functions, large object file primitives and a path to command execution as the database user.
pulsesecurity.co.nz
Published on pulsesecurity.co.nz
An SQL injection bug in an ORDER BY clause came up in a recent engagement, which lead to an interesting rabbit hole regarding exploiting SQLi against a PostgreSQL database. This post details some of that adventure.
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 sort parameter was concatenated into the SQL query without validation, unlike the order parameter which was checked against a whitelist.
Impact
An attacker could read and write arbitrary files as the database user and, by overwriting the config and reloading it, execute commands; the text notes the real target ran an older PostgreSQL without global file read, and that overwriting the config risks breaking the server on restart.
What the attacker needed
Injection point in ORDER BY; database user is a superuser; error messages returned to client
How it was found
- Send a single quote in the sort parameter and observe the database error confirming the injection.
- Use a CASE expression in the ORDER BY clause to test query results via response ordering.
- Use a CAST to a numeric type to leak query output through the error message.
- Use query_to_xml or database_to_xml to return multi-row results as a single XML value.
- Use lo_import and lo_export to read and write files as the database user.
- Overwrite the PostgreSQL config to set a passphrase command and reload the configuration to execute it.
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 app built a database query by gluing user input onto the end of it, so a visitor could slip extra database instructions into the request. Because the database account had very high privileges, the attacker could not only read data but also read and write files on the server and finally make the database run a command. This is a classic SQL injection, where input is treated as code instead of plain data.
What is SQL injection?
Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.