# 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?
一般常見的區域網路架構大概長這樣:

----
### What can OPNsense do?
一般常見的 OPNsense 大概長這樣:

----
### 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

----
### On VLAN5


----
### On VLAN99

---
## 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

----
### WAN (VirtualBox)

----
### LAN (VirtualBox)

----
### WAN (VMWare Workstation)

----
### LAN (VMWare Workstation)

----
### Insert ISO to virtual CD drive (VirtualBox)
載下來的 iso 檔可以先用 `bzip2 -d <檔名>` 解壓縮
接下來掛上 OPNsense 的 iso 檔, 開機就會讀到

----
### Insert ISO to virtual CD drive (VMWare Workstation)

----
### Installation - Live CD
- 在 setup interfaces 的時候,把網卡 1 給 WAN 網卡 2 給 LAN其餘就 enter 鍵按到底就好了
- 安裝完之後關機,然後拔掉 iso 讓 VM 直接從 disk drive boot 起來
- 注意:不能重新開機,不然東西都會不見喔
---
## 4-2. [Server] Connect to webGUI
----

一般來說,
需要透過一台在內網的機器,透過內網連線碰它
但為了 lab 方便,我們今天就用 WAN IP 來碰它
----
### 登入並按 8 選擇 Shell
default account: `root`
default password: `opnsense`
**按 8**
----
### Disable Packet Filtering
在 Shell 打
`pfctl -d` disable 他
`pfctl -e` 重新 enable 他

----
### `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

<!--這張圖的 IP 好像跟下一張圖的不一樣 應該可以口頭說一下-->
----
### Add port forwarding (VirtualBox only !!!)
*VMWare 的話不用做這個設定*
設定值 -> 網路 -> 介面卡1 -> 連接埠轉送

----
### Connect to Web GUI
Browser 輸入 https://127.0.0.1:9999
default account: `root`
default password: `opnsense`

----
### Go Through Setup Wizard
1. Uncheck "block private networks"
2. Uncheck "block bogon networks"
3. 其他都不用動

----
### Allow all on WAN
Firewall > Rules > WAN
新增一個 allow all 的 rule,記得要 apply changes!

----
### 在有 `pfctl` 的情況下,重新連到 GUI
你成功了呀 嘻嘻嘻

---
## 4-3. [Server] VLAN configuration - OPNsense
----
### VLAN

----
### Add VLAN
Interfaces -> Other Types -> VLAN
**Parent Interface 記得選 LAN!**

----
### Add VLAN Interface
Interfaces -> Assignments
點 Add 新增 Interface。

----
### VLAN Interface - Static IP and netmask
Enable 勾起來 並選擇 Static IP 10.5.0.254/24。


----
### Enable VLAN DHCP
Services -> DHCPv4 -> <Your Interface>

----
### 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)

----
### Alpine
帳號:`root`

---
## 5-2. [Client] Get IP from OPNsense DHCP
----
因為接下來要安裝東西,所以要先把它們放在 LAN 底下,去接 OPNsense 的網路。

----
### 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

* 執行 `/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 底下ㄌ

----
### 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 來做設定(自己玩)

----
### 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
{"metaMigratedAt":"2023-06-18T00:08:37.217Z","metaMigratedFrom":"YAML","title":"NASA 2023","breaks":true,"contributors":"[{\"id\":\"fb392181-8524-41cf-86bb-88d6241d81da\",\"add\":15,\"del\":3},{\"id\":\"914a5020-4c19-4f73-afb8-0ce06e275ba9\",\"add\":35,\"del\":35},{\"id\":\"180180a3-ef33-4fff-9967-5d838c4afbbd\",\"add\":10082,\"del\":9527},{\"id\":\"1c7e78c0-070f-48c2-aede-c0567d67036d\",\"add\":9746,\"del\":2175}]"}