Try   HackMD

DMZ Host 基本介紹與設定

DMZ

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

有時候公司或組織會需要提供對外的服務,像是 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 Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

https://en.wikipedia.org/wiki/File:DMZ_network_diagram_1_firewall.svg

此架構只有使用一層防火牆建立 DMZ,從圖片可以看到此防火牆必須要有三個網路 interfaces 才能建立出對外網、LAN、以及 DMZ 的 interface。

Dual Firewall DMZ

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

https://en.wikipedia.org/wiki/File:DMZ_network_diagram_2_firewall.svg

此架構會用到兩層防火牆,第一層防火牆必須被設定成只允許到達 DMZ 的流量,第二層防火牆則是只允許由 LAN 到 DMZ 的流量。

DMZ Host

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

有些路由器會提供 DMZ host 的功能,所謂的 DMZ host 跟前面提到那些位在 DMZ 內的伺服器是不太一樣的概念。

DMZ host 是位在 LAN 內的一個裝置,只要設定好 DMZ host,那所有目的地是 router WAN IP 的流量,且若未符合任何防火牆規則,那就全部都會被轉發到 DMZ host。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

例如,假設現在有個 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 做隔離。

DMZ host 跟 port forwarding 的行為有點像,都是將來自外網的流量轉發到位在內網的裝置,而他們的差異在於:

  • DMZ host 是將沒有符合防火牆規則的外部流量,全部都轉發到指定的 LAN 裝置,也就是 DMZ host
  • Port forwarding 只轉發特定 port 的流量到 LAN 內的裝置

用 OpenWRT 裝置設定 DMZ Host

測試環境

由於拿不到 public IP,所以以下都是用 LAN 來模擬~

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

  • 一台位在 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

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

在 OpenWRT 裝置的 /etc/config/firewall 內新增以下一個新的 redirect config:

config redirect
        option name 'DMZ-Host'
        option src 'wan'
        option dest 'lan'
        option dest_ip '192.168.60.248`
        option target 'DNAT'

設定 SNAT

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

修改 OpenWRT 裝置的 /etc/config/firewallwan zone config:

  • option forward'ACCEPT'
    • 允許 wan 做轉發
  • option masq'1'
    • 允許 masquerade,也就是將封包的 source IP address 改成 wan interface 的 IP
    • PS. OpenWRT 的防火牆的 redirect 主要是拿來做 DNAT,不是拿來做 SNAT。若要做 SNAT 主要就是將 /etc/config/firewallwan zone 的 masq 設為 1

修改完這些防火牆的 configuration 後,記得要輸入以下指令重啟防火牆,這樣剛剛做的設定才會生效喔~

/etc/init.d/firewall reload

使用 iperf3 測試

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

做完前述的設定後,首先,在 Ubuntu 裝置(也就是這裡設定的 DMZ host)執行以下指令,讓 Ubuntu 裝置成為 iperf3 的 server:

$ iperf3 -s

在 MacBook 輸入以下指令,讓 MacBook 成為 iperf3 客戶端,並嘗試連到 192.168.50.99

$ iperf3 -c 192.168.50.99

以下是執行的截圖,可以看到雖然 MacBook 執行的是 iperf3 -c 192.168.50.99,指定的是 Marvell ESPRESSObin v7 開發板的 IP,但是 Ubuntu 的 iperf3 卻有收到封包,代表有成功將流量轉發到 DMZ host。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

若都沒做設定

若前面都沒做 DMZ host 的設定,直接執行 iperf3,會看到以下畫面:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

使用 SSH 測試

接下來來測試看看若在 MacBook 執行 ssh 連線的指令,IP 指定 Marvell ESPRESSObin v7 開發板的 wan IP,看是否會將這個 ssh 連線請求轉發到 DMZ host。

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

以下是測試的結果,可以看到在 MacBook 端執行 ssh 指令,並指定 Marvell ESPRESSObin v7 開發板的 wan IP 後,有成功連上 Ubuntu 裝置:

Image Not Showing Possible Reasons
  • The image was uploaded to a note which you don't have access to
  • The note which the image was originally uploaded to has been deleted
Learn More →

References

https://en.wikipedia.org/wiki/DMZ_(computing)