pelo
(usa Debian)
Enviado em 19/02/2016 - 17:43h
Opa,
Vamos lá!
Teste esse script sem utiliziar nada do teu, apenas como teste.
#!/bin/bash
#############
## Modulos ##
#############
/sbin/modprobe ip_tables
/sbin/modprobe ip_conntrack
/sbin/modprobe iptable_filter
/sbin/modprobe iptable_mangle
/sbin/modprobe iptable_nat
/sbin/modprobe ipt_LOG
/sbin/modprobe ipt_limit
/sbin/modprobe ipt_state
/sbin/modprobe ipt_REDIRECT
/sbin/modprobe ipt_owner
/sbin/modprobe ipt_REJECT
/sbin/modprobe ipt_MASQUERADE
####################
### Funcao START ###
####################
firewall_start() {
echo "Iniciando o Firewall"
#####################
## Limpa as regras ##
#####################
iptables -X
iptables -Z
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 OUTPUT ACCEPT
iptables -t filter -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t mangle -P PREROUTING ACCEPT
iptables -t mangle -P OUTPUT ACCEPT
## Manter conexoes jah estabelecidas para nao parar ##
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
## Aceita todo o trafego vindo do loopback e indo pro loopback ##
iptables -t filter -A INPUT -i lo -j ACCEPT
# Ativando IP FORWARDING
#echo 1 > /proc/sys/net/ipv4/ip_forward
# IPs 192.168.0.X acessam computadores da rede 192.168.1.X
iptables -A FORWARD -s 192.168.0.0/24 -d 192.168.1.0/24 -j ACCEPT
# IPs 192.168.1.X não acessam computadores da rede 192.168.0.X
iptables -A FORWARD -s 192.168.1.0/24 -d 192.168.0.0/24 -j DROP
}
##################
### Fucao STOP ##
##################
firewall_stop() {
echo "Parando firewall e funcionando apenas com mascaramento"
# Limpa as regras #
iptables -X
iptables -Z
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 OUTPUT ACCEPT
iptables -t filter -P FORWARD ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
iptables -t mangle -P PREROUTING ACCEPT
iptables -t mangle -P OUTPUT ACCEPT
# Manter conexoes jah estabelecidas para nao parar
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
# Aceita todo o trafego vindo do loopback e indo pro loopback
iptables -t filter -A INPUT -i lo -j ACCEPT
echo "Regras Limpas e Firewall desabilitado"
}
firewall_restart() {
echo "Reiniciando Firewall"
firewall_stop
sleep 3
firewall_start
echo "Firewall Reiniciado"
}
firewall_status() {
echo ""
echo " ========================================================= REGRAS DE ACESSO ========================================================= "
echo ""
iptables -L -nv --line-numbers
echo ""
echo " ========================================================= REGRAS DO NAT ============================================================ "
echo ""
iptables -L -nv -t nat --line-numbers
echo ""
echo " ========================================================= REGRAS DA TABELA MANGLE ================================================= "
echo ""
iptables -L -nv -t mangle --line-numbers
echo ""
}
case "$1" in
'start')
firewall_start
echo "Firewall Iniciado"
;;
'stop')
firewall_stop
;;
'status')
firewall_status
;;
'restart')
firewall_restart
;;
*)
echo "Opcoes possiveis:"
echo "rc.firewall start"
echo "rc.firewall stop"
echo "rc.firewall status"
echo "rc.firewall restart"
esac