cpdrede
(usa CentOS)
Enviado em 08/11/2010 - 16:10h
Olá a todos, gostaria de pedir uma ajuda no meu conf do Firewall para minha rede. Meu firewall não esta liberando a porta do SSH e queria saber se podem me ajudar, no que eu fiz, se puderam alterar o que fiz de errado e adicionar mais coisas, por questões de segurança e tal e passar umas dicas, ficaria muito agradecido. Muito Obrigado.
#! /bin/bash
# /etc/init.d/firewall
# description: firewall
# Interface da rede EXTERNA
IF_EXTERNA="eth0"
# Interface da rede INTERNA
IF_INTERNA="eth1"
# Definicao da rede interna
REDE_INTERNA="192.168.1.0/24"
# ================== Modulos =================== #
MODPROBE=/sbin/modprobe
IPTABLES=/sbin/iptables
prog=firewall
$MODPROBE ip_conntrack_ftp
$MODPROBE ip_nat_ftp
$MODPROBE ip_tables
$MODPROBE ipt_conntrack
$MODPROBE iptable_filter
$MODPROBE iptable_mangle
$MODPROBE iptable_nat
$MODPROBE ipt_LOG
$MODPROBE ipt_limit
$MODPROBE ipt_state
$MODPROBE ipt_multiport
fw_start()
{
echo ""
echo "Iniciando Firewall..................... [ OK ]"
echo ""
#ativa o roteamento dinamico
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "1" > /proc/sys/net/ipv4/ip_dynaddr
# ================== Limpando as Regras ================ #
$IPTABLES -X
$IPTABLES -Z
$IPTABLES -F
$IPTABLES -F INPUT
$IPTABLES -F OUTPUT
$IPTABLES -F FORWARD
$IPTABLES -F -t nat
$IPTABLES -F -t mangle
# ================ POLITICAS PADRAO =================== #
$IPTABLES -t filter -P INPUT ACCEPT
$IPTABLES -t filter -P FORWARD ACCEPT
$IPTABLES -t filter -P OUTPUT ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P POSTROUTING ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
$IPTABLES -t mangle -P INPUT ACCEPT
$IPTABLES -t mangle -P FORWARD ACCEPT
# ================= CHECA CONEXAO ====================== #
$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
$IPTABLES -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# ================= Libera SSH para Servidor =========== #
$IPTABLES -A INPUT -p tcp -i $IF_INTERNA -s $REDE_INTERNA --dport 22 -j ACCEPT
# ================= Libera Squid ======================= #
$IPTABLES -A INPUT -p tcp -i $IF_INTERNA -s $REDE_INTERNA --dport 3218 -j ACCEPT
# =============== Cria chain com regras de seguranca === #
$IPTABLES -N BLOCK
$IPTABLES -A BLOCK -p icmp --icmp-type echo-request -j DROP
$IPTABLES -A BLOCK -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
$IPTABLES -A BLOCK -p tcp -m limit --limit 1/s -j ACCEPT
$IPTABLES -A BLOCK -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 1/s -j ACCEPT
$IPTABLES -A BLOCK -m unclean -j DROP
$IPTABLES -A BLOCK -m state --state ESTABLISHED,RELATED -j ACCEPT
#$IPTABLES -A BLOCK -j LOG --log-prefix "FW_ALERT: "
$IPTABLES -A BLOCK -j DROP
# =============== Muda a prioridade dos pacotes (Type Of Service) para agilizar as coisas
#$IPTABLES -t mangle -A OUTPUT -o $IF_EXTERNA -p tcp -m multiport --dports 21,22,80,6667 -j TOS --set-tos 0x10
# =================== Protecoes ============================ #
# Protege contra os "Ping of Death"
$IPTABLES -A INPUT -p icmp --icmp-type echo-request -m limit --limit 20/m -j ACCEPT
$IPTABLES -A FORWARD -p icmp --icmp-type echo-request -m limit --limit 20/m -j ACCEPT
# Protege contra port scanners avancados (Ex.: nmap)
$IPTABLES -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST RST -m limit --limit 20/m -j ACCEPT
# Bloqueando tracertroute
$IPTABLES -A INPUT -p udp -s 0/0 -i eth1 --dport 33435:33525 -j REJECT
# Protecoes contra ataques
$IPTABLES -A INPUT -m state --state INVALID -j REJECT
# ==================== Tabela Input ============================= #
# ===== Destino Externo ============ #
# Liberando porta 22
$IPTABLES -A INPUT -i $IF_EXTERNA -p tcp --dport 22 -j ACCEPT
# Liberando porta 21
$IPTABLES -A INPUT -i $IF_EXTERNA -p tcp --dport 21 -j ACCEPT
# ====== Destino Interno =========== #
# Liberando porta 22 - SSH
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 22 -j ACCEPT
# Liberando porta 2222 - SSH
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 2222 -j ACCEPT
# Liberando porta 3128 - SQUID
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 3128 -j ACCEPT
# Liberando porta 80 - HTTP
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 80 -j ACCEPT
# Liberando porta 443 - HTTPS
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 443 -j ACCEPT
# Liberando porta 21 - FTP
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 21 -j ACCEPT
# Liberando porta 25 - SMTP
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 25 -j ACCEPT
# Liberando porta 465 - SMTPS
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 465 -j ACCEPT
# Liberando porta 110 - POP
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 110 -j ACCEPT
# Liberando porta 995 - POPS
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 995 -j ACCEPT
# Liberando porta 143 - IMAP
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 143 -j ACCEPT
# Liberando porta 993 - IMAPS
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 993 -j ACCEPT
# Liberando porta 53 - DNS
$IPTABLES -A INPUT -i $IF_INTERNA -p tcp --dport 53 -j ACCEPT
# ==================== Libera todo o trafego local ============== #
$IPTABLES -t filter -A INPUT -i lo -j ACCEPT
$IPTABLES -t filter -A INPUT -i $IF_INTERNA -j ACCEPT
$IPTABLES -t filter -A FORWARD -i $IF_INTERNA -j ACCEPT
# ==================== Liberacao de Portas ======================= #
# FTP
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 21 -j ACCEPT
# SSH
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 22 -j ACCEPT
# SSH
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 2222 -j ACCEPT
# HTTP
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 80 -j ACCEPT
# HTTPS
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 443 -j ACCEPT
# SMTP
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 25 -j ACCEPT
# SMTPS
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 465 -j ACCEPT
# POP
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 110 -j ACCEPT
# POPS
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 995 -j ACCEPT
# IMAP
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 143 -j ACCEPT
# IMAPS
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 993 -j ACCEPT
# DNS
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 53 -j ACCEPT
# SQUID
$IPTABLES -A FORWARD -p tcp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 3128 -j ACCEPT
$IPTABLES -A FORWARD -p udp -i $IF_INTERNA -s $REDE_INTERNA -o $IF_EXTERNA --dport 3128 -j ACCEPT
# Libera so FTP, SSH e WEB
#$IPTABLES -t filter -A INPUT -i $IF_EXTERNA -p tcp -m multiport --dports 21,22,80,6667 -j ACCEPT
# ==================== Libera a conexao para a rede interna ===== #
$IPTABLES -t nat -A POSTROUTING -s $REDE_INTERNA -j MASQUERADE
# ==================== Redirecionando porta 80 ================== #
$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 udp --dport 80 -j REDIRECT --to-port 3128
# ===================== Cria um NAT para o SSH de uma maquina da rede interna
#$IPTABLES -t filter -A FORWARD -p tcp -d 0/0 --dport 2222 -j ACCEPT
#$IPTABLES -t nat -A PREROUTING -p tcp -d 0/0 --dport 2222 -j DNAT --to 192.168.1.1:22
# ===================== Regras para evitar packet flood
$IPTABLES -A INPUT -j BLOCK
$IPTABLES -A FORWARD -j BLOCK
# ===================== FORWARD CHAIN ============================ #
# Pacotes defeituosos
#$IPTABLES -A FORWARD -p tcp -j bad_tcp_packets
# Pacotes TCP ruins - Bad TCP packets we don't want.
#$IPTABLES -A OUTPUT -p tcp -j bad_tcp_packets
}
fw_stop()
{
echo ""
echo "Parando Firewall ..................... [ OK ]"
echo ""
$IPTABLES -t filter -P INPUT ACCEPT
$IPTABLES -t filter -P FORWARD ACCEPT
$IPTABLES -t filter -P OUTPUT ACCEPT
$IPTABLES -t nat -P PREROUTING ACCEPT
$IPTABLES -t nat -P POSTROUTING ACCEPT
$IPTABLES -t nat -P OUTPUT ACCEPT
$IPTABLES -t mangle -P PREROUTING ACCEPT
$IPTABLES -t mangle -P POSTROUTING ACCEPT
$IPTABLES -t mangle -P OUTPUT ACCEPT
$IPTABLES -t mangle -P INPUT ACCEPT
$IPTABLES -t mangle -P FORWARD ACCEPT
$IPTABLES -t filter -F
$IPTABLES -t nat -F
$IPTABLES -t mangle -F
$IPTABLES -t filter -X
$IPTABLES -t nat -X
$IPTABLES -t mangle -X
$IPTABLES -t filter -Z
$IPTABLES -t nat -Z
$IPTABLES -t mangle -Z
}
fw_usage()
{
echo
echo "$0 (start | stop | restart | clear)"
echo
echo "start - Ativa o firewall"
echo "stop - Desativa o firewall"
echo "restart - Reativa o firewall"
echo "clear - Limpa os contatores"
}
fw_clear()
{
$IPTABLES -t filter -Z
$IPTABLES -t nat -Z
$IPTABLES -t mangle -Z
}
case $1 in
start)
fw_start;
;;
stop)
fw_stop;
;;
restart)
fw_stop;
fw_start;
;;
clear)
fw_clear;
;;
*)
fw_usage;
exit;
;;
esac