Iptables防火牆,iptables
1 位置 
使用vim /usr/sysconfig/iptables
 2 啟動、關閉、儲存 
 
 - service iptables stop 
- service iptables start 
- service iptables restart
- service iptables save
 3 結構 
iptables –> tables –> chains –>rules
 3.1 iptables的表與鏈 
iptables具有Filter,NAT,Mangle,Raw四種內建表
 3.1.1 Filter表 
filter表示iptables的預設表,它具有三種內建鏈:
 
 3.1.2 NAT表 
NAT有三種內建的鏈:
 
 
 - prerouting   - 處理剛到達本機並在路由轉寄前的資料包,它會轉換資料包中的目標IP地址(destination ip address),通常用於DNAT(destination NAT)。 
- postrouting - 處理即將離開本機資料包,它會轉換資料包中的源目標IP地址(source ip address),通常SNAT(source NAT) 
- output        - 處理本機產生的資料包
 3.1.3 Mangle表 
Mangle表用於指定如何處理資料包,它能改變TCP頭中的Qos位,Mangle表具有5個內建鏈
 
 
 - prerouting
- output
- forward 
- input
- postrouting
 3.1.4 Raw表 
raw表使用者處理異常,它具有2個內建鏈
 
 
 - prerouting chain
- output chain
 3.2 Iptables規則(Rules) 
 
 - rules包括一個條件和一個目標(target)
- 如果滿足條件就執行目標target中規則或者特定值
- 如果不滿足條件,就判斷下一條Rules
 3.2.1 目標值 
 
 - accept - 允許防火牆接收資料包
- drop    - 防火牆丟棄資料包
- queue  - 防火牆將資料包移交到使用者空間
- return  - 防火牆停止執行當前鏈中的後續rules規則,並返回到調用鏈(the calling chain)
 4 命令 
#iptables -t filter -L   查看filter表
 
#iptables -t nat  -L    查看nat表
 
#iptables -t mangel -L 查看mangel表 
 
#iptables -t raw  -L 查看Raw表 
 
例如 以下例子表明在filter表的input鏈, forward鏈, output鏈中存在規則:
 
# iptables --listChain INPUT (policy ACCEPT)num  target     prot opt source               destination1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0Chain FORWARD (policy ACCEPT)num  target     prot opt source               destination1    RH-Firewall-1-INPUT  all  --  0.0.0.0/0            0.0.0.0/0Chain OUTPUT (policy ACCEPT)num  target     prot opt source               destinationChain RH-Firewall-1-INPUT (2 references)num  target     prot opt source               destination1    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/02    ACCEPT     icmp --  0.0.0.0/0            0.0.0.0/0           icmp type 2553    ACCEPT     esp  --  0.0.0.0/0            0.0.0.0/04    ACCEPT     ah   --  0.0.0.0/0            0.0.0.0/05    ACCEPT     udp  --  0.0.0.0/0            224.0.0.251         udp dpt:53536    ACCEPT     udp  --  0.0.0.0/0            0.0.0.0/0           udp dpt:6317    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           tcp dpt:6318    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0           state RELATED,ESTABLISHED9    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0           state NEW tcp dpt:2210   REJECT     all  --  0.0.0.0/0            0.0.0.0/0           reject-with icmp-host-prohibited
欄位說明
num:編號
target:目標
prot:協議
source:資料包的源IP地址
destination:資料包的目標地址
4.1 清空所有的規則  
#iptables –flush
4.2 追加命令
iptables -A命令追加新規則,其中-A表示append,一般而言最後一條規則用於丟棄(drop)所有資料包,並且使用-A參數添加新規則,那麼就是無用的。
4.2.1 文法
iptables –A chain firewall-rule
 
 - -A chain 指定要追加的規則的鏈
- firewall-rule 具體規則的參數
4.2.2 基本參數
用於描述資料包的協議,源地址、目的地址、允許經過的網路介面,以及如何處理這些資料包。
 
 - 協議 –p (protocol) 
 如tcp,udp,icmp等,可以使用all來指定所有協議
 不指定-p參數,預設值是all,
 可以使用協議名(tcp,udp),或者協議值(6代表tcp),映射關係可以查看/etc/protocols
- 源地址 –s (source) 
 指定資料包的源地址,參數可以使用IP地址、網路地址、主機名稱,不指定-s參數,就是代表所有地址。
 例如:-s 192.168.1.101 具體的IP地址
 例如:-s 192.168.1.10/24 指定網路地址
 
- 目的地址 –d  (destination) 
 指定目的地址,參數和-s相同
- 執行目標 –j (jump to target) 
 -j代表了當與規則(Rule)匹配時如何處理資料包,可能的值是accept、drop、queue、return,還可以指定其他鏈(chain)作為目標
- 輸入介面 –i (input interface) 
 指定了要處理來自哪個介面的資料包,這些資料包進入input、forward、prepoute鏈,不指定將處理進入所有介面的資料包
 例如:-i eth0 指定了要處理eth0進入的資料
 可以取反  !-i eth0,指eth0以外。
 可以匹配  -i eth+ 指以eth開頭的
- 輸出介面 –o (out interface) 
 資料包有那個介面輸出,類似於 –i
- 源連接埠 –sport 
 例如 –sport 22
 例如 –sport 22:100指定連接埠範圍
- 目的連接埠 –dport 
 類似於-sport
- TCP標誌
- ICMP類型
5 執行個體分析
例如:接收目標連接埠為22的資料包
iptables –A INPUT –i etho –p tcp –dprot 22 –j ACCEPT
例如:拒絕所有其他資料包
iptables –A INPUT –j DROP
6 修改預設策略
上例僅對接收的資料包進行過濾,而對於要發出的資料包卻沒有任何限制。
使用iptables –L 
# iptables -LChain INPUT (policy ACCEPT)target     prot opt source               destinationACCEPT     tcp  --  anywhere             anywhere            tcp dpt:sshDROP       all  --  anywhere             anywhere            Chain FORWARD (policy ACCEPT)target     prot opt source               destination         Chain OUTPUT (policy ACCEPT)target     prot opt source               destination