在Linux下,使用ipteables來維護IP規則表。要封停或者是解封IP,其實就是在IP規則表中對入站部分的規則進行添加操做。linux
要封停一個IP,使用下面這條命令:安全
iptables -I INPUT -s ***.***.***.*** -j DROP
要解封一個IP,使用下面這條命令:服務器
- iptables -D INPUT -s ***.***.***.*** -j DROP
參數-I是表示Insert(添加),-D表示Delete(刪除)。後面跟的是規則,INPUT表示入站,***.***.***.***表示要封停的IP,DROP表示放棄鏈接。tcp
此外,還能夠使用下面的命令來查看當前的IP規則表:spa
- iptables -list
好比如今要將123.44.55.66這個IP封殺,就輸入:rest
- iptables -I INPUT -s 123.44.55.66 -j DROP
要解封則將-I換成-D便可,前提是iptables已經有這條記錄。若是要想清空封掉的IP地址,能夠輸入:code
- iptables -flush
要添加IP段到封停列表中,使用下面的命令:vps
- iptables -I INPUT -s 121.0.0.0/8 -j DROP
其實也就是將單個IP封停的IP部分換成了Linux的IP段表達式。關於IP段表達式網上有不少詳細解說的,這裏就不提了。ip
相信有了iptables的幫助,解決小的DDoS之類的攻擊也不在話下!get
附:其餘經常使用的命令
編輯 iptables 文件
- vi /etc/sysconfig/iptables
關閉/開啓/重啓防火牆
- /etc/init.d/iptables stop
- #start 開啓
- #restart 重啓
驗證一下是否規則都已經生效:
- iptables -L
保存並重啓iptables
- /etc/rc.d/init.d/iptables save
- service iptables restart
linux下實用iptables封ip段的一些常見命令:
封單個IP的命令是:
- iptables -I INPUT -s 211.1.0.0 -j DROP
封IP段的命令是:
- iptables -I INPUT -s 211.1.0.0/16 -j DROP
- iptables -I INPUT -s 211.2.0.0/16 -j DROP
- iptables -I INPUT -s 211.3.0.0/16 -j DROP
封整個段的命令是:
- iptables -I INPUT -s 211.0.0.0/8 -j DROP
封幾個段的命令是:
- iptables -I INPUT -s 61.37.80.0/24 -j DROP
- iptables -I INPUT -s 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或端口,改改便可。
- iptables -I INPUT -p TCP –dport 80 -j DROP
- iptables -I INPUT -s 46.166.150.22 -p TCP –dport 80 -j ACCEPT
在root用戶下執行上面2行命令後,重啓iptables, service iptables restart
查看iptables是否生效:
- [root@www.xxx.com]# iptables -L
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp – 46.166.150.22 anywhere tcp dpt:http
- DROP tcp – anywhere anywhere tcp dpt:http
- Chain FORWARD (policy ACCEPT)
- target prot opt source destination
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
上面命令是針對整個服務器(所有ip)禁止80端口,若是隻是須要禁止服務器上某個ip地址的80端口,怎麼辦?
下面的命令是隻容許來自174.140.3.190的ip訪問服務器上216.99.1.216的80端口
- iptables -A FORWARD -s 174.140.3.190 -d 216.99.1.216 -p tcp -m tcp –dport 80 -j ACCEPT
- iptables -A FORWARD -d 216.99.1.216 -p tcp -m tcp –dport 80 -j DROP
更多iptables參考命令以下:
1.先備份iptables
- # cp /etc/sysconfig/iptables /var/tmp
須要開80端口,指定IP和局域網
下面三行的意思:
先關閉全部的80端口
開啓ip段192.168.1.0/24端的80口
開啓ip段211.123.16.123/24端ip段的80口
- # iptables -I INPUT -p tcp –dport 80 -j DROP
- # iptables -I INPUT -s 192.168.1.0/24 -p tcp –dport 80 -j ACCEPT
- # iptables -I INPUT -s 211.123.16.123/24 -p tcp –dport 80 -j ACCEPT
以上是臨時設置。
2.而後保存iptables
- # service iptables save
3.重啓防火牆
- #service iptables restart
===============如下是轉載================================================
如下是端口,先所有封再開某些的IP
- iptables -I INPUT -p tcp –dport 9889 -j DROP
- iptables -I INPUT -s 192.168.1.0/24 -p tcp –dport 9889 -j ACCEPT
若是用了NAT轉發記得配合如下才能生效
- iptables -I FORWARD -p tcp –dport 80 -j DROP
- iptables -I FORWARD -s 192.168.1.0/24 -p tcp –dport 80 -j ACCEPT
經常使用的IPTABLES規則以下:
只能收發郵件,別的都關閉
- iptables -I Filter -m mac –mac-source 00:0F:EA:25:51:37 -j DROP
- iptables -I Filter -m mac –mac-source 00:0F:EA:25:51:37 -p udp –dport 53 -j ACCEPT
- iptables -I Filter -m mac –mac-source 00:0F:EA:25:51:37 -p tcp –dport 25 -j ACCEPT
- iptables -I Filter -m mac –mac-source 00:0F:EA:25:51:37 -p tcp –dport 110 -j ACCEPT
IPSEC NAT 策略
- iptables -I PFWanPriv -d 192.168.100.2 -j ACCEPT
- iptables -t nat -A PREROUTING -p tcp –dport 80 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.100.2:80
- iptables -t nat -A PREROUTING -p tcp –dport 1723 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.100.2:1723
- iptables -t nat -A PREROUTING -p udp –dport 1723 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.100.2:1723
- iptables -t nat -A PREROUTING -p udp –dport 500 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.100.2:500
- iptables -t nat -A PREROUTING -p udp –dport 4500 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.100.2:4500
FTP服務器的NAT
- iptables -I PFWanPriv -p tcp –dport 21 -d 192.168.1.22 -j ACCEPT
- iptables -t nat -A PREROUTING -p tcp –dport 21 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.1.22:21
只容許訪問指定網址
- iptables -A Filter -p udp –dport 53 -j ACCEPT
- iptables -A Filter -p tcp –dport 53 -j ACCEPT
- iptables -A Filter -d www.ctohome.com -j ACCEPT
- iptables -A Filter -d www.guowaivps.com -j ACCEPT
- iptables -A Filter -j DROP
開放一個IP的一些端口,其它都封閉
- iptables -A Filter -p tcp –dport 80 -s 192.168.1.22 -d www.pconline.com.cn -j ACCEPT
- iptables -A Filter -p tcp –dport 25 -s 192.168.1.22 -j ACCEPT
- iptables -A Filter -p tcp –dport 109 -s 192.168.1.22 -j ACCEPT
- iptables -A Filter -p tcp –dport 110 -s 192.168.1.22 -j ACCEPT
- iptables -A Filter -p tcp –dport 53 -j ACCEPT
- iptables -A Filter -p udp –dport 53 -j ACCEPT
- iptables -A Filter -j DROP
多個端口
- iptables -A Filter -p tcp -m multiport –destination-port 22,53,80,110 -s 192.168.20.3 -j REJECT
連續端口
- iptables -A Filter -p tcp -m multiport –source-port 22,53,80,110 -s 192.168.20.3 -j REJECT iptables -A Filter -p tcp –source-port 2:80 -s 192.168.20.3 -j REJECT
指定時間上網
- iptables -A Filter -s 10.10.10.253 -m time –timestart 6:00 –timestop 11:00 –days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j DROP
- iptables -A Filter -m time –timestart 12:00 –timestop 13:00 –days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
- iptables -A Filter -m time –timestart 17:30 –timestop 8:30 –days Mon,Tue,Wed,Thu,Fri,Sat,Sun -j ACCEPT
禁止多個端口服務
- iptables -A Filter -m multiport -p tcp –dport 21,23,80 -j ACCEPT
將WAN 口NAT到PC
- 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端口
- iptables -t nat -A PREROUTING -p tcp –dport 8000 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.1.22:80
MAIL服務器要轉的端口
- iptables -t nat -A PREROUTING -p tcp –dport 110 -d $INTERNET_ADDR -j DNAT –to-destination 192.168.1.22:110
- 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,別的服務都禁止
- iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT
- iptables -A Filter -j DROP
禁用BT配置
- iptables –A Filter –p tcp –dport 6000:20000 –j DROP
禁用QQ防火牆配置
- iptables -A Filter -p udp –dport ! 53 -j DROP
- iptables -A Filter -d 218.17.209.0/24 -j DROP
- iptables -A Filter -d 218.18.95.0/24 -j DROP
- iptables -A Filter -d 219.133.40.177 -j DROP
基於MAC,只能收發郵件,其它都拒絕
- iptables -I Filter -m mac –mac-source 00:0A:EB:97:79:A1 -j DROP
- iptables -I Filter -m mac –mac-source 00:0A:EB:97:79:A1 -p tcp –dport 25 -j ACCEPT
- iptables -I Filter -m mac –mac-source 00:0A:EB:97:79:A1 -p tcp –dport 110 -j ACCEPT
禁用MSN配置
- iptables -A Filter -p udp –dport 9 -j DROP
- iptables -A Filter -p tcp –dport 1863 -j DROP
- iptables -A Filter -p tcp –dport 80 -d 207.68.178.238 -j DROP
- iptables -A Filter -p tcp –dport 80 -d 207.46.110.0/24 -j DROP
只容許PING 202。96。134。133 其它公網IP都不準PING
- iptables -A Filter -p icmp -s 192.168.1.22 -d 202.96.134.133 -j ACCEPT
- iptables -A Filter -p icmp -j DROP
禁止某個MAC地址訪問internet:
- iptables -I Filter -m mac –mac-source 00:20:18:8F:72:F8 -j DROP
禁止某個IP地址的PING:
- iptables –A Filter –p icmp –s 192.168.0.1 –j DROP
禁止某個IP地址服務:
- iptables –A Filter -p tcp -s 192.168.0.1 –dport 80 -j DROP
- iptables –A Filter -p udp -s 192.168.0.1 –dport 53 -j DROP
只容許某些服務,其餘都拒絕(2條規則)
- iptables -A Filter -p tcp -s 192.168.0.1 –dport 1000 -j ACCEPT
- iptables -A Filter -j DROP
禁止某個IP地址的某個端口服務
- iptables -A Filter -p tcp -s 10.10.10.253 –dport 80 -j ACCEPT
- iptables -A Filter -p tcp -s 10.10.10.253 –dport 80 -j DROP
禁止某個MAC地址的某個端口服務
- iptables -I Filter -p tcp -m mac –mac-source 00:20:18:8F:72:F8 –dport 80 -j DROP
禁止某個MAC地址訪問internet:
- iptables -I Filter -m mac –mac-source 00:11:22:33:44:55 -j DROP
禁止某個IP地址的PING:
- iptables –A Filter –p icmp –s 192.168.0.1 –j DROP