iptables和lvs

解壓密碼6Hai7Gf8 html

 

路由轉發 linux

0是不轉發,1是轉發 web

[root@m01 roles]# cat /proc/sys/net/ipv4/ip_forward 算法

0 數據庫

臨時生效 vim

[root@m01 roles]# echo "1" > /proc/sys/net/ipv4/ip_forward windows

 

從配置文件讀取配置,臨時生效 centos

sysctl -p 緩存

sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf 服務器

sysctl -p

 

 

路由條目解釋

有2個路由到20和10網段的路由

  1. Destination
  2. Gateway
  3. Genmask
  4. Flags 通過幾個路由器
  5. Ref 路由索引
  6. Use 這個路由使用多少次
  7. 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

  1. iptables

  1. 韓立剛環境準備

  1. 虛擬機網絡設置

  1. 內網配置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

  1. 有如下幾條路由條目

[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

 

  1. 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

  1. 添加路由

[root@route ~]# cat /etc/sysconfig/network-scripts/route-eth1

ADDRESS0=192.168.80.0

NETMASK0=255.255.255.0

GATEWAY0=192.168.20.20

  1. 開啓路由轉發

sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf

sysctl -p

  1. 查看路由條目

[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

 

  1. 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

  1. 添加路由

[root@route2 ~]# cat /etc/sysconfig/network-scripts/route-eth1

ADDRESS0=192.168.10.0

NETMASK0=255.255.255.0

GATEWAY0=192.168.20.10

  1. 查看路由

[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

  1. 開啓內核路由轉發

sed -i '/net.ipv4.ip_forward = 0/ net.ipv4.ip_forward = 1/ /etc/sysctl.conf

sysctl -p

 

  1. 外網配置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

  1. 查看路由

[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

 

 

 

  1. iptables 命令格式

  2. 格式

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 刪除表中的全部規則

  1. 舉例

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                 不指定鏈是清除全部

 

  1. iptables 數據包匹配選項

  1. 舉例

禁止外網ping route 路由器,可是能ping通內網,因此,設置 filter參數,只是包過濾,

[root@route ~]# iptables -t filter -I INPUT -p icmp -i eth1 -j DROP

以後

wai網 ping eth1網卡就不通了

可是不影響外網ping10.123的機器

 

 

  1. 禁止 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最上面,匹配到了

 

  1. 使用擴展選項實現單方向ping通

  2. -m icmp --icmp-type

  1. web服務器不主動和外面創建會話

iptables -t filter -I output -m state --state NEW -j DROP

 

  1. 示例

在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表

 

 

  1. 在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不通

 

  1. -m multiport

指定多端口號:

-m multiport

    --sport    源端口

    --port     目標端口

    --ports    源和目標一塊兒指

 

  1. 示例:

禁止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

  1. 指定IP段

-m iprange

    --src-range ip-ip

    --src-range ip-ip

  1. 示例:

禁止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

  1. -m connlimit 鏈接限制

    --comlimit-above 限定大鏈接個數

  2. -m limit --limit 限速

  1. 處理動做

  1. 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

 

  1. log 記錄日誌

    1. 舉例 內網禁止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的日誌

  1. 設置禁止ping並寫到日誌

iptables -t filter -I INPUT -p icmp -j LOG --log-prefix "iptables-icmp"

  1. 老王iptables

  1. iptables的組成

 

  1. 什麼是Raw:iptables鏈接跟蹤

  1. 數據包過濾匹配流程    

數據包通過3個流向:

數據包經過網卡進入到應用層,要通過 prerouting loca_in

從linux系統出去要通過, local_out,post_routing

轉發數據包穿過電腦(把電腦看成路由器)要通過prerouting forward postrouting

數據包通過這些鉤子幹嗎呢?鉤子函數會檢查filter,nat,mangle表,安裝表裏面的規則進行過濾

 

  1. iptabels語法

 

 

  1. table

能夠是filter表 nat表 mangle表 nat表 默認是filter表

  1. COMMAND

    1. 鏈管理:

    -N: new 自定義1條新的規則鏈

    -X:delete 刪除自定義的空的規則鏈

    -P : policy 設置默認策略,對filter表中的鏈而言,默認策略有

        ACCEPT: 接受

        DROP: 丟棄

        REJECT: 拒絕

  1. 規則管理

-A或 --append 在規則列表的最後增長一條規則

-I 或insert         在指定的位置插入一條規則,不指定位置就是在最上面插入

-D 或 --delete     刪除一個規則    -D input 2 或指名規則自己

-R或 --replace替換某條規則 -R INPUT 3

-F或 --flush 刪除表中的全部規則

-Z:置零,iptables有2個計數器

 

  1. 查看語法:

  1. -L :list 列出指定鏈的全部規則,此項必須寫在最後
  2. -n: number 以數字方式顯示地址和端口號
  3. -x: exactly 顯示計數器結果的精確值,而不上單位轉換後的易讀值

 

 

  1. 命令演示

[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

 

  1. --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鏈的第二條規則

 

  1. -R: 替換

  1. 清空表

iptables -F -t nat

  1. 清空計數器

- 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

 

  1. ! 取反

iptables - I INPUT ! -s 192.168.2.103 - j DROP

除2.103地址之外的全都拒絕

  1. 查看支持什麼表支持的鏈

[root@web02 ~]#iptables -vnL --line-numbers -t raw| filter | raw | nat

  1. 鏈管理:

    -N: new 自定義1條新的規則鏈

    -X:delete 刪除自定義的空的規則鏈

    -P : policy 設置默認策略,對filter表中的鏈而言,默認策略有

        ACCEPT: 接受

        DROP: 丟棄

        REJECT: 拒絕

 

  1. 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

 

  1. 鏈管理:

    -N: new 自定義1條新的規則鏈

    -X:delete 刪除自定義的空的規則鏈

    -P : policy 設置默認策略,對filter表中的鏈而言,默認策略有

        ACCEPT: 接受

        DROP: 丟棄

        REJECT: 拒絕

 

  1. 防火牆默認規則

 

-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

 

  1. iptables 匹配條件

  1. 基本匹配條件

  1. 容許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     出

 

  1. 查看 有數據交互了

[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

 

 

  1. 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

 

  1. --icmp-type匹配條件細分

  1. 實驗 容許ping可是不回能夠分別定義

0是迴應,8是請求

容許192.168.2.7ping我

  1. 分析: 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

  1. 測試,數據有去有回

[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

 

  1. 192.168.2.8能夠ping別人

  2. 分析: 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

 

  1. 測試有去有回

[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

 

  1. 擴展匹配條件

隱式和顯示

  1. 隱式擴展:

    1. --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

 

  1. --tcp-falags

  1. 根據tcp的標記位,判斷請求或者響應的握手,是第幾回握手

用--tcp-flags要了解三次握手,四次揮手的標記位.咱們複習一下,

A發的時候ack B回覆ack+syn A回覆ack

分手的時候多了個fin

還有 3種,

緊急標記位

psh若是=1數據就不會放到緩存,直接傳到應用程序裏

reset,發生故障,斷開重連

 

  1. 格式寫法:

  1. 實驗 針對新發起的請求,拒絕(也就是拒絕第一次握手),舊的請求,不拒絕

[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 ~]#

 

  1. --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

  1. 測試

鏈接不上.卡哪裏了

[root@web01 packages]# curl 192.168.2.8/index.html

 

  1. 實驗:新鏈接能夠訪問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

 

  1. 測試,訪問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.

  1. 顯示擴展須要用-m指定模塊名

 

顯示擴展要認爲的指定調用的模塊名:怎麼查看模塊名呢

centos7命令幫助man iptables-extensions

[root@web02 ~]#rpm -ql iptables

  1. 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

 

  1. 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

 

  1. -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

 

  1. 測試:

其餘機器修改了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

 

 

  1. --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

google

 

安裝httpd 測試

這個過濾google成功

[192.168.2.7-root@web01~]#cat index.html

fdafdsa

[192.168.2.7-root@web01~]#cat test.html

google

 

-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

 

  1. 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

 

  1. 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

 

  1. 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

 

  1. limit

--limit rate[/second|/minute|/hour|/day] 放行的速率 每秒/每分鐘/每小時/天天

--limit-burst n : 峯值速率

 

  1. 實驗: 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

 

測試結果

 

  1. 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

 

  1. state

做用:能夠根據鏈接跟蹤狀態,檢查每次發送的請求是新鏈接仍是舊鏈接

  1. state模塊的幾種狀態:

NEW:是新的請求,第一次握手

ESTABLISHED: 是NEW狀態以後,即3次握手以後,有來有回了,不是第一次鏈接了,

有1個請求發過來了,

 

  1. 鏈接追蹤的文件

 

  1. 實驗: 實現.外面能夠經過NEW,ENTABLISHED的狀態,也就是經過第一次握手,和創建鏈接,訪問 web02 ,而web02 經過OUTPUT, ENTABLESHED ,外面通訊,web02,

web02的OUTPUT上不能有NEW,也就是和外面主動創建鏈接,若是有和外面主動創建鏈接,這是不正常的,只有1種可能,web02中了病毒或木馬,主動的發送信息,給黑客,下面就經過實驗,不讓這種事情發生

  1. 規則表

[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

 

  1. 編寫規則

這種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

  1. 處理動做

iptables命令總結

 

  1. -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

 

  1. 關聯自定義鏈到系統的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

 

  1. 結論:鏈能夠把同類的功能分組,方便管理,能夠把重要的服務,規劃到自定義鏈裏,好比: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

 

  1. 刪除自定義關聯步驟: 刪除關聯主鏈的自定義鏈 --> 清空自定義鏈 --> 刪除自定義鏈名

  2. 刪除關聯主鏈的自定義鏈

[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

 

  1. 清空自定義鏈.單獨清空和一次性清空都行,看須要

[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

 

  1. 刪除自定義鏈名

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

 

  1. 下面是擴展模塊的使用示例

 

 

 

    

  1. iptables規則保存,和開機加載策略

  2. centos6保存規則

cat /etc/sysconfig/iptables

執行service iptables save 命令保存到 /etc/sysconfig/iptables裏

  1. 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

 

  1. 把策略導出

iptables-save

[192.168.2.8-root@web02 ~]#iptables-save > /etc/sysconfig/iptables-test

 

  1. 把策略導入

iptables-restore

[192.168.2.8-root@web02 ~]#iptables-restore < /etc/sysconfig/iptables-test

-n | --noflush :不清除原有的規則

-t test : 測試

 

  1. 開機加載規則

[192.168.2.8-root@web02 ~]# cat /etc/rc.d/rc.locatl

iptables-save < /etc/sysconfig/iptables-test

 

  1. iptables/nat 網絡防火牆

SANT

DNAT

MASQUERADE

  1. 實驗環境.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    不能上外網

 

 

  1. 虛擬機僅主機網卡設置

 

  1. windows 上的vmnet1 設置

 

  1. 內網機器 172.16.1.17添加僅主機模式的網卡

  1. linux內網主機: ip地址和路由設置

  1. 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

  1. route 虛擬機添加,橋接模式,和僅主機模式

route 機器: eth0外網.橋接模式

            eth1內網.僅主機模式

  1. 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

 

  1. 外網機器192.168.2.7添加橋接網卡

  1. linux外網主機: ip地址和路由設置

  1. 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

 

  1. 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

 

  1. DNAT 外網訪問內網

    1. 如今基於以前的環境, 訪問不了.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

 

  1. 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

 

  1. 咱們添加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

 

  1. 在用 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

 

  1. REDIRECT: 單機的端口轉換隻能用在單機上,和IP轉發什麼的不要緊.

經過改變目標的IP和端口, 將接收的包轉發到不一樣地址,

把別人訪問個人80端口,轉換成其餘端口

實驗:

  1. 把172.16.1.17的http端口改成8080

 

  1. 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

 

  1. 添加規則

[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

 

  1. LVS

  2. lvs分類

  1. lvs主要是現實調度功能

  1. lvs術語

  1. lvs的類型

 

  1. nat模式請求流程:

  1. nat的特性

  1. lvs DR模式

  1. dR模式:ip衝突解決辦法

  1. lvs tun模式

 

  1. fullnat

  1. lvs模式對比總結

  1. lvs靜態調度算法

  1. lvs動態調度算法

相關文章
相關標籤/搜索