Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

admin 2022年4月1日02:26:07评论705 views字数 2300阅读7分40秒阅读模式

01漏洞概述

Spring Cloud Gateway 是基于 Spring Framework Spring Boot 构建的 API 网关,它旨在为微服务架构提供一种简单、有效、统一的 API 路由管理方式。

3.1.03.0.6版本(包含)以前存在一处SpEl表达式注入漏洞,当攻击者可以访问actuator API的情况下,将可以利用该漏洞执行任意命令。

 02影响版本

Spring Cloud Gateway < 3.1.1

Spring Cloud Gateway < 3.0.7

Spring Cloud Gateway 其他已不再更新的版本

 03修复建议

更新升级Spring Cloud Gateway 到以下安全版本:

Spring Cloud Gateway >= 3.1.1

Spring Cloud Gateway >= 3.0.7

 

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

management.endpoint.gateway.enable:false

 04环境搭建

1)本次使用vulhub搭建本地测试环境进行复现:

  https://github.com/vulhub/vulhub/tree/master/spring/CVE-2022-22947

2)执行如下命令启动一个使用了Spring Cloud Gateway 3.1.0Web服务:

  docker-compose up -d

3)服务启动后,访问http://192.168.253.71:26027/即可看到演示页面,这个页面的上游就是example.com

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

 05漏洞复现

1)首先抓取页面数据包。

 

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现


2)通过GET请求/acutator发现系统开启了acutator功能,可以列出接口路由信息。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现


3)添加一个包含恶意SpEL表达式的路由。

id字段指定新路由的名称,必须全局唯一。

filters字段给这条路由指定若干个过滤器。过滤器用于对请求和响应进行修改。

name字段指定要添加的过滤器,这里添加了一个 AddResponseHeader 过滤器,用于 gateway 给客户端返回响应之前添加一个响应头。

args.name 字段指定要添加的响应头。

args.value 字段指定响应头的值。这里的值是要执行的 SpEL 表达式,用于执行 whoami 命令。注意需要将命令输出结尾的换行符去掉,否则过滤器执行时会抛出异常说「响应头的值不能以 r 或 n 结尾」。

uri 字段指定将客户端请求转发到 http://example.com

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

4)重放数据包发现返回错误。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

5)研究了下发现,由于后端是spring,所以应在前端请求包中添加“Content-Type: application/json”来表示请求体(entity body)中的数据类型,再次重放数据包后返回201 Created新增路由创建成功。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

6)接着以POST方法再请求/actuator/gateway/refresh,用于刷新路由,使刚添加的恶意路由生效。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

7)最后以GET方法请求/actuator/gateway/routes/test,触发恶意路由进行命令执行。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

8)最后,以DELETE方法清理现场,删除所添加的路由。

Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

06原理分析

为什么添加过滤器(路由)会导致代码执行?

回顾流程

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

(2)可以通过/gateway/routes/{id_route_to_create}创建路由。

3)可以通过/actuator/gateway/refresh刷新路由。

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

在payload当中:

#{new String(T(org.springframework.util.StreamUtils).copyToByteArray(T(java.lang.Runtime).getRuntime().exec(new String[]{"whoami"}).getInputStream()))}

payload解析:

new String[]{"whoami"}——创建一个新的字符串,字符串里内容为想要执行的命令whoami

T(java.lang.Runtime).getRuntime().exec——JAVA中执行系统命令的代码,用于执行上面的whoami

.getInputStream()——获得执行的结果

T(org.springframework.util.StreamUtils).copyToByteArray——将获得执行的结果转换成字节数组

new String——将字节数组转换为字符串

#{}——SpEl表达式的格式

 

参考链接

在Spring Cloud Gateway中解析SpEl表达式的代码审计链接:

https://y4er.com/post/cve-2022-22947-springcloud-gateway-spel-rce-echo-response/

https://www.bilibili.com/video/BV1JF41147GY?p=3

vulhub中Spring Cloud Gateway环境代码链接:

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

 

 

 

 

原文始发于微信公众号(Matrix1024):Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年4月1日02:26:07
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Spring Cloud Gateway 远程代码执行漏洞(CVE-2022-22947)复现http://cn-sec.com/archives/858670.html

发表评论

匿名网友 填写信息