#!/bin/bash

##### SCRIPT DESCARGA DIARIA IOCs SQUID #####
#####       			         #####
# ----------------------------------------- #
##### REQUISITOS PREVIOS                #####
#####   apt install curl                #####


#####       	VARIABLES	        #####
# ----------------------------------------- #

SQUID_CFG=/etc/squid/squid.conf
lineasIOCs=0
# Directorio Descarga Ficheros IOCs
dirIOCs=/etc/squid/iOCs
dirLogs=/etc/squid/iOCs/log
timestamp=`date +%Y-%m-%d_%H:%M:%S`
#logIOCs=$dirLogs/`date +%Y%m%d`_logIOCs.log
logIOCs=$dirLogs/logIOCs.log

# COMPROBAMOS EXISTENCIA DE DIRECTORIO IOCs #
if [ -d $dirIOCs ]
then
    # EXISTE EL FICHERO LOG #
    if [ -f $logIOCs ]
    then
    	echo "$timestamp Existe fichero $logIOCs" >> $logIOCs
    else
    	touch $logIOCs
    	echo "$timestamp Creado Satisfatoriamente fichero $logIOCs" >> $logIOCs
    fi
else
    mkdir -p $dirIOCs
    mkdir $dirLogs
    touch $logIOCs
    echo "$timestamp Creado Satisfatoriamente fichero $logIOCs" >> $logIOCs
    echo "$timestamp Creado Satisfatoriamente directorio $dirIOCs" >> $logIOCs
    echo "$timestamp Creado Satisfatoriamente directorio $dirLogs" >> $logIOCs
fi

# Fichero Listado IOCs
fuentesIOCs=$dirIOCs/fuentesIOCs.list
if [ -f $fuentesIOCs ]
then
    echo "$timestamp Existe fichero $fuentesIOCs" >> $logIOCs
else
    touch $fuentesIOCs
    echo "$timestamp Creado Satisfatoriamente fichero $fuentesIOCs" >> $logIOCs
fi


#####           FUNCIONES               #####
# ----------------------------------------- #

