iptable

iptable 組成
四張表
1 raw 對IP包做狀態跟蹤
2 mangle 對IP包打標記
3 nat 做地址轉換
4 filter(默認) 對IP包做過濾

五條鏈 匹配IP包傳輸方向
1 INPUT 進入防火牆本機的IP包
2 OUTPUT 從防火牆出去的IP包
3 FORWARD 經過防火牆的包
4 POSTROUTING 路由後處理
5 PREROUTING 路由前處理

格式
#iptables -t 表名 管理選項 鏈名 匹配條件 -j 處理動作

匹配即停止!沒匹配到走默認規則(accept)

管理選項
-L 列出所有規則
#iptables -t raw -L
#iptables -t mangle -L
#iptables -t nat -L
#iptables -t filter -nL --line-numbers
-nL 以數字顯示服務的端口 --line-numbers 加行號顯示

-F 清空所有規則 -X清除自定義鏈
#iptables -t filter  -F
#service  iptables save #保存規則
#cat /etc/sysconfig/iptables
#service  iptables restart
#cat /etc/sysconfig/iptables

-P 定義默認策略
#iptables  -t  filter  -P  INPUT  DROP

-A 添加規則
-I 插入規則
#iptables -t filter -A INPUT -p icmp -j REJECT  #拒絕ping
#iptables -t filter -I INPUT 1 -p icmp -s 192.168.4.10 -j ACCEPT    #接受ping #在第一條前添加

-D 刪除規則
#iptables -t filter -D INPUT 1  #刪除第一條

處理動作
ACCEPT(允許) DROP(丟棄) REJECT(拒絕)
SNAT(源地址轉換) DNAT(目標地址轉換) REDIRECT(端口轉換)

匹配條件
--dport 端口
-p 協議 --port
-s 源地址
-d 目標地址
#iptables -t filter -A INPUT -s 192.168.4.254 -p tcp --dport 22 -j ACCEPT
#iptables -t filter -A INPUT -p tcp --dport 80 -j DROP
#iptables -t filter -A INPUT -s 192.168.4.102 -p tcp --dport 22 -j REJECT

設置自己可以ping別人 但別人不能ping我
這裏主要是學習一定思路 如何應對一些陌生問題
#iptables -t filter -A INPUT -p icmp --help #遇到不會help啊~一頓找
#iptables -t filter -A INPUT -p icmp --icmp-type echo-request -j DROP


網絡型防火牆
內網------防火牆------外網
#iptables -t filter -F
#iptables -t filter -P FORWARD DROP
#iptables -t filter -nL
#iptables -t filter -A FORWARD -p tcp --dport 80 -j ACCEPT #僅允許進來訪問80端口請求包(目標端口80),但進來了無法回去
#iptables -t filter -A FORWARD -p tcp --sport 80 -j ACCEPT #允許迴應的包通過(源端口80)
#iptables -t filter -nL --line-numbers

#iptables -t filter -I FORWARD -s 192.168.4.50 -p tcp --dport 80 -j REJECT

#iptables -t filter -A FORWARD -p tcp --dport 22 -j ACCEPT  #允許目標端口是22的包通過
#iptables -t filter -A FORWARD -p tcp --sport 22 -j ACCEPT  #允許來源端口是22的包通過
#iptables -t filter -nL --line-numbers

#iptables -t filter -I FORWARD -s 192.168.4.0/24 -p tcp --dport 80 -j REJECT

iptables NAT表的使用 編寫防火牆腳本
SNAT源地址轉換 POSTROUTING---->私有網絡共享一個公有IP地址上網
DNAT目標地址轉換 PREROUTING---->發佈私有網絡內的服務器

#iptables -t nat -A POSTROUTING -o eth1 -s 192.168.4.0/24 -p tcp --dport 80 -j SNAT --to-source 192.168.2.253
##路由後處理 路由後修改 匹配 從eth1出去來源是4.0網段的 tcp/80 包 將源地址改爲192.168.2.253
(SNAT --to-source 192.168.2.253可以簡寫爲 MASQUERADE 自動識別)
#iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 1234 -j DNAT --to-destination 192.168.4.55:22
##路由前處理 匹配 從eth1進來的端口爲 tcp/1234的包 將目標地址改爲192.168.4.55:22 再做路由處理

INPUT 主要用於主機防火牆,設置規則屏蔽處理進入本機的數據包

禁止 192.168.4.15 這個機器訪問我本機的 web 服務
#iptables -t filter -A INPUT -s 192.168.4.15 -p tcp --dport 80 -j DROP

除了 192.168.4.12 這個機器 ,其他 192.168.4.0 網段的機器禁止 ping 我
#iptables -t filter -A INPUT ! -s 192.168.4.12 -j DROP

FORWARD 主要用於網絡防火牆,設置規則處理穿過本機的數據包
禁止 192.168.4.11 的機器穿過防火牆訪問後端的機器
#iptables -t filter -A FORWARD -s 192.168.4.11 -j DROP

nat 防火牆,需要打開內核 ip 轉發 (sysctl -w net.ipv4.ip_forward=1)

POSTROUTING 源地址僞裝
通過僞裝 192.168.4.0 網段的機器上網,首先防火牆本機可以訪問互聯網
#iptables -t nat -A POSTROUTING -s 192.168.4.0 -j SNAT --to-source 防火牆外網ip地址

PERROUTING 目的地址轉換
所有訪問防火牆 10022 端口的請求都轉發給後端的 192.168.4.15 的 22 端口
#iptables -t nat -A PREROUTING -p tcp --dport 10022 -j DNAT --to-destination 192.168.4.15:22

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章