1. 程式人生 > >iptables簡單總結

iptables簡單總結

netfilter/iptables(簡稱為iptables)組成Linux平臺下的包過濾防火牆,與大多數的Linux軟體一樣,這個包過濾防火牆是免費的,它可以代替昂貴的商業防火牆解決方案,完成封包過濾、封包重定向和網路地址轉換(NAT)等功能。

防火牆
iptables
firewalld


核心態
netfilter
使用者態
iptables
firewalld
過濾防火牆

預設策略:拒絕所有資料包從入口方向進入

安全和效能永遠成反比
selinux:給檔案打標籤 ls -Z :檢視selinux安全上下文資訊
pv 併發量


centos:
yum install iptables iptables-services iptables-devel -y
192.168.124.150 iptables-client
192.168.124.234 iptables-server
iptables -L:檢視防火強規則
iptables -F:清空防火強規則
其他清空:
iptables -Z
iptables -X
語法:
# iptables -t 表名 動作 鏈名 匹配條件 -j 目標動作
四表五鏈
-t 表名 //預設不指定的情況下是filter表
表:
raw 資料包跟蹤
mangle 標記資料包
nat 網路地址轉換
filter 資料包過濾
讀表順序 --->上面順序從上到下

動作: #iptables --help
iptables -t filter -A 追加
例子:
# iptables -t filter -A INPUT -p icmp -j REJECT 拒絕ping返回訊息
# iptables -nL --line-numbers -t nat -v
-L 列出規則
-n 以數字形式顯示
--line-numbers 顯示規則行號 用 --line
-t nat 可以跟不同的表
-v verbose 顯示詳細資訊 pkts:處理包的所有數目 bytes:包的大小
-vv 顯示更詳細
# iptables -nL INPUT 1 -v | awk '{print$2}'
1848
# iptables -Z
-Z 清零規則計數 可跟表跟鏈
修改規則: -R
# iptables -R INPUT 1 -p tcp -j REJECT
刪除規則: -D
# iptables -D INPUT 1
插入規則: -I inster
# iptables -I INPUT -p tcp --dport 23 -j DROP
# iptables -I INPUT 2 -p tcp --dport 23 -j DROP


鏈 時間點 位置點
PREROUTING 路由之前
INPUT 資料包流入
FORWARD 資料包經過
OUTPUT 資料包流出
POSTROUTING 路由之後
可新增自定義鏈: suibian 保留規則 如果不主動呼叫不會生效

raw:
PREROUTING 路由之前
OUTPUT 資料包流出
mangle:
PREROUTING 路由之前
INPUT 資料包流入
FORWARD 資料包經過
OUTPUT 資料包流出
POSTROUTING 路由之後
nat:
PREROUTING 路由之前
INPUT 資料包流入
OUTPUT 資料包流出
POSTROUTING 路由之後
filter:
INPUT 資料包流入
FORWARD 資料包經過
OUTPUT 資料包流出
file://C:\Users\16682\AppData\Local\Temp\ct_tmp/1.png


file://C:\Users\16682\AppData\Local\Temp\ct_tmp/2.png
資料包流向:
三種情況:
1.資料包從A網路流向防火牆本地
2.資料包從防火牆本地流向B網路
3.資料包從A網路流向B網路

路由 NAT 網路地址轉換
snat 原地址轉換
dnat 目標地址轉換

建立自定義鏈:
# iptables -N wing
新增規則到自定義鏈:
#iptables -A wing -p icmp -j DROP
關聯自定義鏈:
#iptables -A INPUT -j wing
修改自定義鏈名稱:
# iptables -E wing WING
刪除自定義鏈: 不能有關聯不能有規則
# iptables -F INPUT
# iptables -F WING
# iptables -X WING

企業環境:
我們內網 網際網路
高安全區域 防火牆 低安全區域
drop:拒絕所有資料包從入口方向進入,只放行你想接受的包
修改預設策略: drop/accept
# iptables -P INPUT DROP
檢視規則: -S 列印輸入鏈規則



#iptables -A INPUT -p tcp --dport 80 -j ACCEPT destination 目的地
#iptables -A INPUT -p tcp --sport 80 -j ACCEPT source源

匹配條件:
協議:
-p: tcp udp icmp
tcp:可靠的傳輸協議
udp:不可靠的傳輸協議
埠:必須和協議一起寫
--dport:目標埠 埠範圍:目標埠1:目標埠2
--sport:源埠 多埠匹配:#iptables -A INPUT -p tcp -m multiport --dports 23,80 -j DROP
IP:
-s 源ip,源ip2
-d 目標ip
ip地址範圍:
-m iprange --src-range 192.168.124.2-192.168.124.54 源 source
-m iprange --dst-range 192.168.124.2-192.168.124.54 目標 destination

MAC:
-m mac --mac-source
#iptables -A INPUT -p tcp -m mac --mac-source 52:54:00:0e:18:18 -j drop
目標動作:
drop 丟棄
reject 拒絕
accept 接受
log 記錄日誌 messages裡面 指定日誌
snat 修改源ip地址
masquerade
#iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j MASQUERADE
dnat 修改目標ip地址
(協議頭)資料頭+資料
源ip 源mac 目標ip 目標mac
私有ip和公有ip不能直接通訊
192.168.1.1-192.168.1.254------>公有202.106.18.8----->公網伺服器
#iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to 202.106.18.8
修改目標ip:
echo 1 >/proc/sys/net/ipv4/ip_forward

#iptables -t nat -A PREROUTING -s 202.106.18.8 -j SNAT --to 192.168.1.8
#iptables -t nat -A PREROUTING -p tcp -m tcp --dport 10022 -j DNAT --to-destination 192.168.10.11:22
mangle:
設定標記:#iptables -t mangle -A PREROUTING -s 192.168.1.8 -p tcp --dport 80 --set-mark
#iptables -A OUTPUT -m mark --mark 50 -j drop