首頁 > 軟體

設定 firewall 防火牆的地址偽裝和埠轉發範例

2020-06-16 16:31:22

閘道器伺服器和網站伺服器都採用CentOS 7作業系統;
閘道器伺服器安裝3塊千兆網絡卡,分別連線在Internet、企業內網、網站伺服器。

要求如下:
閘道器伺服器連線網際網路卡ens33設定為公網IP地址,分配到firewall的external區域;連線內網網絡卡ens37地址為192.168.1.1,分配到firewall的trusted區域;連線伺服器網絡卡ens38地址為192.168.2.1,分配到firewall的DMZ區域。
網站伺服器和閘道器伺服器都通過SSH來遠端管理,為了安全,將SSH預設埠改為12345。
網站伺服器開啟https,過濾未加密的http流量。
網站伺服器拒絕ping,閘道器伺服器拒絕來自網際網路上的ping。
內網使用者需要通過閘道器伺服器共用上網。
網際網路使用者需要存取網站伺服器。
基本的環境設定:
閘道器伺服器設定 網絡卡 :

[root@localhost network-scripts]# ip a  = ip addr
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:60:4b brd ff:ff:ff:ff:ff:ff
    inet 100.0.0.1/8 brd 100.255.255.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::5f65:9c3d:d218:7cea/64 scope link 
       valid_lft forever preferred_lft forever
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:60:55 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.1/24 brd 192.168.1.255 scope global ens36
       valid_lft forever preferred_lft forever
    inet6 fe80::7456:2bbc:dc20:31bd/64 scope link 
       valid_lft forever preferred_lft forever
4: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:60:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.1/24 brd 192.168.2.255 scope global ens37
       valid_lft forever preferred_lft forever
    inet6 fe80::1e90:6601:56c2:c9ba/64 scope link 
       valid_lft forever preferred_lft forever

啟動閘道器伺服器上的路由轉發功能:

[root@localhost /]# vim /etc/sysctl.conf 
net.ipv4.ip_forward = 1
[root@localhost /]# sysctl -p
net.ipv4.ip_forward = 1

設定 web 伺服器的網絡卡:

[root@localhost /]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:2f:24:4b brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.10/24 brd 192.168.2.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::19de:d9a7:568a:f34f/64 scope link 
       valid_lft forever preferred_lft forever
[root@localhost /]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.2.1     0.0.0.0         UG    100    0        0 ens33

Internet 測試機網絡卡設定:

[root@localhost /]# ip a
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:45:66:64 brd ff:ff:ff:ff:ff:ff
    inet 100.0.0.10/8 brd 100.255.255.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::3ebc:8f75:cd28:e516/64 scope link tentative 
       valid_lft forever preferred_lft forever

web 伺服器環境搭建:

[root@localhost ~]# systemctl start firewalld           #啟動防火牆
[root@localhost ~]# yum -y install httpd mod_ssl          #安裝httpd和mod_ssl
[root@localhost ~]# systemctl start httpd   #啟動httpd服務
[root@localhost ~]# systemctl enable httpd          #設定為開機自啟
[root@localhost ~]# vim /var/www/html/index.html          #新建網站測試首頁檔案
<h1>test.com </h1>
[root@localhost ~]# vim /etc/ssh/sshd_config              #更改SSH的偵聽埠(需關閉SELinux):
          ................
Port 12345
         ................
[root@localhost ~]# systemctl restart sshd

在網站伺服器上設定firewalld防火牆:
1、設定預設區域為dmz區域:

[root@localhost ~]# firewall-cmd --set-default-zone=dmz
success

2、為dmz區域開啟https服務並新增tcp的12345埠:

[root@localhost ~]# firewall-cmd --zone=dmz --add-service=https --permanent
success
[root@localhost ~]# firewall-cmd --zone=dmz --add-port=12345/tcp --permanent
success

3、禁止ping:

[root@localhost ~]# firewall-cmd --add-icmp-block=echo-request --zone=dmz --permanent
success

4、因為已經更改了預定義SSH服務的預設埠,所以將預定義SSH服務移除:

[root@localhost ~]# firewall-cmd --zone=dmz --remove-service=ssh --permanent
success

5、重新載入firewalld設定,並檢視之前的設定:

[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
dmz (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: https
  ports: 12345/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: echo-request
  rich rules: 

閘道器伺服器上設定firewalld防火牆:
1、將網絡卡新增至指定區域:

[root@localhost /]# systemctl start firewalld                     #啟用防火牆
[root@localhost /]# firewall-cmd --set-default-zone=external          #設定預設區域為external
success
[root@localhost /]# firewall-cmd --change-interface=ens37 --zone=trusted    #將ens37新增至trusted區域
The interface is under control of NetworkManager, setting zone to 'trusted'.
success
[root@localhost /]# firewall-cmd --change-interface=ens38 --zone=dmz     #將ens38新增至dmz區域
The interface is under control of NetworkManager, setting zone to 'dmz'.
success

2、檢視設定,並儲存為永久

[root@localhost /]# firewall-cmd --get-active-zones 
dmz
  interfaces: ens37
external
  interfaces: ens33
trusted
  interfaces: ens36
[root@localhost /]# firewall-cmd --runtime-to-permanent          # 將當前的設定儲存到檔案中
success

3、在企業內部主機上測試:

4、更改SSH的偵聽埠,並重新啟動服務(需關閉SELinux):

[root@localhost ~]# vim /etc/ssh/sshd_config      
          ................
Port 12345
         ................
[root@localhost ~]# systemctl restart sshd

5、設定external區域新增tcp的12345埠:

[root@localhost /]# firewall-cmd --zone=external --add-port=12345/tcp --permanent 
success

6、external區域移除SSH服務:

[root@localhost /]# firewall-cmd --zone=external --remove-service=ssh --permanent 
success

7、設定external區域禁止ping:

[root@localhost /]# firewall-cmd --zone=external --add-icmp-block=echo-request --permanent
success

8、重新載入防火牆設定:

[root@localhost /]# firewall-cmd --reload
success

測試ssh連線:
在 Internet 測試機通過SSH連線閘道器伺服器的外部介面地址的12345埠:

[root@localhost /]# ssh -p 12345 100.0.0.1
The authenticity of host '[100.0.0.1]:12345 ([100.0.0.1]:12345)' can't be established.
ECDSA key fingerprint is 68:df:0f:ac:c7:75:df:02:88:7d:36:6a:1a:ae:27:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[100.0.0.1]:12345' (ECDSA) to the list of known hosts.
root@100.0.0.1's password: 
Last login: Sun Sep  1 16:36:33 2019
[root@localhost ~]# 

使用內網測試機SSH登入web網站伺服器的12345埠:

[root@localhost /]# ssh -p 12345 192.168.2.10
The authenticity of host '[192.168.2.10]:12345 ([192.168.2.10]:12345)' can't be established.
ECDSA key fingerprint is 68:df:0f:ac:c7:75:df:02:88:7d:36:6a:1a:ae:27:23.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.2.10]:12345' (ECDSA) to the list of known hosts.
root@192.168.2.10's password: 
Last login: Sun Sep  1 16:36:39 2019
[root@localhost ~]# 

實現 IP 偽裝與埠轉發:
1、在Internet測試機上搭建web服務,用來測試:

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# vim /var/www/html/index.html
<h1> www.baidu.com</h1>
[root@localhost ~]# systemctl enable httpd
[root@localhost ~]# systemctl start httpd

2、在內部測試機和dmz的網站服務區都可以存取外網的網站(若存取不了,則可能是公網測試機的防火牆設定問題,可先將公網測試機的防火牆關閉,或放行相關服務的流量即可):

3、檢視閘道器伺服器的external區域是否開啟了地址偽裝:

[root@localhost /]# firewall-cmd --list-all --zone=external 
external (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: 
  ports: 12345/tcp
  protocols: 
  masquerade: yes                     # 表示地址偽裝已啟用
  forward-ports: 
  sourceports: 
  icmp-blocks: echo-request
  rich rules: 

4、只為源地址192.168.1.0/24網段的地址開啟地址IP偽裝。
在閘道器伺服器上關閉external預設的地址偽裝,新增富規則,要求external區域內,源地址為192.168.1.0/24網段的地址開啟地址IP偽裝:

[root@localhost ~]# firewall-cmd --remove-masquerade --zone=external 
success
[root@localhost ~]# firewall-cmd --zone=external --add-rich-rule='rule family=ipv4 source address=192.168.1.0/24 masquerade'
success

在dmz區域上測試存取發現無法存取,但內網主機卻可以:

[root@localhost /]# curl http://100.0.0.10
curl: (7) Failed connect to 100.0.0.10:80; No route to host

5、設定埠轉發實現網際網路使用者可以存取內部web伺服器:
在閘道器伺服器上設定:

[root@localhost /]# firewall-cmd --zone=external --add-forward-port=port=443:proto=tcp:toaddr=192.168.2.10
success

在Internet測試機上存取內網的web伺服器成功:

六、使用富規則實現埠轉發:

上述設定完成後,若現在公司申請了一個新的???網ip地址100.0.0.254,那麼就需要重新做埠轉發了:

1、將新申請的公網地址100.0.0.254設定在閘道器伺服器的外網介面ens33上,作為第二個IP地址:

[root@localhost /]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
IPADDR0=100.0.0.1                                 # 改到下四行
PREFIX0=24
IPADDR1=100.0.0.254
PREFIX1=24                                           # 新增成兩個IP
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
DEVICE=ens33
ONBOOT=yes
ZONE=external

[root@localhost /]# ifdown ens33;ifup ens33             # 重新啟動網絡卡使設定生效
[root@localhost /]# ip a                           # 檢視設定是否成功
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:fc:60:4b brd ff:ff:ff:ff:ff:ff
    inet 100.0.0.1/24 brd 100.0.0.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 100.0.0.254/24 brd 100.0.0.255 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::5f65:9c3d:d218:7cea/64 scope link 
       valid_lft forever preferred_lft forever

2、使用富規則設定埠轉發:

[root@localhost /]# firewall-cmd --zone=external --add-rich-rule='rule family=ipv4 destination address=100.0.0.254/24 forward-port port=443 protocol=tcp to-addr=192.168.2.10' 
success

3、驗證:

ss


IT145.com E-mail:sddin#qq.com