Last active
April 29, 2023 15:44
-
-
Save LuisPalacios/b648ef38206caa8c28cbc148a89ff364 to your computer and use it in GitHub Desktop.
Instala las reglas con iptables. Se debe ejecutar antes de tener la red activa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# /root/firewall/firewall_1_pre_network.sh | |
# | |
# Script que instala las reglas con iptables. Se debe ejecutar antes de tener la | |
# red activa, para que nunca pueda activarse la red sin estar las reglas antes puestas. | |
# | |
# Está relacionado con este apunte: | |
# https://www.luispa.com/administración/2023/04/08/networking-avanzado.html | |
# | |
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- | |
## Variables de trabajo | |
## | |
netSetupVars="/etc/default/netSetupVars" | |
basename=`basename "$0"` | |
uso() { | |
echo "${basename}. Copyright (c) 2023 Luis Palacios" | |
exit -1 # Salimos | |
} | |
if [ ! -f ${netSetupVars} ]; then echo "Error! el fichero ${netSetupVars} no existe"; uso; fi | |
. ${netSetupVars} | |
## | |
## ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- ----- | |
## Main | |
## | |
# Opciones de LOGGING | |
export LOGALL="no" | |
export LOGDROP="yes" | |
export LOGSERVICIOS="no" | |
export LOGCHROME="no" | |
export LOGMCAST="no" | |
export LOGINVALID="no" | |
# Hago el loggin con journald | |
export LOGGING="LOG --log-level info --log-prefix" | |
# Limpio iptables por completo | |
/root/firewall/firewall_clean.sh | |
## | |
## Instalo reglas con iptables. | |
## | |
# La policy por defecto es tirar (DROP) todo el tráfico. | |
# | |
iptables -P OUTPUT DROP | |
iptables -P INPUT DROP | |
iptables -P FORWARD DROP | |
iptables -F | |
# TCP MSS | |
# Una de las características de PPPoE es que reduce la MTU a 1492 y el MSS (Maximum Segment Size) | |
# negociado de TCP a 1452, así que tenemos que hacer lo mismo en nuestro Linux | |
iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu | |
iptables -t mangle -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --clamp-mss-to-pmtu | |
# OK a cualquier sesión que ya estuviese establecida. | |
iptables -N CH_ESTABLISHED | |
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j CH_ESTABLISHED | |
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j CH_ESTABLISHED | |
iptables -A FORWARD -m conntrack --ctstate ESTABLISHED,RELATED -j CH_ESTABLISHED | |
if [ "${LOGALL}" = "yes" ]; then | |
iptables -A CH_ESTABLISHED -j $LOGGING "CH_ESTABLISHED -- OK " | |
fi | |
iptables -A CH_ESTABLISHED -j ACCEPT | |
# Permito el trafico multicast, necesario para Movistar TV | |
iptables -N CH_MULTICAST | |
iptables -I INPUT -d 224.0.0.0/4 -j CH_MULTICAST | |
iptables -I OUTPUT -d 224.0.0.0/4 -j CH_MULTICAST | |
iptables -I FORWARD -d 224.0.0.0/4 -j CH_MULTICAST | |
if [ "${LOGMCAST}" = "yes" ]; then | |
iptables -A CH_MULTICAST -j $LOGGING "CH_MULTICAST -- OK " | |
fi | |
iptables -A CH_MULTICAST -j ACCEPT | |
# OK al tráfico de mi Intranet | |
iptables -N CH_Intranet | |
for intranet in ${INTRANET} | |
do | |
iptables -A INPUT -s ${intranet} -m conntrack --ctstate NEW -j CH_Intranet | |
iptables -A OUTPUT -s ${intranet} -m conntrack --ctstate NEW -j CH_Intranet | |
iptables -A FORWARD -s ${intranet} -m conntrack --ctstate NEW -j CH_Intranet | |
iptables -A INPUT -s ${intranet} -p udp -j CH_Intranet | |
iptables -A OUTPUT -s ${intranet} -p udp -j CH_Intranet | |
iptables -A FORWARD -s ${intranet} -p udp -j CH_Intranet | |
done | |
if [ "${LOGALL}" = "yes" ]; then | |
iptables -A CH_Intranet -j ${LOGGING} "CH_Intranet -- OK " | |
fi | |
iptables -A CH_Intranet -j ACCEPT | |
# MASQUERADE. | |
iptables -t nat -A POSTROUTING -o ${ifWAN} -j MASQUERADE # Hacia Internet sale con mi IP Pública | |
# Anti-spoofing en la interfaz publica | |
iptables -N CH_ANTISPOOF | |
for antispoof_net in ${INTRANET} | |
do | |
iptables -A INPUT -i ${ifWAN} -s $antispoof_net -m conntrack --ctstate NEW -j CH_ANTISPOOF | |
iptables -A FORWARD -i ${ifWAN} -s $antispoof_net -m conntrack --ctstate NEW -j CH_ANTISPOOF | |
done | |
if [ "${LOGDROP}" = "yes" ] || [ "${LOGALL}" = "yes" ]; then | |
iptables -A CH_ANTISPOOF -j $LOGGING "CH_ANTISPOOF -- DROP " | |
fi | |
iptables -A CH_ANTISPOOF -j DROP | |
# OK a mi trafico originado o recibido en la loopback | |
iptables -A INPUT -i lo -m conntrack --ctstate NEW -j ACCEPT | |
iptables -A OUTPUT -o lo -m conntrack --ctstate NEW -j ACCEPT | |
# OK que yo pueda salir hacia cualquier sitio | |
# incluso salida UDP desde mi mismo hacia cualquier sitio | |
iptables -A OUTPUT -m conntrack --ctstate NEW -j ACCEPT | |
iptables -A OUTPUT -s ${ipLAN}/32 -p udp -j ACCEPT | |
## ACCESO EXTERNO a mis Servicios | |
## IPSec como Servidor: XXXXXX (Cambiar por el puerto donde escuchas en ipsec) | |
# Dejé de usar esta opción para pasar a usar knockd. | |
#iptables -N CH_OPENPORTS | |
#iptables -A INPUT -p udp -m udp -m multiport --dports XXXXXX -m conntrack --ctstate NEW -j CH_OPENPORTS # OpenVPN en UDP | |
#if [ "${LOGSERVICIOS}" = "yes" ] || [ "${LOGALL}" = "yes" ]; then | |
# iptables -A CH_OPENPORTS -j $LOGGING "CH_OPENPORTS -- OK " | |
#fi | |
#iptables -A CH_OPENPORTS -j ACCEPT | |
# Permito bootp,s en vlan100 | |
iptables -A INPUT -i ${ifLAN} -p udp -m udp -m multiport --dports 68,67 -j ACCEPT | |
iptables -A FORWARD -o ${ifLAN} -p udp -m udp -m multiport --dports 68,67 -j ACCEPT | |
# Cargarse cualquier paquete que no este en ningun estado valido | |
iptables -N CH_Invalido | |
iptables -A INPUT -m conntrack --ctstate INVALID -j CH_Invalido | |
iptables -A OUTPUT -m conntrack --ctstate INVALID -j CH_Invalido | |
iptables -A FORWARD -m conntrack --ctstate INVALID -j CH_Invalido | |
if [ "${LOGINVALID}" = "yes" ]; then | |
iptables -A CH_Invalido -j $LOGGING "CH_Invalido -- DROP " | |
fi | |
iptables -A CH_Invalido -j DROP | |
# El resto de trafico se tira(drop) | |
iptables -N CH_DROP | |
iptables -A INPUT -j CH_DROP | |
iptables -A FORWARD -j CH_DROP | |
iptables -A OUTPUT -j CH_DROP | |
if [ "${LOGDROP}" = "yes" ] || [ "${LOGALL}" = "yes" ]; then | |
iptables -A CH_DROP -j $LOGGING "CH_DROP -- " | |
fi | |
iptables -A CH_DROP -j DROP | |
## END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment