Enviado em 27/05/2013 - 09:18h
Pessoal blz?
#!/bin/bash
echo ""
echo "#| Script de Firewall – IPTABLES 2013"
echo "#| Editado por: Victor Felipe Dolce Cardoso"
echo "#| victorfelipedolcec@hotmail.com"
echo "#| Uso: firewall start|stop"
echo “#| Fonte: http://www.vivaolinux.com.br/artigo/Script-de-firewall-completissimo?pagina=2"
echo ""
# Variáveis
PATH=/sbin:/bin:/usr/sbin:/usr/bin
IPTABLES="/sbin/iptables"
# Arquivos com as portas a liberar e a bloquear
#aqui vou liberar/bloquear portas para entrada(input), saída(output) e forward
#de maneira distinta, por isso a criação de 3variáveis
#PINPUT = portas entrada POUTPUT = portas saída PFORWARD = portas forward
#
PINPUT=”/etc/firewall/PORTSINPUT”
POUTPUT=”/etc/firewall/PORTSOUTPUT”
PFORWARD=”/etc/firewall/PORTSFORWARD”
PBLOQ=”/etc/firewall/PORTSBLOQ”
# Redirecionamento de Portas (NAT)
REDILIST="/etc/firewall/REDILIST"
#IPS com acesso irrestrito
IPSLIBERADOS="/etc/firewall/IPSLIBERADOS"
# Script Firewall
PROGRAMA="/etc/firewall/FIREWALL"
# Interfaces de Rede
WAN=eth0
LAN1=eth1
LAN2=eth2
REDE1="192.168.1.0/25"
REDE2="192.168.2.0/23"
# Carregando Modulos
modprobe ip_tables
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_LOG
modprobe ipt_REJECT
modprobe ipt_MASQUERADE
modprobe ipt_state
modprobe ipt_multiport
modprobe iptable_mangle
modprobe ipt_tos
modprobe ipt_limit
modprobe ipt_mark
modprobe ipt_MARK
case "$1" in
start)
echo "CARREGANDO FIREWALL..." ; sleep 1 ;
#Zerando Regras
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -t mangle -F
$IPTABLES -t nat -F
$IPTABLES -X
#Compartilhar a conexao
$IPTABLES -t nat -A POSTROUTING -o $WAN -j MASQUERADE
echo "1" > /proc/sys/net/ipv4/ip_forward
#Politicas Padrao
$IPTABLES -P INPUT DROP
$IPTABLES -P OUTPUT DROP
$IPTABLES -P FORWARD DROP
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A INPUT -i $LAN1 -j ACCEPT #Habilito o acesso ao servidor de internet para a Rede Interna ($LAN)
echo -n "LIBERANDO PORTAS DE COMUNICACAO..." ;
#portas entrada, INPUT
#variavel que armazena as portas = PINPUT
Sleep 1 ;
CONTPORTA=0
For i in `cat $PINPUT`; do
PORTA=`echo $i | cut -d '#' -f 1`
$IPTABLES -A INPUT -p tcp --dport $PORTA -j ACCEPT
$IPTABLES -A INPUT -p udp --dport $PORTA -j ACCEPT
done
sleep 2 ;
[ $CONTPORTA -ne 0 ] && echo "[OK] $CONTPORTA PORTAS INPUT LIBERADAS!"
[ $CONTPORTA -eq 0 ] && echo "[OK] NENHUMA PORTA INPUT A LIBERAR!"
#portas saida, OUTPUT
#variavel que armazena as portas = POUTPUT
Sleep 1 ;
CONTPORTA=0
For i in `cat $POUTPUT `; do
PORTA=`echo $i | cut -d '#' -f 1`
$IPTABLES -A OUTPUT -p tcp --sport $PORTA -j ACCEPT
$IPTABLES -A OUTPUT -p udp --sport $PORTA -j ACCEPT
done
sleep 2 ;
[ $CONTPORTA -ne 0 ] && echo "[OK] $CONTPORTA PORTAS OUTPUT LIBERADAS!"
[ $CONTPORTA -eq 0 ] && echo "[OK] NENHUMA PORTA OUTPUT A LIBERAR!"
#portas forward,
#variavel que armazena as portas = PFORWARD
Sleep 1 ;
CONTPORTA=0
For i in `cat $PFORWARD `; do
PORTA=`echo $i | cut -d '#' -f 1`
$IPTABLES -A FORWARD -p tcp --dport $PORTA -j ACCEPT
$IPTABLES -A FORWARD -p udp --dport $PORTA -j ACCEPT done
sleep 2 ;
[ $CONTPORTA -ne 0 ] && echo "[OK] $CONTPORTA PORTAS FORWARD LIBERADAS!"
[ $CONTPORTA -eq 0 ] && echo "[OK] NENHUMA PORTA FORWARD A LIBERAR!"
echo -n "REDIRECIONANDO PORTAS (NAT)..." ;
CONTREDIRECT=0;
for i in `cat $REDILIST`; do
IP=`echo $i | cut -d '#' -f 1`
PPRIVADA=`echo $i | cut -d '#' -f 2`
PPUBLICA=`echo $i | cut -d '#' -f 3`
$IPTABLES -t nat -I PREROUTING -i $WAN -p tcp --dport $PPUBLICA -j DNAT --to-destination $IP:$PPRIVADA
let CONTREDIRECT++ ;
done
sleep 2 ;
[ $CONTREDIRECT -ne 0 ] && echo "[OK] $CONTREDIRECT PORTAS REDIRECIONADAS!"
[ $CONTREDIRECT -eq 0 ] && echo "[OK] NENHUMA PORTA A REDIRECIONAR!"
echo -n "LIBERANDO IPS SEM INTERMEDIO DO PROXY..." ;
CONTIP=0;
for i in `cat $IPSLIBERADOS `; do
IPLIBERADO=`echo $i | cut -d ';' -f 1`
$IPTABLES -A FORWARD -s $IPLIBERADO -j ACCEPT
$IPTABLES -t nat -A PREROUTING -p tcp --dport 80 -s $IPLIBERADO -j RETURN
let CONTIP++ ;
done
sleep 2 ;
[ $CONTIP -ne 0 ] && echo "[OK] $CONTIP IPS LIBERADOS!"
[ $CONTIP -eq 0 ] && echo "[OK] NENHUM IP A LIBERAR!"
#$IPTABLES -t nat -A PREROUTING -s $REDE1 -p tcp --dport 80 -j REDIRECT --to-port 3128
#$IPTABLES -t nat -A PREROUTING -s $REDE2 -p tcp --dport 80 -j REDIRECT --to-port 3128
#Não responde pings
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -j DROP
#ProteþÒo contra Ip Spoofing
echo 1 > /proc/sys/net/ipv4/conf/default/rp_filter
$IPTABLES -A INPUT -m state --state INVALID -j DROP
#Impedindo ataque Ping of Death na rede
$IPTABLES -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
#Impedindo ataque de Denial Of Service Dos na rede e servidor
$IPTABLES -I FORWARD -p tcp -m limit --limit 1/s -j ACCEPT
$IPTABLES -A INPUT -p tcp -m limit --limit 1/s -j ACCEPT
#Protecao contra synflood
echo "1" > /proc/sys/net/ipv4/tcp_syncookies
echo "FIREWALL CARREGADO!"
echo " "
;;
stop)
echo "DESCARREGANDO FIREWALL..."; sleep 1 ;
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -t mangle -F
$IPTABLES -t nat -F
$IPTABLES -X
$IPTABLES -Z
echo "FIREWALL DESCARREGADO!"
echo " "
;;
*)
SEG='8'
while [ $SEG != -1 ] ; do
clear;
echo "Argumento nÒo informado ou invßlido, por seguranþa o firewall serß INICIADO em $SEG. [^C] para cancelar"
let SEG--;
sleep 1;
done
$PROGRAMA start
exit 1
esac
exit 0