跳至主要内容

ssh over socks5:通过socks5 proxy来连接ssh服务器[转载]

最近因为不可描述的原因,我在aws soul的云主机访问不了,ssh、80、ss全部都被禁掉了。

80端口在chrome配置SwitchyOmega就可以了,但ssh不太好办,shell上配置http_proxy对ssh没什么用。

其实ssh自己就支持proxy。

注意这跟在ssh端口转发:远程和本地中介绍过使用ssh作为proxy的方法不同,ssh -D是为了用ssh做proxy,而不是通过proxy来连接ssh服务器。

TL;DR

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

原理解析

$ man ssh_config
ProxyCommand
Specifies the command to use to connect to the server. The command string extends to the end of the line, and is executed using the user's shell ‘exec’ directive to avoid a lingering shell process.

        In the command string, any occurrence of ‘%h’ will be substituted by the host name to connect, ‘%p’ by the port, and ‘%r’ by the remote user name.  The command can be basically anything, and should read from its standard input and write to its standard output.  It should eventually connect an sshd(8) server running on some machine, or execute sshd -i somewhere.  Host key management will be done using the HostName of the host being connected (defaulting to the name typed by the user).  Setting the command to “none” disables this option entirely.  Note that CheckHostIP is not available for connects with a proxy command.

        This directive is useful in conjunction with nc(1) and its proxy support.  For example, the following directive would connect via an HTTP proxy at 192.0.2.0:

          ProxyCommand /usr/bin/nc -X connect -x 192.0.2.0:8080 %h %p

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

期待的是,通过ssh-server1做代理,连接到ssh-server2上。没找到直接的做法,不过可以这样实现:

ssh -D 127.0.0.1:1080 user1@ssh-server1,通过ssh-server1建立本地的socks5代理
ssh -o ProxyCommand='nc -x 127.0.0.1:1080 %h %p' user2@ssh-server2,连接到ssh-server2
如果你需要给chrome配置socks5代理,可以在第二步的基础上,再加上-D 127.0.0.1:2080,通过ssh-server1+ssh-server2建立socks5代理。
最近国内网络比较复杂,似乎某些公司网络直接访问有点问题,可以通过上面的方法来绕。

评论

此博客中的热门博文

阿里云通过Nginx中转Linode/digitalocean/AWS的V2ray协议,实现负载均衡

开篇文的废话.原因是Sandy做站服务器太多了,发现利用率不高,皮又痒了又想折腾了.v2ray是利器但是给人的感觉就是只能用一台服务器,官方也说不支持负载均衡,不会A服务器挂机,自动跳转到B服务器.这里就有点折磨人了.每个服务器挂了都要自己去改配置文件. 我们访问国外的服务器,如果没CN2的线路是非常慢的,起码不快!哈哈~现在VPS都便宜起码人手一个人有5台以上吧?这样的话我们就可以开整. 开整条件:国内VPS或者阿里云有CN2线路的机器最好是100MB-200MB的带宽的.保证你爽歪歪.但是Sandy在阿里云HK30MB下也测试了也可以达到30000KB以上,羡慕吧~我们可以开始了! 老规则工具先出: 没被gwf和谐的服务器1台(下文统称:主服务器) 国外服务器2台以上(下文统称:从服务器) nginx 1.9.5版本以上(推荐:lnmp1.4) v2ray croe 第一步: 在 主服务器 安装lnmp1.4,这里就不细说教程了.直接去 官方 看教程 安装完毕以后我们来升级Nginx让他支持stream模块 cd lnmp1.4 vi lnmp.conf 在Nginx_Modules_Options=‘’里面加入--with-stream Nginx_Modules_Options='--with-stream’ 然后执行升级命令 ./upgrade.sh nginx  第二步 修改nginx.conf文件 cd /usr/local/nginx/conf/nginx.conf 在 events { use epoll; worker_connections 51200; multi_accept on; } 下面加入 stream{ server{ listen 0.0.0.0:8080; proxy_pass v2ray_backend; proxy_timeout 10m; proxy_connect_timeout 5000ms; } upstream v2ray_backend{ server ...

Mac下Aria2安装及其配置并设置开机启动

