歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux管理 >> Linux安全 >> iptables策略詳解

iptables策略詳解

日期:2017/2/27 17:10:45   编辑:Linux安全
iptables可用操作
(1)-L : 顯示所選鏈接的所有策略 : # iptables -L -n 查看iptables 策略

(2)-A : 在所選的鏈最尾部添加一條新的策略:# iptables -A INPUT -s 192.168.0.1 -j DROP

(3)-D : 從所選鏈中刪除策略,可以通過將策略的內容完整的寫出來或指定在所願鏈中的序號
# iptables -D INPUT 1

(4)-R:替換所選中的鏈裡指定的策略:# iptables -R INPUT 1 -s 192.168.30.0 -j DROP(替換第一條)

(5)-I: 從所選的鏈中指定策略前面插入一條新的策略 # iptables -I INPUT 2 -s 192.168.10.2 -j DROP

(6)-F:清空所選鏈的策略,如果沒有指定鏈,則清空指定表的所有鏈:# iptables -F INPUT (清空INPUT鏈)

(7)-Z:將所選鏈的所有計數器歸零,如果沒有指定鏈,則將指定表所有鏈中計數器歸零:# iptables -Z INPUT

(8)-N:根據用戶指定的名字建立新的鏈,在定義新鏈時所使用的名字不能和已有的鏈同名,數據包如果在自定義鏈裡被匹配了就會被target/jump執行,如果沒有被匹配則數據包將被送回調用自定義鏈的父鏈匹配
# iptables -N isok
# iptables -A isok -p tcp -s 192.168.0.168 -j DROP
# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP tcp -- 172.28.10.0 0.0.0.0/0 tcp dpt:80

Chain FORWARD (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

Chain isok (0 references)
target prot opt source destination
DROP tcp -- 192.168.0.168 0.0.0.0/0

(9)-X:刪除指定的用戶自定義的鏈。在刪除這個鏈必須沒有被引用,如果沒有指定鏈名稱,則刪除所有自定義的鏈,如果沒有指定表及鏈則刪除默認表中左右自定義的鏈
# iptables -X isok ( iptables -X)

(10)-E:對自定義的鏈進行重命名,該命令僅僅是改變自定義鏈的名字,對這個結構、工作沒有任何影響
# iptables -E isok isallow

(11)-P:為鏈設置默認的策略(如ACCEPT、DROP),所有不符合策略的數據包都被強制使用這個策略
# iptables -P INPUT DROP

(12)--line-number:在顯示策略時,輸入序號,該參數只能和-L配合使用
# iptables -L --line-number

(13)-s:匹配指定的IP源地址匹配數據包,指定網段為:# iptables -A INPUT -s 192.168.1.0/24 -j DROP

(14)-d:匹配指定的IP目標地址匹配數據包,指定網段:# iptables -A INPUT -d 192.168.1.0/24 -j DROP

(15)-i:<網絡接口>:以數據包進入本地所使用的網絡接口來匹配數據包,這個匹配操作只能用於
INPUT、FORWARD和PREROUTING這3個鏈
1、指定時使用網絡接口名稱,比如eth0、ppp0
2、使用加號作為通配符時,如果直接用一個加號,比如iptables -A INPUT -i +表示匹配所有的包,而不考慮使用哪個接口,這也是用-i參數的默認行為。通配符還可以使放在某一類接口的後面,
比如eth+表示所有的Ethernet接口
3、在接口前加感歎號表示取反(注意空格),比如“-i!eth0”意思是匹配來自除eth0外的所有包
4、lo表示本地回環地址
# iptables -A INPUT -i eth0 -s 192.168.0.99 -j DROP(阻止從eth0進入的源IP地址為192.168.0.99的所有通信)

(16)-o:<網絡接口>:以數據包離開本地所使用的網絡接口來匹配數據包,該參數配置方法與-i參數相同
# iptables -A OUTPUT -o eth0 -s 192.168.0.99 -j DROP

(17)--sport<端口>:基於數據包的源端口來匹配數據包:
1、該參數必須與-p參數配合使用
2、不指定--sport參數時,表示所有端口
3、可以使用連續的端口,比如“--sport 1000:1024"表示從1000到1024的所有端口
(包括1000、1024),”--sport 1024:1000“和”--sport 1000:1024“的效果相同
4、當省略第一個冒號時,默認從端口0開始,比如”--sport:1000"表示從0到1000的所有端口
5、當省略第二個冒號時,默認是65535,比如“--sport 1000:"表示從1000到65535的所有端口
6、在端口號前加感歎號表示取反(在感歎號與端口號之間必須有空格),比如”--sport!1000" 表示除1000號外的所有端口,“--sport ! 1000:1024"表示從1000到1024的所喲偶端口(包括1000、1024)之外的所有端口
# iptables -A INPUT -p tcp --sport 1000 -j DROP(阻止源端口為1000的所有tcp通信)
# iptables -A INPUT -p tcp --sport 1000:1024 -j DROP(阻止源端口大於等於1000且小於等於1024的所有tcp通信)

(18)--dport:基於數據包的目的端口來匹配包,該參數配置方法與--sport參數相同
# iptables -A INPUT -p tcp --dport 1000 -j DROP (阻止目標端口為1000的所有tcp通信)
# iptables -A INPUT -p tcp --dport 1000:1024 -j DROP (阻止目標端口大於等於1000且小於等於1024的所有tcp通信)

(19)-m multiport --sport<端口>:源端口多端口匹配,最多可以使用15個端口,使用逗號分隔,該參數必須與-p參數配合使用
# iptables -A INPUT -p tcp -m multiport --sport 1000,1024,1025 -j DROP (阻止源端口為1000,1024,1025的所有tcp通信)

(20)-m multiport --dport<端口>:目的端口多端口匹配,最多可以使用15個端口,以逗號隔開,該參數必須與-p參數配合使用
# iptables -A INPUT -p tcp -m multiport --dport 1000,1024 -j DROP(阻止目的端口為1000,1024的所有tcp通信)

(21)-m multiport --port<端口>:同端口多端口匹配,通端口是指源端口和目的端口使用相同端口的數據包

(22)--tcp-flags<檢查標記列表><條件列表>:匹配指定的tcp標記,有兩個參數,它們都是列表,列表內部用英文的都好作為分隔符,這兩個列表之間用空格分開,第一個參數指定需要檢查的tcp標記,第二個參數指定”在第一個列表中出現過的且必須被設為1(即狀態是打開的)的“標記(第一個列表中其他的標記必須設置0),也就是說第一個參數提供檢查范圍,第二個參數提供被設置的調價(就是哪些位置1).這個匹配操作可以識別SYN、ACK、FIN、RST、URG、PSH標記另外還可以使用ALL、NONE。ALL是指選定所有的標,NONE是指未選定任何標記,也可以在參數錢使用感歎號取反
# iptables -p tcp --tcp-flags SYN,ACK,FIN SYN -j DROP(阻止SYN標記被設置而FIN和ACK標記沒有被設置的所有tcp通信)
# iptables -p tcp --tcp-flags ALL NONE -j DROP (阻止所有標記沒有被設置為1的所有tcp通信)
# iptables -p tcp --tcp-flags ! SYN,FIN,ACK SYN -j DROP (阻止FIN、ACK被設置為而SYN沒有被設置的所有tcp通信)

(23)--syn:匹配SYN標記被設置而ACK和RST標記沒有設置的數據包(該參數與”iptables -p tcp --tcp-flags SYN,ACK,RST SYN" 有相同的效果)

(24)--icmp-type<類型數值>:根據ICMP類型匹配包,類型的指定可以使用十進制數值
# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
# iptables -A OUTPUT -p icmp --icmp-type 0 -j ACCEPT

# iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
# iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT

(25)--mac-source<MAC地址>:基於數據包的源MAC地址匹配數據包,只能在PREROUTING,FORWARD,INPUT鏈中使用
# iptables -A INPUT -m mac --mac-source 00:50:56:C0:00:01 -j DROP

(26)--uid-owner<UID>:按生成包的用戶ID(UID)來匹配數據包,如例阻止所有UID為555的用戶向外的所有通信
# iptables -A OUTPUT -m owner --uid-owner 555 -j DROP

(27)--gid-owner<GID>:按生成包的用戶所在的組的ID(GID)來匹配數據包
# iptables -A OUTPUT -m owner --gid-owner 555 -j DROP

(28)--sid-owner<SID>;按生成數據包的會話ID(SID)來匹配外出數據包(一個進程以及它的子進程或他的多個線程都有同一個SID)
# iptables -A OUTPUT -m owner --sid-owner 178 -j DROP

(29)--state<狀態列表>:匹配數據包的狀態,多個狀態使用逗號分隔,例子將允許連接本機的FTP服務器
# iptables -A INPUT -p tcp --dport 21 -j ACCEPT
# iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
例:# iptables -A INPUT -p tcp -m state --state NEW --dport 80 -j ACCEPT

(30)DNAT:用於進行目標網絡地址轉換,就是重寫數據包的目標IP地址,如果一個數據包被匹配,那麼和他屬於同一個流的所有的包都會被自動的轉換,然後就可以被路由到正確的主機或網絡DNAT在實際工作中非常有用,比如一台Web服務器位於局域網,而且沒有可在Internet上使用的真實 IP地址,這時就可以使用DNAT讓防火牆把所有到它自己HTTP端口的包轉發給局域網內部真正的Web服務器,目的地址也可以是一個范圍,這樣DNAT會為每一個流隨機分配一個IP地址,DNAT只能在nat表的PREROUTING,OUTPUT鏈中使用,或是被這兩條鏈調用的鏈中,DNAT需要使用--to-dest參數指定寫入IP包頭的地址(也就是數據包要被轉發到的地方); 還可以再地址後指定一個活一個范圍的端口,如 --to-dest 192.168.1.10:80或192.168.1.10:80-100

# iptables -t nat -A PREROUTING -p tcp -d 202.13.0.2 --dport 80 -j DNAT --to-dest 192.168.0.1-192.168.0.10
(意思是說外面要訪問內部的web服務器,需要經過202.13.0.2:80端口來訪問,經過這個ip時需要進行目標網絡地址轉換,每個數據流被隨機分配一個要轉發到的地址,但同一個數據流總是使用同一個地址,也可以只指定一個IP地址作為參數)

(31)SNAT:用於進行源網絡地址轉換,就是重寫數據包的源IP地址,SNAT需要使用--to-source參數指定寫入IP包頭的地址,
SNAT的其他語法與DNAT相同,SNAT只能有nat表的POSTROUTING鏈使用

# iptables -t nat -A POSTROUTING -s 192.168.0.0/24 -j SNAT --to-source 202.103.0.2
(意思為當內網的ip:192.168.0.0/24網段要訪問外網時需要將這些內網ip偽裝成公網ip才可以訪問到外部)

(32)MASQUERADE:該目標與SNAT作用相同,只是使用該目標時不需要指明--to-source。MASQUERADE是專門設計用於那些動態獲取IP地址的連接(比如撥號上網等),如果有固定的IP地址,可推薦直接使用SNAT(也可以使用MASQUERADE)和SNAT一樣,只能用於nat表的POSTROUTING鏈,而且它只有一個參數(不是必需的)--to-ports用於指定端口,使用該參數時必需配合-p使用
# iptables -t nat -A POSTROUTING -p tcp -j MASQUERADE --to-ports 1024-5000
# iptables -t nat -A POSTROUTING -p tcp -s 192.168.0.0/24 -j MASQUERADE

(33)REDIRECT:在防火牆所在的主機內部轉發數據包或流到另外一個端口(比如把所有去往端口HTTP的包REDIRECT到HTTP代理,當然這都發生在主機內部,本地生成的數據包都會被映射到127.0.0.1,換句話說這個目標把要轉發的數據包的目的地址改寫為本機主機的IP,在實現透明代理時就需要使用該目標(局域網內的主機不需要知道代理服務器的存在就可以正常上網被稱為透明代理)。該目標只能用在nat表的PREROUTING、OUTPUT鏈和被它們調用的自定義鏈REDIRECT只有一個選項--to-ports用於指定端口(可以使用--to-ports 8080-8888的方式指定一個端口范圍)。
舉個例子:將本機tcp的80端口收到的數據包轉發到本機的8080端口
# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080

(34)REJECT:REJECT和DROP作用相同,區別在於它除了阻止數據包之外,還向發送者返回錯誤信息,該目標只用用在INPUT、FORWARD,OUTPUT和它們的子鏈,而且包含REJECT的鏈也只能被它們調用,否則不能發揮作用,它只有一個選項--reject-with用於控制返回的錯誤信息的類型
# iptables -A FORWARD -p tcp --dport 22 -j REJECT --reject-with tcp-reset

##################### iptables策略配置方法 #########################

(1)使用命令:在使用命令修改了策略後,需要使用如下命令讓策略永久生效 # service iptables save

(2)使用腳本:另外一個方法讓策略永久生效的,編寫一個腳本將所有的策略放入腳本然後每次開機自動運行該腳本(/etc/rc.locl)
#!/bin/sh
# 清除filter、nat表的所有側率及計數器
iptables -t filter -F
iptables -t filter -Z
iptables -t filter -X
iptables -t nat -F
iptables -t nat -Z
iptables -t nat -X
#定義filter、nat表默認策略
iptables -t filter -P INPUT DROP
iptables -t filter -p OUTPUT DROP
iptables -t filter -P FORWARD ACCEPT
iptables -t nat -P OUTPUT ACCEPT
iptables -t nat -P PREROUTING ACCEPT
iptables -t nat -P POSTROUTING ACCEPT
#允許IP轉發
/bin/echo "1" >/pro/sys/net/ipv4/ip_forward
#載入內核模塊
modprode ip_conntrack-ftp
modprode ip_nat_ftp
#允許本機的所有通信
iptables -t filter -A INPUT -i lo -j ACCEPT
iptables -t filter -A OUTPUT -o lo -j ACCEPT
#運行ping
iptables -t filter -A INPUT -m icmp --icmp-type 8 -j ACCEPT
iptables -t filter -A OUTPUT -m icmp --icmp-type 0 -j ACCEPT
iptables -t filter -A INPUT -m icmp --icmp-type 0 -j ACCEPT
iptables -t filter -A OUTPUT -m icmp --icmp-type 8 -j ACCEPT
#允許查詢本機的DNS服務
iptables -t filter -A OUTPUT -o eth0 -p udp --dport 53 -j ACCEPT
iptables -t filter -A INPUT -i eth0 -p udp --sport 53 -j ACCEPT
#允許訪問本機的FTP服務
iptables -t filter -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -t filter -A OUTPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -t filter -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

#################################### 常見服務策略配置 ##########################################
下面是iptables中通過策略允許常見服務通信的例子,除特殊說明外都架設服務於防火牆在同一主機且filter表INPUT及OUTPUT鏈默認策略時DROP,如果將IPtables做為網關或其他情況可根據實現情況選擇不同的表(下列策略都假設服務使用默認端口)

(1)NTP服務
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 123 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 37 -j ACCEPT

# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 123 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 37 -j ACCEPT

(2)FTP服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 21 -j ACCEPT
# iptables -A INPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT

# iptables -A OUTPUT -p tcp -m state --state ESTABLISHED,RELATED -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 21 -j ACCEPT

(3)DHCP服務
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 67 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --sport 68 -j ACCEPT

# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --dport 67 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 68 -j ACCEPT

(4)DNS服務
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 53 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --sport 53 -j ACCEPT

# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --dport 53 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 53 -j ACCEPT

(5)Samba服務
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 137:139 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 137:139 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 445 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 445 -j ACCEPT

# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 137:139 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 137:139 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 445 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 445 -j ACCEPT

(6)NFS 服務
1、在使用iptables允許NFS服務時需要修改/etc/sysconfig/nfs文件的下列參數,是NFS使用固定端口
LOCKD_TCPPORT=4001
LOCKD_UDPPORT=4001
LOCKD_PORT=4002
STATD_PORT=4000
STATD_OUTGOING_PORT=4003
2、配置iptables策略
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 4000:4003 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 4000:4003 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 2049 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 2049 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 111 -j ACCEPT
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 111 -j ACCEPT

# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 4000:4003 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 4000:4003 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 2049 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 2049 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 111 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 111 -j ACCEPT

(7)日志服務
# iptables -A INPUT -p udp -s 192.168.0.0/24 --dport 514 -j ACCEPT
# iptables -A OUTPUT -p udp -d 192.168.0.0/24 --sport 514 -j ACCEPT

(8)HTTP/HTTPS服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 80 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 443 -j ACCEPT

# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 80 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 443 -j ACCEPT

(8)WebDav服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 9800 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 9802 -j ACCEPT

# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 9800 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 9802 -j ACCEPT

(9)SMTP服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 25 -j ACCEPT

# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 25 -j ACCEPT

(10)POP3/POP3s服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 110 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 995 -j ACCEPt

# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 110 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 995 -j ACCEPT

(11)IMAP/IMAPs服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 143 -j ACCEPT
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 993 -j ACCEPT

# iptalbes -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 143 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 993 -j ACCEPT

(12)LDAP服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 389 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 389 -j ACCEPT

(14)SSH服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 22 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 22 -j ACCEPT

(15)telnet服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 23 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 23 -j ACCEPT

(16)MySQL服務
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 3306 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 3306 -j ACCEPT

(17)SQL Server
# iptables -A INPUT -p tcp -s 192.168.0.0/24 --dport 1433 -j ACCEPT
# iptables -A OUTPUT -p tcp -d 192.168.0.0/24 --sport 1433 -j ACCEPT
Copyright © Linux教程網 All Rights Reserved