Enviado em 25/02/2015 - 17:17h
Boa Noite,#!/bin/bash
# -----------------------------------------
# - DECLARANDO AS VARIAVEIS -
# -----------------------------------------
# Interface de rede ligada a internet
IFACE_WEB="eth0"
# Interface de rede ligada a rede interna
IFACE_REDE="eth1"
# Rede interna
REDE_INTERNA="xxx.xxx.xxx.xxx/xx"
# -----------------------------------------
# - FUNCAO DE START -
# -----------------------------------------
function start () {
# -----------------------------------------
# - LIMPA AS REGRAS EXISTENTES -
# -----------------------------------------
# Limpa as regras da tabela filter
iptables -F
# Limpa as regras da tabela nat
iptables -t nat -F
#Liberando conexao para rede local antes de criar as regras.
iptables -A INPUT -s xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx -j ACCEPT
# -----------------------------------------
# - DEFININDO POLITICAS DO IPTABLES -
# -----------------------------------------
iptables -P INPUT DROP
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
# -----------------------------------------
# - HABILITANDO O ROTEAMENTO NO KERNEL -
# -----------------------------------------
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/conf/all/rp_filter
# -----------------------------------------
# - CRIA A IDA E VOLTA DO ACESSO NAS -
# - CHAINS INPUT, OUTPUT E FORWARD, -
# - ASSIM NAO PRECISAMOS CRIAR A IDA E -
# - VOLTA NAS REGRAS -
# -----------------------------------------
iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
# -----------------------------------------
# - REGRAS DE NAT -
# -----------------------------------------
# COMPARTILHA A INTERNET
iptables -t nat -A POSTROUTING -s $REDE_INTERNA -o $IFACE_WEB -j MASQUERADE
# REDERICIONA TUDO PARA O PROXY
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-ports 8080
# -----------------------------------------
# - REGRAS DE INPUT -
# - ---------------------------------------
# Libera SICOOB
iptables -t nat -I PREROUTING -s xxx.xxx.xxx.xxx/xx -p tcp -d xxx.xxx.xxx.xxx/xx --dport 80 -j ACCEPT
# Desabilitando resposta a comando ping
echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
# Libera o acesso SSH de qualquer origem
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
# Libera o squid a partir da rede interna
iptables -A INPUT -s $REDE_INTERNA -p tcp --dport 3128 -j ACCEPT
# Aceita ping apenas da rede interna
iptables -A INPUT -s $REDE_INTERNA -p icmp --icmp-type 8 -j ACCEPT
# BLOQUEIA TRACEROUTE
iptables -A INPUT -p udp --dport 33435:33525 -j DROP
# PROTECAO CONTRA PORTSSCANNERS, PING OF DEATH, DoS
iptables -A INPUT -m state --state INVALID -j DROP
# LIBERAR OU BLOQUEAR SKYPE
iptables -A INPUT -p tcp --dport 23399 -j ACCEPT
# BLOQUEANDO O ACESSO A P2P E TORRENT
iptables -A INPUT -p tcp --dport 6881:6999 -j DROP
iptables -A INPUT -p tcp --sport 6881:6999 -j DROP
#BLOQUEANDO SITES HTTPS
iptables -I FORWARD -m string --algo bm --string "facebook" -j DROP
iptables -I FORWARD -m string --algo bm --string "youtube" -j DROP
iptables -I FORWARD -m string --algo bm --string "twitter" -j DROP
# -----------------------------------------
# - REGRAS DE OUTPUT -
# -----------------------------------------
# Libera as portas constantes na variavel $PORTAS_TCP
#iptables -A OUTPUT -p tcp -m multiport --dports $PORTAS_TCP -j ACCEPT
# Libera ping para qualquer lugar
iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
# -----------------------------------------
# - REGRAS DE FORWARD -
# -----------------------------------------
# Libera as portas constantes em na varial $PORTAS_REDE_INTERNA
#iptables -A FORWARD -p tcp -m multiport --dports $PORTAS_REDE_INTERNA -j ACCEPT
# PROTECAO CONTRA SYN-FLOOD
iptables -A FORWARD -p tcp --syn -m limit --limit 2/s -j ACCEPT
# PROTECAO CONTRA PING DA MORTE
iptables -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# PROTECAO CONTRA PORT SCANNERS
iptables -N SCANNER
iptables -A SCANNER -j DROP
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 1/s -j ACCEPT
iptables -A FORWARD -p tcp --tcp-flags SYN,ACK,FIN,RST SYN,ACK,FIN,RST -m limit --limit 1/s -j ACCEPT
# -----------------------------------------
# - FINAL DA FUNCAO START -
# -----------------------------------------
}
# -----------------------------------------
# - FUNCAO STOP -
# -----------------------------------------
function stop () {
# -----------------------------------------
# - LIMPA AS REGRAS EXISTENTES -
# -----------------------------------------
iptables -F
iptables -t nat -F
# -----------------------------------------
# - DEFINE POLITICAS PADRAO -
# -----------------------------------------
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD ACCEPT
# -----------------------------------------
# - HABILITA O ROTEAMENTO NO KERNEL -
# -----------------------------------------
echo 1 > /proc/sys/net/ipv4/ip_forward
# -----------------------------------------
# - COMPARTILHA A INTERNET -
# -----------------------------------------
iptables -t nat -A POSTROUTING -s $REDE_INTERNA -o $IFACE_WEB -j MASQUERADE
# -----------------------------------------
# - FINAL DA FUNCAO STOP -
# -----------------------------------------
}
# -----------------------------------------
# - FUNCAO DE RESTART -
# -----------------------------------------
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "COMANDO INCOMPLETO DIGITE /etc/init.d/firewall.sh start, stop ou restart"
exit 0
;;
esac
# ------------------------------------------
# - FIM DO SCRIPT DE FIREWALL -
# ------------------------------------------
echo ---------------------------------------
echo - RESUMO DAS REGRAS -
echo ---------------------------------------
iptables -L