程序在解压zip文件时,如果没有验证zip条目,攻击者可能对条目覆盖,从而造成路径遍历
例如:以下代码示例解压zip文件。
static final int BUFFER = 512;
// . ..
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(filename);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Extracting: " + entry);
int count;
byte data[] = new byte[BUFFER];
String fileName = entry.getName();
if (entry.isDirectory()){
new File(fileName ).mkdir();
continue;
}
// write the files to the disk
FileOutputStream fos = new FileOutputStream(fileName );
dest = new BufferedOutputStream(fos, BUFFER);
while ((count = zis.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
...
zis.close();
代码示例未验证zipEntry.getName(),如果zip文件放在/tmp/目录中,zip条目为../etc/hosts,且应用程序在必要的权限下运行,则会导致系统的hosts文件被覆盖。
原文地址:https://www.cnblogs.com/jinqi520/p/9391596.html