Alanjy-Huang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    3
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Linux ssh 遠端連線 [TOC] ## 指令複習 + netstat -ntupl + `-n` 數字 + `-t` tcp,顯示 TCP 的連線狀況 + `-u `udp,顯示 udp 的連線狀況。 + `-pl `Proto: protocal e.g. tcp, tcp6, udp, udp6 + ifconfig 查看網卡資訊 > 稍微提到前面有講過 ### 情境 在學校想用家裡的好電腦 想使用 server 但又不能直接進去機房 ### 為什麼要遠端連線 + 更好的運算能力 + 方便管理主機 + 透過遠端連線伺服器的服務 ### 登入類型 1. 文字介面明碼: telnet, rsh 等為主,目前非常少用; 2. 文字介面密碼: ssh 為主,已經取代上述的 telnet, rsh 等明碼方式; 3. 圖形介面: Xdmcp, VNC, RDP 等較為常見 ## Telnet - 支援的使用者軟體比較多 - 使用明文來傳輸資料(未加密) - 可能會受到有心人士利用監聽軟體 (e.g tcpdump ) 的監聽竊取 - 例子: 遠端連線進主機時,需要輸入帳號密碼,若沒有加密,帳號密碼都會被別人看光光 - 還有哪些明文傳送? - rsh 一個命令列介面的電腦程式 - ftp 檔案傳輸伺服器 - http ## FTP - 古老的檔案傳輸協定 - 最主要的功能: 讓伺服器與用戶端之間進行檔案的傳輸 - 明文傳輸 (沒有加密) 方式 - 缺點 - 沒加密,安全性不足,如果要登入(帳號密碼)危險 - but 匿名下載就沒差,可以紀錄是誰上傳、下載 - ex : 安裝檔案、下載光碟映像檔 - 有些人覺得不記錄誰下載是基本人權(另一部分) - FTP 沒辦法 cache > 在切換目錄時都得重讀一次檔案,不管是否瀏覽過。 - 但是因為很快,現在還是很常用 - 近年因為匿名性及想 cache 所以慢慢變少 :::info ## wireshark > FIXME: dsniff 自動幫我們找出明文的帳密 - 開源的網路數據封包分析軟體 - 其他類似軟體:tcpdump - 安裝 - `sudo apt update ` - `sudo apt install wireshark` - `sudo usermod -aG wireshark <username>` - `reboot` > DEMO telnet + wireshark REF: [How to Install and Use Wireshark on Ubuntu](https://linuxhint.com/install_wireshark_ubuntu/) ::: ## 加密式連線 :::info ### 資料加密?連線加密? + 資料加密: ftps 將資料加密後經ftp傳送 + 連線加密: sftp 利用加密通道ssh傳送資料 ::: 將人們看的懂得原始電子資料,經過一些運算,讓這些資料變成沒有意義的亂碼 (至少對人類來說),然後再將密文在網路上面傳輸,而當使用者想要查閱這個資料時,再透過解密運算 > SSH 使用上,主要是利用 RSA/DSA/Diffie-Hellman ![](https://i.imgur.com/XF6dmD4.png) + **公鑰 (public key)**:提供給遠端主機進行資料加密的行為,也就是說,大家都能取得你的公鑰來將資料加密的意思; + **私鑰 (private key)**:遠端主機使用你的公鑰加密的資料,在本地端就能夠使用私鑰來進行解密。由於私鑰是這麼的重要, 因此私鑰是不能夠外流的!只能保護在自己的主機上。 :::info ### 不同加密方式 1. 對稱式加密 + 雙方都使用同一把密鑰加解密 ![](https://i.imgur.com/wM9nfL4.png) + 對稱式加密最大的弱點是,要是這把鑰匙在第一次傳送時,被中間人攔截且複製,再原封不動傳給接收方,那之後所有攔截到的加密訊息,都能被輕易破解 2. 非對稱式加密 (RSA) + 雙方各有一對公私鑰,互換公鑰後用對方的公鑰加密 ![](https://i.imgur.com/4rlNFQe.png) + 若單純用公私鑰,仍然會有 MITM 的問題 + 數位簽章可解決 + 傳送方除了使用接收方的公鑰加密外,也使用自己的私鑰對該封加密訊息的 Hash 簽名,以達到二次驗證 > Hash 單向雜湊函數 REF: https://medium.com/@RiverChan/%E5%9F%BA%E7%A4%8E%E5%AF%86%E7%A2%BC%E5%AD%B8-%E5%B0%8D%E7%A8%B1%E5%BC%8F%E8%88%87%E9%9D%9E%E5%B0%8D%E7%A8%B1%E5%BC%8F%E5%8A%A0%E5%AF%86%E6%8A%80%E8%A1%93-de25fd5fa537 ::: :::info ### 如何產生一組公私鑰 `ssh keygen -t rsa` ::: >後面會詳述 ## SSH > 美國安全局已經開發出能夠在 Windows 或 Linux 作業系統中竊取 SSH 對談的工具。 - **S**ecure **SH**ell server - 優點:安全、輕量 - SSH 連線建立過程 (1)遠端主機收到使用者的登入請求,把自己的公鑰發給使用者。 (2)使用者使用這個公鑰,將登入密碼加密後,傳送回來。 (3)遠端主機用自己的私鑰,解密登入密碼,如果密碼正確,就同意使用者登入。 ![](https://i.imgur.com/OZNj39r.png) :::info ### SSH 的安全驗證 1. 基於密碼的安全驗證 + 知道帳號和密碼,就可以登入到遠端主機。 + 可能會有別的伺服器在冒充真正的伺服器 =>「中間人」攻擊 + 可能導致密碼外洩、訊息遭到監控、假傳聖旨等等 :::success + 中間人攻擊 Man-in-the-middle attack,縮寫:MITM + 一般情形 1. B 請求登入,A 將公鑰傳給 B > A (A公鑰) -> B 2. B 用 A 的公鑰加密帳號密碼後傳給 A > B (用 A 公鑰加密帳號密碼) -> A 3. A 用私鑰解開後認證登入 + 交換公鑰時遭受攻擊(為加密的 wifi 存取點) 1. A 的公鑰遭到中間人 C 攔截,並且替換成 C 的公鑰,B 回傳公鑰時同樣被替換成 C 的公鑰 > A (A公鑰 )-> C (C公鑰)-> B > A <- C (C公鑰)<- B (B公鑰) 2. B 用 C的公鑰加密帳號密碼後傳給 C,C隨後用A給的公鑰加密後給A > B (C公鑰) ->C (A公鑰)-> A 3. A當作B成功登入,用他收到的C公鑰來回傳訊息給B,殊不知都給了C > A (C公鑰) ->C (B公鑰)-> B ![](https://i.imgur.com/lP0pke5.jpg) 2. 基於金鑰的安全驗證 + 使用者將公鑰放在需要存取的伺服器上。用戶端軟體向伺服器發出請求時把存在伺服器與發送過來的公有密匙進行比較。如果兩個密匙一致,伺服器就用公鑰加密「質詢」(challenge)並把它發送給用戶端軟體。從而避免被「中間人」攻擊。 + 需要已經伺服器有一把使用者的公鑰才能防 > 無法防止第一次連線就MITM >REF :[SSH的安全驗證](https://www.syscom.com.tw/ePaper_Content_EPArticledetail.aspx?id=84&EPID...) ::: - 安裝SSH `sudo apt install ssh` ssh安裝 :::info ## openssh-server & ssh & openssh-client ??? SSH 使用 client-server 模型,也就是說,要建立一個 SSH 連線,遠端的機器必須跑一個 SSH daemon,而本地機器則要有一個 SSH client 程序。SSH daemon 會預設聽從 TCP port 22 進來的連線,並在認證後提供相對應的環境給使用者。SSH client 則負責使用 SSH protocol 來傳送認證訊息與連線細節給遠端機器。 ![](https://i.imgur.com/fsfzHRS.png) REF: [ssh.com](https://www.ssh.com/academy/ssh) > DEMO `apt-cache search ssh | less` + `apt-cachee search ssh | less` 查看可安裝的ssh 有哪些 + ![](https://i.imgur.com/QsaiDiE.png) + `ssh` Secure SHell client and server (metapackage) + metapackage: 不包含軟體本身的安裝檔,而是指向所有需要的相依檔 > These packages do not contain actual software, they simply depend on other packages to be installed. + REF: [What is the difference between a meta-package and a package?](https://askubuntu.com/questions/66257/what-is-the-difference-between-a-meta-package-and-a-package) :::success ### server client model ![](https://i.imgur.com/jQ2avUa.png) [圖源](https://madooei.github.io/cs421_sp20_homepage/client-server-app/) + 伺服器端的特徵: + 被動的角色(從)。 + 等待在設定好的port等待ssh 來連接 + 等待來自使用者端的請求。 + 處理請求並傳回結果。 + 回傳public key > REF: [ssh server doc](https://www.ssh.com/academy/ssh/server) + 使用者端的特徵: + 主動的角色(主)。 + 驗證server 的 pulic key 確定安全無誤 + 發送請求。 > REF: [ssh clinet doc](https://www.ssh.com/academy/ssh/client) ::: ::: > DEMO all ssh command - `sudo service ssh status ` 確認ssh server狀態 > FIXME: systemctl service ::: info ### systemctl service + system V 以前AT&T開發的一種啟動服務的架構,會依照系統給的啟動順序啟動service + system D 改進後的架構,依照服務內的需求啟動(只開啟服務敘述的需求) -> 較快速 + service 最早用來呼叫system V但現在也支援system D,可是呼叫system D時只能呼叫start status 等簡單指令 + systemctl 用來呼叫system D 的指令,有很多system V 沒有的酷東西 ::: - `sudo netstat -tnlp | grep ssh` 可以看到ssh是在 22 port 登入 > `sudo netstat -tnlp | grep :22` 查看 22 port 狀態 ![](https://i.imgur.com/bKSjrKw.png) - `ssh 127.0.0.1` 登入本機 - `ssh <userName>@localhost` 登入本機 > 若不指定 userName 則會以本機目前的使用者名稱去登入 遠端主機 - `ssh <userName>@<user_ip>` :::info 127.0.0.1 == localhost == 本機 ::: - `ssh -p <port num> <user name>@localhost` :指定 server 的 port - `ssh -v <user name>@localhost`: 偵錯模式,會印出執行中發生的事 ![](https://i.imgur.com/hZyhGXt.png) - `ssh -f <user name>@localhost 指令`: 只做一次指令就登出(結束連線) ![](https://i.imgur.com/uzJlqtn.png) - `ssh -A ` 允許 forwarding ssh - `ssh -o ConnectTimeout=3 <IP>`:等待連線時間超過3秒就斷線 - `ssh -X <user name>@<ip>` 遠端打開圖形化介面視窗 - gnome-terminal 如有在 server 跳出視窗則成功 - `exit` 退出此次連線 ### 使用憑證來遠端連線 > DEMO 全部步驟 + 什麼是憑證 + 一個可以證明自身身份的東西,like 身份證 1. 產生一對鑰使 `ssh keygen` * `-t` 指定用哪種加密方式,預設為 rsa * 以安全性推薦使用 ed25519 代替 rsa,但舊的伺服器可能只支援 rsa * passphrase 用來產生憑證的 seed ![](https://i.imgur.com/56Y0z4p.png) ::: info * 注意 .ssh 資料夾和公私鑰的權限!!!!! `chmod 700 ~/.ssh/ ` `chmod 600 ~/.ssh/*` 確保其他人看不到你的鑰匙 ::: 2. 產生的鑰使預設在 `~/.ssh/` * 顯示隱藏資料夾 `ls -al` * `id_rsa`:私鑰 -> 留在主機 > 再次強調不要把私鑰給別人 * `id_rsa.pub` : 公鑰 -> 傳出去 * 注意若要複製私鑰 頭尾 `-----BEGIN OPENSSH PRIVATE KEY------` 也要複製 ![](https://i.imgur.com/a4Q2iS4.png) 3. 複製公鑰到遠端主機 * 方法一 * 在遠端主機新增檔案 `touch authorized_keys` > 在 `~/.ssh/`中 * 將 `id_rsa.pub` 公鑰放入 `authorized_keys`中 `sudo vim authorized_keys` * 方法二 * 用 ssh 傳送公鑰到遠端主機 `ssh-copy-id -i ~/.ssh/id_rsa.pub <usr>@ip` 4. 直接 ssh 到遠端主機 * 其他方式 `ssh -i ~/.ssh/id_rsa.pub <usr>@ip` ### 公鑰認證檔 * 當你登入遠端伺服器時,本機會主動的用接收到的伺服器的 public key 去比對 `~/.ssh/known_hosts` 有無相關的公鑰, 然後進行底下的動作: > FIXME: 再提一次 MITM * 若接收的公鑰尚未記錄,則詢問使用者是否記錄。若要記錄 (範例中回答 yes 的那個步驟) 則寫入 `~/.ssh/known_hosts` 且繼續登入的後續工作;若不記錄 (回答 no) 則不寫入該檔案,並且離開登入工作; * 若接收到的公鑰已有記錄,則比對記錄是否相同,若相同則繼續登入動作;若不相同,則出現警告資訊, 且離開登入的動作。這是用戶端的自我保護功能,避免你的伺服器是被別人偽裝的。 > FIXME: 強調記錄對方的公鑰 * `~/.ssh/known_hosts` * 查看認證過得公鑰(對方已經登入的公鑰) ![](https://i.imgur.com/OgYA6A9.png) > FIXME: 先嚇人再解決 :::info ### 利用 Windows 遠端登入 + Windows 10 之前 Windows 不支援 ssh,可使用putty 遠端連線 + Windows 10 之後可直接用 cmd 的原生 ssh 連線,指令與linux相同 > Ref: https://learn.microsoft.com/en-us/windows/terminal/tutorials/ssh ::: ### ssh 設定檔 #### Client - ssh 相關設定檔放在 `/etc/ssh` - ssh_config : 針對 client 端的設定檔 - 查看 `sudo vim /etc/ssh/ssh_config` ![](https://i.imgur.com/OQmpYcZ.png) - 藍色字為預設的狀態 - ForwardAgent 驗證代理 - ![](https://i.imgur.com/ktKH4f1.png =150x) - 讓本地的 SSH key 可以在遠端 server 轉送 - PasswordAuthentication 允許帳號密碼來做登入 - CheckHostIP 發現已連過主機 ip 位址不同就會提醒 - Port 22 : 預設會連線到 server 的 port - IdentifyFile 預設查找放私鑰的文件路徑 > ![](https://i.imgur.com/7t9gcF1.png =250x) - Cipher - 設定加密演算法 (愈前面代表愈先使用) ` Ciphers aes128-ctr,aes192-ctr,aes256-ctr,aes128-cbc,3des-cbc` #### server - sshd_config : 針對 server 端的設定檔 - 查看 `sudo vim /etc/ssh/sshd_config` - 若這台電腦扮演 server 端,相關設定都存在這裡 ![](https://i.imgur.com/3otWeKX.png) - AllowAgentForwarding 允許 Forwardagent - port 22 其他人連進來的 port - ListenAddress 所有介面卡都會去監聽 ssh 的要求 - HostKey: server 端的私鑰 - ![](https://i.imgur.com/nMQavtX.png) - PermitRootLogin **prohibit**-password: 禁止 root 權限登入 - PubkeyAuthentication: 能不能允許使用者產生公鑰私鑰 - PasswordAuthentication: 可不可以用~~憑證~~帳號密碼進行登入 :::info ### Include `/etc/ssh/sshd_config.d/*.conf` 如果修改過 ssh_config 的話,會需要重新啟動ssh才能更新內容(啟動中修改容易產生錯誤),所以現在都不讓你改 ssh_config了。取而代之直接多用一個 ssh_config.d/*.conf 讓你在裡面改 ::: ### ssh 連線步驟細節 1. 驗證階段 1. 第一次連接的話server請clinet 提供public key > 放在know_hosts 2. 若不是則去~/.ssh 看 know_hosts 檢查public key是否相同 2. 產生ssesion key + 利用Diffi-Hellman產生一組相同的session key 用來加解密(對稱式) ::: info Diffi-Hellman ![](https://i.imgur.com/nymvbNo.png) >[圖片來源](http://www.tsnien.idv.tw/Security_WebBook/chap3/3-6%20Diffie-Hellman%20%E9%91%B0%E5%8C%99%E4%BA%A4%E6%8F%9B%E6%B3%95.html) ::: 3. 驗證 ssesion key和身分 > ssh公私鑰認證 1. client 傳送一組要登入的key id 2. server 看 authorized key 裡面有沒有已經存在的key id(憑證登入) 3. server 如果有找到對應的public key則用他加密一組隨機數字 4. clinet 用prvite key 解密後 把隨機數字跟 session key 一同用MD5 hash 傳送出去 5. 若兩者皆相同則代表可以開始傳送資料 ### Forward Agent > DEMO ![](https://i.imgur.com/AKcWKE2.png =400x) SSH agent forwarding 可以讓本地的 SSH Key 在遠端 Server 上進行轉送,也就是當你需要在選端 Server 上使用 SSH Key 時,就不需要將你的 key pair 手動複製到 server 上,是個既方便又安全的作法。 1. `eval $(ssh-agent)` 啟動ssh-agent 2. `ssh-add ~/.ssh/id_rsa` 將要 forwarding 的 private key 加到清單裡面 4. `ssh-add -L` 檢視是否新增成功 5. `ssh -A user@<server1 ip>` + 或是直接修改 ssh_config 的ForwardAgent + `Host serverA ` `HostName 192.168.0.1` `ForwardAgent yes` + ![](https://i.imgur.com/ktKH4f1.png =150x) > ssh-agent 幫你轉送key的協議 ## ssh 相關指令 利用 ssh 建立的通道來進行其他功能 ### scp > DEMO - 可以在不同的 Linux 主機之間複製檔案 - s = SSH - cp = copy - 上傳檔案 - `scp <要上傳的檔案路徑> <使用者>@<server IP>` - 下載檔案 - `scp <使用者>@<server IP>:<伺服器上檔案路徑(來源)> <把抓到的資料放本機的哪(目的)>` - `-r` recursion,目錄底下的檔案 ### sftp > DEMO SFTP與FTP有著一樣的語法和功能,只不過SFTP是SSH中的一部分而已,並且其具有互動性。 - ` sftp [-P] [user_name]@[ip_address] #-P -- 指定連線port` - `get [遠端路徑] [本地路徑]` 下載檔案 - `put [本地路徑] [遠端路徑]` 上傳檔案 - `lcd` 變換本地端機器的目錄 - `lpwd` 列出本地端機器目前所在的目錄名稱 - `exit` 退出 ## ssh tunnel SSH Tunnel 是一種基於 SSH Protocol 所延伸出來的應用 主要運用 : + 在兩個端點間建立安全的存取通道讓一些不具有加密存取能力的通訊協定,可以被包覆在ssh協定之中(將通訊內容加密避免洩漏) + 讓兩端的通訊能夠穿透過某些限制(例如防火牆) + 類似VPN ::: info ### VPN 將專用網路延伸到公共網路上,使使用者能夠在共享或公共網路上透過專用的tunnel傳送和接收資料,就像他們的電腦直接連接到專用網路上一樣,VPN的好處包括增加專用網路的功能、安全性和管理。 ::: ![](https://i.imgur.com/Z2qJhvA.png) + ssh Tunnel 範例 + ssh client 和 ssh server 建立連線架起 tunnel + tunnel啟用的情況下,application 對 ssh sever 開啟的port 啟動連線, + ssh client 藉由加密通道將 application 傳送至 ssh server + ssh server 在連結到真正的 application server (通常兩者會在同一台機器) + 在上述情況下,就可以在不更動application server下讓application 跟 server 加密通訊 + port forwarding 的角色 + Client + 任何你可以敲 ssh 指令來啟動 Port Forwarding 的機器 + SSH Server + 可以被 Client 用 SSH 連進去的機器 + Target Server + 某一台你想建立連線的機器,通常是為了對外開放這台機器上的服務 > 注意, Client 與 SSH Server 本身都可以是 Target Server ,不是真的要有三台機器才可以進行 Port Forwarding! :::info ### nginx + install `sudo apt install nginx` + start `service nginx start` + look up `sudo netstat -ntupl` ![](https://i.imgur.com/7cnLAkU.png) + change port `sudo vim /etc/nginx/sites-enabled/default` ![](https://i.imgur.com/Ih1KUkb.png) ::: ### Local Tunnel (Local Forwarding) + Local Tunnel 就是將一個ssh server 接收到的port 轉送至背後要使用的server + 使用情境 1. 在建立tunnel 或是檔案傳輸經過跳板機 2. 在外網與內網的服務串連 3. 連結一個遠端的雲端資料庫 + `ssh -L <local IP>:<local port>:<target IP>:<target port> <target server user name>@<target IP>` + 指令參數 - `-N` : 不要另外開啟一個 Shell,ssh 連線後不要執行任何的 command - `-f` : 在背景執行 - `-L` : Local Tunnel - local IP 如果不寫的話,預設就是本機端 localhost #### 情境一 連到位在防火牆後的開發伺服器上的服務 ![](https://i.imgur.com/p0XGFVA.png) 防火牆後有一台你的伺服器,服務在8080 port 開啟,防火牆只開放22 port ![](https://i.imgur.com/iBnyLMV.png) 依靠ssh server幫你轉送資料到8080 port + `ssh -L 9090:localhost:8080 demo@192.168.0.127 -f` + Client:你的電腦 > :9090 + SSH Server:防火牆後的伺服器 > 192.168.0.127 + Target Server:防火牆後的伺服器 > 192.168.0.127:8080 #### 情境二 透過防火牆後的機器,連到防火牆後的特定服務 ![](https://i.imgur.com/LU7WMD8.png) 防火牆內同樣有一台你的機器,但是你沒有存放服務機器的權限,無法直接ssh 服務 ![](https://i.imgur.com/uc9jPgH.png) 藉由你的機器當跳板存取服務 + `ssh -L 9090:192.168.0.127:8080 jump@192.168.0.130 -Nf` + `ps aux | grep ssh `可看是否建立好tunnel + `kill {pid} `刪除通道 :::info `ps -aux` 列出執行中的process + -A :所有的 process 均顯示出來,與 -e 具有同樣的效用; + -a :不與 terminal 有關的所有 process ; + -u :有效使用者 (effective user) 相關的 process ; + x :通常與 a 這個參數一起使用,可列出較完整資訊。 + `USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND` + SER: process擁有者 + PID: pid + %CPU: 佔用的 CPU 使用率 + %MEM: 佔用的記憶體使用率 + VSZ: 佔用的虛擬記憶體大小 + RSS: 佔用的記憶體大小 ::: + Client:你的電腦 > :9090 + SSH Server:防火牆後你的機器 > jump@192.168.0.130 + Target Server: 防火牆後的伺服器 > 192.168.0.127:8080 ### Remote Tunnel (Remote Forwarding)(Reverse Tunnel) + 和 Local Tunnel 相反,把本地機器上的服務 port 導入到 server 讓他連線。 在 Server 端(80 port)想讀取 Client 端 (8080 port)的本機服務 + `ssh -R <server IP>:<server port>:<Client IP>:<Client port> -Nf <Clinet server user>@<Client server IP>` + 注意需要更改 sshd_config 內的 `GatewayPorts yes` ![](https://i.imgur.com/MqavQge.png) #### 情境一 透過對外機器,讓其他人能夠連到你的電腦上的服務 ![](https://i.imgur.com/O9CEhG2.png) 你將一個服務架在8080 port,想讓其他人使用但你的電腦只有內部ip ![](https://i.imgur.com/P1n4reQ.png) 藉由一台有外網IP的機器開啟上面的port,讓他轉送到8080 + Client:你的電腦 + SSH Server:對外的機器 > 192.168.0.130:9090 + Target Server: 客戶的電腦 > 192.168.1.100:8080 `ssh -R :9090:localhost:8080 jump@192.168.130` #### 情境二 透過對外機器,從外面連回內部網路上的服務 跟情境一類似,但這時候服務不在你的電腦 ![](https://i.imgur.com/N1MsJ4l.png) `ssh -R 9090:<insternel server ip>:8080 jump@192.168.130` ### Dynamic forward 只要有一台位於內網且具有外部 IP 的機器,你就可以利用這個方法建立一個 HTTP 代理伺服器,讓你能夠從外面連回內網的所有 HTTP(S) 服務 (跟 local forward 的差別,是可以前往任意網站) (有點像是 VPN) ![](https://i.imgur.com/pyLp5kd.png) + local tunnel 是一開始連線的時候就有指定的 port + 沒有指定目的地的 port 的話可以用 Dynamic forward >REF: https://www.redhat.com/sysadmin/ssh-dynamic-port-forwarding ### port attacking + 掃瞄一台電腦所有的連接埠,看看哪些連接埠有開啓,而每個開啟的連接埠也會對應一個使用這個連接埠的應用程式,通常一些常見的應用程式其開啓的連接埠都是固定的,攻擊者就可以從中挑選一些安全性較低、容易入侵的程式進行攻擊,透過這樣的方式入侵到系統中 + 解決方式: firewall, tcp wrapper, port knocking(現在很少用) :::info #### port knocking + Port Knocking 指的是 Client 必須用特殊的順序來對 SSH Server 上的某些 Port 發出連線請求後,SSH Server 才會開放 Client 連線的技巧(比如依序對 Port 1000、2000、3000 發出請求,才會對你開放 Port 22)。這樣的好處是平時 Port 22 就會是關閉的狀態,讓攻擊者以為 SSH 沒有開放,減少被攻擊的機會。 ![](https://i.imgur.com/CdbkflI.png=150x) > [圖片來自](http://blogger.gtwang.org/2013/10/port-knocking.html) + 優點 + 有效防止port attacking + 容易移植到其他應用程式 + 缺點 + 伺服器必須告知 client 暗號是什麼 + 暗號系統若故障影響重大 ::: REF: + [SSH Tunneling (Port Forwarding) 詳解](https://johnliu55.tw/ssh-tunnel.html) + [ssh document](https://www.ssh.com/academy/ssh/tunneling-example) + [port knoking](http://blogger.gtwang.org/2013/10/port-knocking.html)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully