Zip 路径遍历

OWASP 类别: MASVS-STORAGE:存储

概览

Zip 路径遍历漏洞,也称为 ZipSlip,与处理压缩档案有关。在此页面上,我们以 ZIP 格式为例演示此漏洞,但类似的问题也可能出现在处理其他格式(如 TAR、RAR 或 7z)的库中。

此问题的根本原因是,在 ZIP 档案中,每个打包的文件都存储有一个完全限定名称,该名称允许包含斜杠和点等特殊字符。来自 java.util.zip 包的默认库不检查档案条目的名称中是否包含目录遍历字符 (../),因此在将从档案中提取的名称与目标目录路径连接时必须格外小心。

验证来自外部来源的任何 ZIP 提取代码片段或库非常重要。许多此类库都存在 Zip 路径遍历漏洞。

影响

Zip 路径遍历漏洞可用于实现任意文件覆盖。根据具体条件,影响可能有所不同,但在许多情况下,此漏洞可能导致重大的安全问题,例如代码执行。

缓解措施

为缓解此问题,在提取每个条目之前,您应始终验证目标路径是否是目标目录的子路径。下面的代码假定目标目录是安全的——仅您的应用可写且不受攻击者控制——否则您的应用可能会容易受到其他漏洞(例如符号链接攻击)的影响。

Kotlin

companion object {
    @Throws(IOException::class)
    fun newFile(targetPath: File, zipEntry: ZipEntry): File {
        val name: String = zipEntry.name
        val f = File(targetPath, name)
        val canonicalPath = f.canonicalPath
        if (!canonicalPath.startsWith(
                targetPath.canonicalPath + File.separator)) {
            throw ZipException("Illegal name: $name")
        }
        return f
    }
}

Java

public static File newFile(File targetPath, ZipEntry zipEntry) throws IOException {
    String name = zipEntry.getName();
    File f = new File(targetPath, name);
    String canonicalPath = f.getCanonicalPath();
    if (!canonicalPath.startsWith(targetPath.getCanonicalPath() + File.separator)) {
      throw new ZipException("Illegal name: " + name);
    }
    return f;
 }

为避免意外覆盖现有文件,您还应确保在开始提取过程之前目标目录为空。否则,您可能会面临潜在的应用崩溃,或者在极端情况下,应用受到威胁。

Kotlin

@Throws(IOException::class)
fun unzip(inputStream: InputStream?, destinationDir: File) {
    if (!destinationDir.isDirectory) {
        throw IOException("Destination is not a directory.")
    }
    val files = destinationDir.list()
    if (files != null && files.isNotEmpty()) {
        throw IOException("Destination directory is not empty.")
    }
    ZipInputStream(inputStream).use { zipInputStream ->
        var zipEntry: ZipEntry
        while (zipInputStream.nextEntry.also { zipEntry = it } != null) {
            val targetFile = File(destinationDir, zipEntry.name)
            // ...
        }
    }
}

Java

void unzip(final InputStream inputStream, File destinationDir)
      throws IOException {
  if(!destinationDir.isDirectory()) { 
    throw IOException("Destination is not a directory.");
  }

  String[] files = destinationDir.list();
  if(files != null && files.length != 0) { 
    throw IOException("Destination directory is not empty.");
  }

  try (ZipInputStream zipInputStream = new ZipInputStream(inputStream)) {
    ZipEntry zipEntry;
    while ((zipEntry = zipInputStream.getNextEntry()) != null) {
      final File targetFile = new File(destinationDir, zipEntry);
        
    }
  }
}

资源

  • 注意:关闭 JavaScript 时显示链接文本
  • 路径遍历