Terrance Chen
    • 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

      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
    • Engagement control
    • 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 Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
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
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

    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: NASA type: slide --- # NASA 2023 ### 防火牆 Lab B09902017 李安傑 ~~B08902073 陳宇浩~~ B08902037 王苡涵 <!-- .slide: data-background="https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg" --> --- ## 0. Before We Start (都用最新版的) * OPNsense iso [official website](https://opnsense.org/download/) * Alpine Linux iso (載 virtual 版的) [official website](https://alpinelinux.org/downloads/) --- ## 1. Firewall Introduction ---- Monitor and Control network traffic. ( based on pre-established rules ) For example, “Source address 172.18.1.1 is allowed to reach destination 172.18.2.1 over port 22." <!-- .element: class="fragment" data-fragment-index="1" --> ---- ### Two Network Interfaces (typically) - WAN (Wide Area Network) - LAN (Local Area Network) --- ## 2. OPNsense Introduction ---- <!-- .slide: theme: solarized --> ### What is OPNsense? - open source firewall - based on FreeBSD - 以前是教 Pfsense,今年來變點新花樣 (厭世 ._.) ---- ### What can OPNSense do? 一般常見的區域網路架構大概長這樣: ![](https://i.imgur.com/csYWUxX.png =600x400) ---- ### What can OPNsense do? 一般常見的 OPNsense 大概長這樣: ![](https://i.imgur.com/KGFjAWX.png =600x400) ---- ### What can OPNsense do? - Firewall - NAT - DHCP - VPN - ... ---- ### Why OPNsense? <span><!-- .element: class="fragment highlight-red" -->WebGUI</span> 不太豐富的 Document 跟資源!(但跟 Pfsense 很像所以可以直接參考) <!-- .element: class="fragment" data-fragment-index="1" --> 設定監測都很方便!介面也很漂亮喔! <!-- .element: class="fragment" data-fragment-index="2" --> 壞處是部份自由被限制!但其實太自由也不是好事(?) <!-- .element: class="fragment" data-fragment-index="3" --> --- ## 3. Lab ---- ### Lab Goal * Setup an OPNsense server * Set up 2 client VMs on two different VLANs (vlan5 & vlan99) * Rules * VLAN99 可以 ping/ssh 到 VLAN5,但 VLAN5 不能 ping/ssh 到 VLAN99 * VLAN5 可以 ping/ssh 到外面,但 VLAN99 不能 ping/ssh 到外面 * 所有 DNS 的流量都要被允許 * 所有其他流量都要被 block ---- ### Lab Goal ![](https://i.imgur.com/Z4s0qw5.png =600x500) ---- ### On VLAN5 ![](https://i.imgur.com/wtYbES4.png) ![](https://i.imgur.com/xecs6ZP.png) ---- ### On VLAN99 ![](https://i.imgur.com/k9WOZ8j.png) --- ## Disclaimer 以下 lab 只在以下軟體測試過,如果你使用的不是以下的軟體請自行摸索後續的設定: * VMware® Workstation (16 Pro) * VirtualBox (6.1) <!-- 204 實測,完成所需時間 31m27s 21m02s --> --- ## 4-1. [Server] Create a VM for OPNsense 新增一個虛擬機,大概是個 FreeBSD 64-bit 的東西 ---- ### 2 network interfaces ![](https://i.imgur.com/1WpRBWJ.png =600x500) ---- ### WAN (VirtualBox) ![](https://i.imgur.com/bYljl5Y.png) ---- ### LAN (VirtualBox) ![](https://i.imgur.com/iUhKER9.png) ---- ### WAN (VMWare Workstation) ![](https://i.imgur.com/Pul9Rwa.png) ---- ### LAN (VMWare Workstation) ![](https://i.imgur.com/UdnyHLw.png) ---- ### Insert ISO to virtual CD drive (VirtualBox) 載下來的 iso 檔可以先用 `bzip2 -d <檔名>` 解壓縮 接下來掛上 OPNsense 的 iso 檔, 開機就會讀到 ![](https://i.imgur.com/eASLRIx.png) ---- ### Insert ISO to virtual CD drive (VMWare Workstation) ![](https://i.imgur.com/3LYNyRP.png) ---- ### Installation - Live CD - 在 setup interfaces 的時候,把網卡 1 給 WAN 網卡 2 給 LAN其餘就 enter 鍵按到底就好了 - 安裝完之後關機,然後拔掉 iso 讓 VM 直接從 disk drive boot 起來 - 注意:不能重新開機,不然東西都會不見喔 --- ## 4-2. [Server] Connect to webGUI ---- ![](https://i.imgur.com/KxlD52W.png =600x500) 一般來說, 需要透過一台在內網的機器,透過內網連線碰它 但為了 lab 方便,我們今天就用 WAN IP 來碰它 ---- ### 登入並按 8 選擇 Shell default account: `root` default password: `opnsense` **按 8** ---- ### Disable Packet Filtering 在 Shell 打 `pfctl -d` disable 他 `pfctl -e` 重新 enable 他 ![](https://i.imgur.com/4N2Q5dC.png) ---- ### `pfctl` 注意事項 - 關掉的時候 - 你(WAN)可以不被擋 - 他也不會做封包轉傳 - 關掉他,只是暫時讓防火牆不擋所有的封包,理論上實際生活中別做這種事 - 每次在 `Apply Changes` 之後,他都會被重新開啟,這時候你就會連不到 GUI(如果你沒有開放 WAN 的話) - [參考連結](https://forum.opnsense.org/index.php?topic=573.0#:~:text=in%20order%20to%20gain%20access%20to%20your%20opnsense,-%3E%20WAN%20address%20-%3E%20OPNsense%20Managment%20Port%20%28443%29) ---- ### Find OPNsense WAN IP address ![](https://i.imgur.com/KzuUMHH.png) <!--這張圖的 IP 好像跟下一張圖的不一樣 應該可以口頭說一下--> ---- ### Add port forwarding (VirtualBox only !!!) *VMWare 的話不用做這個設定* 設定值 -> 網路 -> 介面卡1 -> 連接埠轉送 ![](https://i.imgur.com/zHUMRxM.png) ---- ### Connect to Web GUI Browser 輸入 https://127.0.0.1:9999 default account: `root` default password: `opnsense` ![](https://i.imgur.com/kwKrh4a.png) ---- ### Go Through Setup Wizard 1. Uncheck "block private networks" 2. Uncheck "block bogon networks" 3. 其他都不用動 ![](https://i.imgur.com/AoQ92ff.png) ---- ### Allow all on WAN Firewall > Rules > WAN 新增一個 allow all 的 rule,記得要 apply changes! ![](https://i.imgur.com/nMLMVk2.png) ---- ### 在有 `pfctl` 的情況下,重新連到 GUI 你成功了呀 嘻嘻嘻 ![](https://i.imgur.com/Z1CIVOX.png) --- ## 4-3. [Server] VLAN configuration - OPNsense ---- ### VLAN ![](https://i.imgur.com/KTTYLHG.png =600x500) ---- ### Add VLAN Interfaces -> Other Types -> VLAN **Parent Interface 記得選 LAN!** ![](https://i.imgur.com/7wxS5Wf.png) ---- ### Add VLAN Interface Interfaces -> Assignments 點 Add 新增 Interface。 ![](https://i.imgur.com/3RdrvKh.png) ---- ### VLAN Interface - Static IP and netmask Enable 勾起來 並選擇 Static IP 10.5.0.254/24。 ![](https://i.imgur.com/Ix2LRSv.png) ![](https://i.imgur.com/fDRVKMn.png) ---- ### Enable VLAN DHCP Services -> DHCPv4 -> <Your Interface> ![](https://i.imgur.com/aWRkIxh.png) ---- ### VLAN99 依照上述的步驟一樣弄一個 VLAN99 防火牆的 interface IP 設 10.99.0.254/24 --- ## 5-1. [Client] Install Client VMs 這邊選用的是 Alpine,原因是因為他是輕量化的 Linux 系統。當然如果你想要用其他的作業系統也可以 (就是個 Other 64-bit Linux) ---- 接下來我們要安裝 2 台 VM,一台取名為 Client5、一台取名為 Client99 當做 Client,網卡只需要一張 LAN(就是 OPNsense 的 LAN) ![](https://i.imgur.com/vL3CSLh.png) ---- ### Alpine 帳號:`root` ![](https://i.imgur.com/xv1hqx5.png) --- ## 5-2. [Client] Get IP from OPNsense DHCP ---- 因為接下來要安裝東西,所以要先把它們放在 LAN 底下,去接 OPNsense 的網路。 ![](https://i.imgur.com/kPC4j2M.png =600x500) ---- ### Manual way * Find network interface name: `ip a` (For example, interface is `eth0`) * `vi /etc/network/interfaces` ``` auto eth0 iface eth0 inet dhcp ``` * 執行 `/etc/init.d/networking restart` ---- ### Guided way * 執行 `setup-interfaces` * 按照他的指示,把 interface 設成拿 DHCP ![](https://i.imgur.com/2jit5jk.png) * 執行 `/etc/init.d/networking restart` --- ## 5-3. [CLient] OpenSSH server ---- ### OpenSSH server 安裝並執行 ``` setup-apkrepos apk update apk add openssh /etc/init.d/sshd start ``` 設定並生效 ``` echo "PermitRootLogin yes" >> /etc/ssh/sshd_config /etc/init.d/sshd restart ``` ---- ### Set password * 設定密碼:`passwd` * 測試:`ssh root@localhost` --- ## 5-4. [Client] VLAN configuration ---- 接下來終於要把它們搬到 VLAN 底下ㄌ ![](https://i.imgur.com/b7MUaLS.png =600x500) ---- ### Install VLAN 執行 `apk add vlan` ---- ### Add vlan 假設要設定的是 vlan5,則把 `/etc/network/interfaces` 裡面的所有 `eth0` 都改成 `eth0.5`,其中 5 是 vlan 的 tag,而 eth0 是連接到 LAN 的網卡的名稱。 vlan99 可以用同樣的道理設定。 ---- ### Restart network service ``` /etc/init.d/networking restart ``` 成功後 `ip a` 就能看到新加的 vlan 了。 ---- ### Delete IP address of `eth0` 執行 `ip addr del <address> dev eth0` * 如果不刪除的話,之後的測試可能會不小心用到 `eth0`,造成很多意想不到的結果 --- ## 6. OPNSense - Firewall ---- Firewall > Rules > VLAN5 用很直觀的 GUI 來做設定(自己玩) ![](https://i.imgur.com/riiujzE.png) ---- ### Firewall Rules 幾個重點: * rule 由上到下來 match * 都沒 match 到的話 default 會 block * 只需要設定 incoming traffic 的 rule,不需要設定讓 response traffic 通過的 rule * protocol 跟 port 記得設好(想清楚) * 有些設定要等個幾分鐘才會生效,如果剛設完 rule 發現有怪怪的行為,先去做別的設定,等個 5~10 分鐘再來測試一次 --- ## Postscript: More on Alpine Linux ---- * Alpine Linux 是一個非常 lightweight 的 Linux 版本,很常被用來快速佈署專案(或用在 docker) * 預設是重新開機會洗掉所有資料,如果要 Alpine Linux 到硬碟上的話,要執行 `setup-alpine` --- Revisit: [Lab Goal](#/4/1) #### (reminder: demo vlan5 dns rule) --- ## 7. DEMO ---- * On Client5: * `ping <Client99 IP>` * `ping linux1.csie.ntu.edu.tw` * `ssh root@<Client99 IP>` * `ssh <學號>@linux1.csie.ntu.edu.tw` ---- * On Client99: * `ping <Client5 IP>` * `ping linux1.csie.ntu.edu.tw` * `ssh root@<Client5 IP>` * `ssh <學號>@linux1.csie.ntu.edu.tw` ---- * On OPNsense * Client5 Firewall Rules 設定 * Client99 Firewall Rules 設定 --- 若今天來不及 demo,請將以上結果錄影,並將影片連結繳交至以下表單 https://forms.gle/aVMP57dmRdP73Pu4A

    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