Telnet 是一种客户端-服务器协议,它通过 TCP 通过端口 23 连接到远程服务器。 Telnet 不加密数据,被认为是不安全的,而且密码很容易被嗅探,因为数据是以明文形式发送的。 但是,仍然有遗留系统需要使用它。 这是哪里 隧道 前来救援。
Stunnel 旨在为具有不安全连接协议的程序添加 SSL 加密。 本文将向您展示如何使用它,以 telnet 作为 example.
服务器安装
使用 telnet 服务器和客户端安装 stunnel sudo:
sudo dnf -y install stunnel telnet-server telnet
添加防火墙规则,在提示时输入您的密码:
firewall-cmd --add-service=telnet --perm
firewall-cmd --reload
接下来,生成 RSA 私钥和 SSL 证书:
openssl genrsa 2048 > stunnel.key
openssl req -new -key stunnel.key -x509 -days 90 -out stunnel.crt
系统会一次一行地提示您输入以下信息。 当要求输入公用名时,您必须输入正确的主机名或 IP 地址,但您可以通过点击 Enter 钥匙。
You are about to be asked to enter information that will be
incorporated into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:
State or Province Name (full name) []:
Locality Name (eg, city) [Default City]:
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []
将 RSA 密钥和 SSL 证书合并到一个 .pem 文件中,并将其复制到 SSL 证书目录:
cat stunnel.crt stunnel.key > stunnel.pem
sudo cp stunnel.pem /etc/pki/tls/certs/
现在是时候定义用于加密连接的服务和端口了。 选择一个尚未使用的端口。 这 example 使用端口 450 进行隧道 telnet。 编辑或创建 /etc/stunnel/telnet.conf 文件:
cert = /etc/pki/tls/certs/stunnel.pem
sslVersion = TLSv1
chroot = /var/run/stunnel
setuid = nobody
setgid = nobody
pid = /stunnel.pid
socket = l:TCP_NODELAY=1
socket = r:TCP_NODELAY=1
[telnet]
accept = 450
connect = 23
这 接受 选项是服务器将侦听传入 telnet 请求的端口。 这 连接 option 是 telnet 服务器监听的内部端口。
接下来,制作允许您覆盖打包版本的 systemd 单元文件的副本:
sudo cp /usr/lib/systemd/system/stunnel.service /etc/systemd/system
编辑 /etc/systemd/system/stunnel.service 文件以添加两行。 这些行在服务启动时为服务创建了一个 chroot 监狱。
[Unit]
Description=TLS tunnel for network daemons
After=syslog.target network.target
[Service]
ExecStart=/usr/bin/stunnel
Type=forking
PrivateTmp=true
ExecStartPre=-/usr/bin/mkdir /var/run/stunnel
ExecStartPre=/usr/bin/chown -R nobody:nobody /var/run/stunnel
[Install]
WantedBy=multi-user.target
接下来,配置 SELinux 以在您刚刚指定的新端口上侦听 telnet:
sudo semanage port -a -t telnetd_port_t -p tcp 450
最后,添加新的防火墙规则:
firewall-cmd --add-port=450/tcp --perm
firewall-cmd --reload
现在您可以启用并启动 telnet 和 stunnel。
systemctl enable telnet.socket [email protected] --now
关于 systemctl 命令的注释是有序的。 Systemd 和 stunnel 包默认提供了一个额外的模板单元文件。 该模板允许您将 stunnel 的多个配置文件放入 /etc/stunnel,并使用文件名启动服务。 例如,如果您有一个 foobar.conf 文件,您可以使用 systemctl start [email protected] 启动该 stunnel 实例,而无需自己编写任何单元文件。
如果需要,可以将此 stunnel 模板服务设置为在启动时启动:
systemctl enable [email protected]
客户端安装
本文的这一部分假设您以普通用户身份登录(使用 sudo 权限)在客户端系统上。 安装 stunnel 和 telnet 客户端:
dnf -y install stunnel telnet
将 stunnel.pem 文件从远程服务器复制到您的客户端 /etc/pki/tls/certs 目录。 在这个 example,远程telnet服务器的IP地址为192.168.1.143。
sudo scp [email protected]:/etc/pki/tls/certs/stunnel.pem
/etc/pki/tls/certs/
创建 /etc/stunnel/telnet.conf 文件:
cert = /etc/pki/tls/certs/stunnel.pem
client=yes
[telnet]
accept=450
connect=192.168.1.143:450
这 接受 option 是用于 telnet 会话的端口。 这 连接 选项是您的远程服务器的 IP 地址和它正在侦听的端口。
接下来,启用并启动 stunnel:
systemctl enable [email protected] --now
测试您的连接。 由于您已建立连接,因此您将 telnet 到 localhost 而不是远程 telnet 服务器的主机名或 IP 地址:
[user@client ~]$ telnet localhost 450
Trying ::1...
telnet: connect to address ::1: Connection refused
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Kernel 5.0.9-301.fc30.x86_64 on an x86_64 (0)
server login: myuser
Password: XXXXXXX
Last login: Sun May 5 14:28:22 from localhost
[myuser@server ~]$