Skip to content

Instantly share code, notes, and snippets.

@ignaciogutierrez
Created July 24, 2024 21:35
Show Gist options
  • Save ignaciogutierrez/65784cb4cedbf9155e34e48ec98e8914 to your computer and use it in GitHub Desktop.
Save ignaciogutierrez/65784cb4cedbf9155e34e48ec98e8914 to your computer and use it in GitHub Desktop.
Linux Redirect Port using IPTables

Redireccionar las conexiones de un puerto a otro, usando IPTABLES

En este ejemplo veremos como redireccionar el puerto 2525 para enviar todas sus conexiones al programa o servicio que se encuentre escuchando en el puerto 25

Revisar la configuracion de la tabla NAT


iptables -t nat -L -n -v


Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
4403K  231M DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 DOCKER     all  --  *      *       0.0.0.0/0           !127.0.0.0/8          ADDRTYPE match dst-type LOCAL

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 MASQUERADE  all  --  *      !docker0  172.17.0.0/16        0.0.0.0/0           
    0     0 MASQUERADE  tcp  --  *      *       172.17.0.2           172.17.0.2           tcp dpt:3306

Chain DOCKER (2 references)
 pkts bytes target     prot opt in     out     source               destination         
    0     0 RETURN     all  --  docker0 *       0.0.0.0/0            0.0.0.0/0           
    0     0 DNAT       tcp  --  !docker0 *       0.0.0.0/0            127.0.0.1            tcp dpt:3306 to:172.17.0.2:3306

Agregar la regla para redireccionar el puerto 2525 al 25


iptables -t nat -A PREROUTING -p tcp --dport 2525 -j REDIRECT --to-port 25

Volver a revisar la configuracion de la tabla NAT

Deberá salir la nueva regla


iptables -t nat -L -n -v

Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
4403K  231M DOCKER     all  --  *      *       0.0.0.0/0            0.0.0.0/0            ADDRTYPE match dst-type LOCAL
    0     0 REDIRECT   tcp  --  *      *       0.0.0.0/0            0.0.0.0/0            tcp dpt:2525 redir ports 25
...
...

Probar con Telnet la conexion


telnet <dominio-ipaddr> <nPuerto>

telnet postal5.saitnube.com 2525
Trying 85.239.247.66...
Connected to postal5.saitnube.com.
Escape character is '^]'.
220 postal5.saitnube.com ESMTP Postal/KVKOLBGZ
HELO
250 postal5.saitnube.com
EHLO
250-My capabilities are
250 AUTH CRAM-MD5 PLAIN LOGIN
QUIT
221 Closing Connection
Connection closed by foreign host.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment