StartIptables.sh 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/sh
  2. # No spoofing
  3. if [ -e /proc/sys/net/ipv4/conf/all/rp_filter ]
  4. then
  5. for filtre in /proc/sys/net/ipv4/conf/*/rp_filter
  6. do
  7. echo 1 > $filtre
  8. done
  9. fi
  10. # No icmp
  11. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all
  12. echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
  13. #load some modules you may need
  14. modprobe ip_tables
  15. modprobe ip_nat_ftp
  16. modprobe ip_nat_irc
  17. modprobe iptable_filter
  18. modprobe iptable_nat
  19. modprobe ip_conntrack_irc
  20. modprobe ip_conntrack_ftp
  21. # Remove all rules and chains
  22. iptables -F
  23. iptables -X
  24. # first set the default behaviour => accept
  25. # connections
  26. iptables -P INPUT ACCEPT
  27. iptables -P OUTPUT ACCEPT
  28. iptables -P FORWARD ACCEPT
  29. # Create 2 chains, it allows to write a clean script
  30. iptables -N FIREWALL
  31. iptables -N TRUSTED
  32. # Allow ESTABLISHED and RELATED incoming connection
  33. iptables -A FIREWALL -i eth0 -m state --state ESTABLISHED,RELATED -j ACCEPT
  34. #SSH
  35. iptables -A FIREWALL -i eth0 -p tcp -m tcp --dport 9922 -m state --state NEW -j ACCEPT
  36. iptables -A FIREWALL -i eth0 -p tcp -m tcp --sport 9922 -m state --state NEW -j ACCEPT
  37. iptables -A FIREWALL -i eth0 -p udp -m udp --dport 9922 -m state --state NEW -j ACCEPT
  38. iptables -A FIREWALL -i eth0 -p udp -m udp --sport 9922 -m state --state NEW -j ACCEPT
  39. #WEB
  40. iptables -A FIREWALL -i eth0 -p tcp -m tcp --dport 8080 -m state --state NEW -j ACCEPT
  41. iptables -A FIREWALL -i eth0 -p tcp -m tcp --sport 8080 -m state --state NEW -j ACCEPT
  42. iptables -A FIREWALL -i eth0 -p udp -m udp --dport 8080 -m state --state NEW -j ACCEPT
  43. iptables -A FIREWALL -i eth0 -p udp -m udp --sport 8080 -m state --state NEW -j ACCEPT
  44. # Allow loopback traffic
  45. iptables -A FIREWALL -i lo -j ACCEPT
  46. # Send all package to the TRUSTED chain
  47. iptables -A FIREWALL -j TRUSTED
  48. # DROP all other packets
  49. iptables -A FIREWALL -j DROP
  50. # DROP INCOMING MALFORMED XMAS PACKETS
  51. iptables -A INPUT -p tcp --tcp-flags ALL ALL -j DROP
  52. # DROP INVALID
  53. iptables -A INPUT -m state --state INVALID -j DROP
  54. # DROP INCOMING MALFORMED NULL PACKETS
  55. iptables -A INPUT -p tcp --tcp-flags ALL NONE -j DROP
  56. # Send all INPUT packets to the FIREWALL chain
  57. iptables -A INPUT -j FIREWALL
  58. # DROP all forward packets, we don't share internet
  59. # connection in this example
  60. iptables -A FORWARD -j DROP
  61. # Allow loopback interface traffic
  62. iptables -A TRUSTED -i lo -j ACCEPT
  63. iptables -A TRUSTED -o lo -j ACCEPT
  64. # Allow amule
  65. #iptables -A TRUSTED -i eth0 -p udp -m udp --dport 5349 -j ACCEPT
  66. #iptables -A TRUSTED -i eth0 -p udp -m udp --dport 5351 -j ACCEPT
  67. #iptables -A TRUSTED -i eth0 -p tcp -m tcp --dport 5348 -j ACCEPT
  68. # Allow bittorrent
  69. #iptables -A TRUSTED -i eth0 -p tcp -m tcp --dport 6881:6889 -j DROP
  70. iptables -A TRUSTED -p tcp -m tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
  71. #FTP
  72. iptables -A TRUSTED -p tcp -m tcp --dport 21 -m state --state NEW,ESTABLISHED -j ACCEPT
  73. iptables -A TRUSTED -p tcp -m tcp --sport 20 -m state --state ESTABLISHED,RELATED -j ACCEPT
  74. #SSH
  75. iptables -A TRUSTED -p tcp -m tcp --dport 22 -m state --state NEW,ESTABLISHED -j DROP
  76. #DNS
  77. iptables -A TRUSTED -p tcp -m tcp --dport 53 -m state --state NEW,ESTABLISHED -j DROP
  78. iptables -A TRUSTED -p udp -m udp --dport 53 -m state --state NEW,ESTABLISHED -j DROP
  79. #HTTP
  80. iptables -A TRUSTED -p tcp -m tcp --dport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
  81. iptables -A TRUSTED -p tcp -m tcp --sport 80 -m state --state NEW,ESTABLISHED -j ACCEPT
  82. #HTTPS
  83. iptables -A TRUSTED -p tcp -m tcp --dport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
  84. iptables -A TRUSTED -p tcp -m tcp --sport 443 -m state --state NEW,ESTABLISHED -j ACCEPT
  85. iptables -A TRUSTED -p tcp -m tcp --dport 3306 -m state --state NEW,ESTABLISHED -j ACCEPT
  86. iptables -A TRUSTED -p tcp -m tcp --dport 110 -m state --state NEW,ESTABLISHED -j DROP
  87. iptables -A TRUSTED -p tcp -m tcp --dport 465 -m state --state NEW,ESTABLISHED -j DROP
  88. iptables -A TRUSTED -p tcp -m tcp --dport 485 -m state --state NEW,ESTABLISHED -j DROP
  89. iptables -A TRUSTED -p tcp -m tcp --dport 587 -m state --state NEW,ESTABLISHED -j DROP
  90. iptables -A TRUSTED -p tcp -m tcp --dport 995 -m state --state NEW,ESTABLISHED -j DROP
  91. iptables -A TRUSTED -p tcp -m tcp --dport 8443 -m state --state NEW,ESTABLISHED -j DROP
  92. iptables -A TRUSTED -p tcp -m tcp --dport 8447 -m state --state NEW,ESTABLISHED -j DROP
  93. #SSH
  94. iptables -A TRUSTED -p tcp -m tcp --dport 9922 -m state --state NEW,ESTABLISHED -j ACCEPT
  95. iptables -A TRUSTED -p tcp -m tcp --sport 9922 -m state --state NEW,ESTABLISHED -j ACCEPT
  96. iptables -A TRUSTED -p udp -m udp --dport 9922 -m state --state NEW,ESTABLISHED -j ACCEPT
  97. iptables -A TRUSTED -p udp -m udp --sport 9922 -m state --state NEW,ESTABLISHED -j ACCEPT
  98. #WEB
  99. iptables -A TRUSTED -p tcp -m tcp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  100. iptables -A TRUSTED -p tcp -m tcp --sport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  101. iptables -A TRUSTED -p udp -m udp --dport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  102. iptables -A TRUSTED -p udp -m udp --sport 8080 -m state --state NEW,ESTABLISHED -j ACCEPT
  103. # End message
  104. echo " [End iptables rules setting]"