您現在的位置是:首頁 > 籃球
配置 firewall 防火牆的地址偽裝和埠轉發例項
- 由 監控那些事兒 發表于 籃球
- 2022-01-02
如何設定防火牆埠
閘道器伺服器和網站伺服器都採用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 addr2: ens33:
啟動閘道器伺服器上的路由轉發功能:
[root@localhost /]# vim /etc/sysctl。conf net。ipv4。ip_forward = 1[root@localhost /]# sysctl -pnet。ipv4。ip_forward = 1
配置 web 伺服器的網絡卡:
[root@localhost /]# ip a2: ens33:
Internet 測試機網絡卡配置:
[root@localhost /]# ip a2: ens33:
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 #新建網站測試首頁檔案
test。com
[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=dmzsuccess
2、為dmz區域開啟https服務並新增tcp的12345埠:
[root@localhost ~]# firewall-cmd ——zone=dmz ——add-service=https ——permanentsuccess[root@localhost ~]# firewall-cmd ——zone=dmz ——add-port=12345/tcp ——permanentsuccess
3、禁止ping:
[root@localhost ~]# firewall-cmd ——add-icmp-block=echo-request ——zone=dmz ——permanentsuccess
4、因為已經更改了預定義SSH服務的預設埠,所以將預定義SSH服務移除:
[root@localhost ~]# firewall-cmd ——zone=dmz ——remove-service=ssh ——permanentsuccess
5、重新載入firewalld配置,並檢視之前的配置:
[root@localhost ~]# firewall-cmd ——reloadsuccess[root@localhost ~]# firewall-cmd ——list-alldmz (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 #設定預設區域為externalsuccess[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: ens37external interfaces: ens33trusted 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 ——permanentsuccess
8、重新載入防火牆配置:
[root@localhost /]# firewall-cmd ——reloadsuccess
測試ssh連線:
在 Internet 測試機透過SSH連線閘道器伺服器的外部介面地址的12345埠:
[root@localhost /]# ssh -p 12345 100。0。0。1The 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)? yesWarning: 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。10The 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)? yesWarning: 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
www。baidu。com
[root@localhost ~]# systemctl enable httpd[root@localhost ~]# systemctl start httpd2、在內部測試機和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。10curl: (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。10success
在Internet測試機上訪問內網的web伺服器成功:
六、使用富規則實現埠轉發:
上述配置完成後,若現在公司申請了一個新的公網ip地址100。0。0。254,那麼就需要重新做埠轉發了:
1、將新申請的公網地址100.0.0.254配置在閘道器伺服器的外網介面ens33上,作為第二個IP地址:
[root@localhost /]# vim /etc/sysconfig/network-scripts/ifcfg-ens33TYPE=EthernetBOOTPROTO=staticIPADDR0=100。0。0。1 # 改到下四行PREFIX0=24IPADDR1=100。0。0。254PREFIX1=24 # 新增成兩個IPDEFROUTE=yesPEERDNS=yesPEERROUTES=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_PEERDNS=yesIPV6_PEERROUTES=yesIPV6_FAILURE_FATAL=noIPV6_ADDR_GEN_MODE=stable-privacyNAME=ens33DEVICE=ens33ONBOOT=yesZONE=external[root@localhost /]# ifdown ens33;ifup ens33 # 重啟網絡卡使配置生效[root@localhost /]# ip a # 檢視配置是否成功2: ens33:
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、驗證: