Advertisement
bvek1

Proxmox

Aug 4th, 2012
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.19 KB | None | 0 0
  1. #!/bin/sh
  2. ## Firewall iptables qui bloque tout sauf le necessaire
  3. ## Alexis le 28/03/2012
  4.  
  5. ##
  6. # Configuration du Firewall
  7. ##
  8.  
  9. # Liste des ips des VM pour débloquer SSH dessus
  10. # Plus le port souhaité pour chaque VM
  11. # Exemple : 192.168.1.2:2222
  12. # Redirige le port 2222 du serveur vers le 22 de la VM
  13. listeVMssh="
  14. 192.168.1.2:2222
  15. 192.168.1.3:2223
  16. 192.168.1.4:2224
  17. 192.168.1.5:2225
  18. "
  19.  
  20. # Liste des VM suivi du port a forwarder
  21. # suivi du protocole a debloquer aussi
  22. # TCP/udp/both (dns par exemple)
  23. listeVMforward="
  24. 192.168.1.2:80:tcp
  25. 192.168.1.4:53:both
  26. "
  27.  
  28. # L'ip du serveur qui host
  29. ipHost="91.121.204.136"
  30.  
  31. echo "Lancement du Firewall"
  32.  
  33. # Nettoyage des anciennes conf
  34. iptables -F
  35. iptables -X
  36. iptables -t nat -F
  37. iptables -t nat -X
  38. iptables -t mangle -F
  39. iptables -t mangle -X
  40. echo "Nettoyage : OK"
  41.  
  42. # On bloque tout
  43. iptables -t filter -P INPUT DROP
  44. iptables -t filter -P FORWARD DROP
  45. iptables -t filter -P OUTPUT DROP
  46. echo "Blocage : OK"
  47.  
  48. ##
  49. # Configuration général
  50. ##
  51.  
  52. # Autoriser le loopback
  53. iptables -t filter -A INPUT -i lo -j ACCEPT
  54. iptables -t filter -A OUTPUT -o lo -j ACCEPT
  55. echo "Loopback : OK"
  56.  
  57. # Ne pas casser les connexions etablies
  58. iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  59. iptables -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  60. echo "Connexion Related/Established : ok"
  61.  
  62. ##
  63. # Configuration des ports
  64. ##
  65.  
  66. # Autorise le ping
  67. iptables -t filter -A INPUT -p icmp -j ACCEPT
  68. iptables -t filter -A OUTPUT -p icmp -j ACCEPT
  69. echo "Ping : OK"
  70.  
  71. # Autorise SSH (IN/OUT)
  72. iptables -t filter -A INPUT -p tcp --dport 22 -j ACCEPT
  73. iptables -t filter -A OUTPUT -p tcp --dport 22 -j ACCEPT
  74. echo "SSH : OK"
  75.  
  76. # Autorise les DNS (TCP/UDP)
  77. iptables -t filter -A OUTPUT -p tcp --dport 53 -j ACCEPT
  78. iptables -t filter -A OUTPUT -p udp --dport 53 -j ACCEPT
  79. iptables -t filter -A INPUT -p tcp --dport 53 -j ACCEPT
  80. iptables -t filter -A INPUT -p udp --dport 53 -j ACCEPT
  81. echo "DNS : OK"
  82.  
  83. # Autorise le HTTP/HTTPS (web, IN/OUT)
  84. iptables -t filter -A OUTPUT -p tcp --dport 80 -j ACCEPT
  85. iptables -t filter -A OUTPUT -p tcp --dport 443 -j ACCEPT
  86. iptables -t filter -A INPUT -p tcp --dport 80 -j ACCEPT
  87. iptables -t filter -A INPUT -p tcp --dport 443 -j ACCEPT
  88. # Admin de Proxmox
  89. iptables -t filter -A INPUT -p tcp --dport 8006 -j ACCEPT
  90. iptables -t filter -A OUTPUT -p tcp --dport 8006 -j ACCEPT
  91. echo "HTTP/HTTPS : OK"
  92.  
  93. # Autorise le FTP
  94. # FTP Out
  95. iptables -t filter -A OUTPUT -p tcp --dport 21 -j ACCEPT
  96. iptables -t filter -A OUTPUT -p tcp --dport 20 -j ACCEPT  
  97.  
  98. # FTP In
  99. # modprobe ip_conntrack_ftp # ligne facultative avec les serveurs OVH
  100. iptables -t filter -A INPUT -p tcp --dport 20 -j ACCEPT
  101. iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT  
  102.  
  103. # Autorise le mail (Desactive)
  104. # Mail SMTP:25
  105. #iptables -t filter -A INPUT -p tcp --dport 25 -j ACCEPT
  106. #iptables -t filter -A OUTPUT -p tcp --dport 25 -j ACCEPT
  107. # Mail POP3:110
  108. #iptables -t filter -A INPUT -p tcp --dport 110 -j ACCEPT
  109. #iptables -t filter -A OUTPUT -p tcp --dport 110 -j ACCEPT
  110. # Mail IMAP:143
  111. #iptables -t filter -A INPUT -p tcp --dport 143 -j ACCEPT
  112. #iptables -t filter -A OUTPUT -p tcp --dport 143 -j ACCEPT
  113. # Mail POP3S:995
  114. #iptables -t filter -A INPUT -p tcp --dport 995 -j ACCEPT
  115. #iptables -t filter -A OUTPUT -p tcp --dport 995 -j ACCEPT
  116. #echo "Mail : OK"
  117.  
  118. # Autorise OpenVNC
  119. iptables -t filter -A INPUT -p tcp --dport 5900 -j ACCEPT
  120. iptables -t filter -A OUTPUT -p tcp --dport 5900 -j ACCEPT
  121. echo "OpenVNC : OK"
  122.  
  123. ##
  124. # Configuration Nat/Pat pour les VM
  125. ##
  126.  
  127. echo "Configuration du Nat/Pat"
  128.  
  129. ## Informations pratiques
  130. # eth0 : interface côté internet
  131. # venet0 : interface du réseau virtuel
  132. ##
  133.  
  134. # Autorise le forwarding
  135. echo 1 > /proc/sys/net/ipv4/ip_forward
  136.  
  137. # Autorise le Masquerading
  138. # Permet aux VM d'avoir accès a internet
  139. iptables -A POSTROUTING -t nat -o vmbr0 -s 192.168.1.0/24 -d 0/0 -j MASQUERADE
  140.  
  141. # Autorise les connexions déja établies
  142. iptables -A FORWARD -t filter -o vmbr0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  143. iptables -A FORWARD -t filter -i vmbr0 -m state --state ESTABLISHED,RELATED -j ACCEPT
  144.  
  145. # Déblocage de SSH sur les machines virtuelles avec une petite boucle
  146. # listeVMssh = liste des VM sous forme d'ip
  147.  
  148. for vm in $listeVMssh
  149. do
  150.     # Recupere l'ip de la VM
  151.     ip=`echo $vm | cut -d ":" -f 1`
  152.  
  153.     # Recupere le port
  154.     port=`echo $vm | cut -d ":" -f 2`
  155.  
  156.     echo "Deblocage de : $ip, associé au port $port"  
  157.  
  158.     # Pat pour SSH
  159.     # Internet vers les VM
  160.     iptables -t nat -A PREROUTING -p tcp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:22
  161.     # VM vers internet
  162.     iptables -A FORWARD -p tcp -i vmbr0 -o venet0 -d $ip --dport 22 --sport 1024:65535 -m state --state NEW -j ACCEPT
  163.  
  164. done
  165. unset vm
  166. echo "SSH sur VM : OK"
  167.  
  168. # Forward des ports sur les VM
  169. for vm in $listeVMforward
  170. do
  171.     # Recupere l'ip de la VM
  172.     ip=`echo $vm | cut -d ":" -f 1`
  173.     # Recupere le port de la VM
  174.     port=`echo $vm | cut -d ":" -f 2`
  175.     # Recupere le protocole a débloquer
  176.     protocole=`echo $vm | cut -d ":" -f 3`
  177.  
  178.     # Juste TCP ou UDP seulement
  179.     if [ "$protocole" == "tcp" ] || [ "$protocole" == "udp" ]
  180.     then
  181.         # IN
  182.         iptables -t nat -A PREROUTING -p $protocole -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
  183.         # OUT
  184.         iptables -A FORWARD -p $protocole -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
  185.     fi
  186.  
  187.     # TCP et UDP (cas des DNS)
  188.     if [ "$protocole" == "both" ]
  189.     then
  190.         # IN
  191.         iptables -t nat -A PREROUTING -p tcp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
  192.         iptables -t nat -A PREROUTING -p udp -i vmbr0 -d $ipHost --dport $port --sport 1024:65535 -j DNAT --to $ip:$port
  193.         # OUT
  194.         iptables -A FORWARD -p tcp -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
  195.         iptables -A FORWARD -p udp -i vmbr0 -o venet0 -d $ip --dport $port --sport 1024:65535 -m state --state NEW -j ACCEPT
  196.     fi
  197.  
  198.     echo "Forward du port $port sur $ip en $protocole"
  199. done
  200.  
  201. # Autorise le traffic entre les VM
  202. iptables -A FORWARD -t filter -i venet0 -o venet0 -m state --state NEW,ESTABLISHED,RELATED -j ACCEPT
  203.  
  204. echo "Port forwarding : OK"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement