Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ## Firewall iptables qui bloque tout sauf le necessaire
- ## Alexis le 28/03/2012
- ##
- # Configuration du Firewall
- ##
- # Liste des ips des VM pour débloquer SSH dessus
- # Plus le port souhaité pour chaque VM
- # Exemple : 192.168.1.2:2222
- # Redirige le port 2222 du serveur vers le 22 de la VM
- listeVMssh="
- 192.168.1.2:2222
- 192.168.1.3:2223
- 192.168.1.4:2224
- 192.168.1.5:2225
- "
- # Liste des VM suivi du port a forwarder
- # suivi du protocole a debloquer aussi
- # TCP/udp/both (dns par exemple)
- listeVMforward="
- 192.168.1.2:80:tcp
- 192.168.1.4:53:both
- "
- # L'ip du serveur qui host
- ipHost="91.121.204.136"
- echo "Lancement du Firewall"
- # Nettoyage des anciennes conf
- iptables -F
- iptables -X
- iptables -t nat -F
- iptables -t nat -X
- iptables -t mangle -F
- iptables -t mangle -X
- echo "Nettoyage : OK"
- # On bloque tout
- iptables -t filter -P INPUT DROP
- iptables -t filter -P FORWARD DROP
- iptables -t filter -P OUTPUT DROP
- echo "Blocage : OK"
- ##
- # Configuration général
- ##
- # Autoriser le loopback
- iptables -t filter -A INPUT -i lo -j ACCEPT
- iptables -t filter -A OUTPUT -o lo -j ACCEPT
- echo "Loopback : OK"
- # Ne pas casser les connexions etablies
- iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
- iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
- echo "Connexion Related/Established : ok"
- ##
- # Configuration des ports
- ##
- # Autorise le ping
- iptables -t filter -A INPUT -p icmp -j ACCEPT
- iptables -t filter -A OUTPUT -p icmp -j ACCEPT
- echo "Ping : OK"
- # Autorise SSH (IN/OUT)
- iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
- iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT
- echo "SSH : OK"
- # Autorise les DNS (TCP/UDP)
- iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
- iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
- iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
- iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT
- echo "DNS : OK"
- # Autorise le HTTP/HTTPS (web, IN/OUT)
- iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
- iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT
- iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
- iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
- # Admin de Proxmox
- iptables -t filter -A INPUT -p tcp --dport 8006 -j ACCEPT
- iptables -t filter -A OUTPUT -p tcp --dport 8006 -j ACCEPT
- echo "HTTP/HTTPS : OK"
- # Autorise le FTP
- # FTP Out
- iptables -t filter -A OUTPUT -p tcp --dport 21 -j ACCEPT
- iptables -t filter -A OUTPUT -p tcp --dport 20 -j ACCEPT
- # FTP In
- # modprobe ip_conntrack_ftp # ligne facultative avec les serveurs OVH
- iptables -t filter -A INPUT -p tcp --dport 20 -j ACCEPT
- iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT
- # Autorise le mail (Desactive)
- # Mail SMTP:25
- #iptables -t filter -A INPUT -p tcp --dport 25 -j ACCEPT
- #iptables -t filter -A OUTPUT -p tcp --dport 25 -j ACCEPT
- # Mail POP3:110
- #iptables -t filter -A INPUT -p tcp --dport 110 -j ACCEPT
- #iptables -t filter -A OUTPUT -p tcp --dport 110 -j ACCEPT
- # Mail IMAP:143
- #iptables -t filter -A INPUT -p tcp --dport 143 -j ACCEPT
- #iptables -t filter -A OUTPUT -p tcp --dport 143 -j ACCEPT
- # Mail POP3S:995
- #iptables -t filter -A INPUT -p tcp --dport 995 -j ACCEPT
- #iptables -t filter -A OUTPUT -p tcp --dport 995 -j ACCEPT
- #echo "Mail : OK"
- # Autorise OpenVNC
- iptables -t filter -A INPUT -p tcp --dport 5900 -j ACCEPT
- iptables -t filter -A OUTPUT -p tcp --dport 5900 -j ACCEPT
- echo "OpenVNC : OK"
- ##
- # Configuration Nat/Pat pour les VM
- ##
- echo "Configuration du Nat/Pat"
- ## Informations pratiques
- # eth0 : interface côté internet
- # venet0 : interface du réseau virtuel
- ##
- # Autorise le forwarding
- echo 1 > /proc/sys/net/ipv4/ip_forward
- # Autorise le Masquerading
- # Permet aux VM d'avoir accès a internet
- iptables -A POSTROUTING -t nat -o vmbr0 -s 192.168.1.0/24 -d 0/0 -j MASQUERADE
- # Autorise les connexions déja établies
- iptables -A FORWARD -t filter -o vmbr0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- iptables -A FORWARD -t filter -i vmbr0 -m state --state ESTABLISHED,RELATED -j ACCEPT
- # Déblocage de SSH sur les machines virtuelles avec une petite boucle
- # listeVMssh = liste des VM sous forme d'ip
- for vm in $listeVMssh
- do
- # Recupere l'ip de la VM
- ip=`echo $vm | cut -d ":" -f 1`
- # Recupere le port
- port=`echo $vm | cut -d ":" -f 2`
- echo "Deblocage de : $ip, associé au port $port"
- # Pat pour SSH
- # Internet vers les VM
- iptables -t nat -A PREROUTING -p tcp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:22
- # VM vers internet
- iptables -A FORWARD -p tcp -i vmbr0 -o venet0 -d $ip --dport 22 --sport 1024:65535 -m state --state NEW -j ACCEPT
- done
- unset vm
- echo "SSH sur VM : OK"
- # Forward des ports sur les VM
- for vm in $listeVMforward
- do
- # Recupere l'ip de la VM
- ip=`echo $vm | cut -d ":" -f 1`
- # Recupere le port de la VM
- port=`echo $vm | cut -d ":" -f 2`
- # Recupere le protocole a débloquer
- protocole=`echo $vm | cut -d ":" -f 3`
- # Juste TCP ou UDP seulement
- if [ "$protocole" == "tcp" ] || [ "$protocole" == "udp" ]
- then
- # IN
- iptables -t nat -A PREROUTING -p $protocole -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
- # OUT
- iptables -A FORWARD -p $protocole -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
- fi
- # TCP et UDP (cas des DNS)
- if [ "$protocole" == "both" ]
- then
- # IN
- iptables -t nat -A PREROUTING -p tcp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
- iptables -t nat -A PREROUTING -p udp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
- # OUT
- iptables -A FORWARD -p tcp -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
- iptables -A FORWARD -p udp -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
- fi
- echo "Forward du port $port sur $ip en $protocole"
- done
- # Autorise le traffic entre les VM
- iptables -A FORWARD -t filter -i venet0 -o venet0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
- echo "Port forwarding : OK"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement