Java代码审计-路径遍历 目录覆盖 举例:解压zip文件,未对zip目录进行验证

举例:项目中在解压zip文件时,未对zip条目进行验证,存在路径遍历的风险,为了系统安全,应加上安全黑名单特殊字符过滤。

示例 代码如下:
  String name= zipEntry getName O
  通过上述语句获取zip条目的名字,
  File unzipFile- new File(path+ name)
  随后通过获取的zip条目名字直接创建文件夹或文件。

    Web应用接收恶意zip压缩包,会造成受保护的文件或目录被覆盖等危险。


  修复建议:在解压zip文件时,对zip条目名称进行危险字符过滤,禁止解压缩路径中包含(.)等特殊字符


//获取压文件对象,gbk解决中文文件夹乱码   zip =
new ZipFile(zipFile, Charset forName("GBK)):   Enumeration <? extends ZipEntry> entries=zip. entries();
  ExcellmportData excellmportData
null:   while(entries.hasMoreElements(){     zipEntry zipEntry = entries.nextElement();     String name = zpEntry.getName();
    File unzipFiles =
new File(path name);     if (unzipFile.isDirectory || unzipFile.getName().contains(".")){       unzipFile.mkdirs();     }else       File parentFile unzipFilegetParentFileo:     if (parentFileexists){       parentFilemkdirsO:       bis new BufferedInputstream(zip. getinpu tstream(zipEntry2):       bos new Bufferedoutputstream(new FileOutputStream(unzip File));
      ... ...
      ... ...




原文地址:https://www.cnblogs.com/jayus/p/11423769.html