轉載 iptables 禁用與 解封ip

Linux下,使用ipteables來維護IP規則表。要封停或者是解封IP,其實就是在IP規則表中對入站部分的規則進行添加操做。linux

要封停一個IP,使用下面這條命令:安全

iptables -I INPUT -s ***.***.***.*** -j DROP

要解封一個IP,使用下面這條命令:服務器

 
 
  1. iptables -D INPUT -***.***.***.*** -j DROP

參數-I是表示Insert(添加),-D表示Delete(刪除)。後面跟的是規則,INPUT表示入站,***.***.***.***表示要封停的IP,DROP表示放棄鏈接。tcp

此外,還能夠使用下面的命令來查看當前的IP規則表:spa

 
 
  1. iptables -list

好比如今要將123.44.55.66這個IP封殺,就輸入:rest

 
 
  1. iptables -I INPUT -123.44.55.66 -j DROP

要解封則將-I換成-D便可,前提是iptables已經有這條記錄。若是要想清空封掉的IP地址,能夠輸入:code

 
 
  1. iptables -flush

要添加IP段到封停列表中,使用下面的命令:vps

 
 
  1. iptables -I INPUT -121.0.0.0/8 -j DROP

其實也就是將單個IP封停的IP部分換成了Linux的IP段表達式。關於IP段表達式網上有不少詳細解說的,這裏就不提了。ip

相信有了iptables的幫助,解決小的DDoS之類的攻擊也不在話下!get

附:其餘經常使用的命令

編輯 iptables 文件

 
 
  1. vi /etc/sysconfig/iptables

關閉/開啓/重啓防火牆

 
 
  1. /etc/init.d/iptables stop
  2.  
  3. #start 開啓
  4.  
  5. #restart 重啓

驗證一下是否規則都已經生效:

 
 
  1. iptables -L

保存並重啓iptables

 
 
  1. /etc/rc.d/init.d/iptables save
  2. service iptables restart

linux下實用iptables封ip段的一些常見命令:

封單個IP的命令是:

 
 
  1. iptables -I INPUT -211.1.0.0 -j DROP

封IP段的命令是:

 
 
  1. iptables -I INPUT -211.1.0.0/16 -j DROP
  2. iptables -I INPUT -211.2.0.0/16 -j DROP
  3. iptables -I INPUT -211.3.0.0/16 -j DROP

封整個段的命令是:

 
 
  1. iptables -I INPUT -211.0.0.0/8 -j DROP

封幾個段的命令是:

 
 
  1. iptables -I INPUT -61.37.80.0/24 -j DROP
  2. iptables -I INPUT -61.37.81.0/24 -j DROP

想在服務器啓動自運行的話有三個方法:

一、把它加到/etc/rc.local中

二、iptables-save >;/etc/sysconfig/iptables能夠把你當前的iptables規則放到/etc/sysconfig/iptables中,系統啓動iptables時自動執行。

三、service iptables save 也能夠把你當前的iptables規則放/etc/sysconfig/iptables中,系統啓動iptables時自動執行。

後兩種更好此,通常iptables服務會在network服務以前啓來,更安全。

解封的話:
iptables -D INPUT -s IP地址 -j REJECT
iptables -F 全清掉了

Linux防火牆Iptable如何設置只容許某個ip訪問80端口,只容許特定ip訪問某端口?參考下面命令,只容許46.166.150.22訪問本機的80端口。若是要設置其餘ip或端口,改改便可。

 
 
  1. iptables -I INPUT -p TCP dport 80 -j DROP
  2. iptables -I INPUT -46.166.150.22 -p TCP dport 80 -j ACCEPT

在root用戶下執行上面2行命令後,重啓iptables, service iptables restart

查看iptables是否生效:

 
 
  1. [root@www.xxx.com]# iptables -L
  2. Chain INPUT (policy ACCEPT)
  3. target           prot opt source               destination
  4. ACCEPT     tcp    46.166.150.22    anywhere            tcp dpt:http
  5. DROP         tcp    anywhere             anywhere            tcp dpt:http
  6.  
  7. Chain FORWARD (policy ACCEPT)
  8. target     prot opt source               destination
  9.  
  10. Chain OUTPUT (policy ACCEPT)
  11. target     prot opt source               destination

上面命令是針對整個服務器(所有ip)禁止80端口,若是隻是須要禁止服務器上某個ip地址的80端口,怎麼辦?

下面的命令是隻容許來自174.140.3.190的ip訪問服務器上216.99.1.216的80端口

 
 
  1. iptables -A FORWARD -174.140.3.190 -216.99.1.216 -p tcp -m tcp dport 80 -j ACCEPT
  2. iptables -A FORWARD -216.99.1.216 -p tcp -m tcp dport 80 -j DROP

更多iptables參考命令以下:

1.先備份iptables

 
 
  1. # cp /etc/sysconfig/iptables /var/tmp

須要開80端口,指定IP和局域網

下面三行的意思:

先關閉全部的80端口

開啓ip段192.168.1.0/24端的80口

開啓ip段211.123.16.123/24端ip段的80口

 
 
  1. # iptables -I INPUT -p tcp –dport 80 -j DROP
  2. # iptables -I INPUT -s 192.168.1.0/24 -p tcp –dport 80 -j ACCEPT
  3. # iptables -I INPUT -s 211.123.16.123/24 -p tcp –dport 80 -j ACCEPT

以上是臨時設置。

2.而後保存iptables

 
 
  1. # service iptables save

3.重啓防火牆

 
 
  1. #service iptables restart

===============如下是轉載================================================

如下是端口,先所有封再開某些的IP

 
 
  1. iptables -I INPUT -p tcp dport 9889 -j DROP
  2. iptables -I INPUT -192.168.1.0/24 -p tcp dport 9889 -j ACCEPT

若是用了NAT轉發記得配合如下才能生效

 
 
  1. iptables -I FORWARD -p tcp dport 80 -j DROP
  2. iptables -I FORWARD -192.168.1.0/24 -p tcp dport 80 -j ACCEPT

經常使用的IPTABLES規則以下:
只能收發郵件,別的都關閉

 
 
  1. iptables -Filter -m mac mac-source 00:0F:EA:25:51:37 -j DROP
  2. iptables -Filter -m mac mac-source 00:0F:EA:25:51:37 -p udp dport 53 -j ACCEPT
  3. iptables -Filter -m mac mac-source 00:0F:EA:25:51:37 -p tcp dport 25 -j ACCEPT
  4. iptables -Filter -m mac mac-source 00:0F:EA:25:51:37 -p tcp dport 110 -j ACCEPT

IPSEC NAT 策略

 
 
  1. iptables -PFWanPriv -192.168.100.2 -j ACCEPT
  2. iptables -t nat -A PREROUTING -p tcp dport 80 -d $INTERNET_ADDR -j DNAT to-destination 192.168.100.2:80
  3.  
  4. iptables -t nat -A PREROUTING -p tcp dport 1723 -d $INTERNET_ADDR -j DNAT to-destination 192.168.100.2:1723
  5.  
  6. iptables -t nat -A PREROUTING -p udp dport 1723 -d $INTERNET_ADDR -j DNAT to-destination 192.168.100.2:1723
  7.  
  8. iptables -t nat -A PREROUTING -p udp dport 500 -d $INTERNET_ADDR -j DNAT to-destination 192.168.100.2:500
  9.  
  10. iptables -t nat -A PREROUTING -p udp dport 4500 -d $INTERNET_ADDR -j DNAT to-destination 192.168.100.2:4500
  11.  

FTP服務器的NAT

 
 
  1. iptables -PFWanPriv -p tcp dport 21 -192.168.1.22 -j ACCEPT
  2. iptables -t nat -A PREROUTING -p tcp dport 21 -d $INTERNET_ADDR -j DNAT to-destination 192.168.1.22:21

只容許訪問指定網址

 
 
  1. iptables -Filter -p udp dport 53 -j ACCEPT
  2. iptables -Filter -p tcp dport 53 -j ACCEPT
  3. iptables -Filter -d www.ctohome.com -j ACCEPT
  4. iptables -Filter -d www.guowaivps.com -j ACCEPT
  5. iptables -Filter -j DROP

開放一個IP的一些端口,其它都封閉

 
 
  1. iptables -Filter -p tcp dport 80 -192.168.1.22 -d www.pconline.com.cn -j ACCEPT
  2. iptables -Filter -p tcp dport 25 -192.168.1.22 -j ACCEPT
  3. iptables -Filter -p tcp dport 109 -192.168.1.22 -j ACCEPT
  4. iptables -Filter -p tcp dport 110 -192.168.1.22 -j ACCEPT
  5. iptables -Filter -p tcp dport 53 -j ACCEPT
  6. iptables -Filter -p udp dport 53 -j ACCEPT
  7. iptables -Filter -j DROP

多個端口

 
 
  1. iptables -Filter -p tcp -m multiport destination-port 22,53,80,110 -192.168.20.3 -j REJECT

連續端口

 
 
  1. iptables -Filter -p tcp -m multiport source-port 22,53,80,110 -192.168.20.3 -j REJECT iptables -Filter -p tcp source-port 2:80 -192.168.20.3 -j REJECT

指定時間上網

 
 
  1. iptables -Filter -10.10.10.253 -m time timestart 6:00 timestop 11:00 days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
  2. iptables -Filter -m time timestart 12:00 timestop 13:00 days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
  3. iptables -Filter -m time timestart 17:30 timestop 8:30 days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT

禁止多個端口服務

 
 
  1. iptables -Filter -m multiport -p tcp dport 21,23,80 -j ACCEPT

將WAN 口NAT到PC

 
 
  1. iptables -t nat -A PREROUTING -i $INTERNET_IF -d $INTERNET_ADDR -j DNAT to-destination 192.168.0.1

將WAN口8000端口NAT到192。168。100。200的80端口

 
 
  1. iptables -t nat -A PREROUTING -p tcp dport 8000 -d $INTERNET_ADDR -j DNAT to-destination 192.168.1.22:80

MAIL服務器要轉的端口

 
 
  1. iptables -t nat -A PREROUTING -p tcp dport 110 -d $INTERNET_ADDR -j DNAT to-destination 192.168.1.22:110
  2. iptables -t nat -A PREROUTING -p tcp dport 25 -d $INTERNET_ADDR -j DNAT to-destination 192.168.1.22:25

只容許PING 202。96。134。133,別的服務都禁止

 
 
  1. iptables -Filter -p icmp -192.168.1.22 -202.96.134.133 -j ACCEPT
  2. iptables -Filter -j DROP

禁用BT配置

 
 
  1. iptables Filter p tcp dport 6000:20000 j DROP

禁用QQ防火牆配置

 
 
  1. iptables -Filter -p udp dport ! 53 -j DROP
  2. iptables -Filter -218.17.209.0/24 -j DROP
  3. iptables -Filter -218.18.95.0/24 -j DROP
  4. iptables -Filter -219.133.40.177 -j DROP

基於MAC,只能收發郵件,其它都拒絕

 
 
  1. iptables -Filter -m mac mac-source 00:0A:EB:97:79:A1 -j DROP
  2. iptables -Filter -m mac mac-source 00:0A:EB:97:79:A1 -p tcp dport 25 -j ACCEPT
  3. iptables -Filter -m mac mac-source 00:0A:EB:97:79:A1 -p tcp dport 110 -j ACCEPT

禁用MSN配置

 
 
  1. iptables -Filter -p udp dport 9 -j DROP
  2. iptables -Filter -p tcp dport 1863 -j DROP
  3. iptables -Filter -p tcp dport 80 -207.68.178.238 -j DROP
  4. iptables -Filter -p tcp dport 80 -207.46.110.0/24 -j DROP

只容許PING 202。96。134。133 其它公網IP都不準PING

 
 
  1. iptables -Filter -p icmp -192.168.1.22 -202.96.134.133 -j ACCEPT
  2. iptables -Filter -p icmp -j DROP

禁止某個MAC地址訪問internet:

 
 
  1. iptables -Filter -m mac mac-source 00:20:18:8F:72:F8 -j DROP

禁止某個IP地址的PING:

 
 
  1. iptables Filter p icmp 192.168.0.1 j DROP

禁止某個IP地址服務:

 
 
  1. iptables Filter -p tcp -192.168.0.1 dport 80 -j DROP
  2. iptables Filter -p udp -192.168.0.1 dport 53 -j DROP

只容許某些服務,其餘都拒絕(2條規則)

 
 
  1. iptables -Filter -p tcp -192.168.0.1 dport 1000 -j ACCEPT
  2. iptables -Filter -j DROP

禁止某個IP地址的某個端口服務

 
 
  1. iptables -Filter -p tcp -10.10.10.253 dport 80 -j ACCEPT
  2. iptables -Filter -p tcp -10.10.10.253 dport 80 -j DROP

禁止某個MAC地址的某個端口服務

 
 
  1. iptables -Filter -p tcp -m mac mac-source 00:20:18:8F:72:F8 dport 80 -j DROP

禁止某個MAC地址訪問internet:

 
 
  1. iptables -Filter -m mac mac-source 00:11:22:33:44:55 -j DROP

禁止某個IP地址的PING:

 
 
  1. iptables Filter p icmp 192.168.0.1 j DROP
相關文章
相關標籤/搜索