天天看點

iptables 包過濾防火牆

防火牆的結構

dmz(非軍事區):允許外網随時通路

linux的防火牆:

ipfawdn ---> ipchains ---> iptables

iptables

7.0以前預設開啟的是iptables

7.0以後預設開啟的是firewall

linux中防火牆都屬于包過濾防火牆,對經過的資料包中的ip位址、協定、port、标記位進行稽核

實驗拓撲圖:用戶端a:202.0.0.1 ------ re0:202.0.0.2;re1:192.168.10.1 ------- b:192.168.10.2

實驗:

1、安裝軟體包

iptables.xxxx.xxxx.rpm

iptables-services.xxxx.xxxx.rpm

2、啟動服務

systemctl mask firewalld

systemctl unmask iptables

systemctl restart iptables

systemctl enable iptables

iptables通過規則集實作保護功能,規則集稱為鍊,每個接口都可以設定這三種鍊:

input鍊:通信目标為本裝置時,稽核相應接口的input鍊      a ping r

forward鍊:路由器實作轉發功能,稽核發送方近口的forward鍊

output鍊:當通信發起方為本裝置時,稽核相應接口的output鍊  r ping  a

内網 防火牆限制 

【限制ip】

iptables -l 檢視所有鍊的規則集

iptables -f 清空所有鍊的規則集

iptables -p input drop(拒絕) 修改鍊的預設規則(policy)   accept(接受)

按添加規則順序,進行判斷通信是否可行 (稽核規則按照順序稽核)

制定規則指定網卡 

-a 制定規則鍊

-i 指定插入位置   

-d 指定删除位置

-i 指定網卡 (eno16777736)

-j 指定操作規則 (drop拒絕)(accept接受)

-s 指定發送方ip位址

-d 指定接受方ip位址 

制定規則指定網卡   -i 指定插入位置  -a 制定規則鍊 -d 指定删除位置   -i 指定網卡   -j 指定操作  -d 指定接受方ip位址  

iptables -i forward 1 -i eno16777736 -s 202.0.0.1 -d 192.168.10.2 -j accept 在指定位置插入規則

iptables -d forward 3    删除指定位置的規則

telnet ip  遠端連接配接控制

【協定和端口】  

-p 指定協定  tcp  udp  icmp  拒絕所有外網的tcp通信

--dport 指定目的端口

--sport 指定發送端口

iptables -a forward -i eno16777736 -p tcp -j drop                 拒絕所有tcp通信

iptables -a forward -i eno16777736 -p tcp --dport 23 -j drop       隻拒絕23号端口

iptables -a forward -i eno16777736 -p tcp -d 192.168.10.2 --dport 23 -j drop   隻拒絕10.2上的23号端口

iptables -a forward -i eno16777736 -p udp --sport 53 -j drop       使用戶端無法獲得dns解析

設定a到b不可以ping通   b到a通 

【标記位】icmp

拒絕外網所有的主動連接配接,但是外網的回複資訊能收到

c ---> s syn     請求

s ---> c syn,ack 回複

c ---> s ack  回複

iptables -a forward -i eno16777736 -p tcp --syn -j drop

【icmp:内置指令ping、tracert】請求(requset 8)  回複(relay 0)

iptables -a forward -i eno16777736 -p icmp --icmp-type 8 -j drop

内網通路外網    開啟 <nat網絡位址轉換(内訪外)postrouting鍊>

iptables -t nat -a postrouting -o eno16777736 -s 192.168.1.197 -j snat --to-source 36.111.136.8

-t nat 指定規則

-o 指定外網卡

--to-source 外網卡ip

iptables -l -t nat 檢視nat鍊規則

iptables -f -t nat 清空nat鍊規則

實驗驗證:

用戶端和伺服器端 netstat -n  檢視通信端口

外網通路内網    開啟 <端口映射(外訪内)prerouting>

iptables -t nat -a prerouting -i eno16777736 -d 202.0.0.2 -p tcp --dport 2300 -j dnat --to-destination 192.168.10.2:23

内網上:telent 202.0.0.2 2300

        netstat -n

iptables -t nat -vnl 檢視

iptables -t nat -d prerouting 1 删除

上述指令重新開機iptables服務将失效,永久生效将指令寫入配置檔案

vim /etc/rc.d/rc.local

iptables -f

iptables -f -t nat

書寫相應的防火牆規則

儲存退出

給該檔案增加執行權限

chmod a+x /etc/rc.d/rc.local

下一篇: NSF共享服務

繼續閱讀