Shell script iptables based firewall for virtuozzo VPS for REDHAT Linux

by on April 10, 2008 · 4 comments

  1. #!/bin/bash
  2. # Firewall for Red hat enterprise linux Virtuozzo VPS
  3. # It is simple firewall but effective one on Red hat enterprise linux Virtuozzo VPS :)
  4. # ---------------------------------------------------------
  5. # 1) DO NOT FORGEDT TO SETUP CORRECT IPS first
  6. # 2) touch /root/allbadips.txt; echo "192.1678.0.10"> /root/allbadips.txt
  7. # 3) To load/start firewall from this script
  8. # chmod +x virtuozzo-iptables-firewall-script.bash
  9. # ./virtuozzo-iptables-firewall-script.bash
  10. # -----------------------------------------------------
  11. # Laste updated : Aug - 08 - 2005
  12. # -----------------------------------------------------
  13. # Copyright (C) 2004,2005 nixCraft <http://cyberciti.biz/fb/>
  14. # This script is licensed under GNU GPL version 2.0 or above
  15. # For more info, please visit:
  16. # http://www.cyberciti.biz/nixcraft/vivek/blogger/2004/12/virtuozzo-iptables-firewall.html
  17. #-----------------------------------------------------
  18. # ip = can be setup once - Aug-2005.
  19. # -------------------------------------------------------------------------
  20. # This script is part of nixCraft shell script collection (NSSC)
  21. # Visit http://bash.cyberciti.biz/ for more information.
  22. # -------------------------------------------------------------------------
  23.  
  24. # BAD IPS FILE all ip in this file are droped
  25. BADIPS="$(cat /root/allbadips.txt|grep -v -E "^#")"
  26. # setup your IPS here
  27. myIPS="xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx xxx.xxx.xxx.xxx"
  28.  
  29. # Setup VPS main IP here
  30. ip="xxx.xxx.xxx.xxx"
  31.  
  32. # stop RedHAT linux iptables
  33. service iptables stop
  34.  
  35. # Setting default filter policy DROP ALL :D
  36. iptables -P INPUT DROP
  37. iptables -P OUTPUT DROP
  38. iptables -P FORWARD DROP
  39.  
  40. # allow unlinited traffic on both lo and venet0
  41. iptables -A INPUT -i venet0 -s 127.0.0.1 -j ACCEPT
  42. iptables -A OUTPUT -o venet0 -d 127.0.0.1 -j ACCEPT
  43.  
  44. iptables -A INPUT -i lo -s 127.0.0.1 -j ACCEPT
  45. iptables -A OUTPUT -o lo -d 127.0.0.1 -j ACCEPT
  46.  
  47. # Block all those IPs
  48. for ip in $BADIPS
  49. do
  50. iptables -A INPUT -s $ip -j DROP
  51. iptables -A OUTPUT -d $ip -j DROP
  52. done
  53. # Stop flood
  54. iptables -N flood
  55. iptables -A INPUT -p tcp --syn -j flood
  56. iptables -A flood -m limit --limit 1/s --limit-burst 3 -j RETURN
  57. iptables -A flood -j DROP
  58. # Spoofing and bad addresses
  59. # Bad incoming source ip address i.e server IP drop all here
  60. for myip in $myIPS
  61. do
  62. iptables -A INPUT -s $myip -j DROP
  63. done
  64.  
  65. # Drop all incoming fragments
  66. iptables -A INPUT -f -j DROP
  67.  
  68. # Drop all incoming malformed XMAS packets
  69. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  70.  
  71. # Drop all incoming malformed NULL packets
  72. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  73.  
  74. # Bad incoming source ip address 0.0.0.0/8
  75. iptables -A INPUT -s 0.0.0.0/8 -j DROP
  76.  
  77. # Bad incoming source ip address 127.0.0.0/8
  78. iptables -A INPUT -s 127.0.0.0/8 -j DROP
  79.  
  80. # Bad incoming source ip address 10.0.0.0/8
  81. iptables -A INPUT -s 10.0.0.0/8 -j DROP
  82.  
  83. # Bad incoming source ip address 172.16.0.0/12
  84. iptables -A INPUT -s 172.16.0.0/12 -j DROP
  85.  
  86. # Bad incoming source ip address 192.168.0.0/16
  87. iptables -A INPUT -s 192.168.0.0/16 -j DROP
  88.  
  89. # Bad incoming source ip address 224.0.0.0/3
  90. iptables -A INPUT -s 224.0.0.0/3 -j DROP
  91.  
  92. #Open Port 80 , no statful fw as VPS don't support it :(
  93. #ip="xxx.xxx.xxx.xxx" # IP of your www service
  94. iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $ip --dport 80 -j ACCEPT
  95. iptables -A OUTPUT -p tcp -s $ip --sport 80 -d 0/0 --dport 1024:65535 -j ACCEPT
  96.  
  97. #Open Port 443
  98. #ip="xxx.xxx.xxx.xxx" # IP of your wwws service
  99. iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $ip --dport 443 -j ACCEPT
  100. iptables -A OUTPUT -p tcp -s $ip --sport 443 -d 0/0 --dport 1024:65535 -j ACCEPT
  101.  
  102. #Open Port 25
  103. #ip="xxx.xxx.xxx.xxx"
  104. iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $ip --dport 25 -j ACCEPT
  105. iptables -A OUTPUT -p tcp -s $ip --sport 25 -d 0/0 --dport 1024:65535 -j ACCEPT
  106.  
  107. #Open port 22 for all
  108. #ip="xxx.xxx.xxx.xxx"
  109. iptables -A INPUT -p tcp -s 0/0 --sport 513:65535 -d $ip --dport 22 -j ACCEPT
  110. iptables -A OUTPUT -p tcp -s $ip --sport 22 -d 0/0 --dport 513:65535 -j ACCEPT
  111.  
  112. # Outgoing DNS
  113. # udp first
  114. NSIP="ns1_IP ns2_IP" # NS1 NS2 of ISP
  115. #ip="your_main_IP"
  116. for mip in $NSIP
  117. do
  118. iptables -A OUTPUT -p udp -s $ip --sport 1024:65535 -d $mip --dport 53 -j ACCEPT
  119. iptables -A INPUT -p udp -s $mip --sport 53 -d $ip --dport 1024:65535 -j ACCEPT
  120. # tcp next
  121. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d $mip --dport 53 -j ACCEPT
  122. iptables -A INPUT -p tcp -s $mip --sport 53 -d $ip --dport 1024:65535 -j ACCEPT
  123. done
  124.  
  125. #outgoin ICMP
  126. #ip="your_main_IP"
  127. iptables -A OUTPUT -p icmp -s $ip -d 0/0 -j ACCEPT
  128. iptables -A INPUT -p icmp -s 0/0 -d $ip -j ACCEPT
  129.  
  130. #outgoing traceroute
  131. #ip="your_main_IP"
  132. iptables -A OUTPUT -p udp -s $ip --sport 1024:65535 -d 0/0 --dport 33434:33523 -j ACCEPT
  133.  
  134. #outgoing SMTP
  135. #ip="your_main_IP"
  136. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d 0/0 --dport 25 -j ACCEPT
  137. iptables -A INPUT -p tcp -s 0/0 --sport 25 -d $ip --dport 1024:65535 -j ACCEPT
  138.  
  139. #outgoing FTP
  140. #ip="your_main_IP"
  141. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d 0/0 --dport 21 -j ACCEPT
  142. iptables -A INPUT -p tcp -s 0/0 --sport 21 -d $ip --dport 1024:65535 -j ACCEPT
  143. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d 0/0 --dport 1024:65535 -j ACCEPT
  144. iptables -A INPUT -p tcp -s 0/0 --sport 1024:65535 -d $ip --dport 1024:65535 -j ACCEPT
  145.  
  146. #outgoin SSH
  147. #ip="your_main_IP"
  148. iptables -A OUTPUT -p tcp -s $ip --sport 513:65535 -d 0/0 --dport 22 -j ACCEPT
  149. iptables -A INPUT -p tcp -s 0/0 --sport 22 -d $ip --dport 513:65535 -j ACCEPT
  150.  
  151. #outgoin http and https
  152. # for up2date and other stuff
  153. #ip="your_main_IP"
  154. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d 0/0 --dport 80 -j ACCEPT
  155. iptables -A INPUT -p tcp -s 0/0 --sport 80 -d $ip --dport 1024:65535 -j ACCEPT
  156. iptables -A OUTPUT -p tcp -s $ip --sport 1024:65535 -d 0/0 --dport 443 -j ACCEPT
  157. iptables -A INPUT -p tcp -s 0/0 --sport 443 -d $ip --dport 1024:65535 -j ACCEPT
  158. # Okay Drop everything from here :D
  159. iptables -A INPUT -s 0/0 -j DROP
  160. iptables -A OUTPUT -d 0/0 -j DROP
  161. # EOF SFW


4000+ howtos and counting! If you enjoyed this article, join 45000+ others and get free email updates!

Click here to subscribe via email.

  • Boris

    Nice, I’d just suggest one thing:

    iptables -A flood -m limit –limit 1/s –limit-burst 3 -j RETURN

    If the IP address of the source of attack is spoofed, this rule will return the packets to the spoofed IP, so technically it will hit the innocent victim back – saw this happen, those type of attacks are often performed, so it would be better to DROP those packets.

  • Internetagentur

    Thank you, nice script. This script has me very helped.

  • dann

    Can you please make a decent shell script where you dont get logged out, drop and deny access to ONLY the bad IPs? How hard is that because this script what it does is logout me – the serveradmin :(

  • peace

    # iptables -A flood -m limit -limit 1/s -limit-burst 3 -j RETURN
    iptables v1.4.3.1: option `limit’ requires an argument
    Try `iptables -h’ or ‘iptables –help’ for more information.
    hi after doing this without the script
    i get the error can you help Boris .
    i’m new to iptable thanks

Previous Script:

Next Script: