File-Traversal(路径遍历,Path Traversal)漏洞是攻击者绕过 Web 应用程序的文件系统权限,访问任意文件的漏洞,被指定为 CWE-22 的一种攻击类型。
Spring WebFlux 是 Spring 5 引入的支持响应式编程的模块,支持异步非阻塞应用程序开发,WebFlux.fn 提供函数式端点路由,可使用 lambda 表达式实现简洁灵活的路由配置。
CVE-2024-38819 出现在使用 WebFlux.fn、WebMVC.fn 的应用程序中,本文档将基于 WebFlux 探讨该漏洞是如何通过代码流程发生的,并寻求相应的应对方案。
[图 1] FileApplication.java
[图 1] 显示了 Spring WebFlux 将所有对 /static/** 的请求从服务器 C:/file 目录查找并提供服务。这里
[图 2] PathResourceLookupFunction.class - apply
[图 2] PathResourceLookupFunction - apply 方法在 Spring WebFlux 中检查给定路径是否指向有效资源,并以
[图 3] PathResourceLookupFunction.class - isInvalidPath
[图 3] PathResourceLookupFunction - isInvalidPath 中,存在条件 `StringUtils.cleanPath(path).contains("../")`,
[图 4] StringUtils.class - cleanPath
[图 4] StringUtils.class - cleanPath 中,`StringUtils.cleanPath(path)` 在移除 TOP_PATH("..") 后将路径放入 pathElements,top 变为 0,因此
[图 5] PathResourceLookupFunction.class - apply - 2
[图 6] PathResourceLookupFunction.class - isResourceUnderLocation
[图 5] 中的 isResourceUnderLocation 通过调用 cleanPath,[图 6] 显示再次经过 cleanPath 验证,[图 5] 传递的路径是 `C:/file../Windows/System32/drivers/etc/hosts`
[图 7] StringUtils.class - cleanPath -2
通过 [图 7] 的代码,prefix 变为 `C:/`,路径通过 [图 4] 变为 `/Windows/System32/drivers/etc/hosts`,最终返回目前在 Windows 上使用 C:/file 进行了测试,但该案例在 Linux 上配合符号链接运行时非常危险。
(当设置两次 ../ 时逻辑正常,仅当 ../ 出现一次时触发漏洞)

在 Linux 服务器上配合符号链接进行攻击
public RouterFunction<ServerResponse> staticResourceRouter() {
return RouterFunctions.resources("/static/**", new FileSystemResource("/app/static/"));
}
添加符号链接 ln -s /static /app/static/link 后攻击

迄今为止,我们检查了在 Spring Webflux 环境中 File Traversal (CVE-2024-38819) 的执行流程。由于这是一种窃取服务器信息的攻击方式,所以应对方案非常重要。为此,我们将提出更新到最新版本、制作额外的审查逻辑、以及通过 IPS 进行阻断的方案。
@Bean
public RouterFunction<ServerResponse> staticResourceRouter() {
return RouterFunctions.resources("/static/**", new FileSystemResource("C:/file"))
.filter((request, next) -> {
String path = request.path();
if (path.contains("..")) {
if(!StringUtils.cleanPath(path).contains("../")) {
return ServerResponse.status(HttpStatus.FORBIDDEN).bodyValue("Vuln path access.");
}
}
return next.handle(request);
});
}
完全阻断导致 ../ 上级目录移动的 URL 请求
到目前为止,我们了解了通过 Webflux 的 File Traversal 漏洞。该漏洞是由于没有意识到 StringUtils.cleanPath() 逻辑中的问题而使用的案例。
(POC)
https://github.com/masa42/CVE-2024-38819-POC
(Spring 官方)
https://spring.io/security/cve-2024-38819
(CVE-DETAIL)
https://www.cvedetails.com/cve/CVE-2024-38819/
(NIST)
https://nvd.nist.gov/vuln/detail/cve-2024-38819