Actualizado el sábado, 27 agosto, 2016
Este pequeño script restringe el acceso de IP’S cuando su servidor este recibiendo un ataque. Este script solo se debe ejecutar en el momento en que detecten que el servidor este siendo atacado, su función como lo indica el titulo es ayudar al firewall a bloquear las IPS atacantes para poder restablecer el servicio más rápidamente.
El script consta de dos partes:
- blockIP.sh
- ataques-reading.sh
Debemos subir los scripts a la carpeta root y asignarles los permisos.
- Chmod 777 blockIP.sh
- Chmod 777 ataques-reading.sh
Cuando detectamos un ataque debemos entrar por consola, a la carpeta root donde están los scripts y ejecutar:
./ataques-reading.sh
Pueden usar el script blockIP.sh para filtrar ips individuales:
./blockIP.sh LAIP ./blockIP.sh 192.168.1.3
blockIP.sh
#!/bin/bash if [ $# -eq 0 ] ; then echo -e " Uso: $0 IPs_a_bloquearn" exit 1 fi while [ $# -ne 0 ] ; do if [ "$1" == "127.0.0.1" -o "`echo $1 | cut -d'.' -f 1-3`" == "170.210.136" -o "`echo $1 | cut -d'.' -f 1-3`" == "170.210.156" ] ; then echo " No se bloquea $1!!!" else IP="$1" echo -n "Bloqueando $IP..." iptables -I INPUT -s $IP -j DROP echo -en "nRegistrando $IP..." echo "$IP" >> /root/blockIP.rules echo fi shift done exit 0
ataques-reading.sh
#!/bin/bash cd /root CONTADOR=0 while true ; do [ "$1" == "-d" ] && echo -e "nEntro al whilen" for IP in $(netstat -napt | grep -E "SYN_RECV|_WAIT" | sort -n -k 5 | awk '{print $5}' | cut -d':' -f 1 | sort -u | grep -vE "170.210.15[2-6]|127.0.0.1|170.210.156" ; do CANT=$(netstat -napt | grep "$IP" | wc -l) [ "$1" == "-d" ] && echo "Entro al for - $IP - $CANT" if [ $CANT -gt 15 ] ; then (iptables -nL | grep -qw $IP) && continue CONTADOR=$[CONTADOR+1] echo "$IP: $CANT conexiones - CONTADOR: $CONTADOR" /root/blockIP.sh $IP /etc/init.d/apache2 restart 2>/dev/null /etc/init.d/postgres restart 2>/dev/null if [ $CONTADOR -eq 20 ] ; then CONTADOR=0 fi echo fi done sleep 5 done exit
Nota: En el ejemplo 170.210.156 es la IP de salida del servidor. debe colocar la suya.
Reiniciar apache y la base de datos.
/etc/init.d/apache2 restart 2>/dev/null /etc/init.d/postgres restart 2>/dev/null
Ya lo tenemos preparado para ejecutarse en cualquier momento.