Elsiguiente script analiza el log auth.log y busca intentos de entrada con usuarios no registrados en el sistema, genera reglas IPTABLESpara impedir el acceso desde las IP atacantes y genera reglas IPTABLES para permitir el acceso desde IP's autorizadas
#!/bin/sh # ataques.sh (C) 2007 Joaquin Herrero # # Analiza el log auth.log y busca intentos de entrada con usuarios no registrados en el sistema # Genera reglas IPTABLES / PF para impedir el acceso desde las IP atacantes # Genera reglas IPTABLES / PF para permitir el acceso desde IP's autorizadas # ----------------------------------------------------- # Modify this variables to suit to your system # Modificar estas variables para adaptarlo a su sistema # ----------------------------------------------------- _EMPRESAS=/root/IP_AUTORIZADAS _MYINTERFACE=eth0 _AUTH_LOG=/var/log/auth.log _REGLAS_IPTABLES=reglas_iptables.sh _REGLAS_PF=reglas.pf # ------------------------------------------------------------------ # Don't need to modify beyond this point # No hace falta modificar nada a partir de este punto # ------------------------------------------------------------------ _MYIP=`ifconfig $_MYINTERFACE | grep "inet " | awk {'print $2'} | cut -d":" -f2` _START_LOG=`head -n 1 $_AUTH_LOG | awk '{print $1 " " $2 " " $3}'` _END_LOG=`tail -n 1 $_AUTH_LOG | awk '{print $1 " " $2 " " $3}'` _SIZE_LOG=`ls -lh $_AUTH_LOG | awk '{print $5}'` echo echo "Datos de $_AUTH_LOG ($_SIZE_LOG)" echo "desde $_START_LOG hasta $_END_LOG" _TEMPFILE=/tmp/_ataques`date +%Y%m%d%H%M%S` grep "Failed password for" $_AUTH_LOG | awk '{print $13}' | sort | uniq -c > $_TEMPFILE # # Intentos de entrar con nombres de usuario inexistentes # echo echo "\"Failed password for invalid user\" detectados" echo "---------------------------------------------" printf "%15s %15s %10s %-20s \n" "IP" "EMPRESA" "INTENTOS" "USUARIOS PROBADOS" _IPLIST_1=`grep "Failed password for invalid user" $_AUTH_LOG | awk '{print $13}' | sort | uniq` for _IP in $_IPLIST_1 do _NOMBRES_DE_USUARIO_PROBADOS_1=`\ grep "Failed password for invalid user" $_AUTH_LOG | \ awk -v ip=$_IP '$0 ~ ip {print $11}' | \ sort | uniq` _NUMERO_DE_INTENTOS=`\ grep "Failed password for invalid user" $_AUTH_LOG | \ awk -v ip=$_IP '$0 ~ ip {print $11}' | \ sort | wc -l` _EMPRESA=`awk -v ip=$_IP '$0 ~ ip {print $2}' $_EMPRESAS` if [ -z $_EMPRESA ]; then _EMPRESA="---" fi _CUANTOS=`echo $_NOMBRES_DE_USUARIO_PROBADOS_1 | uniq | wc -w` if [ $_CUANTOS -eq 1 ]; then _USERS=`echo $_NOMBRES_DE_USUARIO_PROBADOS_1 | sed 's/^M/ /g'` _USERS_LIST=$_USERS else _USERS_LIST="($_CUANTOS)" fi printf "%15s %15s %10s %-20s \n" $_IP $_EMPRESA $_NUMERO_DE_INTENTOS $_USERS_LIST done # # Intentos de entrar con nombres de usuario registrados # y que han fallado por password incorrecta # echo echo "\"Failed password\" detectados para usuarios registrados" echo "------------------------------------------------------" printf "%15s %15s %10s %-20s \n" "IP" "EMPRESA" "INTENTOS" "USUARIOS PROBADOS" _IPLIST_2=`grep "Failed password for" $_AUTH_LOG | grep -v "invalid user" | awk '{print $11}' | sort | uniq` for _IP in $_IPLIST_2 do _NOMBRES_DE_USUARIO_PROBADOS_2=`\ grep "Failed password for" $_AUTH_LOG | \ grep -v "invalid user" | \ awk -v ip=$_IP '$0 ~ ip {print $9}' | \ sort | uniq` _NUMERO_DE_INTENTOS=` grep "Failed password for" $_AUTH_LOG | \ grep -v "invalid user" | \ awk -v ip=$_IP '$0 ~ ip {print $11}' | \ sort | wc -l` _CUANTOS_NOMBRES_DE_USUARIO_PROBADOS_2=`\ grep "Failed password for" $_AUTH_LOG | \ grep -v "invalid user" | \ awk -v ip=$_IP '$0 ~ ip {print $9}' | \ sort | uniq | wc -l` _EMPRESA=`awk -v ip=$_IP '$0 ~ ip {print $2}' $_EMPRESAS` if [ -z $_EMPRESA ]; then _EMPRESA="---" fi if [ $_CUANTOS_NOMBRES_DE_USUARIO_PROBADOS_2 -eq 1 ] then _USERS=`echo $_NOMBRES_DE_USUARIO_PROBADOS_2 | sed 's/^M/ /g'` _USERS_LIST=$_USERS else _USERS_LIST="($_CUANTOS)" fi printf "%15s %15s %10s %-20s \n" $_IP $_EMPRESA $_NUMERO_DE_INTENTOS $_USERS_LIST done # ------------------------------------------------------------- # Generacion de reglas PF # ------------------------------------------------------------- echo "" > $_REGLAS_PF echo "# Reglas packet filter" > $_REGLAS_PF # -------------------------------------------------------------------------- # Reglas BLOCK para los que han intentado entrar con usuarios no registrados echo "\n# reglas DROP para atacantes tipo 1 \n" >> $_REGLAS_PF for _IP in $_IPLIST_1 do echo "block drop in quick on $_MYINTERFACE inet from $_IP to any" >> $_REGLAS_PF done # ----------------------------------------------------------------------- # Reglas BLOCK para los que han intentado entrar con usuarios registrados echo "\n# reglas DROP para atacantes tipo 2 \n" >> $_REGLAS_PF for _IP in $_IPLIST_2 do echo "block drop in quick on $_MYINTERFACE inet from $_IP to any" >> $_REGLAS_PF done # Reglas ACCEPT para direcciones IP autorizadas echo "\n# reglas ACCEPT para direcciones IP autorizadas\n" >> $_REGLAS_PF while read line do _IP_EMPRESA=$(echo $line|cut -d" " -f1) if [ ! -z $_IP_EMPRESA ] then echo "pass in quick on $_MYINTERFACE from $_IP_EMPRESA to $_MYIP keep state" >> $_REGLAS_PF fi done < ${_EMPRESAS} # ------------------------------------------------------------- # Generacion de reglas IPTABLES # ------------------------------------------------------------- echo "" > $_REGLAS_IPTABLES echo "# Reglas iptables para default accept" >> $_REGLAS_IPTABLES echo "iptables -P INPUT ACCEPT" >> $_REGLAS_IPTABLES echo "iptables -F INPUT" >> $_REGLAS_IPTABLES # ------------------------------------------------------------------------- # Reglas DROP para los que han intentado entrar con usuarios no registrados echo "\n# reglas DROP\n" >> $_REGLAS_IPTABLES for _IP in $_IPLIST_1 do echo "iptables --append INPUT --protocol TCP --source $_IP --destination $_MYIP --jump DROP" >> $_REGLAS_IPTABLES done # ---------------------------------------------------------------------- # Reglas DROP para los que han intentado entrar con usuarios registrados echo "\n# reglas DROP\n" >> $_REGLAS_IPTABLES for _IP in $_IPLIST_1 do echo "iptables --append INPUT --protocol TCP --source $_IP --destination $_MYIP --jump DROP" >> $_REGLAS_IPTABLES done # --------------------------------------------- # Reglas ACCEPT para direcciones IP autorizadas echo "\n# reglas ACCEPT\n" >> $_REGLAS_IPTABLES while read line do _IP_EMPRESA=$(echo $line|cut -d" " -f1) if [ ! -z $_IP_EMPRESA ] then echo "iptables --append INPUT --protocol TCP --source $_IP_EMPRESA --destination $_MYIP --jump ACCEPT" >> $_REGLAS_IPTABLES fi done < ${_EMPRESAS} echo echo "Se han generado los archivos \"$_REGLAS_IPTABLES\" y \"$_REGLAS_PF\"" echo "para impedir el acceso a estos atacantes y permitir el acceso a las" echo "direcciones IP contenidas en el fichero \"$_EMPRESAS\"" echo echo "Para ver las reglas activas de IPTABLES teclee: iptables -vL" echo "Para ver las reglas activas de PACKET FILTER teclee: pfctl -sr"
Suponiendo que hemos creado el fichero /root/IP_AUTORIZADAS y que este contiene
195.77.152.5 ISTECTRA 203.144.1.20 ASMILA
# ataques.sh Datos de /var/log/auth.log (771K) desde Jul 26 06:25:04 hasta Jul 28 17:29:13 "Failed password for invalid user" detectados --------------------------------------------- IP EMPRESA INTENTOS USUARIOS PROBADOS 192.148.123.61 --- 999 (800) 195.77.152.5 ISTECTRA 2 rfdez23 203.144.1.20 ASMILA 6 (4) 222.255.3.68 --- 12 (12) "Failed password" detectados para usuarios registrados ------------------------------------------------------ IP EMPRESA INTENTOS USUARIOS PROBADOS 192.148.123.61 --- 43 (12) 211.140.51.52 --- 3 root 211.238.49.80 --- 3 root 213.134.40.24 --- 1 user_1 222.255.3.68 --- 1 root Se han generado los archivos "reglas_iptables.sh" y "reglas.pf" para impedir el acceso a estos atacantes y permitir el acceso a las direcciones IP contenidas en el fichero "/root/IP_AUTORIZADAS" Para ver las reglas activas de IPTABLES teclee: iptables -vL Para ver las reglas activas de PACKET FILTER teclee: pfctl -sr