Dissecting PostgreSQL CVE-2013-1899
PostgreSQL misinterpreted a database name starting with a hyphen as a command-line flag, allowing an unauthenticated attacker to write log output to arbitrary files. The authors leveraged this to inject commands into the postgres user's .profile, achieving code execution when an administrator logged in.
blog.blackwinghq.com
Published on blog.blackwinghq.com
So, last week the Postgresql group released an update to its popular open-source RDBMS to address a security issue – pretty standard…
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 server passed the client-supplied database name directly into its command-line argument parsing without sanitising leading hyphens.
Impact
An unauthenticated attacker could write arbitrary log data to files writable by the postgres user, potentially leading to code execution if a privileged user later logs in, though this requires specific conditions.
What the attacker needed
Network access to PostgreSQL port; postgres user can write target file; administrator logs in as postgres
The fix
The vulnerability was fixed in the PostgreSQL update mentioned at the start of the article.
How it was found
- Connect to the PostgreSQL server and supply a database name beginning with '-r' followed by a target file path.
- Observe that the server writes its log output, including the attacker-controlled username, to that file.
- Craft a custom client to send a username containing a double quote, newline, the desired command, and a hash to comment out the rest.
- Target the postgres user's .profile file so the injected command runs when an administrator logs in as postgres.
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.
PostgreSQL is a database server. When a client connects, it sends a database name. If that name starts with a dash, the server treated it like a command-line option, so an attacker could make the server write its log messages to any file the database user can write. By writing a crafted line into a startup file, the attacker could run commands when an administrator logged in.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.