路径遍历
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
OWASP 类别:MASVS-STORAGE:存储
概览
当攻击者能够控制路径的一部分,因而导致该部分路径会在未经验证的情况下被传递到文件系统 API 时,则存在路径遍历漏洞。这可能会导致攻击者能够在未经授权的情况下操作文件系统。例如,通过在目标目录之外进行遍历,攻击者可能会使用 ../
等特殊字符以意想不到的方式更改资源目标。
影响
具体影响因操作和文件内容而异,但通常会导致文件覆盖(写入文件时)、数据泄露(读取文件时)或权限更改(更改文件/目录权限时)。
缓解措施
使用 File.getCanonicalPath()
对路径进行规范化,并将前缀与预期目录进行比较:
Kotlin
@Throws(IllegalArgumentException::class)
fun saferOpenFile(path: String, expectedDir: String?): File {
val f = File(path)
val canonicalPath = f.canonicalPath
require(canonicalPath.startsWith(expectedDir!!))
return f
}
Java
public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {
File f = new File(path);
String canonicalPath = f.getCanonicalPath();
if (!canonicalPath.startsWith(expectedDir)) {
throw new IllegalArgumentException();
}
return f;
}
另一种最佳实践是通过验证来确保仅发生预期结果。示例如下:
- 检查文件是否已经存在,以防发生意外覆盖。
- 检查目标文件是否为预期目标,以防止数据泄露或错误地更改权限。
- 检查相应操作的当前目录是否如预期一样,与来自规范路径的返回值中的目录完全一致。
- 确保权限系统的作用域明确限定为相应操作,例如确保其未将相关服务作为 root 来运行,并确保目录权限的作用域限定为指定的服务或命令。
为您推荐
- 注意:当 JavaScript 处于关闭状态时,系统会显示链接文字
- 压缩路径遍历
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2023-12-13。
[null,null,["最后更新时间 (UTC):2023-12-13。"],[],[],null,["# Path traversal\n\n\u003cbr /\u003e\n\n**OWASP category:** [MASVS-STORAGE: Storage](https://mas.owasp.org/MASVS/05-MASVS-STORAGE)\n\nOverview\n--------\n\nPath traversal vulnerabilities occur when an attacker can control part of the\npath that is then passed to the file system APIs without validation. This can\nlead to unauthorized file system operations. For example, an attacker might use\nspecial characters such as `../` to unexpectedly change the resource target, by\ntraversing outside of the targeted directory.\n\nImpact\n------\n\nThe impact varies depending on the operation and file content, but generally\nleads to a file overwrite (when writing files), data leak (when reading files),\nor permission changes (when changing file or directory permissions).\n\nMitigations\n-----------\n\nCanonicalize the path using [`File.getCanonicalPath()`](/reference/java/io/File#getCanonicalPath()) and compare the\nprefix with the expected directory: \n\n### Kotlin\n\n @Throws(IllegalArgumentException::class)\n fun saferOpenFile(path: String, expectedDir: String?): File {\n val f = File(path)\n val canonicalPath = f.canonicalPath\n require(canonicalPath.startsWith(expectedDir!!))\n return f\n }\n\n### Java\n\n public File saferOpenFile (String path, String expectedDir) throws IllegalArgumentException {\n File f = new File(path);\n String canonicalPath = f.getCanonicalPath();\n if (!canonicalPath.startsWith(expectedDir)) {\n throw new IllegalArgumentException();\n }\n return f;\n }\n\nAn additional best practice is to use validation to ensure only expected\noutcomes occur. Examples include the following:\n\n- Checking if the file already exists to prevent an accidental overwrite.\n- Checking if the targeted file is an expected target to prevent leaking data or incorrectly changing permissions.\n- Checking if the current directory of the operation is exactly as expected in the return value from the canonical path.\n- Ensuring a permissions system is explicitly scoped to the operation, such as checking that it isn't running services as root, and ensuring that the directory permissions are scoped to the service or command specified."]]