spring-boot-actuator-logview 文件包含(CVE-2021-21234)漏洞分析
字数 1019 2025-08-05 08:19:04
Spring Boot Actuator Logview 任意文件读取漏洞(CVE-2021-21234)分析
漏洞概述
CVE-2021-21234是Spring Boot Actuator Logview组件中存在的一个任意文件读取漏洞。该漏洞允许攻击者通过构造特殊的请求参数绕过安全检查,读取服务器上的任意文件。
漏洞影响
- 影响组件:spring-boot-actuator-logview
- 漏洞类型:任意文件读取
- CVSS评分:中等
- 影响版本:所有版本(在修复前)
环境搭建
- 下载漏洞环境:
git clone https://github.com/cristianeph/vulnerability-actuator-log-viewer
- 启动应用程序后,访问管理端点:
http://localhost:8887/manage/log/
漏洞分析
漏洞位置
漏洞位于eu.hinsch.spring.boot.actuator.logview.LogViewEndpoint.view方法中:
@RequestMapping("/view")
public void view(@RequestParam String filename,
@RequestParam(required = false) String base,
@RequestParam(required = false) Integer tailLines,
HttpServletResponse response) throws IOException {
securityCheck(filename);
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
Path path = loggingPath(base);
FileProvider fileProvider = getFileProvider(path);
if (tailLines != null) {
fileProvider.tailContent(path, filename, response.getOutputStream(), tailLines);
} else {
fileProvider.streamContent(path, filename, response.getOutputStream());
}
}
漏洞成因
-
安全检查不完整:
- 只对
filename参数进行了安全检查(securityCheck(filename)) base参数未经过安全检查
- 只对
-
路径拼接问题:
loggingPath(base)方法将application.properties中的logging.path与base参数拼接- 攻击者可以通过控制
base参数来构造恶意路径
-
文件读取:
- 最终通过
fileProvider.streamContent()或fileProvider.tailContent()读取文件内容 - 由于路径可以被控制,导致任意文件读取
- 最终通过
安全绕过机制
securityCheck()方法仅检查filename参数是否包含..(路径遍历字符)- 攻击者可以通过
base参数注入恶意路径,绕过对filename的安全检查
漏洞复现
构造如下请求读取任意文件:
http://localhost:8887/manage/log/view?filename=任意文件名&base=恶意路径
示例(读取/etc/passwd):
http://localhost:8887/manage/log/view?filename=passwd&base=/etc/
修复建议
- 升级到最新版本的spring-boot-actuator-logview组件
- 临时修复方案:
- 对
base参数也进行安全检查 - 限制可访问的目录范围
- 使用白名单机制验证路径
- 对
代码修复示例
@RequestMapping("/view")
public void view(@RequestParam String filename,
@RequestParam(required = false) String base,
@RequestParam(required = false) Integer tailLines,
HttpServletResponse response) throws IOException {
// 对base参数也进行安全检查
securityCheck(filename);
if(base != null) {
securityCheck(base);
}
response.setContentType(MediaType.TEXT_PLAIN_VALUE);
Path path = loggingPath(base);
// 添加路径规范化检查
path = path.normalize();
if(!path.startsWith(loggingPath(""))) {
throw new IllegalArgumentException("Invalid path");
}
FileProvider fileProvider = getFileProvider(path);
if (tailLines != null) {
fileProvider.tailContent(path, filename, response.getOutputStream(), tailLines);
} else {
fileProvider.streamContent(path, filename, response.getOutputStream());
}
}
总结
CVE-2021-21234漏洞展示了在Web应用程序中处理文件路径时需要特别注意的几个关键点:
- 对所有用户可控的路径参数都要进行安全检查
- 路径拼接前要进行规范化处理
- 使用白名单机制比黑名单更安全
- 限制文件访问的范围到最小必要目录
开发人员在实现文件操作功能时应特别注意这些安全实践,避免类似漏洞的出现。