ssh over socks5

由于你懂的原因,肯能需要通过代理连接ssh,不废话,放命令:

ssh -o ProxyCommand='nc -x 192.0.2.0:1080 %h %p' -p port user@awshost

以下是原理,小白止步

ProxyCommand可以利用nc做中间通道,从而达到代理的目的。要明白这个过程,需要先了解下nc的用法。

$ man nc
DESCRIPTION
     The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets.  It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6.

     Common uses include:

           ·   simple TCP proxies
           ·   shell-script based HTTP clients and servers
           ·   network daemon testing
           ·   a SOCKS or HTTP ProxyCommand for ssh(1)
           ·   and much, much more

     -X proxy_protocol
             Requests that nc should use the specified protocol when talking to the proxy server.  Supported protocols are “4” (SOCKS v.4), “5” (SOCKS v.5) and “connect” (HTTPS proxy).  If the protocol is not specified, SOCKS version 5 is used.
     -x proxy_address[:port]
             Requests that nc should connect to destination using a proxy at proxy_address and port.  If port is not specified, the well-known port for the proxy protocol is used (1080 for SOCKS, 3128 for HTTPS).

举个例子。

nc以服务器身份,起一个tcp监听连接。

$ nc -l 2222

netstat可以看到一个2222的监听socket。

然后,nc以客户端身份,连接到上面的tcp的监听socket。

$ nc localhost 2222

此时nc接管shell,随便输入几个字符,就可以在nc服务器端看到刚刚输入的字符。

从这个例子可以看到,nc其实是创建了一个非常单纯的tcp连接,没有任何协议;任何从client输入的数据,都会直接传给server端。

那么,在ssh over socks5这里,nc做了什么呢?

在一个已经通过socks5代理的ssh连接建立的环境上,可以看到有个nc的进程:

nc -x 192.0.2.0:1080 awshost 22

试试直接在shell里输入上面nc的命令,会发现nc与ssh-server建立连接以后,ssh-server发过来了其ssh版本,然后等着nc继续按ssh协议与其交互。随便输入一个’ha!’,由于这个显然不符合ssh协议,所以ssh-server会关掉连接,提示Protocol mismatch。

nc -x 192.0.2.0:1080 awshost 22
SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.1
>ha!
Protocol mismatch.

明白了吗?我无法手工输入ssh协议,但ssh-client可以;nc作为客户端连接到了awshost(当然通过-x指定了nc的socks5代理)后,ssh-client将nc作为一个管道,按ssh协议与ssh-server交互,从而建立了ssh连接。

拓扑如下。

ssh-client --- nc --- proxy --- ssh-server
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值