SpringCloud?Function?SpEL注入漏洞分析及環(huán)境搭建
SpringCloud Function 介紹
SpringCloud 是一套分布式系統(tǒng)的解決方案,常見的還有阿里巴巴的Dubbo,Fass(Function As A Service )的底層實現就是函數式編程,在視頻轉碼、音視頻轉換、數據倉庫ETL等與狀態(tài)相關度低的領域運用的比較多。開發(fā)者無需關注服務器環(huán)境運維等問題上,專注于自身業(yè)務邏輯實現即可。
SpringCloud Function 就是Spring提供的分布式函數式編程組件。
漏洞環(huán)境搭建
通過idea新建一個Spring項目,pom中引入spring-boot-starter-web
、spring-cloud-function-web
,如下:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.5</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringCloudDemo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringCloudDemo</name> <description>SpringCloudDemo</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>2021.0.1</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-function-web</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project>
其中spring-cloud-function-web
的依賴如上圖,核心實現為spring-cloud-function-core
包。
先在main函數中新建兩個方法(uppercase
將字符串變?yōu)榇髮懀?code>reverse字符串反轉):
當在pom中引入spring-cloud-function-web
后,函數會自動添加為HTTP端點。
然后漏洞關鍵是在application.properties
或者yaml配置文件中新增一行:
spring.cloud.function.definition=functionRouter
這里的屬性spring.cloud.function.definition
表示聲明式函數組合,這個功能允許在提供屬性時使用|
(管道),
或;
(過濾)分隔符以聲明的方式提供組合指令。例如
--spring.cloud.function.definition=uppercase|reverse
舉例:
當配置該屬性為uppercase時,訪問根路徑提交的參數會自動被uppercase函數接受轉化為大寫:
反之若配置為reverse則默認路徑函數功能為反轉字符串:
通俗來講這個屬性就是一個默認路由, 可以手動指定相關函數,也可以使用functionRouter
,指定的方式可以是配置文件、環(huán)境變量或者啟動參數等。
functionRouter
如果設置為functionRouter則默認路由綁定的具體函數交由用戶進行控制,在 Spring Cloud Function Web里面,可以通過設置http頭的方式來控制,使用spring.cloud.function.definition
和spring.cloud.function.routing-expression
都可以,區(qū)別是后者允許使用Spring表達式語言(SpEL)。
舉例:
因為spring.cloud.function.routing-expression
允許使用SpEL表達式,所以就可能存在SpEL注入。
SpEL注入
這里簡單介紹下SpEL,Spring Expression Language 是Spring提供的具有方法調用和基本的字符串模版功能的套件。類似OGNL、MVEL、JBoss EL。
SpEL可以字符串之間進行嵌套也可以單獨使用,嵌套時使用#{}
(實現ParserContext
接口)。
舉例:
但因為Spel支持方法調用,所以如果使用的是StandardEvaluationContext 進行解析(默認),則可能會被濫用,如使用new ProcessBuilder('/System/Applications/Calculator.app/Contents/MacOS/Calculator').start()
可觸發(fā)命令執(zhí)行:
漏洞復現
既然SpringCloud Function 中的functionRouter支持SpEL那是不是存在SpEL注入呢,我們在HTTP頭中插入上面調起計算器的SpEL表達式
Payload: spring.cloud.function.routing-expression: new ProcessBuilder('/System/Applications/Calculator.app/Contents/MacOS/Calculator').start()
非常簡單粗暴,漏洞復現成功:
原理分析
在命令執(zhí)行出下斷點,看下程序執(zhí)行流程。
SpringCloud Function之所以能自動將函數建立http端點,是因為在包mvc.FunctionController
中使用/**
監(jiān)聽了get/post類型的所有端點。
1.當一個請求進入時,程序首先基于Springboot的自動配置,將配置文件注入到functionProperties,隨后將以“WebRequestConstants.handler”為key,function為值添加到request數組里面。
2.請求正式進入Controller節(jié)點,Controller首先會將請求使用wrapper進行包裝,wrapper就是將request轉成FunctionInvocationWrapper 格式。
3.隨后進入processRequest 對request進行處理,執(zhí)行function的apply方法,跳轉到doApply()時會對function進行判斷,判斷是不是functionRouter方法,根據咱們的配置文件此時的function為RoutingFunction.FUNCTION_NAME
既 functionRouter
所以會,一路跳轉到RoutingFunction.route
5.隨后進入else if 分支,http頭spring.cloud.function.routing-expression
不為空,則傳入其值到functionFromExpression
方法。
6.使用標準的StandardEvaluationContext
對header的值進行SpEL表達式解析:
后續(xù)就不用再跟下去了,至此可以發(fā)現,只要通過環(huán)境變量、配置文件或者參數等方式配置為spring.cloud.function.definition=functionRouter
即可觸發(fā)SpEL注入。
補丁分析
SpringCloud官方已經修復了此問題(https://github.com/spring-cloud/spring-cloud-function/commit/0e89ee27b2e76138c16bcba6f4bca906c4f3744f)
和其他SpEL注入修復方式一樣,使用了SimpleEvaluationContext
替換StandardEvaluationContext
,那這個漏洞基本就算修復完成了。但因為這個commit還沒有納入版本,所以目前springcloud Function3.0以上版本仍然暴露在風險之中。
引用
https://spring.io/projects/spring-cloud-function#overview
https://cloud.spring.io/spring-cloud-function/reference/html/spring-cloud-function.html#_function_catalog_and_flexible_function_signatures
https://github.com/spring-cloud/spring-cloud-function/commit/0e89ee27b2e76138c16bcba6f4bca906c4f3744f
http://itmyhome.com/spring/expressions.html
到此這篇關于SpringCloud Function SpEL注入漏洞分析的文章就介紹到這了,更多相關SpringCloud Function SpEL注入漏洞分析內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
AsyncHttpClient KeepAliveStrategy源碼流程解讀
這篇文章主要為大家介紹了AsyncHttpClient KeepAliveStrategy源碼流程解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12SpringMVC HttpMessageConverter消息轉換器
??HttpMessageConverter???,報文信息轉換器,將請求報文轉換為Java對象,或將Java對象轉換為響應報文。???HttpMessageConverter???提供了兩個注解和兩個類型:??@RequestBody,@ResponseBody???,??RequestEntity,ResponseEntity??2023-04-04Maven如何構建可執(zhí)行的jar包(包含依賴jar包)
這篇文章主要介紹了Maven如何構建可執(zhí)行的jar包(包含依賴jar包) ,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11