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 11 (2022/11/24) [TOC] - Book mode: https://hackmd.io/@ncnu-opensource/book --- ### iptables #### Iptables-apply - 嘗試設定新的 iptables 規則,然後詢問使用者是否 ok ,不 ok 就變回舊的 - 如果新規則讓使用者斷掉連線,那使用者就無法回應,timeout就會變成舊的 - 預設 rulefile(iptables-save 輸出的內容)路徑 - `/etc/network/iptables...` ##### 使用 iptables command - `iptables-apply -c` - 預設路徑:`/etc/network/iptables.up.run` - 也可以在後面指定路徑 ##### 將成功的apply寫入檔案 - `iptables-apply -w <file>` - 如果 iptables 的規則成功 apply,rule file 會被寫入檔案 - 可以存取可用的 rule file,以便以後再次 apply ## NAT (Network Address Translation) ![](https://i.imgur.com/wsnCSTf.png) - 網路位址轉換用 > 只要有一個外網 ip,就可以讓底下很多私有 ip 上網 ### Why? - ipv4 位置有限(32bits) - 讓私有網路下的機器也能對外連線 > 如果沒有做 NAT ,內部網路的封包可以傳出去,但外網的封包無法傳進來(因為192...是內網的虛擬網址,所以外網找不到source) ### SNAT > source NAT ![](https://i.imgur.com/BbOi9ph.png) > 會有一個表紀錄是從哪一個 port 出去的,以此得知接收到封包時要傳到哪 - 修改 IP header 的 source IP - 從虛擬 ip $\to$ 真實 ip - 透過 iptables 的 nat table 的 postrouting chain 處理 ### DNAT > Destination NAT > 跟SNAT相反 ![](https://i.imgur.com/Yfu3Dja.png) - 修改 IP header 的 destination IP > 其 destination IP 要改成內網或是外網的 IP 都可以 - 外網可以透過 DNAT 把封包轉給內網,也可以外網轉外網 情況一 - 把 public IP 改成內部網路的虛擬 IP > 改寫收信人然後丟出去,故可以將外網封包轉進內網,也可以轉送到其他地方 > 如果目標沒有設定NAT,會改寫收件人 (處理destination) - 透過 iptables 的 nat table 的 pretrouting chain 處理 ### MASQUERADE - 動態 SNAT - 像是 SNAT,但不用指定 source IP ,由演算法自行分配 - 透過 iptables 的 nat table 的 postrouting chain 處理 - 常用場景 - NAT 等需要轉傳的時候 - 以自己本身這台機器作為 source IP 傳出去 - 自動修改 IP(不用自己寫 IP) - 不管 destination ip 是誰,source ip 都改為自己 >利用出口的IP決定他的source IP ### NAT 實作 ![](https://i.imgur.com/AGAcKFX.png) #### iptables的前置處理 > NAT > nic(對外): Network Interface Card - 開啟kernel轉送的功能 ```= sudo vim /etc/sysctl.conf sudo sysctl -p ``` ![](https://i.imgur.com/KObETX4.png) > 內網電腦 - 設定 default gateway ```= sudo route del default sudo route add default gw {NAT IP} ``` :::info 也可以用 ```= sudo ip route replace default via <NAT IP> dev (本機對外網卡) ``` ::: ![](https://i.imgur.com/8P4geug.png) > 可以用國外的旅客搭機來想像,路由判斷A、B路徑,A路徑就是從國外入境的旅客,之後旅行結束會從台灣離開; B路徑則是在台灣轉機但沒有要入境的旅客,所以只是經過 - 往外網的流量允許 FORWARD ```= sudo iptables -A FORWARD -i {lan nic} -o {wan nic} -j ACCEPT ``` - 送出去的封包要做 MASQUERADE ```= sudo iptables -t nat \ -A POSTROUTING -o {wan nic} -j MASQUERADE ``` > ⚠沒有加 `-t nat` 的話,預設 `filter` > 要透過 postrouting 把內網 IP 包裝成外網 IP ![](https://i.imgur.com/6Kv2B7X.png) - 設定 FORWARD Chain要 accept 回來的封包(已建立的封包、相關的連線) > 針對進來的封包 ```= sudo iptable -A FORWARD\ -m conntrack --ctstate "ESTABLISHED,RELATED" \ -j ACCEPT ``` :::info #### conntrack - 紀錄連線的狀態 - state - NEW: 新的連線 - ESTABLISHED: 已建立的連線 - RELATED: 與 ESTABLISHED 連線有關的連線 - INVALID: 無效 ::: - 拒絕轉送其他的封包 ```= sudo iptables -A FORWARD -j DROP ``` > 非必要但建議,可以防治其他來路不明的流量 ### DMZ > Demilitarized Zone > 中文名稱:非軍事區 如果有一些 service 要開放給外網,DMZ 會將其與內網機器(沒有要開放的)分離 ![](https://i.imgur.com/pHfrbGn.png) > 若有網路攻擊,可以協助網管人員控制被攻擊區域或是減少傷害 #### why? - 有某服務必須得開放給外網使用 - 當 server 被攻下時可減少災害(可以將傷害限縮在DMZ區域) #### 特點 - 讓外網有限制的存取內網資源 - 提供特定服務給外界存取,但外網沒辦法存取所有內網資源 - **DMZ 不能連到內網其他機器**(沒任何機密資料) ### 設定 - Firewall 有三張網卡 - 對外 (wan nic) - 對 DMZ (dmz nic) - 對內 ( lan nic ) - 連 Firewall 的 8080 port 可以通到 Web server 80 port ### DMZ實作 ![](https://i.imgur.com/dK7Woma.png) #### 前置作業 - 設定 gateway (在 DMZ 跟內網機器) ```= sudo route del default sudo route add default gw {Firewall IP} ``` >gw是gateway #### 外網進來的流量 - 要連 DMZ - 透過防火牆要連 DMZ - 本來 Destination IP 是寫連到 firewall,DMZ 在就會將其更改為內部 server IP - 針對外網進來的流量 ```= sudo iptables -t nat -A PREROUTING \ -i {wan nic} \ -d {destination ip} -p tcp --dport 8080 \ -j DNAT --to {web server IP}:80 ``` ```= sudo iptable -I FORWARD -d{web server ip} -p tcp --dport 80 -j ACCEPT ``` - 要連內網機器 - 不給連(指令後面提 #### DMZ 發出的流量 - 要連外網 - 進來的網卡是 DMZ 出去的網卡是 WAN ```= # 與前面 NAT 相似 sudo iptables -I FORWARD -i {dmz} -o {wan nic} -j ACCEPT sudo iptables -t nat POSTROUTING -o {wan nic} -j MASQUERADE ``` - 要連內網機器 - **不給連** ##### 內網機器發出的流量 - 要連外網 ```= # 與前面NAT相似 sudo iptables -I FORWARD -i {lan nic} -o {wan nic} -j ACCEPT sudo iptables -t nat -I POSTROUTING -o {wan nic} -j MASQUERADE ``` - 要連DMZ ```= sudo iptables -I FORWARD -i {lan nic} -o {dmz nic} -j ACCEPT ``` :::info **為何連進 DMZ 不用 MASQUERADE** DMZ 裡的機器有設定 default gateway 去 firewall,所以封包會傳回給 firewall 私人 IP 傳私人 IP 不用 MASQUEGRADE ::: > 與外網來連的差別是不限制只能連80 port,允許從內網做SSH等其他功能 #### 其他 - 允許已建立的連線做**雙向溝通** ```= sudo iptables -I FORWARD \ -m conntrack --ctstate "ESTABLISHED,RELATED" \ -j ACCEPT ``` - 拒絕轉發其他流量 ```= sudo iptables -A FORWARD -j DROP ``` > 注意要-A 才會放在最後執行 - 拒絕進來的防火牆流量 ```= sudo iptables -A INPUT -j DROP ``` :::danger DROP 所有 INPUT 流量會讓 Firewall 的外部流量完全無法存取,也就不能進行遠端控制(但從本機直接控制還是可以) ::: ### DMZ 題目 :::info ### LAB 環境/已有設定同上 #### 新增要求 - 只有內網能 ssh 進防火牆 ``` iptables -I INPUT -i {lan} -p tcp --dport 22 -j ACCEPT ``` - 內網能連外網跟 只能連 DMZ 的 80 port (其他 port 無法) ``` iptables -I FORWARD -i {lan} -o {wan} -j ACCEPT ``` ``` iptables -I FORWARD -i {lan} -o {dmz} -p tcp --dport 80 -j ACCEPT ``` - 只有 `192.168.1.101` 這個 IP 可以 ssh 進 DMZ 裡面"所有"機器,其他的 IP 無法 ``` iptables -I FORWARD -s 192.168.1.101 -i {lan} -o {dmz} -p tcp --dport 22 -j ACCEPT ``` - 外網可以連防火牆的 8081 port 來 ssh 進 DMZ 的 web server ``` iptables -t nat -A PREROUTING -i {wan} -p tcp --dport 8081 -j DNAT --to {http ip}:22 ``` ``` iptables -I FORWARD -d {web server IP} -p tcp --dport 22 -j ACCEPT ``` #### 問題 - 要增加甚麼指令或哪個指令變更 ::: #### 檢討 - 只有內網能 ssh 進防火牆: - 只有內網可以連防火牆且可以連SSH port - 內網: - 能連外網跟 - 只能連 DMZ 的 80 port (其他 port 無法)

    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