通过telnet连接,ssh转发

SSH端口转发
SSH 会自动加密和解密所有 SSH 客户端与服务端之间的网络数据。但是,SSH 还能够将其他 TCP 端口的网络数据通过 SSH 链接来转发,并且自动提供了相应的加密及解密服务。这一过程也被叫做“隧道”(tunneling),这是因为 SSH 为其他 TCP 链接提供了一个安全的通道来进行传输而得名。例如,Telnet,SMTP,LDAP 这些 TCP 应用均能够从中得益,避免了用户名,密码以及隐私信息的明文传输。而与此同时,如果工作环境中的防火墙限制了一些网络端口的使用,但是允许 SSH 的连接,也能够通过将 TCP 端口转发来使用 SSH 进行通讯
SSH 端口转发能够提供两大功能:
加密 SSH Client 端至 SSH Server 端之间的通讯数据
突破防火墙的限制完成一些之前无法建立的 TCP 连接
1.目标
    原机器 通过一台中间机器连接另一台机器
    机器A需要连接到机器C,但是机器C拒绝A的连接,此时,机器A通过机器B取连接机器C。 机器A本机打开的端口 
192.168.39.3  A
192.168.39.7  B
192.168.39.8  C
2.环境三台Centos 虚拟器  机器A,C为CentOS8  机器B为CentOS7
A机器需要安装telnet客户端  yum install -y telnet
C机器需要安装telnet服务端  yum install -y telnet-server
首先在机器C上拒绝A的连接
iptables -A INPUT -s 192.168.39.3 -j REJEC
[root@CentOS-8-LinuxIV ~]# ssh 192.168.39.8
ssh: connect to host 192.168.39.8 port 22: Connection refused
现在就基本的准备完成了。各个主机之间没有连接了
3.开始连接 
3.1 本地转发流程

3.2 操作流程
    在A机器上操作
     ssh -L localport:remotehost:remotehostport sshserver
    ssh -L 9123:192.168.52.84:21 192.168.52.26   #若机器C的ssh端口不是默认的22 需要指定ssh的端口
可以加上选项:
    -f   转到后台
    -N 不执行远程命令
    -g 开启网关 如果用于多路转换器 连接   同一网段的机器访问此机器的此端口会被转发 
若第一次连接到B需要输入yes来确定,没有使用密钥的话需要使用密码。
使用ss -nt 来确认是否建立连接
若连接建立,则此时访问自己的9123端口便可访问C
ftp 127.0.0.1 -P 9123

3.3 远程转发
3.3.1 远程转发流程

3.3.2 远程转发操作
    在机器B上操作
ssh -R sshserverport:remotehost:remotehostport sshserver
ssh -R 9123:192.168.52.33:21 192.168.52.56    #若机器C的ssh端口不是默认的22 需要指定ssh的端口
同上使用ss -nt在查看连接是否与A建立,若建立连接,此时A机器访问自己的9123端口便可访问C
4.本地转发与远程的区别
1.本地转发与远程转发皆可当作中间的节点来使用,通过此节点可以访问本机无法访问的网络。
2.本地转发是打开本地的一个指定端口,与远程主机通讯。
3.远程转发是打开远程机器的一个端口(可以理解为指定一个远程端口与自己通讯,但此端口必须无其他服务使用,否则冲突)
4.本地转发与远程转发原理相同,只是逻辑上的思维不同。

5.动态端口转发
-D [bind_address:]port
             Specifies a local “dynamic” application-level port forwarding. This works by allocating
             a socket to listen to port on the local side, optionally bound to the specified
             bind_address. Whenever a connection is made to this port, the connection is forwarded
             over the secure channel, and the application protocol is then used to determine where to
             connect to from the remote machine. Currently the SOCKS4 and SOCKS5 protocols are sup‐
             ported, and ssh will act as a SOCKS server. Only root can forward privileged ports.
             Dynamic port forwardings can also be specified in the configuration file.

             IPv6 addresses can be specified by enclosing the address in square brackets. Only the
             superuser can forward privileged ports. By default, the local port is bound in accor‐
             dance with the GatewayPorts setting. However, an explicit bind_address may be used to
             bind the connection to a specific address. The bind_address of “localhost” indicates
             that the listening port be bound for local use only, while an empty address or ‘*’ indi‐
             cates that the port should be available from all interfaces.
动态端口转发:
ssh -D 9123 root@sshserver -fNg
curl --socks5 127.0.0.1:9123 http://www.google.com



猜你喜欢

转载自www.cnblogs.com/dreamfreedom/p/11829088.html