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: `lsa` `ncnu` # Week 04 (2023/03/16) - Book mode: https://hackmd.io/@ncnu-opensource/book [TOC] --- ## Transport Layer ### TCP (Transmission Control Protocol) ![](https://i.imgur.com/dQJ96Ln.png) > [圖片來源](https://commons.wikimedia.org/wiki/File:TCP_header.png) - `FIN` 通知對方是不是傳輸完了,要不要斷線 - 三向交握 : 保證資料可以送達給收件者 - 四次揮手:準備斷開連線 ### UDP ![](https://i.imgur.com/Wxal5BO.png) > [圖片來源](https://upload.wikimedia.org/wikipedia/commons/0/0c/UDP_header.png) - 快速寫紙條的通訊協定 - 適合不用分割的短封包 - 要求傳輸速度要快 ![](https://i.imgur.com/UBqrFPV.png) ### Port - 每個 Port 對應到不同的服務 (Application) - `cat /etc/services` : 查看每個 port 的占用狀況 ## HTTP - HTTP Method - HTTP Header ![](https://i.imgur.com/qlCnXDw.png) >[圖片來源](http://clubfile.cupoy.com/0000016F1181369A000000056375706F795F72656C656173654B5741535354434C55424E455753/1574048359035/large) ## FTP - FTP 用來在網路上傳輸檔案的一種協定 ![](https://i.imgur.com/YsYtUpe.png) ![](https://i.imgur.com/1o4VxO5.png) > [圖片來源](http://www.tsnien.idv.tw/Manager_WebBook/chap5/5-2%20%E6%AA%94%E6%A1%88%E5%82%B3%E8%BC%B8%E7%B3%BB%E7%B5%B1%E5%88%86%E6%9E%90.html) - 同樣採 Server-Client 架構 - 控制連線 (port 21) : 用來控制系統連線? - 資料?線 (port 20) ### DHCP(Dynamic Host Configuration Protocol) ![](https://i.imgur.com/nVhCueg.png) > [圖片來源](https://www.stockfeel.com.tw/dhcp%E4%BC%BA%E6%9C%8D%E5%99%A8%E6%98%AF%E4%BB%80%E9%BA%BC%EF%BC%9F%E5%A6%82%E4%BD%95%E9%81%8B%E7%94%A8%EF%BC%9F/) - 負責動態分配 IP 位址 : 1. 當網路中有任何一台電腦要連線時,才向 DHCP 伺服器要求一個 IP 位址 2. DHCP 伺服器會從資料庫中找出一個目前尚未被使用的 IP 位址提供給該電腦使用 3. 使用完畢後電腦再將這個 IP 位址還給 DHCP 伺服器,提供給其他上線的電腦使用 ### DNS (Domain Name Service) - 如果沒有 DNS 就要記住 IP,但是 IP 是一串數字,可能會很難記住,於是有了 domain name - 輸入網址,就可以幫 User 找到 IP ![](https://i.imgur.com/ghua0uP.png) #### 命名方式 - 採用階層式命名 - TLD (top level domain) ![](https://i.imgur.com/BFGukdf.png) > [圖片來源](http://www.tsnien.idv.tw/Manager_WebBook/chap5/5-3%20%E7%B6%B2%E5%9F%9F%E5%90%8D%E7%A8%B1%E7%B3%BB%E7%B5%B1%E5%88%86%E6%9E%90.html) #### 查詢過程 - 先找自己的機台內有沒有這個 domain name 的 IP - `/etc/hosts` - 如果沒有就會開始向 DNS server 請求 IP - `/etc/resolv.conf` - 如果仍然沒有就會向更上層的 DNS 進行 request - 一路往上查到 tld ![](https://i.imgur.com/EY3m7iD.png) #### 相關設定檔 - `etc/hosts` : 本機的 IP 位址對應表 - 紀錄特殊的 IP - `/etc/resolv.conf`: 預設的 DNS server - 可以設定多個 DNS server IP? (備援) ### DNS 紀錄類型 - A record - domain -> ipv4 - AAAA record - domain -> ipv6 - NS record - 該 DNS 的主機位置 - MX record - 該 domain name mail server 的位置 - CNAME record - 實際代表這個主機別名的主機名字 - 指令 - `dig` + 網址 - 暫存時間 - TTL(time to live) - 已經有查詢過此 IP 就會被紀錄在快取 - 目的為加速查詢速度 - `dig -t ns + <網址>` - 由哪些 DNS server 提供 - `dig +trace <網址>` - 查看 DNS 查詢的過程 - `host -a <網址>` - 找 IP 位址 跟全部的 DNS 紀錄 ( A, AAAA...) - `whois [domain name]` - 查看當初註冊此 domain name 的使用者相關資訊 - `nslookup + <網址>` - 可查看 IPv4 網址、預設 DNS server ![](https://i.imgur.com/zpmJhv9.png) ## Network Conclusion - network layering - data link layer: 硬體規範 wifi, ethernet - network layer: IP, ICMP - transport layer: TCP, UDP - application layer: HTTP ## 資訊安全 - 對一般人 :帳號密碼、信用卡要避免被別人知道,避免點連結而中毒 - 對網管:遠端燈入主機的安全性 ### 社交工程 - 利用人性弱點 - ex: - fb: 我打賭你會在看到這個視頻之後去洗手間 - 防範方式 - 多查證 - 不要亂點網站 ### 暴力破解 - 把每個字元的排列組合都試試看 - 防範方式 - 密碼取複雜一點 - 密碼複雜度 - 英文數字符號通通來 - 假設密碼長度 = X,可能的的字元為 Y,要嘗試的次數就是 Y^X - 密碼種類 - 一個根基密碼 + 不同的字元 - 根基密碼 = 1234, 加上每個網站產生的不同組合 => google1234 - fail2ban 限制登入次數 ### 字典攻擊 - 使用常見的字元進行攻擊 - admin, test ... - 臺灣: wang, lin ... - 防範方式(同上一個) - 密碼取複雜一點 - 用 fail2ban 限制登入次數 ### zero-day vulnerability - 利用套件本身的漏洞進行攻擊 - ex: https://www.exploit-db.com/exploits/50580 - RCE(Remote Code Execution) 遠端 code 執行漏洞 - ex. `eval` - XSS(Cross-site Script) - 演練網站 : https://xss-game.appspot.com - 在網站中執行惡意的 javascript - 防範方式 - server 管理員把 `<`, `>` 編碼,讓前端沒辦法直接執行 tag - CSRF - 一般使用者 - 不要亂點其他的連結 :::info - Docker 用 Docker 可以把每個網站包在一個容器,讓網站的相關檔案跟主機的其他隔絕 ::: ### 得到主機資源可以做什麼 - 加密資料後勒索 - 預防 - 備份 - 被當作 mail server 的跳板機 - 症狀: - 伺服器很慢 - netstat -nupl 很多連線到 25 port - 如何處理 - 用防火牆牆掉一些奇怪的連線 ### 簡單檢查 - ps aux - watch some sus process - top - watch cpu resource - 查看版本漏洞 - [CVE Detail](https://www.cvedetails.com/) - [ecploit database ](https://www.exploit-db.com/) 檢查是否有漏洞 ### fail2ban - 如果沒有限制次數,使用者就可以無限登入,駭客說不定可以用暴力破解法非法登入別人的帳號 - `sudo apt install fail2ban` : 安裝 - `sudo systemctl start fail2ban`: 啟用 fail2ban - 預設會先保護 sshd - `sudo systemctl enable fail2ban`: 開機自動啟動 - `/etc/fail2ban/jail.conf` : 設定檔的預設路徑 - 先複製設定檔成jail.local 因為如果更新fial2ban jail.conf會被更新,而且 jail.local 會覆蓋 jail.conf ``` [sshd] # To use more aggressive sshd modes set filter parameter "mode" in jail.local: # normal (default), ddos, extra or aggressive (combines all). # See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details. #mode = normal port = ssh logpath = %(sshd_log)s backend = %(sshd_backend)s action = iptables[name=SSH, port=22, protocol=tcp] telegram maxretry = 3 findtime = 1d bantime = 1d ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 ``` - filter `/etc/fail2ban/filter.d` - 用來過濾哪一些事此服務失敗的條件(failregex) - action `/etc/fail2ban/filter.d` - 用來決定超過 maxretry 後,之後要執行的動作 - `maxretry` : 最多可以試幾次 - `bantime` : 被禁止的時間,預設單位為秒 - `fail2ban-client status sshd` - 產生最近被 ban 掉的 ip 和狀態 - 可以去 `/var/log/auth.log` 看被登入的狀況 (成功或失敗都會被記錄) ### portsentry - `sudo apt install portsentry` : 安裝 - `sudo systemctl start portsentry` : 啟動 ### 更改 SSH 的登入限制 #### 改 port 號 - 決定要換哪個 port - 1~1023 是較常用的 port,建議 2024~65535 #### 限制 ssh 登入 ip - `/etc/hosts.deny` ```terminel= sshd:ALL ``` #### 取消密碼登入 ### Rookit - 藉由某些服務的漏洞取得 root 的權限,再藉由修改某些程式隱藏身分 - 如何找到 rootkit - rkhunter - 使用 md5 checksum 確認常見 binary 的 fingerprint ``` --checkall (-c) :全系統檢測,rkhunter 的所有檢測項目 --createlogfile :建立登錄檔,一般預設放在 /var/log/rkhunter.log --cronjob :可以使用 crontab 來執行,不會有顏色顯示 --report-warnings-only :僅列出警告訊息,正常訊息不列出! --skip-application-check :忽略套件版本檢測(如果您已確定系統的套件已patch) --skip-keypress :忽略按鍵後繼續的舉動(程式會持續自動執行) --quiet :僅顯示有問題的訊息,比 --report-warnings-only 更少訊息 --versioncheck :檢測試否有新的版本在伺服器上 ``` ### sudoers - 可以紀錄使用者與群組使用 sudo 的紀錄 - `/etc/sudoers` - 紀錄 sudo 的權限 - `visudo` - 可以檢查 sudoer 的格式 - 若要使用 sudo 可以簡單將 user 加到 sudo 群組(但較不建議) - 檔案內容 - 使用者名稱 主機名稱=(可切換的帳號:群組) 可執行指令的執行檔路徑 - 可以在指令後面接針對的參數 #### 新增規則 - 讓 user1 可執行 iptables : `user1 ALL=(ALL) /usr/sbin/iptables` - 使用 sudo 可以不用使用密碼 - `user2 ALL=(ALL) NOPASSWD:ALL` - 讓 user 可以更改常規 user 的,但不能修改 root 的 user - `user1 ALL=(ALL) !/usr/bin/passwd, /usr/bin/passswd [A-Za-z0-9]*, !/usr/bin/passwd` ## iptables - Linux 上過濾封包、保護環境的套件 - 架構:表(table)、鏈(chain)、規則(rule) - 表裡有鏈,鏈裡有規則,規則裡有 target - Table 種類 - filter: 預設使用的表,用於過濾封包 - nat:用於位址轉換操作 - mangle:處理封包 - raw:處理異常用 ### SELinux - 安全增強式 Linux - 用於 fedora, redhat - 除了判斷 rwx 的另一種安全方式 - 檢查 porcess 是否有權限使用這個資源 - 避免 rwx 為 777 導致任何 process 都可以存取這個檔案 - Subject(process) 要取得 Object(資源) 的流程 - FIXME: NEED IMAGE~ - Subject ### Apparomer - primary used by Ubuntu SUSE - simpler than SElinux - use file path to specified files - use process's absolute path to make the rules #### Chain - INPUT:用於進入本主機的網路封包。 - OUTPUT : 用於由本主機送出的網路封包。 - FORWARD : 用於經由本主機轉送的網路封包。 - PREROUTING : 用於進行路由前的網路封包。 - POSTORTING : 用於被送出前的網路封包。 ### Rule - 設定給 iptables 的條件。若符合 rule 就會執行相對的 action - 有順序性,越前面的 rule 越先執行 ### Target - ACCEPT - REJECT, 回覆一個 icmp port unreachable - DROP - SNAP - DNAT - MASQUERADED - LOG, 紀錄 log - RETURN, 回到當初呼叫這個 chain 的地方 #### 查看 rules - `-t` 選擇 table,預設是 filter - `-L` : 列出所有的 chain - `-n` : 不把 IP 位址解析成 domain name - `-v` 列出更多資訊 - `--line-number` 列出行號 #### 更改 policy ```terminel= sudo iptable [-t table] -P <INPUT、OUTPUT> ``` - `-t`: chose table - `-p`: policy #### 新增 rule - 新增 - `-A`: Append, 加在最後 - `-I` : insert, 新增在最前面 - 網卡 - `-i` : 封包從哪張網卡近來 - `-o` : 封包從哪張網卡出去 - 協定 - `-p` : protocol - IP address - `-s`: source address - `-d`: destinatiom address - `!`: 目前的條件的否定 - `! -s 10.0.0.0/24` - 使用的 iptables 模組 - `-m` 呼叫模組 - `-m conntrack` - NEW: 建立新的連線 - RELATED 新的連線,但是和之前的連線有相關 - IVAILD 無法辨認或惡意的連線 - UNTRACKED 已經關閉,所以無法追蹤的連線 - 刪除 rule - `-D`: delect the rule in that chain - `-F`: Flush all rule in that chain - delete rules - sudo iptables -D [-t table name] chain_name line_number ### 保存 iptables 設定 - `iptables-save` 把所有 rule 保存到一個檔案 - 用 iptables-persistent - `sudo apt install iptables-persistent` - `sudo bash -c "iptables-save > /etc/iptables/rules.v4"` - iptables-apply - `sudo ipables-apply iptable_config_file` - 60 秒沒有確定會自動 roll-back 回原本的 iptables config ### NAT - Network Address Translation - 協助內網與外網之間的 IP 轉換 - IPv4 的位址數量不夠用 -> 使用內網 ip #### SNAT (Source NAT) 原本的 rrr #### MASQUERADE - 動態 SNAT - 如果外網 IP 更動,SNAT 就需要手動去修改(很麻煩),MASQUERADE 能自動連上外網 (???) #### DNAT

    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