防簡單***iptables策略

  1. #!/bin/sh 
  2.  
  3. IPTABLES=/sbin/iptables 
  4.  
  5.  
  6.  
  7. # clear 
  8.  
  9. $IPTABLES -F 
  10.  
  11.  
  12.  
  13. # if pkg type is allow, then accept 
  14.  
  15. #$IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 
  16.  
  17.  
  18.  
  19. # 如果同時在80端口的連接數大於10,就Drop掉這個ip 
  20.  
  21. netstat -an | grep :80 | awk -F: '{ print $8 }' | sort | uniq -c | awk -F\   '$1>10 && $2!="" { print $2 }' >> /etc/fw.list 
  22.  
  23. less /etc/fw.list | sort | uniq -c | awk -F\   '$2!="" { print $2 }' > /etc/fw.list2 
  24.  
  25. less /etc/fw.list2 > /etc/fw.list 
  26.  
  27. while read line 
  28.  
  29.        do 
  30.  
  31.        t=`echo "$line"` 
  32.  
  33.        $IPTABLES -A INPUT -p tcp -s $t -j DROP 
  34.  
  35. done < /etc/fw.list2 
  36.  
  37.  
  38.  
  39. # IP轉發 
  40.  
  41. $IPTABLES -A INPUT -p tcp --dport 20002 -j ACCEPT 
  42.  
  43. $IPTABLES -A INPUT -d 172.16.204.7 -p tcp -m tcp --dport 20002 -i eth0 -j ACCEPT 
  44.  
  45. $IPTABLES -t nat -A PREROUTING -d 211.100.39.44 -p tcp -m tcp --dport 20002 -j DNAT --to-destination 172.16.204.7:20002 
  46.  
  47. $IPTABLES -t nat -A POSTROUTING -d 172.16.204.7 -p tcp -m tcp --dport 20002 -j SNAT --to-source 10.6.39.44 
  48.  
  49.  
  50.  
  51. # if pkg visit 80,7710 port then accept 
  52.  
  53. $IPTABLES -A INPUT -p tcp --dport 80 -j ACCEPT 
  54.  
  55. $IPTABLES -A INPUT -p tcp --dport 8080 -j ACCEPT 
  56.  
  57. $IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT 
  58.  
  59. $IPTABLES -A INPUT -p tcp --dport 873 -j ACCEPT 
  60.  
  61. # $IPTABLES -A INPUT -i eth0 -m limit --limit 1/sec --limit-burst 5 -j ACCEPT 
  62.  
  63. $IPTABLES -A INPUT -p tcp --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 30/m --limit-burst 2 -j ACCEPT 
  64.  
  65. $IPTABLES -A FORWARD -p tcp --syn -m limit --limit 10/s -j ACCEPT 
  66.  
  67. $IPTABLES -A FORWARD -f -m limit --limit 100/s --limit-burst 100 -j ACCEPT 
  68.  
  69.  
  70.  
  71. # if pkg from allow ip then accept 
  72.  
  73. $IPTABLES -A INPUT -p tcp -s 127.0.0.1   -j ACCEPT 
  74.  
  75.  
  76.  
  77. # if pkg not above then deny 
  78.  
  79. $IPTABLES -A INPUT -p tcp --syn -j DROP 
  80.  
  81. 下面這個防火牆測試結果更正確,能起到一定的防***的功能 
  82.  
  83.  
  84.  
  85. #!/bin/sh 
  86.  
  87. IPTABLES="/sbin/iptables" 
  88.  
  89. echo "1" > /proc/sys/net/ipv4/ip_forward 
  90.  
  91. $IPTABLES -P INPUT DROP 
  92.  
  93. $IPTABLES -P FORWARD DROP 
  94.  
  95. $IPTABLES -P OUTPUT DROP 
  96.  
  97. $IPTABLES -F 
  98.  
  99. $IPTABLES -X 
  100.  
  101.  
  102.  
  103. $IPTABLES -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT 
  104.  
  105. $IPTABLES -A INPUT -p tcp --dport 22 -j ACCEPT 
  106.  
  107. $IPTABLES -A INPUT -p tcp --dport 80 --tcp-flags SYN,ACK,FIN,RST SYN -m limit --limit 30/m --limit-burst 2 -j ACCEPT 
  108.  
  109.  
  110.  
  111. $IPTABLES -A OUTPUT -p tcp -s 127.0.0.1 -j ACCEPT 
  112.  
  113. $IPTABLES -A OUTPUT -p tcp -s 192.168.1.102 -j ACCEPT 
  114.  
  115. $IPTABLES -A OUTPUT -p udp -s 127.0.0.1 -j ACCEPT 
  116.  
  117. $IPTABLES -A OUTPUT -p udp -s 192.168.1.102 -j ACCEPT 
  118.  
  119.  
  120.  
  121. $IPTABLES -A INPUT -p tcp --syn -j DROP 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章