NCNU-OpenSource
      • 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
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners 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 New
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
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
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `1111` `lsa` `ncnu` # Week 06 (2022/10/20) [TOC] - Book mode: https://hackmd.io/@ncnu-opensource/book --- ## Sticky bit - `vim SUID.c` ```c----= #include <stdio.h> #include <unistd.h> int main() { printf("The uid is: %d\n", geteuid()); return 0; } ``` - `gcc SUID.c -o whoareyou` - 執行檔 ![](https://i.imgur.com/FxU3630.png) - `4755` 的 `4` 會將執行檔案的人(x) 改成檔案擁有者的身分 ![](https://i.imgur.com/JVx2sji.png) - `-t`代表的是讓擁有那個檔案權限的人和 root 可以移除掉檔案 - stick bit - `ifconfig` : 可以看電腦的網路資訊 - `127.0.0.1`: 代表是 local host - `netstat` : 也可以看一些網路資訊的東西 - `|`: 把前一個指令的輸出當作下一個指令的輸入 - `netstat -ntupl | awk '{print $1 "\t" $4}'` - ![](https://i.imgur.com/XGrM8Uf.png) - 我只要印第一行 ~ 第四行 - `$`: 對應到第幾列 ## Linux ssh 遠端連線 - 前置設定:將網卡設定成橋接介面卡 ![](https://i.imgur.com/cFFdcHI.png) ### 指令複習 - `netstat -ntupl` - `-n`: 數字 - `t`: `tcp`,顯示TCP的連線狀況 - `-u`: upd,顯示UDP的連線狀況 - `pl`: protocal - ifconfig - 查看網卡 ### 情境 在學校享用家裡的好用電腦 想使用`sever`但又不能直接進去機房 ### 登入類型 1. 文字介面明碼 2. 文字介面密碼 3. 圖形介面 ### Telnet - 支援的使用者軟體比較多 - 明文傳輸(未加密) - 還有哪些使用明文傳輸? - rsh 一個命令列介面的電腦程式 - ftp 檔案傳輸伺服器 - http ### FTP - 古老的檔案傳輸協定 - 最主要的功能: 讓伺服器與用戶端進行檔案的傳輸 - 明文傳輸 - 缺點 - 沒加密,安全行性不足,如果要登入(帳號密碼)危險 - BUT 匿名下載就沒差,可以記錄是誰上傳、下載 - ex: 安裝檔案、下載光碟映像檔 - FTP 沒辦法 cache > 在切換目錄時都得重讀一次檔案 - 但因為很快,現在還是常用 :::info **wireshark** - 可以監聽封包的工具 ::: ### 加密式連線 :::info **資料加密?連線加密?** - 資料加密: ftps 加密後經 ftp 傳送 > 把**資料本身加密**,透過 ftp 傳送,收件者收到後要再把資料解密 [color=skyblue] - 連線加密: sftp 利用加密通道 ssh 傳送 > 使用**加密通道 SSH**來傳送資料,資料本身並沒有加密 [color=skyblue] ::: 將人們看得懂的原始電子資料經過一些運算讓這些資料變成沒有意義的亂碼(至少對人類來說),然後再將祕聞在網路上面傳輸,而當使用者想要查閱這個資料時,再透過解密運算 >SSH使用上,主要是利用RSA/DSA/Diffile-Hellman ![](https://i.imgur.com/1u5LhuP.png) - 公鑰 public key - 提供給遠端主機進行資料加密的行為,也就是說,大家都能取得你的公鑰來將資料加密的意思。 - 私鑰 private key - 遠端主機使用你的公鑰加密文件後,傳輸資料給你,你再用你那把公鑰相對應的私鑰進行解密 - 私鑰絕對不能外流,只能保護在自己的主機上 :::info **不同的加密方式** 1. 對稱式加密 > 兩個人都拿著同一把 key ![](https://i.imgur.com/GvoYbaH.png) 2. 非對稱式加密 (RSA) - 雙方各有一對公、私鑰,公鑰和私鑰是一組的 > 我拿著檔案用 A 的公鑰做加密,A 收到後,A 再拿自己的私鑰做解密 - 和 N 個人聯繫,就要有 N 把鑰匙 ![](https://i.imgur.com/B4McSMp.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) ::: ## SSH > 美國安全局已經開發出能夠在 windows 或 Linux 作業系統中竊取 SSH 對談的工具 - **S**ecure **SH**ell server - 優點 : 安全、輕量 - SSH 建立過程 1. 遠端主機收到使用者的登入請求 2. 使用者使用這個公鑰,將登入密碼加密後傳送回來 3. 遠端主機用自己的私鑰,解密登入密碼,如果密碼正確,就同意使用者登入 ![](https://i.imgur.com/tTEqz0r.png) :::info ### SSH的安全驗證 1. ==**基於密碼的安全認證**== - 知道帳號密碼,就可以登入遠端主機 - 可能會有別的伺服器再冒充真正的伺服器 => 中間人攻擊 - 可能導致密碼外洩,訊息遭到監控,假傳聖旨等 > 有 MITM 的問題 2. ==**基於金鑰的安全認證**== - 使用者將公鑰放在需要存取的伺服器上 - 需要已經伺服器有一把使用者的公鑰才能防 >[ssh兩種認證方式的原理介紹](https://www.796t.com/content/1541397366.html) ::: - 安裝SSH : `sudo apt install ssh` - `apt-cache search ssh | less` 查看可安裝的SSH有哪些 :::success #### 中間人攻擊(MITM) ![](https://i.imgur.com/jkDrLdB.png) - 交換公鑰時受到攻擊(為加密的wifi存取點) 1. A的公鑰遭到C攔截,並且替換成C的公鑰,B回傳公鑰時同時被替換成C的公鑰 > A(A公鑰) $\to$ C(C公鑰) $\to$ B > A $\to$ C(C公鑰) $\to$ B(B公鑰) 2. B 用 C 的公鑰加密帳號密碼後傳給 C, C 隨後用 A 給的公鑰加密後給 A > B(C公鑰) $\to$ C(A公鑰) $\to$ A 3. A 當作 B 登入,用其他收到的 C 公鑰來回傳訊息給 B,殊不知都給了 C > A(C公鑰) $\to$ C(B公鑰) $\to$ B ::: :::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/RvM7Cs6.png) - `apt-cachee search ssh | less`: 查看可安裝的 ssh 有哪些 - ![](https://i.imgur.com/uiHUB3U.png) - `ssh` : Secure Shell client and server - 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) - server client model ![](https://i.imgur.com/6nuGjsu.png) - server(伺服器端): - 被動等待連線請求 - 再設定好的 port 等待SSH來連線 - client(使用者端): - 主動請求 - 驗證server的public key確定安全連線 ::: ### SSH 指令 - `sudo service ssh status` : 確認 SHH 當前狀態 - ![](https://i.imgur.com/A3HEAV6.png) - 預設 port 為 22 - `sudo service ssh start` : 打開 SHH - ![](https://i.imgur.com/0t5qwFJ.png) - `sudo service ssh stop` : 關掉 SHH - `ssh 127.0.0.1`: 登入本機 - `ssh -p <port number> 127.0.0.1`:指定特定port連到本機 - `exit`: 離開連線 - `sudo netstat -tnlp | grep ssh` ![](https://i.imgur.com/GtKetp7.png) - 22: port22 - tnlp: tcp連線 - `ifconfig`: 可以查看ip位址 - `who`:可以看現在有誰連到這台host(因為兩台名子一樣用whoami結果一樣) ### B ssh 連到 A 步驟: :::info **兩台都要裝 SSH** 1. 在 **A** 利用 `ifconfig` 查看 **A** 的 ip(inet後面那串數字) ![](https://i.imgur.com/djiG47Y.png) 2. 確認 **B** 的 ssh 開了,再下指令: `ssh <A的username>@<A的 ip>` ![](https://i.imgur.com/fblQOcg.png) 3. 查看有沒有連線成功,在 **B** 下指令: `whoami` - 若連線成功,會回覆的則是 A 的 username 4. 連完想要離開,在 **B** 下指令: `exit` - 此時再用 `whoami` 查看使用者,會回覆的則是 B 的 username ::: - `w`: 可以查看現在有甚麼使用者 - A: ![](https://i.imgur.com/ZP9E4fq.png) > 10.105.24.185 就是正在連線到 A 的 B 的 ip >> B exit 後下 `ifconfig`: >> ![](https://i.imgur.com/QHdC0jO.png) - `ssh -p <port num> <user name>@<ip>` - `ssh -v <user name>@localhost`: - 偵錯模式,會印出執行中的事,可以觀察整個連線過程 - `ssh -f <username>@<ip>`: - 只做一次就登出(結束連線) - `ssh -A`: - 允許 forwarding ssh - `ssh -o ConnectTimeout=3 <IP>`: - 等待時間超過三秒就斷線 - `ssh -X <username>@<ip>`: 遠端打開圖形化介面視窗 - `gnome-terminal`: 開一個新的 terminal,若有在 server 端成功跳出新的 terminal 視窗就代表連線成功 - `ssh -N`: - 不執行命令,用來forwarding port - `exit`: - 退出本次連線 :::spoiler 如何改hostname - [如何改 hostname](https://caloskao.org/ubuntu-use-hostnamectl-to-change-the-host-name/) - `sudo hostnamectl set-hostname 要改的名字` > 改完 hostname 要重開 terminal 才看的到 ::: ### 使用憑證來遠端連線 > 甚麼是憑證: 一個可以證明自身身分的東西 1. 產生一對鑰匙 `ssh-keygen -t rsa` - ![](https://i.imgur.com/nqKwIRX.png) - `-t` 指定用哪種加密方式,預設為rsa - 以安全性推薦使用 ed25519 代替 rsa,但舊的伺服器可能只支援 rsa - passphrase 用來產生憑證的seed - enterpassphrase : 用什麼東西當密碼的基底 :::info 在ssh資料夾下,可以使用`chmod`來調整權限 - `chmod 700 ~./ssh` - `chmod 600 ~/.ssh/id_rsa` - `chmod 600 ~/.ssh/id_rsa.pub` ::: 2. 產生的鑰匙預設在 `~/.ssh/` - 金鑰創建成功後會儲存在隱藏的資料夾中,要打 `ls -al` 看詳細 - `id.rsa.pub`: 產生之公鑰 - `id.rsa`: 私鑰,要留在主機 - `authorized_keys`: 存放收到要連線的公鑰 - ![](https://i.imgur.com/4MRU4PW.png) 3. 複製公鑰到遠端主機 - 方法一 - 在遠端主機新增檔案 `touch authorized_keys` > 在 `~/.ssh/` 中 - 將 `id_rsa.pub` 公鑰放入 `authorized_keys` 中 `sudo vim authorized_keys` - 方法二 - 用 ssh 傳送金鑰到遠端主機 `ssh-copy-id -i ~/.ssh/id_rsa.pub <usrname>@ip` 4. 直接 ssh 到遠端主機 - 其他方式 `ssh -i ~/.ssh/id_rsa <username>@ip` > 成功之後不用輸入密碼,輸入 `ssh <usrname>@ip` 就可以直接登入遠端伺服器 ### 公鑰認證檔 - 當你登入遠端伺服器時,主機會主動用接收到的伺服器 public key 去比對 - 可以在`~/.ssh` 裡面的 `/known_host` 查看已確認連過的host(可以防止中間人攻擊) ### SSH設定檔 #### Client - ssh 相關設定檔放在 `/etc/ssh` ![](https://i.imgur.com/7kbguKU.png) - 針對 client 端的設定檔: `ssh_config` - IdentityFile: 放你的私鑰位址,所以獲得公鑰後鑰要來這個 file 找私鑰的路徑 - ![](https://i.imgur.com/yr1IRIa.png) #### Server - 針對 sever 端的設定檔: `sshd_config` > ssh**d**: daemon,負責啟動這個 server 的服務 - ![](https://i.imgur.com/3Wjl9AA.png) - PermiRootLogin **prohibit**-password: 禁止 root 權限登入 - PubkeyAuthentication:能不能允許使用者產生公鑰私鑰 - PasswordAuthentication:可不可以用-憑證-帳號密碼 :::info ### Include `/etc/ssh/sshd_config.d/*.conf` 如果修改過 ssh_config 的話,會需要重新啟動 ssh 才能更新內容(啟動中修改容易產生錯誤),所以現在都不認擬改 ssh_config 了。取而代之直接多用一個 `sshd_config.d/*.conf` 讓你改 ::: #### ssh 連線步驟細節 1. 驗證階段 1. 第一次連結的話server 請 client 提供 public key > 放在 know_hosts 2. 若不是則去` ~/.ssh` 看 know_hosts 檢查public key 是否相同 2. 產生session key - 利用Diffi-Hellman產生一組相同的session key 用來加解密(對稱式) - 確定兩端的使用者是相同之後,就會用同一把鑰匙加密,加快溝通速度 3. 驗證 session key 和身份 > ssh 公私鑰認證 1. client 傳送一組鑰登入的 key id 2. server 看 authorized_keys 裡面有沒有已經存在的 key id(憑證登入) 3. server如果有找到對應的 public key 則用他加密一組隨機數字 4. client 用 private key 解密後 把隨機數字跟session key 一同用 MD5 hash 傳送出去 5. 若兩者皆相同則代表可以開始傳送資料 #### Forward Agent ![](https://i.imgur.com/Se1ppXO.png) SSH agent forwarding 可以讓本地的 SSH key 在遠端 Server 上進行轉送,也就是當你需要在遠端 Server 上使用 SSH Key 時,就不需要手動將你的 key pair 手動複製到 servier 上。 > 就像把鑰匙帶出門一樣的功能 1. `eval $(ssh-agent)`: 啟動 ssh-agent 2. `ssh-add ~/.ssh/id_rsa`: 將要 forwarding 的 private key 加到清單內 3. `ssh-add -L`: 檢視是否更新成功 4. `ssh -A user@<server1 ip>` - 或是直接修改 ssh_config 的 ForwardAgent - `Host serverA` - `HostName 192.168.0.1` - `ForwardAgent:yes` - ![](https://i.imgur.com/1pwMxBs.png) ### SSH 相關指令 #### scp - 可以在不同 Linux 主機之間複製檔案 - s: SSH - cp: copy - 上傳檔案 - `scp <要上傳的檔案路徑> <使用者>@<server ip>` - 下載檔案 - `scp <使用者>@<server ip>:<伺服器上的檔案路徑(來源)> <把抓到的資料放本機中的哪(目的)>` - `-r`: recursion,目錄底下的檔案 #### stfp 在 SSH 管道底下的 FTP stfp 和 scp 有一樣的語法和功能,只不過 SFTP 是 SSH 中的一部分而已,並且其具有互動性 - `sftp [-P][user_name]@[ip_address]` : -P 為指定連線port - `get [遠端路徑] [本地路徑]`: 下載檔案 - `put [本地路徑] [遠端路徑]`: 上傳檔案 - `lcd`: 變換本地端機器的目錄 - `lpwd`: 列出本地端機器目前所在的目錄名稱 - `exit`: 退出 :::danger ### [本週作業](https://hackmd.io/@t-xGVzsgQgeALIEaFYQctg/BJAzk8g7j) :::

    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