1.安装Homebrew 注:(如已安装,请忽略) 打开终端键入以下命令,等待完成 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" 2.安装最新版本aria2并配置 1.终端下输入以下命令 ➜ ~ brew install aria2 2.等待安装完成后,下载附件 Aria2相关配置下载地址 aria2 config 3.建立aria2文件夹 cd ~ // 创建aria2文件夹 mkdir ~/.aria2 4.打开刚才创建好的aria2文件夹 ➜ ~ open ~/.aria2 5.解压下载好的附件,将 下列文件 复制到aria2文件夹内 aria2.conf aria2.log aria2.session com.aria2c.plist com.google.Chrome.mobileconfig 6.将下载文件夹内的 aria2c 文件复制到 /usr/local/Cellar/aria2/1.33.1/bin 下 操作方式,终端输入命令 cd /usr/local/Cellar/aria2/1.33.1/bin 将该文件夹里的 aria2c 文件重命名为 aria2c.bak,再将下载文件夹内的 aria2c 复制进去 注: 1.理由是官方的 aria2c 版本最多支持16线程,sarkrui改好的aria2c版本支持128线程 2.文中所说的1.33.1是我当前从brew上下载下来的版本,实际以你brew下载下来的版本为准,如果和我版本不一致,请修改相关的1.33.1这个路径 7.右键使用文本编辑或sublime打开 aria2.conf 找到如下语句,将hsuyelin(本人电脑用户名)修改为你电脑的用户,修改完成 command+s 保存退出 #文件保存路径, 默认为当前启动位置 dir=/Users/hsuyelin/Downloads # 日志保存路径 log=/Users/hsuyelin/.aria2/aria2.log # 从会话文件中读取下载任务 input-file=/Users/hsu...

Android 用户必备良药「黑域」,专治「全家桶」唤醒烦恼

显然,这不是广告! 这么好的应用,不分享给尾巴们简直说不过去,于是我决定自己动手写一篇关于「黑域」的简单介绍。 那么在聊「黑域」之前,先说一下国产 Android 应用的尴尬之处。注意,以下信息不一定准确。 众所周知,谷歌服务在国内是被你懂的 XX 所干扰,而 Android 系统的诸多服务没有了这些支持的话就会变成残缺状态。比如推送服务,Android 是和 iOS 一样拥有系统层级的推送服务,这项服务叫做 Google Cloud Messaging,现在新名称是 Firebase Cloud Messaging 。 由于这项服务被干扰,国产 Android 应用如果想要推送消息给用户,通常都会选择「小米推送」、「极光推送」、「个推」等第三方推送服务,为了确保服务的可靠性,他们就得让应用处于运行状态,而非彻底从后台任务中清除。 然后,问题就来了。 国内各大手机厂商们为了确保手机运行的流畅性和稳定性,会选择让系统杀掉那些常驻在后台的应用进程。由于各家手机厂商的系统功能完全不同,杀进程的方法也就都不一样。举个例子,采用「小米推送」服务的 Android 应用在 MIUI 下可以获得最高的消息送达率,即使应用被切到后台也没关系,正常情况下它依然能够收到通知。但如果是在 Flyme OS 的话,当这款应用切到了后台那么很可能它的进程就会被杀掉,导致消息无法正常送达。   ▲ 让人又爱又恨的某些国产应用 为此,这些 Android 应用不得不采取一些措施以确保自家应用的推送通知能够送达用户,于是……噔噔噔!掌声有请「全家桶」登场!随后就…… 一个推送通知直接把多个应用同时唤醒,后台电量哗啦啦下降,机身发热,滑动卡顿……天呐,这简直就是灾难。如果手机厂商不针对这种状况做处理,最终结果可想而知。 当然,你可别以为这些国产应用只是为了推送才要常驻后台,比如自动下载安装包什么的,因为 Google Play 商店没有入华,应用也无法实现自动更新。为了确保用户使用最新版本的应用,常驻后台自动下载安装包什么的 ,呵呵呵。 这就是没有谷歌服务的 Android 在中国大环境下的现状,它显得非常具有中国特色,但本质上它是因为先天性缺陷才造成了今天的局面,是的,这一缺陷不是你光靠科学上网就能改变得了。 说到这里,某些非 Android 用户估计又要觉...