Espero que les sea útil. Por supuesto son libres de copiar, compartir y modificar el código.
- #!/bin/bash
- ### BEGIN INIT INFO
- # Provides: iptables.sh
- # Required-Start: $local_fs $remote_fs $network $syslog $named
- # Required-Stop: $local_fs $remote_fs $network $syslog $named
- # Default-Start: 2 3 4 5
- # Default-Stop: 0 1 6
- ### END INIT INFO
- ###REGLAS APLICADAS AL KERNEL /etc/sysctl.conf###
- #net.ipv6.conf.all.disable_ipv6=1#
- #net.ipv4.ip_forward=1#
- #net.ipv4.tcp_syncookies=1#
- #net.ipv4.icmp_echo_ignore_broadcasts=1#
- #net.ipv4.conf.all.rp_filter=1#
- #net.ipv4.conf.all.accept_redirects=0#
- #net.ipv4.conf.all.log_martians=1#
- ######################################VARIABLES###################################
- ##VARIABLES MENU##
- OUTPUT=/tmp/output.tx$$
- INPUT=/tmp/input.sh$$
- >$OUTPUT
- trap "rm $INPUT; rm $OUTPUT; exit" SIGHUP SIGINT SIGTERM
- ##BLOQUEOS INPUT##
- #PUERTOS TCP DESTINO INPUT
- INTCPPORTD=( )
- #PUERTOS TCP ORIGEN INPUT
- INTCPPORTS=( 20 21 22 80 443 )
- #PUERTOS UDP DESTINO INPUT
- INUDPPORTD=( 123 )
- #PUERTOS UDP ORIGEN INPUT
- INUDPPORTS=( 53 123 )
- ##BLOQUEOS OUTPUT##
- #PUERTOS TCP ORIGEN OUTPUT
- OTCPPORTS=( 31337 31338 31339 31340 )
- #PUERTOS UDP ORIGEN OUTPUT
- OUDPPORTS=(31337 31338 31339 31340)
- ##VARIABLES EXTRA
- #CONTADOR INTERFACES
- INTERFACES=$(ifconfig |grep eth |wc -l)
- #INTEFAZ
- I=0
- ##DATOS RED
- IP=0
- MASCARA=0
- ################################START############################
- Arrancar_cortafuegos()
- {
- ###POLITICAS GENERALES###
- iptables -F
- iptables -t nat -F
- iptables -t mangle -F
- iptables -P INPUT DROP
- iptables -P FORWARD DROP
- iptables -P OUTPUT ACCEPT
- #############################FILTER#########################
- INTERFACES=$((INTERFACES-1))
- while [ ${I} -le ${INTERFACES} ]
- do
- if [ $(cat /etc/issue |awk '{print $1}') == "Debian" ];
- then
- IP=$(ifconfig eth${I} |grep "inet " |awk {'print $2'} |cut -d ":" -f 2)
- MASCARA=$(ifconfig eth${I} |grep "inet " |awk {'print $4'} |cut -d ":" -f 2)
- else
- IP=$(ifconfig eth${I} |grep "inet:" |awk {'print $2'} |cut -d ":" -f 2)
- MASCARA=$(ifconfig eth${I} |grep "inet " |awk {'print $4'} |cut -d ":" -f 2)
- fi
- ###INPUT ACCEPT INTERNAL###
- iptables -A INPUT -p icmp --icmp-type echo-reply -m state --state ESTABLISHED -j ACCEPT
- #TCP INPUT
- if [ -n "${INTCPPORTD}" ];
- then
- for i in "${INTCPPORTD[@]}"
- do
- iptables -A INPUT -s ${IP}/${MASCARA} -p tcp --dport $i -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- done
- fi
- if [ -n "${INTCPPORTS}" ];
- then
- for i in "${INTCPPORTS[@]}"
- do
- iptables -A INPUT -p tcp --sport $i -m state --state ESTABLISHED,RELATED -j ACCEPT
- done
- fi
- #UDP INPUT
- if [ -n "${INUDPPORTD}" ];
- then
- for i in "${INUDPPORTD[@]}"
- do
- iptables -A INPUT -s ${IP}/${MASCARA} -p udp --dport $i -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- done
- fi
- if [ -n "${INUDPPORTS}" ];
- then
- for i in "${INUDPPORTS[@]}"
- do
- iptables -A INPUT -p udp --sport $i -m state --state ESTABLISHED,RELATED -j ACCEPT
- done
- fi
- ###OUTPUT BLOCK###
- #TCP OUTPUT
- if [ -n "${OTCPPORTS}" ];
- then
- for i in "${OTCPPORTS[@]}"
- do
- iptables -A OUTPUT -p tcp --sport $i -m state --state ESTABLISHED,RELATED -j DROP
- done
- fi
- #UDP OUTPUT
- if [ -n "${OUDPPORTS}" ];
- then
- for i in "${OUDPPORTS[@]}"
- do
- iptables -A OUTPUT -p udp --sport $i -m state --state ESTABLISHED,RELATED -j DROP
- done
- fi
- I=$((I+1))
- done
- ###REGLAS EXTRA###
- #DNS
- cat /etc/resolv.conf |awk {'print $2'} > /tmp/dns.txt
- while read LINEA
- do
- iptables -A INPUT -s ${LINEA} -m state --state NEW,ESTABLISHED,RELATED -p udp --sport 53 -j ACCEPT
- done < /tmp/dns.txt
- rm /tmp/dns.txt
- #LOOPBACK
- iptables -A INPUT -i lo -j ACCEPT
- }
- ################################STOP############################
- Parar_cortafuegos()
- {
- #POLITICAS GENERALES ABIERTAS
- iptables -F
- iptables -t nat -F
- iptables -t mangle -F
- iptables -P INPUT ACCEPT
- iptables -P FORWARD ACCEPT
- iptables -P OUTPUT ACCEPT
- }
- Modificacion_puertos()
- {
- FINAL=n
- clear
- echo "Los puertos actuales seran eliminados y sustituidos por los introducidos ahora:"
- while [ ${FINAL} != 'y' ];
- do
- echo "Introduce un puerto:"
- read PUERTO
- if [ ${PUERTO} -gt 0 ];
- then
- echo ${PUERTO} >> /tmp/puertos.txt
- else
- echo "NO ES UN VALOR VALIDO!"
- fi
- echo "Quiere añadir otro puerto?[s/n]"
- read RESPUESTA
- if [ ${RESPUESTA} != 's' ];
- then
- FINAL='y'
- fi
- done
- }
- ENTRANTES_TCP_DESTINO()
- {
- dialog --clear \
- --title "Puertos TCP de destino ENTRANTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${INTCPPORTD}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${INTCPPORTD[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- ENTRANTES_TCP_DESTINO
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="INTCPPORTD=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 30 /etc/init.d/iptables.sh |grep INTCPPORTD)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- ENTRANTES_TCP_DESTINO
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- ENTRANTES_TCP_ORIGEN()
- {
- dialog --clear \
- --title "Puertos TCP de origen ENTRANTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${INTCPPORTS}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${INTCPPORTS[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- ENTRANTES_TCP_ORIGEN
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="INTCPPORTS=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 32 /etc/init.d/iptables.sh |grep INTCPPORTS)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- ENTRANTES_TCP_ORIGEN
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- ENTRANTES_UDP_DESTINO()
- {
- dialog --clear \
- --title "Puertos UDP de destino ENTRANTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${INUDPPORTD}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${INUDPPORTD[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- ENTRANTES_UDP_DESTINO
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="INUDPPORTD=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 34 /etc/init.d/iptables.sh |grep INUDPPORTD)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- ENTRANTES_UDP_DESTINO
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- ENTRANTES_UDP_ORIGEN()
- {
- dialog --clear \
- --title "Puertos UDP de origen ENTRANTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${INUDPPORTS}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${INUDPPORTS[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- ENTRANTES_UDP_ORIGEN
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="INUDPPORTS=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 36 /etc/init.d/iptables.sh |grep INUDPPORTS)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- ENTRANTES_UDP_ORIGEN
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- SALIENTES_TCP_ORIGEN()
- {
- dialog --clear \
- --title "Puertos TCP de origen SALIENTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${OTCPPORTS}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${OTCPPORTS[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- SALIENTES_TCP_ORIGEN
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="OTCPPORTS=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 40 /etc/init.d/iptables.sh |grep OTCPPORTS)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- SALIENTES_TCP_ORIGEN
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- SALIENTES_UDP_ORIGEN()
- {
- dialog --clear \
- --title "Puertos UDP de origen SALIENTES abiertos" \
- --menu "" 15 50 4 \
- LEER "Consultar puertos configurados" \
- MODIFICAR "Modificacion de puertos" \
- ATRAS "Volver al menu principal" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- LEER)
- if [ -n "${OUDPPORTS}" ];
- then
- echo " LISTADO DE PUERTOS" >> /tmp/temporal.txt
- for i in "${OUDPPORTS[@]}"
- do
- echo " PUERTO:${i}" >> /tmp/temporal.txt
- done
- clear
- cat /tmp/temporal.txt
- rm /tmp/temporal.txt
- echo " Pulse cualquier tecla para continuar"
- read
- else
- dialog --clear \
- --title "Vacio" \
- --msgbox "No hay puertos configurados" 5 50
- fi
- SALIENTES_UDP_ORIGEN
- ;;
- MODIFICAR)
- Modificacion_puertos
- SENTENCIA="OUDPPORTS=("
- while read LINEA
- do
- SENTENCIA="${SENTENCIA} ${LINEA}"
- done < /tmp/puertos.txt
- SENTENCIA="${SENTENCIA} )"
- LINEAORIG=$(head -n 42 /etc/init.d/iptables.sh |grep OUDPPORTS)
- sed -i "s/${LINEAORIG}/${SENTENCIA}/g" /etc/init.d/iptables.sh
- rm /tmp/puertos.txt
- dialog --clear \
- --title "Cambios realizados" \
- --msgbox "Cambios implantados, reiniciar cortafuegos para aplicar la nueva config" 5 80
- SALIENTES_UDP_ORIGEN
- ;;
- ATRAS) Cambiar_parametros;;
- SALIDA) exit 0;;
- esac
- }
- Cambiar_parametros()
- {
- dialog --clear \
- --title "Parametros cortafuegos" \
- --menu "" 15 100 7 \
- TCPDESTENT "Puertos TCP de destino ENTRANTES" \
- TCPORIGENT "Puertos TCP de origen ENTRANTES" \
- UDPDESTENT "Puertos UDP de destino ENTRANTES" \
- UDPORIGENT "Puertos UDP de origen ENTRANTES" \
- TCPORIGSAL "Puertos TCP de origen SALIENTES" \
- UDPORIGSAL "Puertos UDP de origen SALIENTES" \
- SALIDA "Salir" 2>"${INPUT}"
- menuitem=$(<"${INPUT}")
- case $menuitem in
- TCPDESTENT) ENTRANTES_TCP_DESTINO;;
- TCPORIGENT) ENTRANTES_TCP_ORIGEN;;
- UDPDESTENT) ENTRANTES_UDP_DESTINO;;
- UDPORIGENT) ENTRANTES_UDP_ORIGEN;;
- TCPORIGSAL) SALIENTES_TCP_ORIGEN;;
- UDPORIGSAL) SALIENTES_UDP_ORIGEN;;
- SALIDA) exit 0;;
- esac
- }
- #SECUENCIA
- case $1 in
- start)
- Arrancar_cortafuegos
- ;;
- stop)
- Parar_cortafuegos
- ;;
- restart)
- $0 stop
- $0 start
- ;;
- change)
- Cambiar_parametros
- ;;
- *)
- echo "Usage /etc/init.d/iptables.sh start|stop|restart|change"
- ;;
- esac
- exit 0
Me ha encantado tu post, lo tendre en cuenta para probarlo
ResponderEliminarMe alegro que te haya sido útil. Espero que le puedas sacar mucho partido.
EliminarUn saludo.