Hunter的大杂烩 技术学习笔记

2012-06-08

iptables 默认安全规则脚本

Filed under: 技术话题 — hunter @ 6:05 pm

from: http://space.itpub.net/70109/viewspace-689580

默认脚本只开启常规web服务器的80,3306,22端口

#vi default_firewall.sh

  1. #!/bin/bash
  2. #########################################################################
  3. #
  4. # File: default_firewall.sh
  5. # Description:
  6. # Language: GNU Bourne-Again SHell
  7. # Version: 1.0
  8. # Date: 2010-6-23
  9. # Corp.: c1gstudio.com
  10. # Author: c1g
  11. # WWW: http://blog.c1gstudio.com
  12. ### END INIT INFO
  13. ###############################################################################
  14. IPTABLES=/sbin/iptables
  15. # start by flushing the rules
  16. $IPTABLES -P INPUT DROP
  17. $IPTABLES -P FORWARD ACCEPT
  18. $IPTABLES -P OUTPUT ACCEPT
  19. $IPTABLES -t nat -P PREROUTING ACCEPT
  20. $IPTABLES -t nat -P POSTROUTING ACCEPT
  21. $IPTABLES -t nat -P OUTPUT ACCEPT
  22. $IPTABLES -t mangle -P PREROUTING ACCEPT
  23. $IPTABLES -t mangle -P OUTPUT ACCEPT
  24. $IPTABLES -F
  25. $IPTABLES -X
  26. $IPTABLES -Z
  27. $IPTABLES -t nat -F
  28. $IPTABLES -t mangle -F
  29. $IPTABLES -t nat -X
  30. $IPTABLES -t mangle -X
  31. $IPTABLES -t nat -Z
  32. ## allow packets coming from the machine
  33. $IPTABLES -A INPUT -i lo -j ACCEPT
  34. $IPTABLES -A OUTPUT -o lo -j ACCEPT
  35. # allow outgoing traffic
  36. $IPTABLES -A OUTPUT -o eth0 -j ACCEPT
  37. # block spoofing
  38. $IPTABLES -A INPUT -s 127.0.0.0/8 -i ! lo -j DROP
  39. $IPTABLES -A INPUT -m state –state RELATED,ESTABLISHED -j ACCEPT
  40. $IPTABLES -A INPUT -p icmp -j ACCEPT
  41. # stop bad packets
  42. #$IPTABLES -A INPUT -m state –state INVALID -j DROP
  43. # NMAP FIN/URG/PSH
  44. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags ALL FIN,URG,PSH -j DROP
  45. # stop Xmas Tree type scanning
  46. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags ALL ALL -j DROP
  47. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags ALL SYN,RST,ACK,FIN,URG -j DROP
  48. # stop null scanning
  49. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags ALL NONE -j DROP
  50. # SYN/RST
  51. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags SYN,RST SYN,RST -j DROP
  52. # SYN/FIN
  53. #$IPTABLES -A INPUT -i eth0 -p tcp –tcp-flags SYN,FIN SYN,FIN -j DROP
  54. # stop sync flood
  55. #$IPTABLES -N SYNFLOOD
  56. #$IPTABLES -A SYNFLOOD -p tcp –syn -m limit –limit 1/s -j RETURN
  57. #$IPTABLES -A SYNFLOOD -p tcp -j REJECT –reject-with tcp-reset
  58. #$IPTABLES -A INPUT -p tcp -m state –state NEW -j SYNFLOOD
  59. # stop ping flood attack
  60. #$IPTABLES -N PING
  61. #$IPTABLES -A PING -p icmp –icmp-type echo-request -m limit –limit 1/second -j RETURN
  62. #$IPTABLES -A PING -p icmp -j REJECT
  63. #$IPTABLES -I INPUT -p icmp –icmp-type echo-request -m state –state NEW -j PING
  64. #################################
  65. ## What we allow
  66. #################################
  67. # tcp ports
  68. # smtp
  69. #$IPTABLES -A INPUT -p tcp -m tcp –dport 25 -j ACCEPT
  70. # http
  71. $IPTABLES -A INPUT -p tcp -m tcp –dport 80 -j ACCEPT
  72. # pop3
  73. #$IPTABLES -A INPUT -p tcp -m tcp –dport 110 -j ACCEPT
  74. # imap
  75. #$IPTABLES -A INPUT -p tcp -m tcp –dport 143 -j ACCEPT
  76. # ldap
  77. #$IPTABLES -A INPUT -p tcp -m tcp –dport 389 -j ACCEPT
  78. # https
  79. #$IPTABLES -A INPUT -p tcp -m tcp –dport 443 -j ACCEPT
  80. # smtp over SSL
  81. #$IPTABLES -A INPUT -p tcp -m tcp –dport 465 -j ACCEPT
  82. # line printer spooler
  83. #$IPTABLES -A INPUT -p tcp -m tcp –dport 515 -j ACCEPT
  84. # cups
  85. #$IPTABLES -A INPUT -p tcp -m tcp –dport 631 -j ACCEPT
  86. # mysql
  87. $IPTABLES -A INPUT -p tcp -m tcp –dport 3306 -j ACCEPT
  88. # tomcat
  89. #$IPTABLES -A INPUT -p tcp -m tcp –dport 8080 -j ACCEPT
  90. # squid
  91. #$IPTABLES -A INPUT -p tcp -m tcp –dport 81 -j ACCEPT
  92. # nrpe
  93. #$IPTABLES -A INPUT -p tcp -m tcp –dport 15666 -j ACCEPT
  94. ## restrict some tcp things ##
  95. # ssh
  96. $IPTABLES -A INPUT -p tcp -m tcp –dport 22 -j ACCEPT
  97. #$IPTABLES -A INPUT -p tcp -m tcp –dport 6022 -j ACCEPT
  98. # samba (netbios)
  99. #$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 –dport 137:139 -j ACCEPT
  100. # ntop
  101. #$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 –dport 3000 -j ACCEPT
  102. # Hylafax
  103. #$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 –dport 4558:4559 -j ACCEPT
  104. # webmin
  105. #$IPTABLES -A INPUT -p tcp -m tcp -s 192.168.0.0/16 –dport 10000 -j ACCEPT
  106. # udp ports
  107. # DNS
  108. #$IPTABLES -A INPUT -p udp -m udp –dport 53 -j ACCEPT
  109. # DHCP
  110. #$IPTABLES -A INPUT -p udp -m udp –dport 67:68 -j ACCEPT
  111. # NTP
  112. #$IPTABLES -A INPUT -p udp -m udp –dport 123 -j ACCEPT
  113. # SNMP
  114. #$IPTABLES -A INPUT -p udp -m udp –dport 161:162 -j ACCEPT
  115. ## restrict some udp things ##
  116. # Samba (Netbios)
  117. #$IPTABLES -A INPUT -p udp -m udp -s 192.168.0.0/16 –dport 137:139 -j ACCEPT
  118. #$IPTABLES -A INPUT -p udp -m udp –sport 137:138 -j ACCEPT
  119. # finally – drop the rest
  120. #$IPTABLES -A INPUT -p tcp –syn -j DROP

设置权限

  1. chmod u+x ./default_firewall.sh

运行脚本

  1. ./default_firewall.sh

查看iptables

  1. #/sbin/iptables -nL

保存iptables

  1. #/sbin/iptables-save > /etc/sysconfig/iptables

重启iptables

  1. #/etc/init.d/iptables restart

脚本下载:

No Comments

No comments yet.

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

Powered by WordPress