ZipSlipAnalyzer implements MaliciousnessAnalyzer { @Override public MaliciousnessAnalysisResult isMalicious(File _file) { MaliciousnessAnalysisResult mal = null; final String name = _file.getName(); try (final InputStream fis = new FileInputStream(_file)) { if (name.endsWith(".zip") || name.endsWith(".jar") || name.endsWith(".war") || name.endsWith(".ear") || name.endsWith(".aar")) { mal = this.isMalicious(new ZipInputStream(fis), false); } else if (name.endsWith(".tar")) { mal = this.isMalicious(new TarArchiveInputStream(fis), false); } else if (name.endsWith(".tar.gz")) { mal = this.isMalicious( new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(fis))), false); } else { log.warn("Cannot analyze [" + _file.toPath().toAbsolutePath() + "]"); } if (mal != null) { mal.setReason("[" + _file.toPath().toAbsolutePath() + "] " + mal.getReason()); if (mal.isMalicious()) log.warn(mal.getReason()); else log.info(mal.getReason()); } } catch (FileNotFoundException fnfe) { log.error("File [" + _file.getAbsolutePath() + "] cannot be found"); } catch (IOException ioe) { log.error( ioe.getClass().getSimpleName() + " when opening [" + _file.getAbsolutePath() + "]: " + ioe.getMessage(), ioe); } return mal; }  @Override MaliciousnessAnalysisResult isMalicious(File _file); @Override MaliciousnessAnalysisResult isMalicious(InputStream _is, boolean _log);  }
@Test public void testZipSlipFile() { final MaliciousnessAnalyzer mala = new ZipSlipAnalyzer(); MaliciousnessAnalysisResult is_mal; is_mal = mala.isMalicious(new File("src/test/resources/zip-slip.zip")); assertEquals(1d, is_mal.getResult(), 0d); is_mal = mala.isMalicious(new File("src/test/resources/zip-slip-win.zip")); assertEquals(1d, is_mal.getResult(), 0d); is_mal = mala.isMalicious(new File("src/test/resources/zip-slip.tar")); assertEquals(1d, is_mal.getResult(), 0d); is_mal = mala.isMalicious(new File("src/test/resources/zip-slip-win.tar")); assertEquals(1d, is_mal.getResult(), 0d); is_mal = mala.isMalicious(new File("src/test/resources/zip-slip.jar")); assertEquals(1d, is_mal.getResult(), 0d); is_mal = mala.isMalicious(new File("src/test/resources/no-zip-slip.zip")); assertEquals(0d, is_mal.getResult(), 0d); }