2012年12月1日 星期六

如何利用virtual box當tcp/ip跳板

前言: 

    想像有一種狀況:當有一個網站的portal(firewall, gateway, vpn)必須執行一些windows特有的小程式才能登入(其實這種狀況其實蠻常見的),而我想access後面的工作站,但我卻不想離開我的Linux時,這時候該怎麼作呢?

畫一個簡易的示意圖如下:
MyLaptop(L)------------------->Portal(P)----->Workstation(W)
Linux      這裡只能由Windows登入

這時候我們就可以利用Virtual Box產生一個虛擬的guest Windows
變成
MyLaptop(L)->VirtualWindows(V)---------->Portal(P)->Workstation(W)
                        這裡只能由Windows登入


1.利用Virtual Box建立guest Windows(V):

首先下載Virtual Box (https://www.virtualbox.org/wiki/Linux_Downloads)和extension pack(http://download.virtualbox.org/virtualbox/4.2.4/Oracle_VM_VirtualBox_Extension_Pack-4.2.4-81684.vbox-extpack)
2.安裝Virtual Box並把network card從NAT改成Bridge Adapter//這步是為了讓Linux能連進Windows
3.安裝Windows
4.從Devices選單選擇Install Guest Additions
5.不要忘了將Windows設密碼這樣才能從外部連入


2. 安裝SSH server on Windows(V):

1. 安裝SSH server,這裡我選擇的是freeSSHd(http://www.freesshd.com/freeSSHd.exe
2. 記得將使用者加入可連入的名單中
3. 測試一下ssh server是否能夠正確動作
:以上只能在XP上使用,在Win7下freeSSHd有權限上的問題,須改用WinSSHD,步驟如下
1. 安裝WinSSHD(http://dl.bitvise.com/BvSshServer-Inst.exe)
2. 選擇personal edition只能使用Windows帳戶登入


3. 設定Linux端的SSH(L):

1. 更改ssh config
$ vim ~/.ssh/config
加入下列幾行
Host MyTarget # put any name you like here
User MyUserName # the username on Target machine
HostName IPAddrOfTarget # IP of Target
Port 22 # port on target
ProxyCommand ssh -W %h:%p UserNameOnBridge@IPAddrOfBridge
或者 
ProxyCommand ssh -W %h:%p  -C -c blowfish UserNameOnBridge@IPAddrOfBridge #用壓縮和較快速的加密減少overhead
WinSSHD使用下列
ProxyCommand ssh -W %h:%p -C -c aes128-cbc UserNameOnBridge@IPAddrOfBridge

2. 測試:
先從V登入Portal後
$ ssh MyTarget
會看到問兩次password,第一次是bridget的password,第二次是Target的password。
如果覺的麻煩可以使用public key authentication的方法

4. 設定Public key(optional):

在本機端產生public key
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ cd ~/.ssh

$ ssh-keygen -t rsa 

這時會產生兩個key
id_rsa和id_rsa.pub
將id_rsa.pub複製到Virtual Windows和遠端機器中
1.將id_rsa.pub放在並改名C:\Program Files\freeSSHd\USERNAME
在freeSSHd的user裡authorization選擇public key
2.在遠端機器(Target)執行下列動作
$ cat id_rsa.pub >> ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/authorized_keys

這樣就可以不用使用password登入了

參考資料:
Forward SSH traffic through a middle machine
Creating a transparent SSH tunnel through a bastion host using the ProxyCommand configuration parameter
How To Set Up SSH With Public-Key Authentication On Debian Etch

沒有留言:

張貼留言