iptables

基本功能
iptables -P INPUT ACCEPT                 #預設政策
iptables -A INPUT -p tcp -s 192.168.100.100/32 –dport 21 -j ACCEPT  #新增規則
iptables-save                             #顯示所有規則
iptables -L INPUT –line-number    #顯示input同時帶行數
iptables -D INPUT 9        #刪除input chain第9行
service iptables save   #寫入iptables

LOG功能
首先修改LOG資料另外存放位置
vim /etc/rsyslog.conf
kern.warning                                            /var/log/iptables.log

iptables -A INPUT -p tcp -s 192.168.100.100/32 –dport 21 -j LOG

建立自己的規則(chain)
iptables -N allow  #新增一條chain
iptables -A INPUT -j allow #進入的封包也要經過allow
iptables -A allow -p tcp -s 192.168.100.100 –dport 21 -j ACCEPT  #新增規則
iptables -X allow #清除allow chain的所有規則

1. 限制每個IP連接HTTP最大併發50個連接數

iptables -A INPUT -p tcp –dport 80 -m connlimit –connlimit-above 50 -j REJECT

2. 限制每個IP同時最多100個連接數

iptables -I INPUT -p tcp –syn –dport 80 -m connlimit –connlimit-above 100 -j REJECT

or

iptables -I INPUT -p tcp –syn –dport 80 -m connlimit ! –connlimit-above 100 -j ACCEPT

3. 限制每組C Class IP同時最多100個連接數

iptables -I INPUT -p tcp –syn –dport 80 -m connlimit –connlimit-above 100 –connlimit-mask 24 -j REJECT

4. 限制每個IP同時5個80 port轉發,超過的丟棄

iptables -I FORWARD -p tcp –syn –dport 80 -m connlimit –connlimit-above 5 -j DROP

5. 限制每個IP在60秒內允許新建立30個連接數

iptables -A INPUT -p tcp –dport 80 -m recent –name BAD_HTTP_ACCESS –update –seconds 60 –hitcount 30

-j REJECT

6. (By CentOS 5.x)調整ipt_recent參數,記錄1000個IP,每個IP記錄60個封包

vim /etc/modprobe.conf

options ipt_recent ip_list_tot=1000 ip_pkt_list_tot=60

完成後執行

modprobe ipt_recent

7. 每秒最多允許5個新連接封包數

iptables -A INPUT -p tcp –syn -m limit –limit 1/s –limit-burst 5 -j ACCEPT

8. 防止各種端口掃描

iptables -A INPUT -p tcp –tcp-flags SYN,ACK,FIN,RST SYN -m limit –limit 1/s -j ACCEPT

iptables -L -n –line-number |grep 21 //–line-number可以顯示規則序號,在刪除的時候比較方便