Skip to content
SQL injection Write-up Intermediate

SQLi → RCE: Exploiting PostgreSQLi

A CTF web application was vulnerable to SQL injection in a login form and a product search, allowing authentication bypass and data extraction. Because the database user was a superuser, the attacker could execute operating system commands and achieve remote code execution.

infosecwriteups.com

Published on infosecwriteups.com

I am here not going to explain SQLi and its types. There are plenty of resources out there that you can check here

Raj Prasad Kuiri, on infosecwriteups.com

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 concatenated user input into SQL queries and the database user had superuser privileges, enabling command execution.

Impact

An attacker could bypass authentication, extract data, and execute arbitrary operating system commands, leading to full remote code execution.

What the attacker needed

CTF environment; PostgreSQL database; Superuser database role

The fix

The application should use parameterized queries and run the database with a non-superuser role, and dangerous features should be disabled.

How it was found

  1. Enter a single quote in the username field to trigger a database error and identify PostgreSQL.
  2. Use a comment sequence to bypass authentication.
  3. Use ORDER BY to determine the number of columns in a UNION query.
  4. Use UNION SELECT with NULLs to find which columns accept which data types.
  5. Extract database version and table names via UNION SELECT.
  6. Check if the current user is a superuser and then execute OS commands via SQL injection.

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.

SQL injection is when a website puts user input directly into a database command, so an attacker can change the command. Here, the attacker could log in without a password and read data, and because the database account had too much power, they could also run system commands on 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.

Similar reports

View all

Same bug class and techniques, clearest first.

SQL injection

The lesson behind this report.

Input reaches a database query as code, letting an attacker read or change data the application never meant to expose.