实验环境: centos 7.9 iptables tcpwrappers ssh
实验目的: 深入领会地址转换以及安全性服务
实现拓扑图形:
实现步骤:
1.准备3台linux虚拟机,其中一台作为防火墙,两块网卡。另外两台分别做跳板机和web服务器
手工配置ip地址
防火墙ens33ip地址为: 192.168.2.119
ens36ip地址为: 192.168.91.254
跳板机ip地址为: 192.168.91.123
web服务器ip地址为: 192.168.91.80
2.分别关闭三台机器的防火墙iptables以及selinux
[root@localhost ~]# iptables -F -t nat
[root@localhost ~]# iptables -F
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
3.在防火墙上配置对应的snat策略和dnat策略
[root@localhost ~]# iptables -t nat -A PREROUTING -i ens33 -d 192.168.2.119 -p tcp --dport 2288 -j DNAT --to-destination 192.168.91.123:6677
#DNAT策略 这条命令表示将访问网卡ens33 目标地址为192.168.2.119 tcp协议 端口为2288
访问以映射到内网的 192.168.91.123:6677 这台机器
[root@localhost ~]# iptables -t nat -A POSTROUTING -o ens33 -s 192.168.91.0/24 -j SNAT --to-source 192.168.2.119
#SNAT策略 这条命令表示将内网为192.168.91.0网段通过ens33的取访问外网的请求,将源地址改为192.168.2.119进行访问
4.开启防火墙对应的路由功能,可以转发流量
echo 1 >/proc/sys/net/ipv4/ip_forward //临时修改
5.修改web服务器上的tcpwrapper黑白名单
[root@localhost /]# vim /etc/hosts.allow
sshd:192.168.91.123 //放开来自192.168.91.123 的主机
[root@localhost /]# vim /etc/hosts.deny
sshd:ALL //限制所有访问sshd服务的主机
6.在跳板机上修改ssh服务的端口以及禁止root用户远程登录创建一个新的用户superman
[root@server shell]# vim /etc/ssh/sshd_config
Port 6677
PermitRootLogin no
[root@server shell]# useradd superman
[root@server shell]# echo 123456|passwd superman --stdin
7.验证防火墙的SNAT策略是否成功以及内网机器是否能够上网 #使用ping命令
[root@server shell]# ping www.baidu.com
PING www.a.shifen.com (14.119.104.254) 56(84) bytes of data.
64 bytes from 14.119.104.254 (14.119.104.254): icmp_seq=1 ttl=54 time=21.2 ms
64 bytes from 14.119.104.254 (14.119.104.254): icmp_seq=2 ttl=54 time=24.1 ms
64 bytes from 14.119.104.254 (14.119.104.254): icmp_seq=3 ttl=54 time=20.9 ms
^C
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 20.954/22.122/24.165/1.459 ms
[root@localhost /]# ping www.baidu.com
PING www.a.shifen.com (14.119.104.254) 56(84) bytes of data.
64 bytes from 14.119.104.254 (14.119.104.254): icmp_seq=1 ttl=54 time=23.6 ms
64 bytes from 14.119.104.254 (14.119.104.254): icmp_seq=2 ttl=54 time=23.0 ms
8.在windows主机上使用xshell进行连接防火墙的ip地址 192.168.2.119:2288 用户为superman用户
9.最后验证:此时已经ssh连接到跳板机,使用ssh命令连接到web服务器
superman@server ~]$ ssh root@192.168.91.80
The authenticity of host '192.168.91.80 (192.168.91.80)' can't be established.
ECDSA key fingerprint is SHA256:asAWgGKcP+VYplhgeJLzJfKqqtfCKOKMrQhGEzq/YSE.
ECDSA key fingerprint is MD5:21:fa:59:d6:69:f7:b9:6a:66:97:41:d4:39:73:36:ce.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.91.80' (ECDSA) to the list of known hosts.
root@192.168.91.80's password:
Last login: Wed Aug 2 17:27:15 2023 from 192.168.91.123