2015年6月6日 星期六

SSH ProxyCommand to tunnel connections

在講解使用SSH proxycommand前, 要先談到為什麼需要tunnel機 (或是稱跳板機)

1. 在管理機房的大量機器時, 為了安全性還有Public IP有限的情況下, 我們會將一些機器擺在內網內, 而一般情況下外面的機器連不進裡面的機器, 例如資料庫, 或是一些檔案系統 (NFS)的機器。

2. 當我們的機器越來越多時, 不太可能一台一台的去設定使用者的帳號密碼, 尤其是公司能access機器的人越來越多時, 這是一件不可能的任務 為了解決以上兩點, 我們就要動用tunnel或是稱bastion機的solution, 詳細像下圖

為了要能連進內網, 我剛開始都是用SSH agent forwarding的方式, 這個也是大部份人 比較熟悉的方式, 詳情可以看ihower前輩的講解

而這篇其實是要記錄使用SSH proxyCommand 的方式連結, 先講解如何使用
修改~/.ssh/config
Host bastion
  User                   kakashi
  HostName               tunnel.test.com (or ip address)
  ProxyCommand           none
  IdentityFile           ~/.pem/your-tunnel.pem
  BatchMode              yes
  PasswordAuthentication no

Host *.test.com
  ServerAliveInterval    60
  TCPKeepAlive           yes
  ProxyCommand           ssh -q -W %h:%p bastion
  User                   ubuntu
  IdentityFile           ~/.pem/your-server.pem

在這裡可以先測試看看能不能連上Bastion server
ssh bastion

可以連上後再試試看, 使用
ssh server1.test.com

可以看到我們使用ProxyCommand的方式, 在bastion和server1.test.com中間建立一道proxy 而這邊要注意的的地方, 是需要使用-W的方式, 就可以直接forward你的SSH client, 這部分跟SSH agent forwarding的方式很像

後話: 有人會問, 為什麼要繞一大圈使用ProxyCommand的方式去連接target server呢?原本使用Agent forwarding的方式就不錯了啊。其實有下列兩個原因:

1. 因為最近在學習ansible作部署, 而ansible要透過bastion連接target server 好像只能用這種方式, 因為ssh agent forwarding只能用交談模式, 必須先連上bastion在連上target server

2. 在網路上看到這篇文章, http://heipei.github.io/2015/02/26/SSH-Agent-Forwarding-considered-harmful/ 使用Agent forwarding時, 其實是有security的問題的, 去man ssh就可以看到下列講解

-A  Enables forwarding of the authentication agent connection.  
    This can also be specified on a per-host basis in a
    configuration file.

    Agent forwarding should be enabled with caution.  
    Users with the ability to bypass file permissions on theremote host (for the agent's 
    UNIX-domain socket) can access the local agent through the forwarded connection.
    An attacker cannot obtain key material from the agent, however they can perform 
    operations on the keys that enable them to authenticate using the identities loaded 
    into the agent.


Ref:
http://backdrift.org/transparent-proxy-with-ssh
http://heipei.github.io/2015/02/26/SSH-Agent-Forwarding-considered-harmful/
http://alexbilbie.com/2014/07/using-ansible-with-a-bastion-host/

0 意見:

張貼留言