echo "#########################################################################"
echo " APÓS O TÉRMINO DESTE PROCESSO SEU SERVIDOR SERÁ REINICIADO "
echo " SE DESEJAR ABORTAR PRESSIONE CTRL+C "
echo " ESTE PROCESSO COMEÇARÁ EM 5 SEGUNDOS "
echo "#########################################################################"
sleep 5
# AJUSTANDO O APT #####################################################
echo "#################AJUSTANDO OS REPOSITÓRIOS###############################"
sleep 5
${MV} ${APT}/sources.list ${APT}/sources.list.bkp
${CAT} <<EOF > ${APT}/sources.list
# Repositório Oficial
deb ftp://ftp.br.debian.org/debian lenny main contrib non-free
deb-src ftp://ftp.br.debian.org/debian lenny main contrib non-free
# Repositório de atualizações frequentes
deb http://volatile.debian.org/debian-volatile lenny/volatile main contrib non-free
deb-src http://volatile.debian.org/debian-volatile lenny/volatile main contrib non-free
# Repositório de atualizações de segurança
deb http://security.debian.org/ lenny/updates main contrib non-free
deb-src http://security.debian.org/ lenny/updates main contrib non-free
# Repositório de atualizações propostas
deb ftp://ftp.br.debian.org/debian lenny-proposed-updates main contrib non-free
deb-src ftp://ftp.br.debian.org/debian lenny-proposed-updates main contrib non-free
# Repositório Backport
deb http://www.backports.org/debian lenny-backports main contrib non-free
deb-src http://www.backports.org/debian lenny-backports main contrib non-free
# Plugins multimídia
deb http://ftp.br.debian.org/debian-multimedia/ lenny main
#deb http://www.debian-multimedia.org lenny main
# PHP5 backport
deb http://packages.dotdeb.org lenny all
deb-src http://packages.dotdeb.org lenny all
EOF
###########ATUALIZANDO AS INFORMAÇÕES DOS NOVOS REPOSITÓRIOS##################
echo "################ATUALIZANDO AS INFORMAÇÕES DOS NOVOS REPOSITÓRIOS########"
sleep 5
${APTITUDE} -y update
###########ATUALIZANDO AS CHAVES DE REPOSITÓRIO (KEYRINGS)####################
${APTITUDE} -y install debian-backports-keyring
#########ENTRAR O DIRETÓRIO BASE PARA O KERNEL################################
${CD} ${PATH_SRC}
##############BAIXAR O KERNEL E OS OUTROS PACOTES QUE SERÃO CUSTOMIZADO#######
echo "############BAIXANDO O KERNEL E OUTROS PACOTES NECESSÁRIOS############"
${WGET} -c ${KERNEL_URL}
${WGET} -c ${NETFILTER_URL}
${WGET} -c ${PROTOCOLS_URL}
${WGET} -c ${CONFIG_URL}
##############DESEMPACOTAR O KERNEL###########################################
${TAR} -xvjpf ${KERNEL}.tar.bz2
##############CRIAR UM LINK PARA DEFINIR QUAL KERNEL FICARA EM USO#####################
${LN} -sf ${KERNEL} linux
############APLICANDO O PATCH AO KERNEL#######################################
echo "##############APLICANDO PATCH AO KERNEL########################"
sleep 5
${CD} netfilter-layer7-v2.22
${CP} kernel-2.6.25-2.6.28-layer7-2.22.patch ${PATH_SRC}/linux/
${CD} ${PATH_SRC}/linux
${PATCH} -p1 < kernel-2.6.25-2.6.28-layer7-2.22.patch
###########INSERINDO O MÓDULO DO LAYER7 NO KERNEL#############################
${SED} '/CONFIG_NETFILTER_XT_MATCH_STATE=m/ a \CONFIG_NETFILTER_XT_MATCH_LAYER7=m' ${PATH_SRC}/config-2.6.28-1-686 >
/tmp/teste2
${SED} '/CONFIG_NETFILTER_XT_MATCH_LAYER7=m/ a \# CONFIG_NETFILTER_XT_MATCH_LAYER7_DEBUG is not set' /tmp/teste2 > /tmp/.config
###########COPIANDO O ARQUIVO DE CONFIGURAÇÃO DO KERNEL#######################
${CP} /tmp/.config ${PATH_SRC}/linux/.config
###################GERANDO O NOSSO NOVO KERNEL#################################
echo "###########GERANDO O NOSSO NOVO KERNEL######################"
echo "########VAI SER NECESSÁRIO PRESSIONAR 8 VEZES ENTER##########"
sleep 8
echo
${CD} ${PATH_SRC}/linux
${MAKE} oldconfig
${MAKE_KPKG} clean
${FAKEROOT} ${MAKE_KPKG} --initrd --append-to-version=-layer7 kernel_image kernel_headers
###############INSTALANDO O NOVO KERNEL########################################
${CD} ${PATH_SRC}
${DPKG} -i linux-image-2.6.28-layer7_2.6.28-layer7-10.00.Custom_i386.deb
echo "#########################################################################"
echo " APOS O TERMINO DESTE PROCESSO SEU SERVIDOR SERA REINICIADO "
echo " SE DESEJAR ABORTAR PRESSIONE CTRL+C "
echo " ESTE PROCESSO COMEÇARÁ EM 5 SEGUNDOS "
echo "#########################################################################"
sleep 5
${CD} ${PATH_SRC}
echo "##############BAIXANDO O IPTABLES####################"
sleep 5
${APTGET} source iptables
GERA_FIREWALL ()
{
echo "################################################################"
echo "# Esta sendo gerado um arquivo de firewall "
echo "# Que sera gravado no /tmp com o nome de "
echo "# firewall.sh um exemplo Simples mas pratico "
echo "################################################################"
sleep 5
${CAT} <<EOF > /tmp/firewall.sh
#/bin/bash #
#############################################
# Autor:Douglas Q. dos Santos #
# Data:23/10/2009 #
# Scripts de firewall #
#############################################
#############################################
#Para consultar as portas dos serviços #
#consulte o arquivo /etc/services #
#############################################
#Serviços utilizados neste servidor #
#############################################
LAN=192.168.0.0/24
MODPROBE=\$(which modprobe)
IPT=\$(which iptables)
PROSYS=/proc/sys/net/ipv4
MSN="192.168.0.100"
case \$1 in
start)
echo -e "[ \033[01;32m Iniciando Firewall \033[m ]"
#####################################
#Ativa o Modulo o iptables #
#####################################
\${MODPROBE} iptable_nat
#####################################
#Ativa o Modulo para FTP #
#####################################
\${MODPROBE} ip_conntrack_ftp
#####################################
#Ativa o Modulo para ip_conntrack #
#####################################
\${MODPROBE} ip_conntrack
#####################################
#Ativa o Modulo do nf_conntrack #
#####################################
\${MODPROBE} nf_conntrack
#####################################
#Ativa o repasse de pacotes #
#####################################
echo 1 > \${PROSYS}/ip_forward
#####################################
#Desativa o suporte icmp redirects #
#####################################
echo 0 > \${PROSYS}/conf/all/accept_redirects
#####################################
#Desativa o ping broadcast #
#####################################
echo 1 > \${PROSYS}/icmp_echo_ignore_broadcasts
#####################################
#Desativa source routing #
#####################################
echo 0 > \${PROSYS}/conf/all/accept_source_route
#####################################
#Ativa Protecao contra synflood #
#####################################
echo 1 > \${PROSYS}/tcp_syncookies
##########################################
#Habilita a verificacao de rota de origem#
##########################################
for RP in \${PROSYS}/conf/*/rp_filter ; do echo 1 > \$RP ; done
#####################################
#Controle de ICMP #
#####################################
echo 0 > \${PROSYS}/icmp_echo_ignore_all
echo 1 > \${PROSYS}/icmp_echo_ignore_broadcasts
#####################################
#Limpa todas as regras #
#####################################
\$IPT -t filter -F
\$IPT -t nat -F
\$IPT -t mangle -F
\$IPT -t filter -X
\$IPT -t nat -X
\$IPT -t mangle -X
#####################################
#Define politicas default #
#####################################
\$IPT -P INPUT DROP
\$IPT -P OUTPUT ACCEPT
\$IPT -P FORWARD DROP
#####################################
#Liberar LoopBack #
#####################################
\${IPT} -A INPUT -i lo -j ACCEPT
#####################################################################
#Liberar MSN apenas para os IPs definidos em \${MSN} usando L7PROTO#
#####################################################################
#for MSN in \${MSN}
#do
#\${IPT} -A FORWARD -m layer7 --l7proto msnmessenger -s \${MSN} -j ACCEPT
#\${IPT} -A FORWARD -m layer7 --l7proto msnmessenger -d \${MSN} -j ACCEPT
#done
#####################################
# Bloquear protocolos L7PROTO #
#####################################
L7PROTO="ares bittorrent edonkey fasttrack ssh msnmessenger napster"
for PROTO in \${L7PROTO}
do
\${IPT} -A FORWARD -m layer7 --l7proto \${PROTO} -j DROP
\${IPT} -A INPUT -m layer7 --l7proto \${PROTO} -j DROP
done
#####################################
#Liberar a LAN menos porta 80 #
#vamos direcionar ela para o squid #
#####################################
\${IPT} -A INPUT -s \${LAN} -j ACCEPT
\${IPT} -A FORWARD -s \${LAN} -p tcp -m tcp --dport 80 -j DROP
\${IPT} -A FORWARD -s \${LAN} -j ACCEPT
#####################################
#Liberar retorno das conexões #
#####################################
\${IPT} -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
\${IPT} -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
####################################
#Liberar consulta DNS #
####################################
\${IPT} -A INPUT -s \${LAN} -p tcp -m tcp --dport 53 -j ACCEPT
\${IPT} -A INPUT -s \${LAN} -p udp -m udp --dport 53 -j ACCEPT
\${IPT} -A INPUT -p tcp -m tcp --sport 53 -j ACCEPT
\${IPT} -A INPUT -p udp -m udp --sport 53 -j ACCEPT
\${IPT} -A FORWARD -s \${LAN} -p tcp -m tcp --dport 53 -j ACCEPT
\${IPT} -A FORWARD -s \${LAN} -p udp -m udp --dport 53 -j ACCEPT
####################################
#Libera NTP #
####################################
\${IPT} -A INPUT -p udp -m udp --sport 123 -j ACCEPT
\${IPT} -A FORWARD -p udp -m udp --dport 123 -j ACCEPT
####################################
#Libera pings #
####################################
\${IPT} -A INPUT -p icmp --icmp-type 0 -m limit --limit 1/s -j ACCEPT
\${IPT} -A INPUT -p icmp --icmp-type 3 -m limit --limit 1/s -j ACCEPT
\${IPT} -A INPUT -p icmp --icmp-type 5 -m limit --limit 1/s -j ACCEPT
\${IPT} -A INPUT -p icmp --icmp-type 8 -m limit --limit 1/s -j ACCEPT
\${IPT} -A INPUT -p icmp --icmp-type 11 -m limit --limit 1/s -j ACCEPT
\${IPT} -A INPUT -p icmp --icmp-type 12 -m limit --limit 1/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 0 -m limit --limit 2/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 3 -m limit --limit 2/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 5 -m limit --limit 2/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 8 -m limit --limit 2/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 11 -m limit --limit 2/s -j ACCEPT
\${IPT} -A FORWARD -p icmp --icmp-type 12 -m limit --limit 2/s -j ACCEPT
[3] Comentário enviado por hugoalvarez em 29/10/2009 - 19:40h
Ficou bonito o script, acho que poderia deixar a OUTPUT padrão DROP para evitar que usuários com conexões relacionadas conseguissem OUTPUT através de algum software ou vírus que se instalou, muitos podem iniciar conexões dependendo do caso, pelo que vi foi usada OUTPUT padrão em ACCEPT
\$IPT -P INPUT DROP
\$IPT -P OUTPUT ACCEPT
\$IPT -P FORWARD DROP
perdoe minha ignorância, estou voltando agora aos estudos hehehe e o artigo foi alvo de muita pesquisa com certeza antes de ser estar aqui, é bem detalhado, mas um OUTPUT DROP acaba matando todos os msns e todas as porcarias online, o chato é ficar configurando, mesmo assim nota 10.
[4] Comentário enviado por removido em 29/10/2009 - 20:16h
Poderia tambem ter deixado o OUTPUT com a politica default DROP.
eu optei pela utilização da OUTPUT ACCEPT pelo motivo de qua qualquer conexão como OUTPUT tem que sair da maquina local e não das outras maquinas da rede.
Se uma conexão sair de outra maquina vai sair pelo firewall como FORWARD e as conexão com o FORWARD estão com politicas DROP só vai passar o que for definido pelo administrador.
[6] Comentário enviado por removido em 03/11/2009 - 07:47h
Bom dia a todos.
Percebi que a script que postei esta dando problemas para alguma pessoas por causa da configuração inicial do servidor.
Eu bolei um script para a configuração inicial do servidor que vai servir de base para o script do Layer7.
Pois muitas pessoas quando rodarem o script vão perceber que da algum erro por falta de algum pacote que já deveria estar instalado em um ambiente base para servidor.
Quem quiser este Script de configuração inicial do servidor para deixar uma boa base para a implementação do Layer7, eu passo o script por email ou pelo msn.
[8] Comentário enviado por removido em 04/11/2009 - 07:42h
E ai.
Cara no script de instalação que eu te mandei por email, eu garanto o que estará instalado no servidor que é de uso cotidiano, o telnet é muito util para teste de portas abertas e conexões.
e o smbclient eu utilizo muito por questões de precisar de arquivos que estão em servidores Windows e em Servidores Samba.
[9] Comentário enviado por silent-man em 10/11/2009 - 14:52h
Boa tarde douglashx
A intenção e ou automatização do script é uma ótima idéia(também odeio fazer a mesma coisa várias vezes).
Só gostaria de atentar para as regras de firewall
/sbin/iptables -P FORWARD DROP (é isso ai vamos negar TUDO que poderia sair/partir de nossa REDE/LAN).
Ai mas a frente você faz o seguinte:
\${IPT} -A FORWARD -s \${LAN} -p tcp -m tcp --dport 80 -j DROP ( OPA! ISSO AQUI JÁ ESTÁ BLOQUEADO na politica padrão FORWARD DROP).
\${IPT} -A FORWARD -s \${LAN} -j ACCEPT ( AQUI FICOU PERIGOSO EIN!!!??? ) se você der esse FORWARD ACCEPT, todo e qualquer tráfego poderá sair/partir da sua rede para outra(dmz, internet, etc...) inclusive a porta 25 para spammers.
O mesmo vale para a tabela INPUT (toda sua lan tem acesso ao seu FW)
\${IPT} -A INPUT -s \${LAN} -j ACCEPT
Bom isso foi apenas uma dica, me corrija ou simplesmente ignore se estiver errado.
[10] Comentário enviado por consultor.inf em 03/11/2010 - 11:10h
Muito bom seu artigo.
Instalei e aparentemente ocorreu tudo bem, fis um teste bloqueando msn e funcionou.
O problema e quando tento bloquear videos ele não bloqueia. Estou usando a seguite regra
# iptables -t mangle -A POSTROUTING -m layer7 --l7proto httpvideo -j DROP