# Funcion que descarga ficheros IOCs y los pasa a carpeta adecuada SQUID
descargaFicherosIOCs () {
    echo "$timestamp --> COMIENZA DESCARGA FICHEROS..." >> $logIOCs
    while read fuente; do
            #echo -e "##### $fuente #####";
            curl --insecure -s -u 'SRVC_FTPS01:<password_ftp>' ftps://$1//IOC/Actual/Blacklist/$fuente --output /tmp/$fuente > /dev/null
            # Si el fichero EXISTE y NO esta vacio lo movemos a la carpeta correspondiente de SQUID
            if [ -f "/tmp/$fuente" ]; then
                if [ -s "/tmp/$fuente" ]; then
                    echo "$timestamp Fichero $fuente Descargado Corretamente y Con Tamaño > 0" >> $logIOCs
                    sed -i '/^#/d' /tmp/$fuente
                    mv /tmp/$fuente $dirIOCs/$fuente
                else
                    echo "$timestamp Fichero $fuente Descargado Correctamente con TAMAÑO INCORRECTO" >> $logIOCs
                fi
            else
                echo "$timestamp FALLO en la descarga del fichero $fuente" >> $logIOCs
            fi
    done < $fuentesIOCs
}
# Funcion que cuenta las lineas IOCs existentes en el fichero de configuracion de SQUID
cuentaLineasIOCs_squid () {
    lineas=0
    while read lineaSquid; do
		case $lineaSquid in
			*INICIO_IOCs*)
				lineas=0
			;;
			*FIN_IOCs*)
				lineasIOCs=$lineas
			;;
			*)
				lineas=$(($lineas+1))
			;;
		esac
    done < $SQUID_CFG
    if [ $lineasIOCs -ne 0 ]
	then
		lineasIOCs=$((lineasIOCs+2))
    fi
    # echo "LINEAS GENERADAS AUTOMATICAMENTE: $lineasIOCs"
}
# Funcion que añade la configuracion correspondiente a IOCs en el archivo de configuracion de SQUID 
cargaFicherosIOCs_squid () {

    cuentaLineasIOCs_squid
    echo "$timestamp --> COMIENZA CARGA FICHEROS SQUID..." >> $logIOCs
    if [ $lineasIOCs -eq 0 ] 
    ##### SI NO HAY CONFIGURACION PREVIA DE IOCs SE AÑADE AL PRINCIPIO #####
    then
		#echo "LINEAS 0"
		sed -i '1s/^/#####-------------------------FIN_IOCs-------------------------#####\n/' $SQUID_CFG
		while read fuente; do
		  case $fuente in
			*spam_ip*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dst "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dst $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*md5*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*sha*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*hash*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*spam*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*dom*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*domain*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*_ip*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dst "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dst $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*_url*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			  ;;
			*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
		  esac
		done < $fuentesIOCs
		sed -i '1s/^/#####-----------------------INICIO_IOCs------------------------#####\n/' $SQUID_CFG

    ##### SI HAY CONFIGURACION PREVIA DE IOCs SE BORRA LO QUE HAY Y SE AÑADE LO NUEVO #####
    else
		#echo "LINEAS $lineasIOCs"
		##### BORRAMOS CONFIGURACION PREVIA EXISTENTE #####
		sed -i '1,'"$lineasIOCs"'d' $SQUID_CFG
		##### INSERTAMOS LO NUEVO #####
		sed -i '1s/^/#####-------------------------FIN_IOCs-------------------------#####\n/' $SQUID_CFG
		while read fuente; do
		  case $fuente in
			*spam_ip*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dst "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dst $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*md5*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*sha*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*hash*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*spam*)
				echo "$timestamp SKIP $fuente" >> $logIOCs
			;;
			*dom*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*domain*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*_ip*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dst "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dst $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
			*_url*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			  ;;
			*)
				sed -i '1s#^#'"http_access deny $fuente"'\n#' $SQUID_CFG
				sed -i '1s#^#'"acl $fuente dstdomain "'"'"$dirIOCs/$fuente"'"''\n#' $SQUID_CFG
				#echo "acl $fuente dstdomain $dirIOCs/$fuente"
				#echo "http_access deny $fuente"
			;;
		  esac
		done < $fuentesIOCs
		sed -i '1s/^/#####-----------------------INICIO_IOCs------------------------#####\n/' $SQUID_CFG
    fi
}

#####           EJECUCION SCRIPT        #####
# ----------------------------------------- #

# COMPROBAMOS DISPONIBILIDAD DE SERVICIO FTPS IOCs Y DESCARGAMOS DE DONDE PROCEDA FICHEROS IOCs#
if nc -zw1 ftp-automal.dmsas.sda.sas.junta-andalucia.es 990; then
  echo "$timestamp FTPs IOCs Malaga DISPONIBLE" >> $logIOCs
  curl --insecure -l -s -u 'SRVC_FTPS01:<password_ftp>' ftps://ftp-automal.dmsas.sda.sas.junta-andalucia.es//IOC/Actual/Blacklist/ > $fuentesIOCs
  descargaFicherosIOCs ftp-automal.dmsas.sda.sas.junta-andalucia.es
  cargaFicherosIOCs_squid
  echo "$timestamp --> RECARGA SQUID..." >> $logIOCs
  service squid reload
  echo "$timestamp - Script ejecutado corretamente -" >> $logIOCs

elif nc -zw1 ftp-autosev.dmsas.sda.sas.junta-andalucia.es 990; then
  echo "$timestamp FTPs IOCs Sevilla DISPONIBLE" >> $logIOCs
  curl --insecure -l -s -u 'SRVC_FTPS01:<password_ftp>' ftps://ftp-autosev.dmsas.sda.sas.junta-andalucia.es//IOC/Actual/Blacklist/ > $fuentesIOCs
  descargaFicherosIOCs ftp-autosev.dmsas.sda.sas.junta-andalucia.es
  cargaFicherosIOCs_squid
  echo "$timestamp --> RECARGA SQUID <--" >> $logIOCs
  service squid reload
  echo "$timestamp - Script ejecutado corretamente -" >> $logIOCs

else
  echo "$timestamp NO FTPs IOSc DISPONIBLES" >> $logIOCs
  exit
fi



