解壓密碼6Hai7Gf8
路由轉發
0是不轉發,1是轉發
[root@m01 roles]# cat /proc/sys/net/ipv4/ip_forward
0
臨時生效
[root@m01 roles]# echo "1" > /proc/sys/net/ipv4/ip_forward
從配置文件讀取配置,臨時生效
sysctl -p
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf
sysctl -p
路由條目解釋
有2個路由到20和10網段的路由
-
Destination
-
Gateway
-
Genmask
-
Flags 經過幾個路由器
-
Ref 路由索引
-
Use 這個路由使用多少次
-
Iface 連接的網卡接口
[root@route ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
添加路由臨時生效
route -add -net 192.168.80.0 netmask 255.255.255.0 gw 192.168.20.20
route del -net 192.168.80.0 netmask 255.255.255.0
永久生效
vim /etc/sysconfig/network-scripts/route-eth0
ADDERSS0=192.168.80.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.20
systemctl restart network
iptables
韓立剛環境准備
虛擬機網絡設置
內網配置ip
[root@reoute-nei ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.123
NETMASK=255.255.255.0
GATEWAY=192.168.10.10
有以下幾條路由條目
[root@reoute-nei ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.10.10 0.0.0.0 UG 0 0 0 eth0
route1配置ip地址
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.10.10
NETMASK=255.255.255.0
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.20.10
NETMASK=255.255.255.0
添加路由
[root@route ~]# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=192.168.80.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.20
開啟路由轉發
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf
sysctl -p
查看路由條目
[root@route ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.80.0 192.168.20.20 255.255.255.0 UG 0 0 0 eth1
192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
route2配置ip
[root@route2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth[10]
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.80.10
DEVICE=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.20.20
添加路由
[root@route2 ~]# cat /etc/sysconfig/network-scripts/route-eth1
ADDRESS0=192.168.10.0
NETMASK0=255.255.255.0
GATEWAY0=192.168.20.10
查看路由
[root@route2 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.20.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
192.168.10.0 192.168.20.10 255.255.255.0 UG 0 0 0 eth1
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth1
開啟內核路由轉發
sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf
sysctl -p
外網配置ip
[root@route-wai ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.80.123
GATEWAY=192.168.80.10
NDS=223.5.5.5
NETMASK=255.255.255.0
查看路由
[root@route-wai ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.80.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.80.10 0.0.0.0 UG 0 0 0 eth0
數據包通過網卡進入到應用層,要經過 prerouting loca_in
從linux系統出去要經過, local_out,post_routing
轉發數據包要經過prerouting forward postrouting
數據包經過這些鈎子干嘛呢?鈎子函數會檢查filter,nat,mangle表,安裝表里面的規則進行過濾
iptalbes 查看規則
刪除reject規則
臨時生效
iptalbes -D FORWARD 1
永久生效
/etc/rc.d/init.d/iptables save
iptables 命令格式
格式
iptables -t table 命令 chian rules -j target
table
可以是filter nat mangle nat 默認是filter
命令
-P或--policy 用來定義默認策略,就是允許通不通過
-A或 --append 在規則列表的最后增加一條規則
-I 或insert 在指定的位置插入一條規則,不指定位置就是在最上面插入
-D 或 --delete 刪除一個規則
-R或 --replace修改某條規則
-F或 --flush 刪除表中的所有規則
舉例
iptables -t filter -P FORWARD ACCEPT 設置filter表ACCEPT鏈默認接受數據包
iptables -t filter -I INPUT -p icmp -j DROP 在INPUT首插入1條規則
iptables -I INPUT 2 -p icmp -j DROP 在INPUT鏈第二行插入1條規則
iptables -D INPUT 2 刪除INPUT鏈的第二條規則
iptables -R INPUT 2 -p icmp -j DROP 修改INPUT第二條規則,改成DROP
iptables -F INPUT 清除INPUT鏈中的所有規則
iptables -F 不指定鏈是清除所有
iptables 數據包匹配選項
舉例
禁止外網ping route 路由器,但是能ping通內網,所以,設置 filter參數,只是包過濾,
[root@route ~]# iptables -t filter -I INPUT -p icmp -i eth1 -j DROP
之后
wai網 ping eth1網卡就不通了
但是不影響外網ping10.123的機器
禁止 80.123這個機器ssh連接 10.123這台機器
為什么要用FORWARD 因為80.123通過route 連接10.123 對與FORWARD來說.route負責的是轉發
iptables -t filter -I FORWARD -p tcp -s 192.168.80.123/32 -d 192.168.10.123/32 --dport 22 -j DROP
之后 SSH 連接不上了
再次添加 192.168.80.0網段對 192.168.10.0網段有所有權限
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.80.0/24 -d 192.168.10.0/24 -j ACCEPT
又能連接了,因為比較寬泛的規則在FORWARD最上面,匹配到了
使用擴展選項實現單方向ping通
-m icmp --icmp-type
web服務器不主動和外面建立會話
iptables -t filter -I output -m state --state NEW -j DROP
示例
在linux作為路由器上設置規則
禁止192.168.10.0/24 ping通192.168.80.0/24
允許192.168.80.0/24 ping通192.168.10.0/24
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -d 192.168.80.0/24
-p icmp -m icmp --icmp-type echo-request -j DROP
iptables -t 表 -I鏈 -s 誰 到誰哪里 -p 協議 (類型是 -m icmp --icmp--type echo -request) -j 動作
結果:
內網ping 不通外網
外網ping的通neiwang
查看iptables表
在linux住機上設置規則 :linux主機可用ping別人,別人不能ping我
示例
[root@reoute-nei ~]# iptables -t filter -I INPUT -p icmp -m icmp --icmp-type echo-re
quest -j DROP
linux ping別人通
別人ping linxu不通
-m multiport
指定多端口號:
-m multiport
--sport 源端口
--port 目標端口
--ports 源和目標一起指
示例:
禁止192.168.80.0/24 訪問 192.168.80.0/24 1-----1024端口
iptables -t filter -I FORWARD -p tcp -d 192.168.10.0/24 -s 192.168.80.0/24 -m multiport --dports 1:1024 -j DROP
指定IP段
-m iprange
--src-range ip-ip
--src-range ip-ip
示例:
禁止192.168.80.0/24地址段 訪問 192.168.80.0/24
[root@route ~]# iptables -t filter -I FORWARD -m iprange --src-range 192.168.80.1-192.168.80.140 -j DROP
不能連接不能ping
-m connlimit 連接限制
--comlimit-above 限定大連接個數
-m limit --limit 限速
處理動作
REJECT 會顯示目標端口不可達
路由器上設置
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -p icmp -j REJECT
內網顯示目標端口不可達
路由器再在REJECT前面添加DROP動作
內外在次ping 顯示超時
禁止windows遠程桌面連接. 抓包顯示
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.0/24 -p tcp --dport 3380 -j REJECT
log 記錄日志
舉例 內網禁止ssh連接 把日志利用log記錄日志
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.123/24 -p tcp --dport 22 -j DROP
[root@route ~]# iptables -t filter -I FORWARD -s 192.168.10.123/24 -p tcp --dport 22 -j LOG --log-prefix "iptables-ssh"
先寫禁止ssh連接,在寫寄到日志,保證條目里日志在上面
在/var/log/messages里查看,就有iptables-ssh的日志
設置禁止ping並寫到日志
iptables -t filter -I INPUT -p icmp -j LOG --log-prefix "iptables-icmp"
老王iptables
iptables的組成
什么是Raw:iptables連接跟蹤
數據包過濾匹配流程
數據包經過3個流向:
數據包通過網卡進入到應用層,要經過 prerouting loca_in
從linux系統出去要經過, local_out,post_routing
轉發數據包穿過電腦(把電腦當作路由器)要經過prerouting forward postrouting
數據包經過這些鈎子干嘛呢?鈎子函數會檢查filter,nat,mangle表,安裝表里面的規則進行過濾
iptabels語法
table
可以是filter表 nat表 mangle表 nat表 默認是filter表
COMMAND
鏈管理:
-N: new 自定義1條新的規則鏈
-X:delete 刪除自定義的空的規則鏈
-P : policy 設置默認策略,對filter表中的鏈而言,默認策略有
ACCEPT: 接受
DROP: 丟棄
REJECT: 拒絕
規則管理
-A或 --append 在規則列表的最后增加一條規則
-I 或insert 在指定的位置插入一條規則,不指定位置就是在最上面插入
-D 或 --delete 刪除一個規則 -D input 2 或指名規則本身
-R或 --replace替換某條規則 -R INPUT 3
-F或 --flush 刪除表中的所有規則
-Z:置零,iptables有2個計數器
查看語法:
- -L :list 列出指定鏈的所有規則,此項必須寫在最后
- -n: number 以數字方式顯示地址和端口號
- -x: exactly 顯示計數器結果的精確值,而不上單位轉換后的易讀值
命令演示
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 10202 packets, 1085K bytes)
pkts bytes target prot opt in out source destination
2630 259K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
--line-numbers 顯示行號
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 10212 packets, 1086K bytes)
num pkts bytes target prot opt in out source destination
1 2669 262K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
iptables -s 顯示命令.可以復制下來放到腳本里
iptables -D INPUT 2 #刪除INPUT2鏈的第二條規則
-R: 替換
清空表
iptables -F -t nat
清空計數器
- Z
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 3 packets, 285 bytes)
num pkts bytes target prot opt in out source destination
1 3524 330K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
清空INPUT的計數器
[root@web02 ~]#iptables -Z INPUT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 2 packets, 168 bytes)
num pkts bytes target prot opt in out source destination
1 6 396 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
! 取反
iptables - I INPUT ! -s 192.168.2.103 - j DROP
除2.103地址以外的全都拒絕
查看支持什么表支持的鏈
[root@web02 ~]#iptables -vnL --line-numbers -t raw| filter | raw | nat
鏈管理:
-N: new 自定義1條新的規則鏈
-X:delete 刪除自定義的空的規則鏈
-P : policy 設置默認策略,對filter表中的鏈而言,默認策略有
ACCEPT: 接受
DROP: 丟棄
REJECT: 拒絕
iptables的模塊
大寫的是動作.小寫的是模塊,支持很多
[root@web02 ~]#rpm -ql iptables
/etc/sysconfig/ip6tables-config
/etc/sysconfig/iptables-config
/usr/lib64/xtables/libip6t_REJECT.so
/usr/lib64/xtables/libip6t_SNAT.so
/usr/lib64/xtables/libip6t_hbh.so
/usr/lib64/xtables/libip6t_hl.so
/usr/lib64/xtables/libip6t_icmp6.so
查看內核編譯配置文件iptables以模塊方式編譯進內核
[root@web02 ~]#grep -i iptables /boot/config-3.10.0-862.el7.x86_64
CONFIG_IP_NF_IPTABLES=m
CONFIG_IP6_NF_IPTABLES=m
# iptables trigger is under
鏈管理:
-N: new 自定義1條新的規則鏈
-X:delete 刪除自定義的空的規則鏈
-P : policy 設置默認策略,對filter表中的鏈而言,默認策略有
ACCEPT: 接受
DROP: 丟棄
REJECT: 拒絕
防火牆默認規則
-P 修改默認規則
修改為DOOP
查看默認規則
[root@web02 ~]#iptables -vnL
Chain INPUT (policy ACCEPT 220 packets, 19696 bytes) 默認可以進入
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) 默認可以轉發
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 91 packets, 8412 bytes) 默認可以出去
pkts bytes target prot opt in out source destination
設置windows本機能連接linux 192.168.2.103
[root@web02 ~]#iptables -vnL
除了192.168.2.103 其他都不能連接我
[root@web02 ~]#iptables -A INPUT -s 192.168.2.103 -j ACCEPT 源地址2.103可以連接
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.103 -j ACCEPT 目標2.103可以連接
查看添加了規則
[root@web02 ~]#iptables -vnL
Chain INPUT (policy ACCEPT 2 packets, 168 bytes)
pkts bytes target prot opt in out source destination
62 4768 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2 packets, 168 bytes)
pkts bytes target prot opt in out source destination
6 840 ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
改默認規則,至此,除了2.103可以連接機器,其他任何人都不能連接機器了
[root@web02 ~]#iptables -P INPUT DROP #改INPUT的默認規則
[root@web02 ~]#iptables -P OUTPUT DROP #改INPUT的默認規則
查看 policy drop 是拒絕的意思,下面的規則就是白名單,只有192.168.2.103 的可以連接
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 148 packets, 13224 bytes) # 除了192.168.2.103其他都拒絕
pkts bytes target prot opt in out source destination
195 13576 ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
pkts bytes target prot opt in out source destination
81 9528 ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
iptables 匹配條件
基本匹配條件
允許192.168.2.7 訪問 -A OUTPUT INPUT 保證可進可出
[root@web02 ~]#iptables -A INPUT -d 192.168.2.7 -j ACCEPT 進
[root@web02 ~]#iptables -A OUTPUT -s 192.168.2.7 -j ACCEPT 出
查看 有數據交互了
[root@web02 ~]#tcpdump -i eth0 -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
00:41:58.482577 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2525, seq 174, length 64
00:41:58.482630 IP 192.168.2.8 > 192.168.2.7: ICMP echo reply, id 2525, seq 174, length 64
00:41:59.482914 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2525, seq 175, length 64
imcp協議應用
允許192.168.2.7 只可以ping
[root@web02 ~]#iptables -A INPUT -s 192.168.2.7 -p icmp -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.7 -p icmp -j ACCEPT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 3 packets, 351 bytes)
pkts bytes target prot opt in out source destination
1847 719K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
26 2184 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
1204 133K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
16 1344 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
--icmp-type匹配條件細分
實驗 允許ping但是不回可以分別定義
0是回應,8是請求
允許192.168.2.7ping我
分析: 192.168.2.7要ping我要進入網卡,請求,所以 icmp-type要寫8,因為是別人請求我,
收到包后,我還要同意請求,所以output 要寫0,回應對方以下,現在我們互通了
[root@web02 ~]#iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -p icmp --icmp-type 0 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 11 packets, 1287 bytes)
num pkts bytes target prot opt in out source destination
1 12276 1263K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 364 30576 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 16264 3581K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 57 4788 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
測試,數據有去有回
[root@web02 ~]#tcpdump -i eth0 -nn icmp
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), capture size 262144 bytes
03:37:28.997305 IP 192.168.2.7 > 192.168.2.8: ICMP echo request, id 2803, seq 304, length 64
03:37:28.997353 IP 192.168.2.8 > 192.168.2.7: ICMP echo reply, id 2803, seq 304, length 64
192.168.2.8可以ping別人
分析: 192.168.2.8 要ping別人,是請求別人,所以OUTPUT icmp-type 8,請求別人和我連接,
我的包發過去了,別人也給我回應了,還沒完成,我要給別人回應,所以INPUT imcp-type 0
[root@web02 ~]#iptables -A OUTPUT -p icmp --icmp-type 8 -j ACCEPT
[root@web02 ~]#iptables -A INTPUT -p icmp --icmp-type 0 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 13 packets, 1521 bytes)
num pkts bytes target prot opt in out source destination
1 12740 1297K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 684 57456 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
3 1 84 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
num pkts bytes target prot opt in out source destination
1 16574 3612K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 377 31668 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 0
3 3 252 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 icmptype 8
測試有去有回
[root@web02 ~]#ping 192.168.2.7
PING 192.168.2.7 (192.168.2.7) 56(84) bytes of data.
64 bytes from 192.168.2.7: icmp_seq=1 ttl=64 time=0.228 ms
--- 192.168.2.7 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.228/0.228/0.228/0.000 ms
擴展匹配條件
隱式和顯示
隱式擴展:
--dport --sport 指定端口
允許192.168.2.7ssh連接 web02
分析: web02 作為服務器,.2.7連接我是源地址 -s 是2.7的IP .目標端口是我,所以是--dport 22
OUTPUT出去的時候是從22端口回去是源地址,所以是--sport22 , 目標的ip是2.7 所以是 -d
打個比方,比如回去的端口是100,要寫 --sport100
[root@web02 ~]#iptables -A INPUT -s 192.168.2.7 -p tcp --dport 22 -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.7 -p tcp --sport 22 -j ACCEPT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 57 packets, 6099 bytes)
pkts bytes target prot opt in out source destination
9668 1078K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
78 8874 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 16 packets, 1040 bytes)
pkts bytes target prot opt in out source destination
14689 3346K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
134 11256 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
55 8126 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:22
允許192.168.2.7訪問http
道理和上面的22端口一樣,
[root@web02 wordpress]#iptables -A INPUT -s 192.168.2.7 -p tcp --dport 80 -j ACCEPT
[root@web02 wordpress]#iptables -A OUTPUT -d 192.168.2.7 -p tcp --sport 80 -j ACCEPT
[root@web02 wordpress]#iptables -vnL
Chain INPUT (policy DROP 5 packets, 585 bytes)
pkts bytes target prot opt in out source destination
10599 1143K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
29 1848 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
15234 3424K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
134 11256 ACCEPT icmp -- * * 0.0.0.0/0 192.168.2.7
20 2068 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:80
192.168.2.7測試訪問成功
[root@web01 packages]# curl 192.168.2.8
fdasfdsafdasf
--connect-timeout 1|2|3|.... 快速cur 1秒...不通立刻顯示結果
[root@web01 packages]# curl --connect-timeout 1 192.168.2.8
--tcp-falags
根據tcp的標記位,判斷請求或者響應的握手,是第幾次握手
用--tcp-flags要了解三次握手,四次揮手的標記位.我們復習一下,
A發的時候ack B回復ack+syn A回復ack
分手的時候多了個fin
還有 3種,
緊急標記位
psh如果=1數據就不會放到緩存,直接傳到應用程序里
reset,發生故障,斷開重連
格式寫法:
實驗 針對新發起的請求,拒絕(也就是拒絕第一次握手),舊的請求,不拒絕
[root@web01 packages]# ssh 192.168.2.8
root@192.168.2.8's password:
Last login: Tue Jan 22 01:19:16 2019 from 192.168.2.7
[root@web02 ~]#
--syn 第一次握手
[root@web02 ~]#iptables -I INPUT 2 -s 192.168.2.7 -p tcp --syn -j REJECT
[root@web02 wordpress]#iptables -vnL
Chain INPUT (policy DROP 10 packets, 1170 bytes)
pkts bytes target prot opt in out source destination
10982 1172K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 120 REJECT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp flags:0x17/0x02 reject-with icmp-port-unreachable
144 12096 ACCEPT icmp -- * * 192.168.2.7 0.0.0.0/0
331 28595 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
68 4384 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
測試
連接不上.卡哪里了
[root@web01 packages]# curl 192.168.2.8/index.html
實驗:新連接可以訪問80.但是不能訪問其他的任何端口
[root@web02 ~] iptables -I INPUT 2 -s 192.168.2.7 -p tcp --dport 80 --syn -j ACCEPT
[root@web02 ~] iptables -I INPUT 3 -s 192.168.2.7 -p tcp --syn -j REJECT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 172 packets, 15636 bytes)
num pkts bytes target prot opt in out source destination
1 11836 1232K ACCEPT all -- * * 192.168.2.103 0.0.0.0/0
2 1 60 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80 flags:0x17/0x02
3 20 1200 REJECT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp flags:0x17/0x02 reject-with icmp-port-unreachable
4 35 4557 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:22
5 32 2111 ACCEPT tcp -- * * 192.168.2.7 0.0.0.0/0 tcp dpt:80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
num pkts bytes target prot opt in out source destination
1 15989 3550K ACCEPT all -- * * 0.0.0.0/0 192.168.2.103
2 28 4701 ACCEPT tcp -- * * 0.0.0.0/0 192.168. 2.7 tcp spt:22
3 23 2232 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp spt:80
測試,訪問80成功, 隨便個ping命令失敗
[root@web01 packages]# curl 192.168.2.8/index.html
fdasfdsafdasf
[root@web01 packages]# ping 192.168.2.8
PING 192.168.2.8 (192.168.2.8) 56(84) bytes of data.
顯示擴展需要用-m指定模塊名
顯示擴展要認為的指定調用的模塊名:怎么查看模塊名呢
centos7命令幫助man iptables-extensions
[root@web02 ~]#rpm -ql iptables
muliport模塊
以離散方式定義多個端口.最多15個端口
-m multiport --sports 指定源端口
-m multiprot --dports 指定目標端口
--port:不分源目標端口
意思是: 進入本機的20,23,443,80都可以訪問,
[root@web02 ~]#iptables -A OUTPUT -p tcp -m multiport --sports 20:23,443,80 -j ACCEPT
[root@web02 ~]#iptables -A INPUTP -p tcp -m multiport --dports 20:23,443,80 -j ACCEPT
[root@web02 ~]#iptables -nvL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 4978 362K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 20:23,443,80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 2978 349K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport sports 20:23,443,80
iprange定義IP地址范圍
-m iprange --src-range 源ip
-m iprange --dst-range 目標ip
只允許,2.1- 2.100這個網段的機器的20...80等端口連接我
[root@web02 ~]#iptables -A INPUT -p tcp -m iprange --src-range 192.168.2.1-192.168.2.100 -m multiport --dports 20:23,443,80 -j ACCEPT
#注意: INPUT進來的是目標地址,源地址是1-100 所以是--src
--dport 指定應該是訪問web02所有,web02 是目標端口
[root@web02 ~]#iptables -A OUTPUT -p tcp -m iprange --dst-range 192.168.2.1-192.168.2.100 -m multiport --sports 20:23,443,80 -j ACCEPT
#注意: OUTPUT ,出去的是源端口,所以是--sport src
[root@web02 ~]#iptables -vnL -line-numbers
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 8302 622K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 74 9344 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 source IP range 192.168.2.1-192.168.2.100 multiport dports 20:23,443,80
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 5484 643K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 destination IP range 192.168.2.1-192.168.2.100 multiport dports 20:23,443,80
-m mac --mac-source 指定mac地址 - s 指定IP地址,
實現只有指定的IP地址對應的唯一的mac才可以訪問本機,相同的IP地址都訪問不了
[root@web02 ~]#iptables -A INPUT -s 192.168.2.17 -m mac --mac-source 00:0c:29:71:8c:5b -j ACCEPT
[root@web02 ~]#iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 21 packets, 2457 bytes)
num pkts bytes target prot opt in out source destination
1 10621 795K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 13 2741 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0 MAC 00:0C:29:71:8C:5B
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 7 packets, 464 bytes)
num pkts bytes target prot opt in out source destination
1 7068 810K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 12 2509 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
測試:
其他機器修改了ip地址和2.8不通,只有修改了IP地址和mac地址才行
[root@web01 ~]#tail /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0
DEVICE=eth0
IPADDR=192.168.2.17
DNS1=192.168.2.1
MACADDR=00:0C:29:71:8C:5B
[root@web01 ~]#ping 192.168.2.8
PING 192.168.2.8 (192.168.2.8) 56(84) bytes of data.
64 bytes from 192.168.2.8: icmp_seq=1 ttl=64 time=0.291 ms
--string 過濾應用層數據做字符串模式匹配,的關鍵字,無效,
iptables -A INPUT -s 192.168.2.17 -j ACCEPT
iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 9 packets, 1053 bytes)
num pkts bytes target prot opt in out source destination
1 6629 522K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 147 9606 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 240 bytes)
num pkts bytes target prot opt in out source destination
1 3706 554K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 105 11064 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
[root@web02 wordpress]#iptables -I OUTPUT 2 -p tcp -m string --algo bm --string "google" -j REJECT
[root@web02 wordpress]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 6912 545K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 147 9606 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 3927 576K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable3
3 105 11064 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
[root@web02 ~]#echo "welcome to google" >> /code/wordpress/index2.html
curl還顯示google有問題
[root@web03 ~]#curl 192.168.2.8/index2.html
安裝httpd 測試
這個過濾google成功
[192.168.2.7-root@web01~]#cat index.html
fdafdsa
[192.168.2.7-root@web01~]#cat test.html
-s 本機是服務端,響應別人,所以源地址是本機的.2.7 -d是別人, -s 指定本機的端口
[192.168.2.7-root@web01~]#iptables -A OUTPUT -s 192.168.2.7 -d 192.168.2.17 -p tcp --sport 80 -m string --algo bm --string "google" -j REJECT
[192.168.2.7-root@web01~]#iptables -nvL
Chain INPUT (policy ACCEPT 415 packets, 57169 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 190 packets, 35603 bytes)
pkts bytes target prot opt in out source destination
69 21429 REJECT tcp -- * * 192.168.2.7 192.168.2.17 tcp spt:80 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unrea
chable
白名單--tring成功
[192.168.2.7-root@web01~]# iptables -A INPUT -s 192.168.2.105 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -A OUTPUT -d 192.168.2.105 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -P INPUT DROP
[192.168.2.7-root@web01~]#iptables -P OUTPUT DROP
[192.168.2.7-root@web01~]#iptables -A INPUT -s 192.168.2.17 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -A OUTPUT -d 192.168.2.17 -j ACCEPT
[192.168.2.7-root@web01~]#iptables -I OUTPUT 2 -p tcp -m string --algo bm --string "google" -j REJECT添加的規則
[192.168.2.7-root@web01~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 109 packets, 12753 bytes)
num pkts bytes target prot opt in out source destination
1 717 48746 ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 65 4068 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 134 packets, 40388 bytes)
num pkts bytes target prot opt in out source destination
1 382 73328 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
2 116 36060 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
3 27 2542 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
測試過濾成功
[192.168.2.17-root@web01~]#curl 192.168.2.7/index.html
fdafdsa
[192.168.2.17-root@web01~]#curl 192.168.2.7/test.html
time
在--string 過濾google基礎上增加--time
[192.168.2.7-root@web01~]#iptables -I INPUT 2 -p tcp --dport 80 -m time --timestart 9:00 --timestop 22:00 --weekdays 1,3,5 -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy DROP 4 packets, 468 bytes)
pkts bytes target prot opt in out source destination
599 44244 ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 120 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 TIME from 09:00:00 to 22:00:00 on Mon,Wed,Fri UTC reject-with icmp-port-
unreachable 44 4724 ACCEPT all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 4 packets, 284 bytes)
pkts bytes target prot opt in out source destination
408 45080 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
50 15558 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 STRING match "google" ALGO name bm TO 65535 reject-with icmp-port-unreachable
31 4483 ACCEPT all -- * * 0.0.0.0/0 192.168.2.17
time 根據時間進行檢測
--datestart YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
年 月 日 小時 分 秒
--datestop YYYY[-MM[-DD[Thh[:mm[:ss]]]]]
--timestart hh:mm[:ss]
--timestop hh:mm[:ss]
--kerneltz使用內核上的時區,而不是UTC時區
2.0網段每周1,3,5 9點到22點禁止訪問2.7 80端口
[192.168.2.7-root@web01~]#iptables -A INPUT -s 192.168.2.0/24 -d 192.168.2.7 -p tcp --dport 80 -m time --timestart 9:00 --timestop 22:00 --weekdays 1,3,5 --kerneltz -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 8 packets, 630 bytes)
pkts bytes target prot opt in out source destination
1 60 REJECT tcp -- * * 192.168.2.0/24 192.168.2.7 tcp dpt:80 TIME from 09:00:00 to 22:00:00 on Mon,Wed,Fri reject-with icmp-port-unreachable
connlimit擴展
根據每個客戶端ip做並發來連接數量匹配
--connlimit-upto n: 連接數量小於n時匹配
--connlimit-above n: 連接數量小於n時匹配
其他客戶端最多只能用ssh連接我2次,2次以上就拒絕
[192.168.2.7-root@web01~]#iptables -A INPUT -d 192.168.2.7 -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 6 packets, 396 bytes)
pkts bytes target prot opt in out source destination
2 104 REJECT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable
limit
--limit rate[/second|/minute|/hour|/day] 放行的速率 每秒/每分鍾/每小時/每天
--limit-burst n : 峰值速率
實驗: ping2.7主機,初始ping4次.超過4次后每分鍾ping3次
[192.168.2.7-root@web01~]#iptables -A INPUT -p icmp -m limit --limit-burst 4 --limit 3/minute -j ACCEPT
注意這個類功能要加2條規則,因為 指定的時每分種3個 --limit 3/minute,也就是說每分鍾3個以外的包哪里去呢,所以要加一條 icmp都拒絕的,也就是說 每分鍾3個之外的包,用下面的icmp -j DROP的規則都給拒絕掉
[192.168.2.7-root@web01~]#iptables -A INPUT -p icmp -j DROP
[192.168.2.7-root@web01~]#iptables -vnL
Chain INPUT (policy ACCEPT 62 packets, 4255 bytes)
pkts bytes target prot opt in out source destination
2 104 REJECT tcp -- * * 0.0.0.0/0 192.168.2.7 tcp dpt:22 #conn src/32 > 2 reject-with icmp-port-unreachable
24 2020 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 3/min burst 4
12 1008 DROP icmp -- * * 0.0.0.0/0 0.0.0.0/0
測試結果
limit白名單實驗:
允許windows連接 web02 ,其他人ping web02 ,峰值速率4,放行速率每分鍾20個
[192.168.2.8-root@web02 ~]#iptables -A INPUT -p icmp -m limit --limit-burst 4 --limit 20/minute -j ACCEPT
[192.168.2.8-root@web02 ~]#iptables -A OUTPUT -p icmp -m limit --limit-burst 4 --limit 20/minute -j ACCEPT
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 8 packets, 771 bytes)
pkts bytes target prot opt in out source destination
59 4984 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 20/min burst 4
2062 146K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 2 packets, 120 bytes)
pkts bytes target prot opt in out source destination
9138 1078K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
8 672 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
state
作用:可以根據連接跟蹤狀態,檢查每次發送的請求是新連接還是舊連接
state模塊的幾種狀態:
NEW:是新的請求,第一次握手
ESTABLISHED: 是NEW狀態之后,即3次握手之后,有來有回了,不是第一次連接了,
有1個請求發過來了,
連接追蹤的文件
實驗: 實現.外面可以通過NEW,ENTABLISHED的狀態,也就是通過第一次握手,和建立連接,訪問 web02 ,而web02 通過OUTPUT, ENTABLESHED ,外面通信,web02,
web02的OUTPUT上不能有NEW,也就是和外面主動建立連接,如果有和外面主動建立連接,這是不正常的,只有1種可能,web02中了病毒或木馬,主動的發送信息,給黑客,下面就通過實驗,不讓這種事情發生
規則表
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 68 packets, 7956 bytes)
pkts bytes target prot opt in out source destination
4639 334K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 105 packets, 17704 bytes)
pkts bytes target prot opt in out source destination
10852 1257K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
編寫規則
這種ESTABLISTED的狀態都要寫在防火牆規則最上面,因為都是要建立連接的,上來不用找,直接就走這個規則,如果放到后面,用戶訪問,還要一層層的向下找,如果放到下面的話,效率太低,還浪費資源
[192.168.2.8-root@web02 ~]#iptables -I INPUT 1 -s 192.168.2.17 -p tcp --dport 22 -m state --state NEW,ESTABLISHED -j ACCEPT
192.168.2.17可以主動連接web02
[192.168.2.8-root@web02 ~]#iptables -I OUTPUT 1 -d 192.168.2.17 -p tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
192.168.2.17 是主動連接web02的,web02只能通過,這個192.168.2.17主動連接的關系,通過ESTABLESHED響應192.168.2.17實現不主動連接外面
[192.168.2.8-root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 667 packets, 78039 bytes)
pkts bytes target prot opt in out source destination
160 22561 ACCEPT tcp -- * * 192.168.2.17 0.0.0.0/0 tcp dpt:22 state NEW,ESTABLISHED
2220 162K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 115 packets, 6988 bytes)
pkts bytes target prot opt in out source destination
13915 1597K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
102 17868 ACCEPT tcp -- * * 0.0.0.0/0 192.168.2.17 tcp spt:22 state ESTABLISHED
實驗結果: 192.168.2.17能連 web02 ,web02 連接不了別人
[192.168.2.17-root@web03 ~]#ssh 192.168.2.8
root@192.168.2.8's password:
[192.168.2.8-root@web02 ~]#ssh 192.168.2.17
查看下 連接追蹤的數據庫文件,有記錄了
[192.168.2.8-root@web02 ~]#cat /proc/net/nf_conntrack
ipv4 2 tcp 6 299 ESTABLISHED src=192.168.2.105 dst=192.168.2.8 sport=14216 dport=22 src=192.168.2.8 dst=192.168.2.105 sport=22 dport=14216 [ASSURED] mark=0 zone=0
use=2ipv4 2 tcp 6 428037 ESTABLISHED src=192.168.2.17 dst=192.168.2.8 sport=38610 dport=22 src=192.168.2.8 dst=192.168.2.17 sport=22 dport=38610 [ASSURED] mark=0 zone=0
use=2
處理動作
iptables命令總結
-N iptabels自定義鏈
[root@web02 ~]#iptables -N drop_invalid_packets
自定義一條鏈,叫drop_invalid_packets
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
1512 110K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
975 99681 ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (0 references)
pkts bytes target prot opt in out source destination
添加規則
添加tcp協議的數據包全是1 數據包,全是0的,都拒絕掉
[root@web02 ~]#iptables -A drop_invalid_packets -p tcp --tcp-flags ALL ALL -j REJECT
[root@web02 ~]#iptables -A drop_invalid_packets -p tcp --tcp-flags ALL NONE -j REJECT
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
1784 129K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1120 115K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (1 references)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
關聯自定義鏈到系統的INPUT鏈上
自定義的鏈是孤立的需要關聯到系統自帶的鏈上,
[root@web02 ~]#iptables -A INPUT -s 192.168.2.17 -j drop_invalid_packets
來自192.168.2.17的 tcp包全是 1或者0的都走自定義鏈的規則 REJECT.強硬的拒絕掉
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
2214 160K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
1383 140K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain drop_invalid_packets (2 references)
pkts bytes target prot opt in out source destination
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
結論:鏈可以把同類的功能分組,方便管理,可以把重要的服務,規划到自定義鏈里,比如:80,22,53,443端口,放在一個鏈里,然后關聯到主鏈
下面我們就 實驗以下這樣的用法
[root@web02 ~]#iptables -N INTERNETACCESS
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 80 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 443 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 22 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p tcp --dport 53 -j ACCEPT
[root@web02 ~]#iptables -A INTERNETACCESS -p udp --dport 53 -j ACCEPT
[root@web02 ~]#iptables -A INPUT -s 192.168.2.0/24 -j INTERNETACCESS
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 27 packets, 3159 bytes)
num pkts bytes target prot opt in out source destination
1 3151 229K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
3 27 3159 INTERNETACCESS all -- * * 192.168.2.0/24 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 6 packets, 360 bytes)
num pkts bytes target prot opt in out source destination
1 1921 192K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain INTERNETACCESS (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
2 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
3 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:53
5 0 0 ACCEPT udp -- * * 0.0.0.0/0 0.0.0.0/0 udp dpt:53
刪除自定義關聯步驟: 刪除關聯主鏈的自定義鏈 --> 清空自定義鏈 --> 刪除自定義鏈名
刪除關聯主鏈的自定義鏈
[root@web02 ~]#iptables -D INPUT 3
查看已經刪除了INPUT鏈里的INTENTERACCESS自定義鏈
[root@web02 ~]#iptables -vnL
Chain INPUT (policy DROP 2 packets, 234 bytes)
pkts bytes target prot opt in out source destination
3451 250K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
清空自定義鏈.單獨清空和一次性清空都行,看需要
[root@web02 ~]#iptables -D INTERNETACCESS 1
[root@web02 ~]#iptables -F INTERNETACCESS
查看已經把自定鏈都清空了
[root@web02 ~]#iptables -vnL --line-numbers
Chain INPUT (policy DROP 79 packets, 9243 bytes)
num pkts bytes target prot opt in out source destination
1 3510 254K ACCEPT all -- * * 192.168.2.105 0.0.0.0/0
2 2 168 drop_invalid_packets all -- * * 192.168.2.17 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
Chain OUTPUT (policy DROP 14 packets, 840 bytes)
num pkts bytes target prot opt in out source destination
1 2136 217K ACCEPT all -- * * 0.0.0.0/0 192.168.2.105
Chain INTERNETACCESS (0 references) #下面沒規則了,清空完成
num pkts bytes target prot opt in out source destination
Chain drop_invalid_packets (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
刪除自定義鏈名
INTERNETACCEPT 鏈已經刪除
[root@web02 ~]#iptables -vnL --line-numbers
Chain drop_invalid_packets (1 references)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x3F reject-with icmp-port-unreachable
2 0 0 REJECT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp flags:0x3F/0x00 reject-with icmp-port-unreachable
下面是擴展模塊的使用示例
iptables規則保存,和開機加載策略
centos6保存規則
cat /etc/sysconfig/iptables
執行service iptables save 命令保存到 /etc/sysconfig/iptables里
centos7 保存規則
iptables-save把策略保存到文件
[192.168.2.8-root@web02 ~]#iptables-save
# Generated by iptables-save v1.4.21 on Sun Jan 27 16:16:57 2019
*filter
:INPUT DROP [1020:119340]
:FORWARD ACCEPT [0:0]
:OUTPUT DROP [183:10850]
-A INPUT -s 192.168.2.105/32 -j ACCEPT
-A INPUT -s 192.168.2.106/32 -j ACCEPT
-A OUTPUT -d 192.168.2.106/32 -j ACCEPT
-A OUTPUT -d 192.168.2.17/32 -p tcp -m tcp --sport 22 -m state --state ESTABLISHED -j ACCEPT
MMIT
把策略導出
iptables-save
[192.168.2.8-root@web02 ~]#iptables-save > /etc/sysconfig/iptables-test
把策略導入
iptables-restore
[192.168.2.8-root@web02 ~]#iptables-restore < /etc/sysconfig/iptables-test
-n | --noflush :不清除原有的規則
-t test : 測試
開機加載規則
[192.168.2.8-root@web02 ~]# cat /etc/rc.d/rc.locatl
iptables-save < /etc/sysconfig/iptables-test
iptables/nat 網絡防火牆
SANT
DNAT
MASQUERADE
實驗環境.3台機器,
內網: 內網網卡:eth0 ip:172.16.1.17 不能上外網
路由: 外網網卡:eth0 ip 192.168.2.8 內網:eth1 ip:172.16.1.8 eth0可以上外網,eth1不能上外網
外網: 外網網卡:eth1 ip 192.168.2.7 不能上外網
虛擬機僅主機網卡設置
windows 上的vmnet1 設置
內網機器 172.16.1.17添加僅主機模式的網卡
linux內網主機: ip地址和路由設置
ip地址代碼
[172.16.2.17-root@web03 ~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.17
GATEWAY=172.16.1.8
PREFIX=24
DNS1=223.5.5.5
route 虛擬機添加,橋接模式,和僅主機模式
route 機器: eth0外網.橋接模式
eth1內網.僅主機模式
IP地址代碼
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.2.8
NETMASK=255.255.255.0
GATEWAY=192.168.2.1
DNS1=223.5.5.5
[root@route ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
TYPE=Ethernet
BOOTPROTO=none
NAME=eth1
DEVICE=eth1
ONBOOT=yes
IPADDR=172.16.1.8
外網機器192.168.2.7添加橋接網卡
linux外網主機: ip地址和路由設置
ip地址代碼
[192.168.2.7-root@web01~]#cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.2.7
GATEWAY=192.168.2.8
PREFIX=24
DNS1=192.168.2.1
1, 充當網關
開啟內核轉發
[root@route ~]# iptables -A FORWARD -p icmp -d 192.168.2.7 -j REJECT
SNAT實現內網訪問外網:
語法格式:
iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 192.168.2.8-192.168.2.100
格式:-s 指定源地址, -j SNAT作源地址轉換 --to-source指定由哪個外網ip地址轉換
192.168.2.8-192.168.2.100 可以是多個地址 加"-"
route直接添加規則
[root@route ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j SNAT --to-source 192.168.2.8
[root@route ~]# iptables -vnL -t nat
Chain PREROUTING (policy ACCEPT 30 packets, 3088 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 5 packets, 1090 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 4 packets, 304 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 4 packets, 304 bytes)
pkts bytes target prot opt in out source destination
6 360 SNAT all -- * * 172.16.1.0/24 0.0.0.0/0 to:192.168.2.8
snat 轉換的只是IP地址,不轉換端口,我們可以證明一下
telnet 192.168.2.7 80端口
[172.16.2.17-root@web03 ~]#telnet 192.168.2.7 80
Trying 192.168.2.7...
Connected to 192.168.2.7.
Escape character is '^]'.
在內網機器上新開終端,ss 命令查看,端口是33352
[172.16.2.17-root@web03 ~]#ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 172.16.1.17:33370 192.168.2.7:80
192.168.2.7機器查看 連接的ip地址是 防火牆的ip,證明了snat只轉換ip不轉換端口
[192.168.2.7-root@web01~]#ss -tn
[192.168.2.7-root@web01~]#ss -tn
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 0 192.168.2.7:80 192.168.2.8:33370
MASQUERADE 動態ip 適用於小型企業ADSL撥號,不固定的IP
[root@route ~]# iptables -t nat -A POSTROUTING -s 172.16.1.0/24 -j MASQUERADE
[root@route ~]# iptables -t nat -R POSTROUTING 1 -s 172.16.1.0/24 -j MASQUERADE
[root@route ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 MASQUERADE all -- * * 172.16.1.0/24 0.0.0.0/0
DNAT 外網訪問內網
- 現在基於之前的環境, 訪問不了.17,不可到達,根本沒有路由
[192.168.2.7-root@web01~]#ping 172.16.1.17
connect: Network is unreachable
[192.168.2.7-root@web01~]#route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
- route上開啟http服務,添加測試文件echo "防火牆的http" >> /var/www/html/index.html
192.168.2.7 curl route
[192.168.2.7-root@web01~]# curl 192.168.2.8
防火牆的http
- 我們添加DNAT規則
[root@route ~]# iptables -t nat -A PREROUTING -d 192.168.2.8 -p tcp --dport 80 -j DNAT --to-destination 172.16.1.17
還可以直接轉換端口,把192.168.2.8的80端口,替換為 172.16.1.17的8080端口
[root@route ~]# iptables -t nat -A PREROUTING -d 192.168.2.8 -p tcp --dport 80 -j DNAT --to-destination 172.16.1.17:8080
[root@route ~]# iptables -t nat -vnL
Chain PREROUTING (policy ACCEPT 2 packets, 468 bytes)
pkts bytes target prot opt in out source destination
1 60 DNAT tcp -- * * 0.0.0.0/0 192.168.2.8 tcp dpt:80 to:172.16.1.17
Chain INPUT (policy ACCEPT 1 packets, 234 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 3 packets, 228 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 4 packets, 288 bytes)
pkts bytes target prot opt in out source destination
- 在用 curl 命令192.168.2.8 顯示了 172.16.1.17上的web站點信息,轉換成功
[192.168.2.7-root@web01~]# curl 192.168.2.8
172.16.1.17
REDIRECT: 單機的端口轉換只能用在單機上,和IP轉發什么的沒關系.
通過改變目標的IP和端口, 將接收的包轉發到不同地址,
把別人訪問我的80端口,轉換成其他端口
實驗:
- 把172.16.1.17的http端口改為8080
- curl 172.16.1.17 失敗了
[192.168.2.7-root@web01~]# curl 192.168.2.8
curl: (7) Failed connect to 192.168.2.8:80; Connection refused
- 添加規則
[172.16.2.17-root@web03 ~]#iptables -t nat -A PREROUTING -d 172.16.1.17 -p tcp --dport 80 -j REDIRECT --to-ports 8080
[192.168.2.7-root@web01~]# curl 192.168.2.8
172.16.1.17
LVS
lvs分類
lvs主要是現實調度功能
lvs術語
lvs的類型
nat模式請求流程:
nat的特性
lvs DR模式
dR模式:ip沖突解決辦法
lvs tun模式
fullnat
lvs模式對比總結
lvs靜態調度算法
lvs動態調度算法