cpt1020
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # DMZ Host 基本介紹與設定 ## DMZ ![102](https://hackmd.io/_uploads/ryhpV90o1l.png) 有時候公司或組織會需要提供對外的服務,像是 mail server 或 web server,讓別人從外網可以存取到這些提供的服務,但同時公司也有一些東西不想被外界存取、只想給公司內部使用。這時就可以用 Demilitarized zone(DMZ)非軍事化區域(又稱作 perimeter network 或 screened subnet)。 非軍事化區域其實就是在公司/組織的內網和外網之間多一個 physical 或 logical 的 subnetwork。將欲提供給外部使用的伺服器都放在 DMZ 裡面,外網的使用者可以存取到 DMZ 裡面的服務;而公司不想讓外界存取的東西則用防火牆擋住,放在公司的 LAN 裡面,讓外界無法存取到 LAN 內的東西。 ## Architectures of DMZ 有許多不同的方式可以用來設計 DMZ,其中兩種最基本的方式是使用 single firewall 以及 dual firewalls。這些架構可以被擴展來建立更複雜的架構。 ### Single Firewall DMZ ![image](https://hackmd.io/_uploads/Sybfr7E3Jg.png) > https://en.wikipedia.org/wiki/File:DMZ_network_diagram_1_firewall.svg 此架構只有使用一層防火牆建立 DMZ,從圖片可以看到此防火牆必須要有三個網路 interfaces 才能建立出對外網、LAN、以及 DMZ 的 interface。 ### Dual Firewall DMZ ![image](https://hackmd.io/_uploads/Byl1eBQN3yg.png) >https://en.wikipedia.org/wiki/File:DMZ_network_diagram_2_firewall.svg 此架構會用到兩層防火牆,第一層防火牆必須被設定成只允許到達 DMZ 的流量,第二層防火牆則是只允許由 LAN 到 DMZ 的流量。 ## DMZ Host ![104](https://hackmd.io/_uploads/H16sB9Csyg.png) 有些路由器會提供 DMZ host 的功能,所謂的 DMZ host 跟前面提到那些位在 DMZ 內的伺服器是不太一樣的概念。 DMZ host 是位在 LAN 內的一個裝置,只要設定好 DMZ host,那所有目的地是 router WAN IP 的流量,且若未符合任何防火牆規則,那就全部都會被轉發到 DMZ host。 ![image](https://hackmd.io/_uploads/HkKqmZV31l.png) 例如,假設現在有個 LAN 是 `192.168.50.0/24` - DMZ host 是 `192.168.50.60` - Gateway IP 是 `192.168.50.1` - 假設 router 的 WAN IP 是 `123.123.123.123` 那所有通往 router WAN IP `123.123.123.123` 的流量都會全部被轉發到位在內網的 DMZ host,也就是 `192.168.50.60`。 所以可以看到,DMZ host 跟位在真正的 DMZ 裡面的 server 是不一樣的。Router 並沒有將 DMZ host 跟內網做隔離,而真正的 DMZ 則會經由防火牆將內網與 DMZ 做隔離。 :::success DMZ host 跟 port forwarding 的行為有點像,都是將來自外網的流量轉發到位在內網的裝置,而他們的差異在於: - DMZ host 是將沒有符合防火牆規則的外部流量,全部都轉發到指定的 LAN 裝置,也就是 DMZ host - Port forwarding 只轉發特定 port 的流量到 LAN 內的裝置 ::: ## 用 OpenWRT 裝置設定 DMZ Host ### 測試環境 > 由於拿不到 public IP,所以以下都是用 LAN 來模擬~ ![dmz_test](https://hackmd.io/_uploads/HywrSqshJg.png) - 一台位在 `192.168.50.0/24` 的 MacBook,IP 是 `192.168.50.61` - 安裝 OpenWRT 的 Marvell ESPRESSObin v7 開發板,在這裡當作是 router 的腳色 - 其 `wan` interface 位在 `192.168.50.0/24`,IP 是 `192.168.50.99` - 其 `lan` interface 則是 `192.168.60.0/24` 的 gateway,IP 是 `192.168.60.1` - 一個 Ubuntu 裝置,IP 是 `192.168.60.248`,要把它設定為 DMZ host ### 設定 DMZ Host 從前面的說明可以知道要把所有到 Marvell ESPRESSObin v7 開發板 WAN IP 的封包,都轉發到 Ubuntu 裝置 -> 所以必須要做 DNAT,將封包的 Destination IP address 改為 DMZ host 的 IP,也就是 `192.168.60.248` 另外,從 DMZ host 發出的封包,在經過 Marvell ESPRESSObin v7 開發板時,也必須將 source IP address 改成 Marvell ESPRESSObin v7 開發板的 wan IP -> 所以必須要做 SNAT,將封包的 source IP address 改為 `192.168.50.99` #### ++設定 DNAT++ ![pic02](https://hackmd.io/_uploads/Skg_6-43kg.png) 在 OpenWRT 裝置的 `/etc/config/firewall` 內新增以下一個新的 `redirect` config: ```config config redirect option name 'DMZ-Host' option src 'wan' option dest 'lan' option dest_ip '192.168.60.248` option target 'DNAT' ``` #### ++設定 SNAT++ ![pic01](https://hackmd.io/_uploads/rylvT-43Jg.png) 修改 OpenWRT 裝置的 `/etc/config/firewall` 的 `wan` zone config: - `option forward` 是 `'ACCEPT'` - 允許 `wan` 做轉發 - `option masq` 是 `'1'` - 允許 masquerade,也就是將封包的 source IP address 改成 `wan` interface 的 IP - PS. OpenWRT 的防火牆的 `redirect` 主要是拿來做 DNAT,不是拿來做 SNAT。若要做 SNAT 主要就是將 `/etc/config/firewall` 的 `wan` zone 的 `masq` 設為 `1` 修改完這些防火牆的 configuration 後,記得要輸入以下指令重啟防火牆,這樣剛剛做的設定才會生效喔~ ```shell /etc/init.d/firewall reload ``` ### 使用 `iperf3` 測試 ![dmz_iper3_test](https://hackmd.io/_uploads/HyGPr9in1x.png) 做完前述的設定後,首先,在 Ubuntu 裝置(也就是這裡設定的 DMZ host)執行以下指令,讓 Ubuntu 裝置成為 `iperf3` 的 server: ```shell $ iperf3 -s ``` 在 MacBook 輸入以下指令,讓 MacBook 成為 `iperf3` 客戶端,並嘗試連到 `192.168.50.99`: ```shell $ iperf3 -c 192.168.50.99 ``` 以下是執行的截圖,可以看到雖然 MacBook 執行的是 `iperf3 -c 192.168.50.99`,指定的是 Marvell ESPRESSObin v7 開發板的 IP,但是 Ubuntu 的 `iperf3` 卻有收到封包,代表有成功將流量轉發到 DMZ host。 ![iperf3_s](https://hackmd.io/_uploads/B1Ob8csn1e.png) ![pic02](https://hackmd.io/_uploads/HJrjR-Enyx.png) #### 若都沒做設定 若前面都沒做 DMZ host 的設定,直接執行 `iperf3`,會看到以下畫面: ![image](https://hackmd.io/_uploads/SyhiZMN2Je.png) ### 使用 SSH 測試 接下來來測試看看若在 MacBook 執行 ssh 連線的指令,IP 指定 Marvell ESPRESSObin v7 開發板的 `wan` IP,看是否會將這個 ssh 連線請求轉發到 DMZ host。 ![dmz_ssh_test](https://hackmd.io/_uploads/Sk0Orqo2Jg.png) 以下是測試的結果,可以看到在 MacBook 端執行 ssh 指令,並指定 Marvell ESPRESSObin v7 開發板的 `wan` IP 後,有成功連上 Ubuntu 裝置: ![pic03](https://hackmd.io/_uploads/HyCXDfV31g.png) ## References https://en.wikipedia.org/wiki/DMZ_(computing) <!-- --- ![Screen Shot 2025-03-09 at 11.57.19 PM](https://hackmd.io/_uploads/rycVo4sokl.png) ![image](https://hackmd.io/_uploads/HycLjNso1g.png) ![image](https://hackmd.io/_uploads/HkZkhEoo1e.png) INITAIL CONFIG ![Screen Shot 2025-03-09 at 11.58.27 PM](https://hackmd.io/_uploads/rkXroNojJx.png) ![image](https://hackmd.io/_uploads/r1m_iVii1g.png) ```shell root@OpenWrt:/# uci show network.wan network.wan=interface network.wan.device='wan' network.wan.proto='dhcp' ``` ```shell root@OpenWrt:/# uci set network.wan.proto='static' root@OpenWrt:/# uci set network.wan.ipaddr='192.168.50.166' root@OpenWrt:/# uci set network.wan.netmask='255.255.255.0' root@OpenWrt:/# uci set network.wan.gateway='192.168.50.1' root@OpenWrt:/# uci set network.wan.dns='8.8.8.8' root@OpenWrt:/# uci commit network root@OpenWrt:/# /etc/init.d/network restart ``` ```uci config zone option name 'wan' list network 'wan' list network 'wan6' option input 'REJECT' option output 'ACCEPT' option forward 'REJECT' option masq '1' option mtu_fix '1' config forwarding option src 'lan' option dest 'wan' ``` ```uci config redirect option name 'DMZ-Host' option src 'wan' option dest 'lan' option dest_ip '192.168.60.66' option target 'DNAT' ``` ![image](https://hackmd.io/_uploads/SJKci4ooJx.png) ![image](https://hackmd.io/_uploads/Sk-jo4iokl.png) -->

    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
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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