Created
April 9, 2013 23:48
-
-
Save alanyoshida/5350471 to your computer and use it in GitHub Desktop.
Script bash para bloqueio por dominio e range de ip via iptables.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
WORKDIR="/root/firewall" | |
############################################################ | |
# Seta o firewall com iptables para bloquear tudo # | |
# que nao esteja no range de ips ou dominios permitidos. # | |
############################################################ | |
# Arquivo com os ranges de ip permitidos | |
ARQUIVO="accept_ip_range.txt" | |
# Seu ip abaixo para liberar | |
IP='' | |
# loopback | |
LO='127.0.0.1' | |
# Arquivo com os dominios permitidos. | |
DOMINIOS='accept_domains.txt' | |
# APAGA TABELA DO IPTABLES | |
iptables -F | |
echo "Tabela do Iptables apagada." | |
# Seta a CHAIN OUTPUT como padrao ACCEPT, ou seja, aceita tudo. | |
iptables -P OUTPUT ACCEPT | |
# Seta a CHAIN INPUT como padrao DROP, ou seja, bloqueia tudo | |
iptables -P INPUT DROP | |
echo "Tabela INPUT Dropa Tudo por padrao" | |
#LOG | |
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "PORT 25 DROP: " --log-level 7 | |
# Permitir trafego LoopBack | |
iptables -A INPUT -i lo -j ACCEPT | |
echo "Permite LoopBack" | |
# Permitir as conexoes ja estabelecidas | |
iptables -t filter -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT | |
echo "Permite Conexoes ja estabelecidas" | |
# httpd CHAIN INPUT Libera tudo | |
iptables -A INPUT -p tcp --dport 80 -j ACCEPT | |
#Aceita tudo de localhost | |
iptables -A INPUT -s $LO -j ACCEPT | |
# SMTP CHAIN INPUT Libera os enderecos locais | |
iptables -A INPUT -s $LO -p tcp --dport 25 --sport 1024: -j ACCEPT | |
iptables -A INPUT -s $IP -p tcp --dport 25 -j ACCEPT | |
# Libera tudo dos dominios do arquivo dominios.txt (Formato de um dominio por linha) | |
if [ -f $DOMINIOS]; then | |
for DOMINIO in $(cat $DOMINIOS); | |
do | |
#POP3 CHAIN INPUT | |
iptables -A INPUT -s $DOMINIO -p tcp --dport 110 -j ACCEPT | |
#SSH CHAIN INPUT | |
iptables -A INPUT -s $DOMINIO -p tcp --dport 22 -j ACCEPT | |
#FTP CHAIN INPUT | |
iptables -A INPUT -s $DOMINIO -p tcp --dport 21 -j ACCEPT | |
#MYSQL CHAIN INPUT | |
iptables -A INPUT -s $DOMINIO -p tcp --dport 3306 -j ACCEPT | |
# Libera tudo do dominio. Se quiser permitir somente as conexoes acima comente a linha abaixo. | |
iptables -A INPUT -s $DOMINIO -j ACCEPT | |
done | |
fi | |
# Se o arquivo com os ranges existir usar ele para liberar os ips com os ranges do arquivo. | |
if [ -f $ARQUIVO ]; then | |
for IPRANGE in $(cat $ARQUIVO) | |
do | |
#POP3 CHAIN INPUT | |
iptables -A INPUT -p tcp --dport 110 --src-range $IPRANGE -j ACCEPT | |
#SSH CHAIN INPUT | |
iptables -A INPUT -p tcp --dport 22 --src-range $IPRANGE -j ACCEPT | |
#FTP CHAIN INPUT | |
iptables -A INPUT -p tcp --dport 21 --src-range $IPRANGE -j ACCEPT | |
#MYSQL CHAIN INPUT | |
iptables -A INPUT -p tcp --dport 3306 --src-range $IPRANGE -j ACCEPT | |
# Libera o ftp | |
iptables -A INPUT -p tcp --dport 25 -m iprange --src-range $IPRANGE -j ACCEPT | |
# Libera o smtp | |
iptables -A INPUT -p tcp --dport 587 -m iprange --src-range $IPRANGE -j ACCEPT | |
# Libera o pop3 | |
iptables -A INPUT -p tcp --dport 110 -m iprange --src-range $IPRANGE -j ACCEPT | |
echo "Range de ip $IPRANGE Adicionado como Accept na CHAIN INPUT" | |
done | |
fi | |
echo "******* FIM SCRIPT *******" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment