配置 firewall 防火墙的地址伪装和端口转发实例(2)

网关服务器上配置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、在企业内部主机上测试:

配置 firewall 防火墙的地址伪装和端口转发实例

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> </h1> [root@localhost ~]# systemctl enable httpd [root@localhost ~]# systemctl start httpd

2、在内部测试机和dmz的网站服务区都可以访问外网的网站(若访问不了,则可能是公网测试机的防火墙配置问题,可先将公网测试机的防火墙关闭,或放行相关服务的流量即可):

配置 firewall 防火墙的地址伪装和端口转发实例


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

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/11686.html