
Tengo un puerto abierto, 40002, quiero limitar que al mismo tiempo el puerto solo pueda conectarse mediante una dirección IP (no una dirección específica). Si ya hay una dirección IP que se conecta a ese puerto, otras IP no podrán conectarse.
¿Es posible configurarlo mediante Iptables o scripts? mi sistema es Ubuntu 14.04 gracias.
Respuesta1
Puedes hacerlo configurando iptables.
/sbin/iptables -A INPUT -p tcp --syn --dport $port -m connlimit --connlimit-above N -j REJECT --reject-with tcp-reset
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save
Ejemplo: limitar las conexiones SSH por IP/host
/sbin/iptables -A INPUT -p tcp --syn --dport 22 -m connlimit --connlimit-above 3 -j REJECT
# save the changes see iptables-save man page, the following is redhat and friends specific command
service iptables save
PRUEBAS:
#!/bin/bash
ip="202.1.2.3"
port="80"
for i in {1..100}
do
# do nothing just connect and exit
echo "exit" | nc ${ip} ${port};
done
OK: Para limitar n conexiones como máximo, aquí hay un ejemplo usando el módulo de límite de IP:
iptables -A INPUT -p tcp --syn -dport 40002 -m iplimit --iplimit-above 3 -J REJECT
Esto RECHAZARÁ las conexiones si hay 3 IP conectadas. Perdón si entendí mal tu pregunta ;)