En este ejemplo, vamos a tratar con dos maquinas aparte de la nuestra local, tenemos acceso a la segunda (CONFLUENCE01) y queremos conectarnos al servicio de postgresql de la tercera máquina (PGDATABASE01), que no le llegamos desde la nuestra local y sí desde la CONFLUENCE01.
Vamos a abrir el puerto 2345 en la maquina CONFLUENCE01 para que apunte al puerto 5432 de la máquina PGDATABASE01.
confluence@confluence01:/opt/atlassian/confluence/bin$ socat -ddd TCP-LISTEN:2345,fork TCP:10.4.50.215:5432
<ocat -ddd TCP-LISTEN:2345,fork TCP:10.4.50.215:5432
2022/08/18 10:12:01 socat[46589] I socat by Gerhard Rieger and contributors - see www.dest-unreach.org
2022/08/18 10:12:01 socat[46589] I This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit. (<http://www.openssl.org/>)
2022/08/18 10:12:01 socat[46589] I This product includes software written by Tim Hudson ([email protected])
2022/08/18 10:12:01 socat[46589] I setting option "fork" to 1
2022/08/18 10:12:01 socat[46589] I socket(2, 1, 6) -> 5
2022/08/18 10:12:01 socat[46589] I starting accept loop
2022/08/18 10:12:01 socat[46589] N listening on AF=2 0.0.0.0:2345
Ahora, si desde nuestra maquina, apuntamos con pgsql al puerto 2345 de CONFLUENCE01, será lo mismo que apuntar al 5432 de PGDATABASE01 (el puerto por defecto de postgresql).
kali@kali:~$ psql -h 192.168.50.63 -p 2345 -U postgres
Password for user postgres:
psql (14.2 (Debian 14.2-1+b3), server 12.11 (Ubuntu 12.11-0ubuntu0.20.04.1))
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
Type "help" for help.
postgres=# \\l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+-------------+-------------+-----------------------
confluence | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(4 rows)
Este ejemplo siguiente sería para hacer el mismo proceso, pero para abrir una conexión por ssh
confluence@confluence01:/opt/atlassian/confluence/bin$socat TCP-LISTEN:2222,fork TCP:10.4.50.215:22
</bin$ socat TCP-LISTEN:2222,fork TCP:10.4.50.215:22