Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- PATH=/usr/sbin:/sbin:/bin:/usr/bin
- DEFAULT="DROP"
- LAN_PALACIO="192.168.0.0/24"
- HOST_FW="192.168.0.254"
- ANYRED="0.0.0.0/0"
- LOO_RED="127.0.0.0/8"
- INT_IF="eth0"
- EXT_IF="eth1"
- EXT_IP="192.168.1.254"
- MAN_IF="eth2"
- MAN_IP="192.168.5.254"
- MAN_LAN="192.168.5.0/24"
- # flush de reglas
- echo -n "Flush de reglas de Firewall ..."
- iptables -F
- iptables -X
- iptables -Z
- iptables -t nat -F
- iptables -t mangle -F
- # por defecto todo a DROP
- echo -n "Aplicando Politica por predeterminada ..."
- iptables -P INPUT $DEFAULT
- iptables -P OUTPUT ACCEPT
- iptables -P FORWARD $DEFAULT
- ###iptables -P FORWARD ACCEPT
- iptables -t nat -P PREROUTING ACCEPT
- iptables -t nat -P POSTROUTING ACCEPT
- echo -n "Aplicando reglas del Firewall ..."
- # creamos cadenas personalizadas
- echo -n "Creando cadenas personalizadas"
- ###iptables -N SYN-FLOOD # limitar la cantidad de conexiones
- ###iptables -A SYN-FLOOD -m limit --limit 12/s --limit-burst 24 -j RETURN
- ###iptables -A SYN-FLOOD -j DROP
- # definimos que logeuamos
- echo -n "Definimos el nivel de LOG"
- ### Logueamos todo en /var/log/syslog
- #iptables -A INPUT -j LOG
- #iptables -A OUTPUT -j LOG
- #iptables -A FORWARD -j LOG
- #iptables -t nat -A PREROUTING -j LOG
- #iptables -t nat -A POSTROUTING -j LOG
- # activamos la opcion de sync cookies, nos protejemos del ataque DOS de bomba SYNC
- echo "1" > /proc/sys/net/ipv4/tcp_syncookies
- # opcion contra mensajes de error ICMP, evitamos ataques DOS de bomba ICMP
- echo "1" > /proc/sys/net/ipv4/icmp_ignore_bogus_error_responses
- # no respondemos al broadcast
- echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
- # Drop source routed packets
- echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route
- # Evitamos el spoofing
- for interface in /proc/sys/net/ipv4/conf/*/rp_filter; do
- echo "1" > ${interface}
- done
- # Los ICMPs redirigidos que pueden alterar la tabla de rutas.
- for interface in /proc/sys/net/ipv4/conf/*/accept_redirects; do
- echo "0" > ${interface}
- done
- # No guardamos registros de los marcianos.
- echo "1" > /proc/sys/net/ipv4/conf/all/log_martians
- # Denegamos el Broadcast de NetBIOS
- iptables -A FORWARD -p tcp --sport 137:139 -o $EXT_IF -j DROP
- iptables -A FORWARD -p udp --sport 137:139 -o $EXT_IF -j DROP
- iptables -A OUTPUT -p tcp --sport 137:139 -o $EXT_IF -j DROP
- iptables -A OUTPUT -p udp --sport 137:139 -o $EXT_IF -j DROP
- # Evitamos IP Spoofing via eth1 (dat1)
- # Cuando el Firewall dependa del Micronet SP891 solo se permitira el tafico desde la ip privada del Micronet
- iptables -A INPUT -i $EXT_IF -s 172.16.0.0/12 -j DROP
- iptables -A INPUT -i $EXT_IF -s 192.168.0.0/24 -j DROP
- # Chequeamos ataque DOS
- ###iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -p tcp --syn -j SYN-FLOOD
- # Denegamos TeamViewer
- iptables -A FORWARD -p tcp --dport 5938 -o $EXT_IF -j DROP
- # Activamos el Masquerade (NAT) para la red local
- echo 1 > /proc/sys/net/ipv4/ip_forward
- iptables -t nat -A POSTROUTING -s $LAN_PALACIO -o $EXT_IF -j MASQUERADE
- ############### VER ESTA REGLA ###############################
- ############### por ahora hay que dejarla porque en la casado usan pop3
- # Activamos el Masquerade (NAT) para la red MAN
- iptables -t nat -A POSTROUTING -s $MAN_LAN -o $EXT_IF -j MASQUERADE
- ### Permitimos accesos desde lo a la maquina local
- iptables -A INPUT -i lo -j ACCEPT
- iptables -A OUTPUT -o lo -j ACCEPT
- iptables -A INPUT -i lo -s $LOO_RED -d $LOO_RED -j ACCEPT
- iptables -A OUTPUT -o lo -s $LOO_RED -d $LOO_RED -j ACCEPT
- ### Permitimos el PostEnrutado de paquetes enviados localmente
- iptables -t nat -A POSTROUTING -o lo -s $LOO_RED -j ACCEPT
- ### Denegamos el acceso al 8080 para evitar que salteen el DANSGUARDIAN
- iptables -A INPUT -p tcp --dport 8080 -j DROP
- #######################################
- ##### Seccion Ingreso de Paquetes #####
- #######################################
- # Aceptamos conexiones nuevas desde la red interna
- iptables -A INPUT -i $INT_IF -m state --state NEW -j ACCEPT
- # Aceptamos conexiones establecidas o relacionadas desde el exterior
- iptables -A INPUT -i $EXT_IF -m state --state ESTABLISHED,RELATED -j ACCEPT
- # Logueamos las conexiones INVALIDas
- iptables -A INPUT -p tcp -m state --state INVALID -j LOG --log-prefix "[FW: Paquete Invalido]"
- # Aceptamos conexiones a los puertos 80 y 3128 desde la red interna
- iptables -A INPUT -i $INT_IF -s $LAN_PALACIO -p tcp --dport 80 -j ACCEPT
- iptables -A INPUT -i $INT_IF -s $LAN_PALACIO -p tcp --dport 3128 -j ACCEPT
- # Aceptamos conexiones a los puertos 80 y 3128 desde la red MAN
- iptables -A INPUT -i $MAN_IF -p tcp --dport 80 -j ACCEPT
- iptables -A INPUT -i $MAN_IF -p tcp --dport 3128 -j ACCEPT
- ## tengo que ver si habilito 110 y 25 pero necesito el 53
- ### 20141020 ########################################################
- ### habilitamos el acceso ssh y tcp para el sistema de sergio
- ### no usamos la cadena INPUT porque los paquetes pasan hasta la LAN
- ### en el caso del 80 y del 3128 si se usa INPUT porque salen directo hacia internet
- ### ssh para oficina de inspeccion
- ###iptables -A INPUT -i $MAN_IF -p tcp --dport 2222 -j ACCEPT
- ### tcp para oficina de personal externa
- #iptables -A INPUT -i $MAN_IF -p tcp --dport 2510 -j ACCEPT
- #iptables -A INPUT -i $MAN_IF -p tcp --dport 2520 -j ACCEPT
- #iptables -A INPUT -i $MAN_IF -p tcp --dport 2530 -j ACCEPT
- ######################################################################
- ### Aceptamos conexiones DNS
- ###iptables -A INPUT -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
- iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -d $ANYRED -o $EXT_IF -p udp -m udp --dport 53 --sport 1024:65535 -j ACCEPT
- echo -n "Permitimos Ping Pong INPUT,OUTPUT"
- ### Permitimos paquetes ICMP (ping, traceroute) con limites para evitar DOS
- ### Aceptamos ping y pong
- iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A INPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- ### Aceptamos redirecciones
- iptables -A INPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- ### Aceptamos tiempo excedido
- iptables -A INPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- ### Aceptamos destino inalcanzable
- iptables -A INPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- echo -n "Habilitamos los puertos para eth3 3128, 110 y 425"
- # permitimos el forward solo hacia inet
- iptables -A FORWARD -i eth2 -o eth1 -j ACCEPT
- echo -n "Proxy Transparente..."
- ### Proxy Transparente
- iptables -t nat -A PREROUTING -i eth0 -s $LAN_PALACIO -p tcp --dport 80 -j REDIRECT --to-port 3128
- iptables -t nat -A PREROUTING -i eth2 -s $MAN_LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
- ### Aceptamos conexiones DNS
- ###iptables -A INPUT -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
- iptables -A INPUT -m state --state ESTABLISHED,RELATED -s $ANYRED -i $EXT_IF -p udp -m udp --sport 53 --dport 1024:65535 -j ACCEPT
- iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -d $ANYRED -o $EXT_IF -p udp -m udp --dport 53 --sport 1024:65535 -j ACCEPT
- echo -n "Permitimos Ping Pong INPUT,OUTPUT"
- ### Permitimos paquetes ICMP (ping, traceroute) con limites para evitar DOS
- ### Aceptamos ping y pong
- iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A INPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- ### Aceptamos redirecciones
- iptables -A INPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- ### Aceptamos tiempo excedido
- iptables -A INPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- ### Aceptamos destino inalcanzable
- iptables -A INPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- iptables -A OUTPUT -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- echo -n "Habilitamos los puertos para eth3 3128, 110 y 425"
- # permitimos el forward solo hacia inet
- iptables -A FORWARD -i eth2 -o eth1 -j ACCEPT
- echo -n "Proxy Transparente..."
- ### Proxy Transparente
- iptables -t nat -A PREROUTING -i eth0 -s $LAN_PALACIO -p tcp --dport 80 -j REDIRECT --to-port 3128
- iptables -t nat -A PREROUTING -i eth2 -s $MAN_LAN -p tcp --dport 80 -j REDIRECT --to-port 3128
- iptables -A INPUT -i $MAN_IF -p tcp --dport 2222 -j ACCEPT
- iptables -A FORWARD -s $MAN_LAN -p tcp --dport 2222 -j ACCEPT
- iptables -A OUTPUT -d $ANYRED -o $EXT_IF -p tcp --dport 2222 --sport 1024:65535 -j ACCEPT
- ### Proxy Transparente POP3
- iptables -A FORWARD -s $MAN_LAN -p tcp --dport 425 -j ACCEPT
- ### Allow access from Sistemas on ssh port
- # Logueamos las conexiones SSH
- iptables -A INPUT -p tcp -s $SISTEMAS -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- iptables -A INPUT -p tcp -s $VMREMOTA -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- ###iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- # Permitimos las conexiones SSH desde $SISTEMAS
- iptables -A INPUT -i $INT_IF -p tcp -s $SISTEMAS -m tcp --dport 22 -j ACCEPT
- iptables -A INPUT -i $INT_IF -p tcp -s $VMREMOTA -m tcp --dport 22 -j ACCEPT
- echo -n "Reenvios..."
- ### Aceptamos algunos reenvios
- # Permitimos paquetes ICMP (ping, traceroute)
- #+ con limites para evitar ataques DoS
- # Aceptamos ping y pong
- iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A FORWARD -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- # Aceptamos redirecciones
- iptables -A FORWARD -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- # Aceptamos tiempo excedido
- iptables -A FORWARD -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- # Aceptamos destino inalcanzabe
- iptables -A FORWARD -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- # Aceptamos todas en INT_IF
- iptables -t nat -A PREROUTING -i $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
- iptables -t nat -A POSTROUTING -o $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
- echo -n "Conexiones salientes..."
- ### Aceptamos conexiones salientes
- # Permitimos cualquier salida tcp desde la propia maquina
- iptables -A OUTPUT -o $EXT_IF -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- # y conexiones entrantes relacionadas
- iptables -A INPUT -i $EXT_IF -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
- # Permitimos el reenvio de paquetes enviados desde la LAN
- iptables -A FORWARD -i $INT_IF -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el NAT de paquetes enviados desde la LAN
- iptables -t nat -A PREROUTING -i $INT_IF -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el NAT de paquetes enviados desde Inet hacia la ip publica
- iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- echo -n "NAT desde ippublica a Inet..."
- # Permitimos el NAT de paquetes enviados desde la ip publica hacia Inet
- iptables -t nat -A POSTROUTING -o $EXT_IF -s $EXT_IP -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A POSTROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el PostEnrutado de paquets enviados localmente
- iptables -t nat -A POSTROUTING -o $INT_IF -s $LAN_PALACIO -j ACCEPT
- # Permitimos salidas por ftp para las descargar de los repositorios de Debian
- #iptables -A
- ### Proxy Transparente POP3
- iptables -A FORWARD -s $MAN_LAN -p tcp --dport 425 -j ACCEPT
- ### Allow access from Sistemas on ssh port
- # Logueamos las conexiones SSH
- iptables -A INPUT -p tcp -s $SISTEMAS -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- iptables -A INPUT -p tcp -s $VMREMOTA -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- ###iptables -A INPUT -p tcp -m tcp --dport 22 -m state --state NEW -j LOG --log-prefix "[FW - SSH] "
- # Permitimos las conexiones SSH desde $SISTEMAS
- iptables -A INPUT -i $INT_IF -p tcp -s $SISTEMAS -m tcp --dport 22 -j ACCEPT
- iptables -A INPUT -i $INT_IF -p tcp -s $VMREMOTA -m tcp --dport 22 -j ACCEPT
- echo -n "Reenvios..."
- ### Aceptamos algunos reenvios
- # Permitimos paquetes ICMP (ping, traceroute)
- #+ con limites para evitar ataques DoS
- # Aceptamos ping y pong
- iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 2/s -j ACCEPT
- iptables -A FORWARD -p icmp --icmp-type echo-reply -m limit --limit 2/s -j ACCEPT
- # Aceptamos redirecciones
- iptables -A FORWARD -p icmp --icmp-type redirect -m limit --limit 2/s -j ACCEPT
- # Aceptamos tiempo excedido
- iptables -A FORWARD -p icmp --icmp-type time-exceeded -m limit --limit 2/s -j ACCEPT
- # Aceptamos destino inalcanzabe
- iptables -A FORWARD -p icmp --icmp-type destination-unreachable -m limit --limit 2/s -j ACCEPT
- # Aceptamos todas en INT_IF
- iptables -t nat -A PREROUTING -i $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
- iptables -t nat -A POSTROUTING -o $INT_IF -p icmp --icmp-type any -m limit --limit 2/s -j ACCEPT
- echo -n "Conexiones salientes..."
- ### Aceptamos conexiones salientes
- # Permitimos cualquier salida tcp desde la propia maquina
- iptables -A OUTPUT -o $EXT_IF -p tcp -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- # y conexiones entrantes relacionadas
- iptables -A INPUT -i $EXT_IF -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
- # Permitimos el reenvio de paquetes enviados desde la LAN
- iptables -A FORWARD -i $INT_IF -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el NAT de paquetes enviados desde la LAN
- iptables -t nat -A PREROUTING -i $INT_IF -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el NAT de paquetes enviados desde Inet hacia la ip publica
- iptables -t nat -A PREROUTING -i $EXT_IF -d $EXT_IP -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A PREROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- echo -n "NAT desde ippublica a Inet..."
- # Permitimos el NAT de paquetes enviados desde la ip publica hacia Inet
- iptables -t nat -A POSTROUTING -o $EXT_IF -s $EXT_IP -j ACCEPT
- # ... y conexiones salientes relacionadas
- iptables -t nat -A POSTROUTING -m state --state RELATED,ESTABLISHED -j ACCEPT
- # Permitimos el PostEnrutado de paquets enviados localmente
- iptables -t nat -A POSTROUTING -o $INT_IF -s $LAN_PALACIO -j ACCEPT
- # Permitimos salidas por ftp para las descargar de los repositorios de Debian
- #iptables -A OUTPUT -o eth0 -p tcp --dport 21 -j ACCEPT
- #iptables -A OUTPUT -o eth0 -p tcp --dport 20 -j ACCEPT
- iptables -A INPUT -i eth1 -j DROP
- iptables -A INPUT -i eth3 -j DROP
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement