SSH Tunnel On Linux Over Http Proxy

SSH Tunnel On Linux Over Http Proxy

甚麼是 SSH Tunnel?其實我在以前的文章也介紹過,網路上的資源也相當豐富,設定方法不只在之前的文章有提到,網路上的教學也是到處都是,在 Linux 中設定 SSH Tunnel 其實也不會太難,-D 和 -R 的參數網路上教學文章也不少,只是透過 http proxy 再連出去的文章就比較少了,我先前的文章 SSH Tunnel 帶你遨遊 內介紹的是利用 putty 先連過要認證的 http proxy,之後在利用特定 ip 連線後轉 port  的方式來達到可以成功 ssh,只要能 ssh 連線的話建立 SSH Tunnel 私密通道也不會是啥大問題。

前段文章提到 ssh command -D 和 -R 的參數應用,這兩個參數的應用都是在可以直接 ssh 後的進階運用,若今天一切的連線都只能透過 http proxy 出去的話,要建立通道就不是那樣的簡單,而且網路上得中文資源不足,我稍微研究之後在此跟大家分享一些心得。

這次練功所要用到的軟體是 ProxyTunnel,比較可惜的是在 Ubuntu 上無法直接安裝,若是 RedHat 系列的話還有快速的 RPM,所以今天只好自己編譯安裝囉,相信自己編譯安裝不是那麼可怕的!

(此時好死不死這篇文章就此撰寫停頓,這個編譯我搞哩好久,後來問題還是出在一些編譯的軟體身上,開發套件果然還是都裝一裝比較好,編譯就比較不會有問題。)

把簡易的資訊列出來給大家看下,讓家知道到底有哪些參數可以利用
$ proxytunnel
Proxytunnel 1.8.0 (rev 0)
Copyright 2001-2007 Proxytunnel Project
Jos Visser (Muppet) <[email protected]>, Mark Janssen (Maniac) <[email protected]>

Purpose:
  Build generic tunnels trough HTTPS proxy's, supports HTTP authorization

Usage: Proxytunnel [OPTIONS]...
   -h         --help              Print help and exit
   -V         --version           Print version and exit
   -i         --inetd             Run from inetd (default=off)
   -a INT     --standalone=INT    Run as standalone daemon on specified port
   -e         --encrypt           Encrypt proxy<->destination using SSL
   -E         --encrypt-proxy     Encrypt client<->proxy (proxy talks SSL)
   -x STRING  --proctitle=STRING  Set the process-title to STRING
   -p STRING  --proxy=STRING      Proxy host:port combination to connect to
   -d STRING  --dest=STRING       Destination host:port to built the tunnel to

Parameters for proxy-authentication (not needed for plain proxies):
   -u STRING  --user=STRING       Username to send to HTTPS proxy for auth
   -s STRING  --pass=STRING       Password to send to HTTPS proxy for auth
   -U STRING  --uservar=STRING    Env var with Username for HTTPS proxy auth
   -S STRING  --passvar=STRING    Env var with Password for HTTPS proxy auth
   -F STRING  --passfile=STRING   File with credentials for proxy auth
   -N         --ntlm              Use NTLM Based Authentication
   -t STRING  --domain=STRING     NTLM Domain (default: autodetect)
   -r STRING  --remproxy=STRING   Use a remote proxy to tunnel over (2 proxies)
   -H STRING  --header=STRING     Add STRING to HTTP headers sent to proxy

  If you don't provide -s or -S you will be prompted for a password.

Miscellaneous options:
   -v         --verbose           Turn on verbosity (default=off)
   -q         --quiet             Suppress messages  (default=off)

Examples:
Proxytunnel [ -h

-V ]

Proxytunnel -i [ -u user ] -p proxy:port -d host:port [ -v

-q ]

Proxytunnel -i [ -U envvar ] -p proxy:port -d host:port [ -v

-q ]

Proxytunnel -a port -p proxy:port -d host:port [ -v

-q ]
若想仔細瞭解這些參數的話可以慢慢研究,我想可以研究的可多哩!

使用的方式大家可以到官方頁面去參考,相信裡面的資訊應該是挺豐富的,我這裡要回到主題上的 SSH Tunnel On Linux Over Http Proxy,前一篇文章有提到 Text Mode Use Http Proxy On Linux ,若設定 ssh 利用 http_proxy 來連線有前篇文章簡單就好了,可惜就是沒有 ssh_proxy 的設定可以利用,因此要自己要在做一些特殊的設定。

ssh 有幾個簡單的設定檔,/etc/ssh/sshd_config 是給 ssh server 用的設定檔,/etc/ssh/ssh_config 是給連線出用的設定檔,個人家目錄的設定檔得寫在 ~/.ssh/config,這檔案不會自動產生得自己去寫才可以,以下設定是參照這裡

$ cat config
#自己設定一個 port 給建立 tunnel 後來 listen
DynamicForward 8080
ProxyCommand proxytunnel -v -u 帳號 -s 密碼 -p 需要帳密的代理伺服器:port -d %h:%p -H "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)\n"
ServerAliveInterval 30

要測試看看連線嘛?當然要啊~不然設定幹嘛?

$ ssh wawa@遠端ssh
伺服器 -p port
Proxy basic auth is dmRjaGlhaHNpbmxpdTaaNDIxMDMwNg==
Connected to
需要帳密的代理伺服器:port (local proxy)
Tunneling to
遠端ssh伺服器:port (destination)
Connect string sent to local proxy:
-> CONNECT
遠端ssh伺服器:port HTTP/1.0
-> Proxy-authorization: Basic
dmRjaGlhaHNpbmxpdTaaNDIxMDMwNg==
-> User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Win32)\n
-> Proxy-Connection: Keep-Alive
Data received from local proxy:
<- HTTP/1.1 200 Connection established
Tunnel established

之後輸入遠端ssh伺服器帳號密碼,若登入了就是 ok 囉

接下來當然也要驗證給大家看本機是有在 listen 自己設定的 8080 port 的

$ netstat -tnl

grep 8080

tcp        0      0 127.0.0.1:8080          0.0.0.0:*               LISTEN    
tcp6       0      0 ::1:8080                :::*                    LISTEN

以上的步驟來是要提醒大家一個重點,因為是透過 http proxy 連線的,若沒有開放 80 port 以外的對外連線,而你的 ssh server 又是開預設的 22 port ,那一定是連不到的唷!所以比較方便的方式是利用 iptable 的 redirect 的功能來轉 port,這方式也在先前文章 SSH Tunnel 帶你遨遊 內有提到,有需要在回頭看囉!

基本上這跟 windows 上連線的不同就在於 windows 內有 putty 可以用,putty 可以先設定連線方式,也就是可以輕鬆搞定 http_proxy 那個環節,在 linux 上我是還沒有看到類似 putty 類似的工具可以先設定連線方式再 ssh,於是才會透過 proxytunnel 的方式來進行連線,不知道看文章你的懂哩沒有?不懂也沒有關係,因為沒用到懂哩也沒用,需要時在查找就好,不然那麼多資訊文章哪裡記的住啊?對吧!

Text Mode Use Http Proxy On Linux 1

Text Mode Use Http Proxy On Linux 1 相關文章


參考內容推薦

Linux curl command with proxy

2012年2月25日 - General way: export http_proxy=http://your.proxy.server:port/. Then you can connect ... How can I use it with the curl command, for example: curl ... should be either &quot;curl -x 122.72.2.200:80 mysite.com/test.php?id=1&quot;; or &quot;cur

How to set proxy environment variable in Unix/Linux

2014年6月3日 - How To Use Proxy Server To Access Internet at Shell Prompt With http_proxy Variable. by Vivek Gite on ... export http_proxy=http://proxy-server.mycorp.com:3128/ ... How do I use password protected proxy server using various command line optio

apt - Setting proxy from terminal

I have tried setting the proxy via bashrc file but that din work either. As far as I ... edited Feb 26 &#39;14 at 10:46 &middot; Dan. 4,63823360. asked Jul 2 &#39;12 at 8:44. baltusaj. 6831713 ... See https://help.ubuntu.com/community/EnvironmentVariables

Use PuTTY as a secure proxy on Windows

Last month, I wrote about using OpenSSH as a secure Web proxy on UNIX and Linux systems. This time, I&#39;ll show you how to do the same thing on Microsoft Windows using PuTTY ...

10. Using yum with a Proxy Server

Using yum with a Proxy Server By default, yum accesses network repositories with HTTP. All yum HTTP operations use HTTP/1.1, and are compatible with web proxy servers that support this standard. You may also access FTP repositories, and configure ...

14.04 - Systemwide proxy settings in ubuntu

2015年8月23日 - I wanted to use internet on my college proxy server which also requires authentication. ... mode &#39;manual&#39; ; gsettings set org.gnome.system.proxy.http host &#39;$1&#39;; ... And then replace all the existing text by the following line

internet - How to use a proxy on the command line?

How can I use apps such as wget &amp; ping on the command line with a proxy? ... share|improve this question. asked Jun 7 &#39;11 at 4:18. consindo. 146117 ... host &#39;&#39; gsettings set org.gnome.system.proxy.ftp port 0 #Setting the Dynamic socks prox

SSH Tunnel On Linux Over Http Proxy

... 應該是挺豐富的,我這裡要回到主題上的 SSH Tunnel On Linux Over Http Proxy,前一篇文章有提到 Text Mode Use Http Proxy On Linux ,若設定 ssh 利用 http_proxy 來連線有前篇文章簡單就好了 ...

How To Set A Proxy For The Terminal [Quick Linux Tip] ~ Web Upd8 ...

2010年10月11日 - Here is an example. If you&#39;ve set up TOR and you want to use it for the terminal, to set a HTTP and HTTPS proxy for the current terminal session&nbsp;...

Browse The Web Faster, Use Text Mode Browsers

Tips, News, Tutorials, Reviews about Linux, Open Source Software, Ubuntu, Google, Chrome, Android, Apple, Programming, Gadgets, and all things tech. ... Browse The Web Faster, Use Text Mode Browsers: Did you know that you can surf the Internet in text ...