zhangyd6 发表于 2011-12-21 08:43

iptables limit 參數備忘

轉自:<a href="http://hi.baidu.com/baijy/blog/item/ee54e48830bac095a4c272fc.html" target="_blank">http://hi.baidu.com/baijy/blog/item/ee54e48830bac095a4c272fc.html</a><div><span class="Apple-style-span" style="color: rgb(85, 85, 85); font-family: Georgia; font-size: 12px; letter-spacing: 1px; line-height: 25px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables limit 參數備忘</font></div><ul style="line-height: normal; letter-spacing: 0.1em; "><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制特定封包傳入速度</font></li><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制特定埠口連入頻率</font></li><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables Log 記錄參數備忘</font></li><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">自定 Chain 使用備忘</font></li><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">防治 SYN-Flood 碎片攻擊</font></li></ul><div style="font-family: Arial; word-wrap: break-word; word-break: break-all; visibility: visible !important; zoom: 1 !important; filter: none; font-size: 12px; line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制 ping (echo-request) 傳入的速度</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制前, 可正常每 0.2 秒 ping 一次</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">ping your.linux.ip -i 0.2</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制每秒只接受一個 icmp echo-request 封包</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 1 -j ACCEPT<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A INPUT -p icmp --icmp-type echo-request -j DROP</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">--limit 1/s 表示每秒一次; 1/m 則為每分鐘一次</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">--limit-burst 表示允許觸發 limit 限制的最大次數 (預設 5)</font></p></blockquote><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">再以每 0.2 秒 ping 一次, 得到的回應是每秒一次</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">ping your.linux.ip -i 0.2</font></p></blockquote><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">限制 ssh 連入頻率</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">建立自訂 Chain, 限制 tcp 連線每分鐘一次, 超過者觸發 Log 記錄 (記錄在 /var/log/messages)</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -N ratelimit<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A ratelimit -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A ratelimit -p tcp --syn -m limit --limit 1/m --limit-burst 1 -j ACCEPT<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A ratelimit -p tcp -j LOG --log-level "NOTICE" --log-prefix ""<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A ratelimit -p tcp -j DROP</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">引用自訂 Chain, 限制 ssh (tcp port 22) 連入頻率</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -A INPUT -p tcp --dport 22 -s 192.168.0.0/16 -j ACCEPT (特定 IP 來源不受限制)<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A INPUT -p tcp --dport 22 -j ratelimit</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">參考資料:&nbsp;</font><a href="http://www.micheldonais.com/?p=379" style="color: rgb(85, 85, 85); font-size: 12px; text-decoration: none; line-height: normal; letter-spacing: 0.1em; font-family: Georgia; " target="_blank"><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">Mike's Blog - How to limit attack attempts in Linux</font></a></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">sshd_config 設定備忘:</font></p><ul style="line-height: normal; letter-spacing: 0.1em; "><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">LoginGraceTime 30 密碼輸入時限為 30 秒</font></li><li style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">MaxAuthTries 2 最多只能輸入 3 次密碼</font></li></ul></blockquote><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">同理可證</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -N pinglimit<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A pinglimit -m limit --limit 1/s --limit-burst 1 -j ACCEPT<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A pinglimit -j DROP</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -A INPUT -p icmp --icmp-type echo-request -j pinglimit</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">亦可達到每秒只接受一個 echo-request 封包</font></p></blockquote><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">補充: 清除自訂 Chain</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -L -n --line-number<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -D INPUT n<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -F ratelimit<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -X ratelimit</font></p></blockquote><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">防治 SYN-Flood 碎片攻擊</font></p><blockquote style="line-height: normal; letter-spacing: 0.1em; "><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -N syn-flood<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A syn-flood -m limit --limit 100/s --limit-burst 150 -j RETURN<br style="line-height: normal; letter-spacing: 0.1em; ">iptables -A syn-flood -j DROP</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">iptables -I INPUT -j syn-flood</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">模擬攻擊</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">wget&nbsp;</font><a href="http://www.xfocus.net/tools/200102/naptha-1.1.tgz" style="color: rgb(85, 85, 85); font-size: 12px; text-decoration: none; line-height: normal; letter-spacing: 0.1em; font-family: Georgia; " target="_blank"><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">http://www.xfocus.net/tools/200102/naptha-1.1.tgz</font></a><br style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">wget&nbsp;</font><a href="ftp://rpmfind.net/linux/freshrpms/redhat/7.0/libnet/libnet-1.0.1b-1.src.rpm" style="color: rgb(85, 85, 85); font-size: 12px; text-decoration: none; line-height: normal; letter-spacing: 0.1em; font-family: Georgia; " target="_blank"><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">ftp://rpmfind.net/linux/freshrpms/redhat/7.0/libnet/libnet-1.0.1b-1.src.rpm</font></a><br style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">tar -zxf naptha-1.1.tgz<br style="line-height: normal; letter-spacing: 0.1em; ">rpmbuild --recompile libnet-1.0.1b-1.src.rpm<br style="line-height: normal; letter-spacing: 0.1em; ">cp -r /var/tmp/libnet-buildroot/usr/* /usr/local/<br style="line-height: normal; letter-spacing: 0.1em; ">cd naptha-1.1<br style="line-height: normal; letter-spacing: 0.1em; ">make</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">./synsend your.linux.host.ip 80 local.host.eth0.ip 0.1</font></p><p style="line-height: normal; letter-spacing: 0.1em; "><font color="#000000" style="line-height: normal; letter-spacing: 0.1em; ">若成功抵擋, 不久後會出現 Can't send packet!: Operation not permitted 的訊息</font></p></blockquote></div></span></div>
页: [1]
查看完整版本: iptables limit 參數備忘