SSH端口转发

SSH端口转发

SSH会自动加密和解密所有SSH客户端与服务端之间的网络数据。但是ssh还能够将其他TCP端口的网络数据通过SSH连接来转发,并自动提供了相应的加密及解密服务。这一过程也被叫做隧道,这是应为SSH为其他TCP链接提供了一个安全的通道来进行传输而得名。例如,Telnet,SMTP,LDAP这些TCP应用均能够从中得益,避免了用户名,密码以及隐私信息的明文传输。而于此同时,如果工作环境中的防火请限制了一些些网络端口的使用,但是允许SSh的连接,也能够通过将tcp端口转发来使用ssh进行通讯。
SSH端口转发能够提供两大功能:
1.加密SSH Client端至SSH Server端之间的通讯数据。
2.突破防火墙的限制完成一些之前无法家里的TCP连接。


ssh -L localport:remotehost:remotehostport sshserver
ssh -R localport:remotehost:remotehostport sshserver
ssh -D localport root@sshserver -fNg
localport:本地端口
remotehost:目标主机
remotehostport:目标主机端口
sshserver:ssh服务器

选项说明
-f后台启用
-N不打开远程shell,处于等待状态
-g启用网关功能
-L本地转发
-R远程转发
-D动态端口转发

本地转发

实验1:
有A、B、C三台主机,主机C为telnet服务器且只能由主机B去连接,主机B为ssh服务器,实现坐在主机A上通过telnet服务直接去连接主机C
实验准备工作:

主机IP
A192.168.73.128
B192.168.73.132
C192.168.73.133

一、在主机C上安装telnet服务并启用

[root@hostc ~]# yum install telnet-server -y
[root@hostc ~]# systemctl start telnet.socket 

二、在主机C上将主机A加入防火墙

[root@hostc ~]# iptables -A INPUT -s 192.168.73.128 -j REJECT

环境准备完毕


一、在主机A上执行下命令

[root@hosta ~]#  ssh -L 12345:192.168.73.133:23 192.168.73.132 -fN

二、测试

[root@hosta ~]# telnet 127.0.0.1 12345
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Kernel 3.10.0-957.el7.x86_64 on an x86_64
hostc login: masuri
Password: 
Last login: Wed Apr 17 00:55:35 from ::ffff:192.168.73.132
[masuri@hostc ~]$ 

远程转发

实验2:
主机A被防火墙阻挡无法直接访问主机B和主机C,主机B可以访问主机A和主机C,此时主机A若要访问主机C,就需要主机B先去访问主机A为其建立起通道。
实验环境

主机IP
A192.168.73.128
B192.168.73.132
C192.168.73.133

实验环境准备
在主机C上将主机A加入防火墙

[root@hostc ~]# iptables -A INPUT -s 192.168.73.128 -j REJECT

在主机B上将主机A加入防火墙

[root@hostb ~]# iptables -A INPUT -s 192.168.73.128 -j REJECT

实验环境准备完毕


一、在主机B上去访问主机A,为其建立起通道

[root@hostb ~]# ssh -R 9527:192.168.73.133:23 192.168.73.128 -fN
root@192.168.73.128's password: 
[root@hostb ~]# 
#此时本机的9527和A主机的9527端口已经建立起连接

二、在主机A上使用telenet访问本机的9527端口来连接主机C

[root@hosta ~]# telnet 127.0.0.1 9527
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.

Kernel 3.10.0-957.el7.x86_64 on an x86_64
hostc login: masuri
Password: 
Last login: Wed Apr 17 01:01:49 from ::ffff:192.168.73.132
[masuri@hostc ~]$ 

动态端口转发

实验3:
当访问本机的1080端口号,自动将请求发送给虚拟机,虚拟机带为转发请求去访问网站,网站接受到请求,返回页面给虚拟机,虚拟机再转发给本机。
实验环境
主机A无法访问到主机C,主机A能和主机B,主机B能访问主机C

主机IP
A192.168.73.128
B192.168.73.132
C192.168.73.133

实验环境准备
在主机C上将主机A加入防火墙

[root@hostc ~]# iptables -A INPUT -s 192.168.73.128 -j REJECT

在主机C上建立http服务

[root@hostc ~]# yum install httpd -y
[root@hostc html]# echo "www.mylinuxops.com" > index.html
[root@hostc html]# systemctl start httpd

在主机A上访问主机C

[root@hosta ~]# curl 192.168.73.133
curl: (7) Failed connect to 192.168.73.133:80; Connection refused

环境预备完毕


一、在主机A上将主机B设置为代理服务器

[root@hosta ~]# ssh -D 1080 root@192.168.73.132 -fNg
root@192.168.73.132's password: 

二、测试
在主机A使用本机的1080端口位去访问主机C的httpd服务

[root@hosta ~]# curl --socks5 127.0.0.1:1080 192.168.73.133
www.mylinuxops.com

此方法可以实现×××,不过仅限于主机b和主机C为linux系统,以下实现当主机A为windows系统时也能访问主机C


实验4
主机A无法访问到主机C,主机A能和主机B,主机B能访问主机C

主机IP
A192.168.73.128
B192.168.73.132
C192.168.73.133

实验环境准备
在主机C上将主机A加入防火墙

[root@hostc ~]# iptables -A INPUT -s 192.168.73.128 -j REJECT

在主机C上建立http服务

[root@hostc ~]# yum install httpd -y
[root@hostc html]# echo "www.mylinuxops.com" > index.html
[root@hostc html]# systemctl start httpd

在主机A上访问主机C

[root@hosta ~]# curl 192.168.73.133
curl: (7) Failed connect to 192.168.73.133:80; Connection refused

环境预备完毕


一、在主机B上开启sshd的网关功
修改/etc/ssh/sshd_config文件,将GatewayPorts项修改为yes

[root@hostb ~]# vim /etc/ssh/sshd_config 
GatewayPorts yes

二、在主机B上打开本地端口作为网关实现端口转发

[root@hostb ~]# vim /etc/ssh/sshd_config 
[root@hostb ~]# ssh -gD 9527 localhost
root@localhost's password: 
Last failed login: Wed Apr 17 03:25:53 CST 2019 from 192.168.73.128 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Wed Apr 17 03:12:13 2019 from 192.168.73.1
[root@hostb ~]#

三、测试
在主机A上将主机B作为代理服务器去访问A

[root@hosta ~]# curl --socks5 192.168.73.132:9527 192.168.73.133 
www.mylinuxops.com

转载于:https://blog.51cto.com/11886307/2379815

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值