#!/bin/bash
# Script de Execução de Firewall Iptables 1.0
# Criado por Eliseu Cherene eliseurcv@hotmail.com www.eliseucherene.com
# Dia 30/07/2007 4:30 AM
echo "Iniciando Firewall..."
# ------------- #
# VARIÁVEIS #
# ------------- #
IF_EXTERNA=eth0
IF_INTERNA=eth1
IF_DMZ=eth2
# ---------------------------------------- #
# PROTEÇÃO CONTRA IP SPOOFING #
# ---------------------------------------- #
echo "1" > /proc/sys/net/ipv4/conf/all/rp_filter
# --------------------------------------- #
# DETERMINA A POLÍTICA PADRÃO #
# --------------------------------------- #
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
#############
# TABELA FILTER #
#############
# -------------------------------------------- #
# DROPA PACOTES TCP INDESEJÁVEIS #
# -------------------------------------------- #
iptables - A FORWARD -p tcp ! -syn -m state -state NEW -j LOG -log-level 6 -log-prefix "FIREWALL: NEW sem syn:"
iptables - A FORWARD -p tcp ! -syn -m state -state NEW -j DROP
# ---------------------------------------- #
# DROPA PACOTES MAL FORMADOS #
# ---------------------------------------- #
iptables -A INPUT -i $IF_EXTERNA -m unclean -j LOG -log-log-level 6 -log-prefix "FIREWALL: pacote mal formado:"
iptables -A INPUT -i $IF_EXTERNA -m unclean -j DROP
# ------------------------------------------------------------ #
# ACEITA PACOTES QUE REALMENTE DEVEM ENTRAR #
# ------------------------------------------------------------ #
iptables - A INPUT -i ! $IF_EXTERNA -j ACCEPT
iptables - A INPUT -m state - state ESTABILISHED,RELATED -j ACCEPT
iptables - A OUTPUT -m state - state ESTABILISHED,RELATED,NEW -j ACCEPT
iptables - A FORWARD -m state - state ESTABILISHED,RELATED,NEW -j ACCEPT
# ---------------------------------- #
# PROTEÇÃO CONTRA TRINOO #
# ---------------------------------- #
iptables -N TRINOO
iptables -A TRINOO -m limit -limit 15/m -j LOG -log-log-level 6 -log-prefix "FIREWALL: trinoo:"
iptables -A TRINOO -j DROP
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 27444 -j TRINOO
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 27665 -j TRINOO
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 31335 -j TRINOO
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 34555 -j TRINOO
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 35555 -j TRINOO
# ------------------------------------ #
# PROTEÇÃO CONTRA TROJANS #
# ------------------------------------ #
iptables -N TROJAN
iptables -A TROJAN -m limit -limit 15/m -j LOG -log-log-level 6 -log-prefix "FIREWALL: TROJAN:"
iptables -A TROJAN -j DROP
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 666 -j TROJAN
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 6660 -j TROJAN
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 4000 -j TROJAN
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 6000 -j TROJAN
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 6006 -j TROJAN
iptables -A INPUT -p TCP -i $IF_EXTERNA -dport 16660 -j TROJAN
# ---------------------------------- #
# PROTEÇÃO CONTRA WORMS #
# --------------------------------- #
iptables -A FORWARD -p tcp -dport 135 -i $IFINTERNA -j REJECT
# -------------------------------------- #
# PROTEÇÃO CONTRA SYN-FLOOD #
# -------------------------------------- #
iptables -A FORWARD -p tcp -syn -m limit -limit 2/s -j ACCEPT
# -------------------------------------------- #
# PROTEÇÃO CONTRA PING DA MORTE #
# ------------------------------------------- #
iptables -A FORWARD -p icmp-type echo-request -m limit -limit 1/s -j ACCEPT
# -------------------------------------------- #
# PROTEÇÃO CONTRA PORT SCANNERS #
# -------------------------------------------- #
iptables -N SCANNER
iptables -A SCANNER -m limit -limit 15/m -j LOG -log-level 6 -log-prefix "FIREWALL: port scanner:"
iptables -A SCANNER -j DROP
iptables -A INPUT -p tcp -tcp-flags ALL FIN,URG,PSH -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags ALL NONE -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags ALL ALL -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags ALL FIN,SYN -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags ALL SYN,RST,ACK,FIN,URG -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags SYN,RST SYN,RST -i $IF_EXTERNA -j SCANNER
iptables -A INPUT -p tcp -tcp-flags SYN,FIN SYN,FIN -i $IF_EXTERNA -j SCANNER
# ---------------------------------------------------------------------------- #
# CRIA LOG DE TENTATIVA DE ACESSO A DETERMINADAS PORTAS #
# ---------------------------------------------------------------------------- #
iptables -A INPUT -p tcp -dport 21 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: ftp"
iptables -A INPUT -p tcp -dport 23 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: telnet"
iptables -A INPUT -p tcp -dport 25 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: smtp"
iptables -A INPUT -p tcp -dport 80 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: http"
iptables -A INPUT -p tcp -dport 110 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: pop3"
iptables -A INPUT -p tcp -dport 111 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: rpc"
iptables -A INPUT -p tcp -dport 113 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: identd"
iptables -A INPUT -p tcp -dport 137:139 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: samba"
iptables -A INPUT -p tcp -dport 161:162 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: snmp"
iptables -A INPUT -p tcp -dport 6667:6668 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: irc"
iptables -A INPUT -p tcp -dport 3128 -i $IF_EXTERNA -j LOG -log-level 6 -log-prefix "FIREWALL: squid"
# --------------------------------------------------------------- #
# LIBERA ACESSO EXTERNO A DETERMINADAS PORTAS #
# --------------------------------------------------------------- #
iptables -A INPUT -p -dport 22 -i $IF_EXTERNA -j ACCEPT
# ---------------------------------------------------------------------------------- #
# LIBERA ACESSO PARA FORA DA PORTA 9090 para um determinado IP #
# ---------------------------------------------------------------------------------- #
iptables -A FORWARD -p tcp -d ! xxx.xxx.xxx.xxx -dport 9090 -j LOG -log-level 6 -log-prefix "FIREWALL: 9090"
iptables -A FORWARD -p tcp -d ! xxx.xxx.xxx.xxx -dport 9090 -j ACCEPT
# OU
iptables -t nat -A POSTROUTING -s xxx.xxx.xxx.xxx/xx -p tcp --dport 9090 -j MASQUERADE
###########
# TABELA NAT #
###########
# ------------------------------------------- #
# ATIVA O MASCARAMENTO DE SAÍDA #
# ------------------------------------------- #
iptables -A POSTROUTING -t nat -o $IF_EXTERNA -j MASQUERADE
# --------------------------------------------------------------------------------- #
# ATIVA O MASCARAMENTO DE SAÍDA COM VÁRIAS PORTAS AO MESMO #TEMPO
# --------------------------------------------------------------------------------- #
iptables -t nat -A POSTROUTING -p tcp -o $IF_EXTERNA -m multiport --dport xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx,xxx -j MASQUERADE
# --------------------------- #
# PROXY TRANSPARENTE #
# --------------------------- #
#iptables -t nat -A PREROUTING -i $IF_INTERNA -p TCP -DPORT 80 -j REDIRECT -to-port 3128
#iptables -t nat -A PREROUTING -i $IF_INTERNA -p TCP -DPORT 8080 -j REDIRECT -to-port 3128
# -------------------------------------------------- #
# REDIRECIONA PORTAS PARA OUTROS PCS #
# -------------------------------------------------- #
iptables -t nat -A PREROUTING -d xxx.xxx.xxx.xxx -p tcp -dport 22 -j DNAT -to-destination xxx.xxx.xxx.xxx
# ------------------------------------------------------------------------------ #
# REDIRECIONA PORTAS PARA OUTROS PCS VIDAS DO IP EXTERNO #
# ------------------------------------------------------------------------------ #
iptables -A PREROUTING -t nat -p tcp -i $IF_INTERNA -d $IF_INTERNA --dport xxxx -j DNAT --to xxx.xxx.xxx.xxx:xxxx
iptables -A FORWARD -p tcp -i $IF_INTERNA -d xxx.xxx.xxx.xxx --dport xxxx -j ACCEPT
# -------------------------------------------- #
# LIBERA O MSN APENAS PARA ESTE IP #
# -------------------------------------------- #
iptables -t nat -A POSTROUTING -s xxx.xxx.xxx.xxx -p tcp --dport 1863 -j MASQUERADE