Cambiar el orden en que se lee una CADENA IPTABLE (Usando Fail2Ban)

Cambiar el orden en que se lee una CADENA IPTABLE (Usando Fail2Ban)

Tengo algo de experiencia usando iptables y fail2ban. Ambos funcionan como deberían, pero me gustaría optimizar la forma en que se "DROPing" los paquetes cuando se realizan pruebas de IP y puertos.

Fail2Ban está haciendo un buen trabajo bloqueando las IP que intentan acceder a varios puertos (es decir, SSH, MySQL, etc.).

Sin embargo, una vez que se bloquea una IP en un puerto específico (es decir, el puerto 22 para SSH), aún se puede acceder al HOST a través de ICMP, aunque Fail2Ban haya agregado una condición "DROP - all" a iptables.

Puede que me equivoque, pero creo que tiene que ver con el orden en el que iptables lee la CADENA Fail2Ban.

Esto es lo que iptables -Lrevela (las IP y los DNS han sido reemplazados):

user@ SERVER > iptables -L
Chain INPUT (policy ACCEPT)
target     prot opt source               destination
fail2ban-SSH  tcp  --  anywhere             anywhere            tcp dpt:ssh
ACCEPT     all  --  10.10.10.1/25  anywhere
fail2ban-SSH  all  --  anywhere             anywhere
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain FORWARD (policy ACCEPT)
target     prot opt source               destination
fail2ban-SSH  all  --  anywhere             anywhere
RH-Firewall-1-INPUT  all  --  anywhere             anywhere

Chain OUTPUT (policy ACCEPT)
target     prot opt source               destination

Chain RH-Firewall-1-INPUT (2 references)
target     prot opt source               destination
ACCEPT     all  --  anywhere             anywhere
ACCEPT     all  --  anywhere             anywhere
ACCEPT     icmp --  anywhere             anywhere            icmp any
ACCEPT     esp  --  anywhere             anywhere
ACCEPT     ah   --  anywhere             anywhere
ACCEPT     udp  --  anywhere             224.0.0.251         udp dpt:mdns
ACCEPT     udp  --  anywhere             anywhere            udp dpt:ipp
ACCEPT     tcp  --  anywhere             anywhere            tcp dpt:ipp
ACCEPT     all  --  anywhere             anywhere            state RELATED,ESTABLISHED
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ftp
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:ssh
ACCEPT     tcp  --  anywhere             anywhere            state NEW tcp dpt:http
REJECT     all  --  anywhere             anywhere            reject-with icmp-host-prohibited

Chain fail2ban-SSH (3 references)
target     prot opt source               destination
DROP       all  --  badip.baddomain.org  anywhere
DROP       all  --  299.299.299.11         anywhere
DROP       all  --  prober.hackers.com  anywhere
RETURN     all  --  anywhere             anywhere

Además, aquí tenéis mi iptablesarchivo como punto de referencia.:

# Firewall configuration written by system-config-securitylevel
# Manual customization of this file is not recommended.
*filter
:fail2ban-SSH - [0:0]
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:RH-Firewall-1-INPUT - [0:0]
#
#
#
-A INPUT -j fail2ban-SSH
-A FORWARD -j fail2ban-SSH
-A INPUT -j RH-Firewall-1-INPUT 
-A FORWARD -j RH-Firewall-1-INPUT 
-A RH-Firewall-1-INPUT -i lo -j ACCEPT 
-A RH-Firewall-1-INPUT -i eth0 -j ACCEPT 
-A RH-Firewall-1-INPUT -p icmp -m icmp --icmp-type any -j ACCEPT 
-A RH-Firewall-1-INPUT -p esp -j ACCEPT 
-A RH-Firewall-1-INPUT -p ah -j ACCEPT 
-A RH-Firewall-1-INPUT -d 224.0.0.251 -p udp -m udp --dport 5353 -j ACCEPT 
-A RH-Firewall-1-INPUT -p udp -m udp --dport 631 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m tcp --dport 631 -j ACCEPT 
-A RH-Firewall-1-INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 21 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT 
-A RH-Firewall-1-INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT 
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited 
COMMIT

Como puedes ver, hay una línea que PERMITE ICMP:

ACCEPT     icmp --  anywhere             anywhere            icmp any

Esto se hace a propósito porque necesito que los usuarios legítimos puedan hacer ping a ciertos servidores.

Puede ver en mi archivo iptables que agregué la CADENA "fail2ban-SSH" antes que las demás con la esperanza de que se leyera antes que todas las demás reglas, pero eso no funcionó.

Mi objetivo sería ELIMINAR CUALQUIER solicitud de una IP que Fail2Ban haya bloqueado por cualquier motivo, incluidas las solicitudes ICMP.

¿Hay alguna manera de configurar iptables para leer las reglas de Fail2Ban, antes que todas las demás CADENAS y reglas, de modo que realmente pueda bloquear una IP en todos los puertos y protocolos?

Respuesta1

Si entiendo su pregunta correctamente, las IP en su cárcel SSH deberían estar bloqueadas en todos los puertos del sistema y no deberían poder enviarle ping. Todas las demás IP deberían poder hacer ping.

Para prohibir una IP en todos los puertos, deberá configurar su cárcel SSH para usar la configuración de acción iptables-allports. Puede configurar si desea usar DROP, REJECT, etc. en /etc/fail2ban/action.d/iptables-blocktype.conf

[sshd]
enabled  = true
action   = iptables-allports[name=sshd]
filter   = sshd
logpath  = /var/log/auth.log
maxretry = 5
findtime = 300
bantime  = 7200

Si también desea evitar que esta IP específica le haga ping y al mismo tiempo permitir que cualquier otra persona que no esté en la cárcel SSH haga ping, deberá agregar otra acción a su cárcel SSH.

  • Haga copias de iptables-allports.conf e iptables-blocktype.conf.
  • Asigne nuevos nombres a los archivos, como: iptables-blockping.conf e iptables-blocktype-ping.conf.
  • Abra iptables-blockping.conf y actualice la sección [INCLUYE] para que apunte a iptables-blocktype-ping.conf.
  • Abra iptables-blocktype-ping.conf y cambie el tipo de bloque a REJECT --reject-with icmp-host-prohibited.

Las cárceles pueden tener múltiples acciones, por lo que directamente debajo de iptables-allports[nombre=sshd], indique el nombre de su nuevo archivo de configuración de acciones, iptables-blockping.conf.

Esto debería funcionar para sus propósitos: las IP en su cárcel SSH tendrán entradas específicas en iptables para rechazar solicitudes de ping. Estas reglas se leerán después de su regla para permitir pings.

información relacionada