# Spring4Shell - JianHong,Wu
## Introduction
從2022開始,出現了嚴重 0-day 漏洞 Spring4Shell,Spring4Shell 漏洞的發生原因,是在傳送參數時未能進行安全的反序列化(Deserialization),該錯誤可讓駭侵者遠端執行任意程式碼,且其 CVSS 危險程度評分高達 9.8 分(滿分為 10 分),危險程度評級為最高等級的「嚴重」(critical)等級。
**影響地區及比例**[3]
* 在漏洞被發現後的第一個週末,Check Point Research 發現了約 37K 次分配 Spring4Shell 漏洞的嘗試
* 全球 16% 的組織受到 Spring4Shell 漏洞利用的影響
* 軟件供應商是受影響最大的行業,28% 的組織受到影響
* 受影響最嚴重的地區是歐洲,影響為 20%
## 我們可用的Attack Sequence
Step 1. 利用 Spring4Shell get Shell
Step 2. download and execute agent
Step 3. 使用agent進行C&C,並做Persistence
Step 4. Exfiltration
其中Step 2~4,我們可透過我們所學的APT3 or APT29達到,相較比較容易
## Spring4Shell Attack Path [6]

1. 將HTTP Parameters透過Spring轉成Objects
2. 由於CVE-2010-1622有一些保護此RCE漏洞的Module,需要使用JDK 9+的Class.getModule()去存取ClassLoader
3. 又ClassLoader有能力存取處理一些需要高權限的Method,故可以更改logging target去建立revershell
## 漏洞存在條件
* JDK 9+
* 直接或者間接使用了Spring Framework
## Spring
Spring Framework 用於在 Java 中開發企業級Application。它是一個提供全面基礎設施的平台,以增強Memory管理而開發的基於model-view-controller or MVC-based applications。它使Code更更易於維護。
Spring Framework 是 Spring 生態系統的一部分,其中包括用於Cloud、Data和Security等。
## 攻擊行為
如果我們提出了下面請求
```
get_headers = {
"prefix": "<%",
"suffix": "%>//",
# This may seem strange, but this seems to be needed to bypass some check that looks for "Runtime" in the log_pattern
"c": "Runtime",
}
```
則相當於送出以下參數:
* class.module.classLoader.resources.context.parent.pipeline.first.pattern
=帶有prefix和suffix的[jsp webshell]
* class.module.classLoader.resources.context.parent.pipeline.first.suffix
=.jsp
* class.module.classLoader.resources.context.parent.pipeline.first.directory
=shell的文件名(不含suffix)
* class.module.classLoader.resources.context.parent.pipeline.first.prefix
=shell的儲存路徑(相對路徑,預設為webapps/ROOT)
* class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat
=(空)
在此漏洞中,攻擊者可以透過綁定的Class屬性進行任意的寫值,可以利用Class屬性來修改Tomcat的Log configuration,向Log寫入Shell
而這些payload可以分開送,也可以合併在一次的請求中
## Spring4Shell Variation
該漏洞最後會覆寫Tomcat日誌配置進而上傳WebShell
Variation 1 - UPX C&C
1. 先製作一個UPX加殼後的C&C agent
2. 在取得shell後,直接透過shell下載C&C agent取名為System software並執行此加殼的C&C agent
Variation 2 - Beaconing frequency or Command Timing
* WebShell或C&C agent的Beaconing frequency
* command 執行的時間間隔
Variation 3 - Non-standard port
* 開啟受害者電腦的Non-standard port並植入WebShell進行連線,而非在80 port上
* 下載一個會在Non-standard port上執行的C&C agent,執行後透過此agent進行C&C
Variation 4 - Obfuscated Command
* 將指令透過Base64編碼後傳過去,並透過解碼指令對Base64指令解碼
## Seemingly Case
* Google drive Backup and Sync Utilities 會定期傳送 sync info
* Google Drive Sync 的 C&C Beaconing
## Deserialization
序列化簡單說就是將物件變成資料流,可以將物件儲存成檔案,永久儲存在硬碟中,必且可以在兩台電腦中傳遞物件,是非常實用的功能。
反序列化就是將檔案還原成物件,並拿來使用。
下圖參考為序列化與反序列化的示意圖[4]

由於 Spring4Shell 漏洞的發生原因,是在**傳送參數時未能進行安全的反序列化(Deserialization),該錯誤可讓駭侵者遠端執行任意程式碼**。
故接下來將介紹不安全反序列化的弱點成因
### 不安全的反序列化的弱點成因[5]
* 攻擊者在**未過濾使用者輸入的序列化結構**輸入惡意字串
* 導致**伺服器解析**後回傳非預期結果,可能**導致任意指令執行**
* 簡而言之就是後端沒有驗證反序列化的字串是否合法,導致攻擊者可以執行任意指令
而在JAVA反序列化中,JAVA是使用binary形式儲存序列化字串,除此之外像是PHP也會做序列化與反序列化的行為。
## 解決辦法
升級 Spring Framework 至 5.3.18 和 5.2.2 版本,以及 Spring Boot 至 2.5.12 版本,或更高版本。
## POC:
https://github.com/TheGejr/SpringShell
https://github.com/craig/SpringCore0day
https://github.com/dinosn/spring-core-rce/
## Reference
[1] National Cybersecurity FFRDC,**CVE-2022-22965**,2022
https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-22965
[2] TWCERT/CC,**Spring4Shell 0-day 嚴重漏洞已遭駭侵者濫用,建議立即進行更新**,2022-04-06
https://www.twcert.org.tw/newepaper/cp-67-5993-38dfc-3.html
[3] Sumeet Wadhwani,**16% of Organizations Impacted by Spring4Shell Exploitations, Europe Worst Hit**, April 7, 2022
https://www.spiceworks.com/it-security/vulnerability-management/news/spring4shell-impact/
[4] krishankant singhal,**Serialization and Deserialization**,Apr 13, 2020
https://krishankantsinghal.medium.com/serialization-and-deserialization-5046c958c317
[5] REDFOX,**Insecure Deserialization in Java**,September 2, 2022
https://redfoxsec.com/blog/insecure-deserialization-in-java/
[6] Siddhesh Parab ,REDHUNT LABS,**Things You Should Know About the Spring4Shell Vulnerability (CVE-2022-22965)**,April 3, 2022
https://redhuntlabs.com/blog/the-spring4shell-vulnerability.html