From 00d75863f29620d6060566addedb388d50c949b9 Mon Sep 17 00:00:00 2001 From: "patrick.pdb" Date: Wed, 15 May 2024 15:42:54 -0400 Subject: [PATCH] '#2095 Correctly treats getTempFile if it is a reference to an exported file in case output path. --- .../engine/task/leappbridge/Ilapfuncs.java | 7 ++ .../task/leappbridge/LeappBridgeTask.java | 68 +++++++++++++++---- 2 files changed, 63 insertions(+), 12 deletions(-) diff --git a/iped-engine/src/main/java/iped/engine/task/leappbridge/Ilapfuncs.java b/iped-engine/src/main/java/iped/engine/task/leappbridge/Ilapfuncs.java index 113022732e..0652b4760a 100644 --- a/iped-engine/src/main/java/iped/engine/task/leappbridge/Ilapfuncs.java +++ b/iped-engine/src/main/java/iped/engine/task/leappbridge/Ilapfuncs.java @@ -28,6 +28,8 @@ public static void install(Jep jep) { Ilapfuncs.class.getMethod("media_to_html", String.class, Collection.class, String.class)); pt.overrideModuleFunction("scripts.ilapfuncs", "logdevinfo", Ilapfuncs.class.getMethod("logdevinfo", String.class)); + pt.overrideModuleFunction("scripts.ilapfuncs", "tsv", Ilapfuncs.class.getMethod("tsv", String.class, + Collection.class, Collection.class, String.class)); } catch (Exception e) { e.printStackTrace(); } @@ -66,6 +68,11 @@ public static void timeline(String reportFolder, String tlactivity, Collection d } + public static void tsv(String reportFolder, Collection data_headers, Collection data_list, String tsvname, + String srcfile) { + + } + public static String media_to_html(String mediaPath, Collection filesFound, String report_folder) { for (Object file : filesFound) { if (file.toString().contains(mediaPath)) { diff --git a/iped-engine/src/main/java/iped/engine/task/leappbridge/LeappBridgeTask.java b/iped-engine/src/main/java/iped/engine/task/leappbridge/LeappBridgeTask.java index d8403fa173..5599aee4b4 100644 --- a/iped-engine/src/main/java/iped/engine/task/leappbridge/LeappBridgeTask.java +++ b/iped-engine/src/main/java/iped/engine/task/leappbridge/LeappBridgeTask.java @@ -45,6 +45,7 @@ import iped.properties.ExtraProperties; import iped.search.SearchResult; import iped.utils.DateUtil; +import iped.utils.IOUtil; import iped.utils.pythonhook.FileHook; import iped.utils.pythonhook.PythonHook; import jep.Jep; @@ -103,6 +104,7 @@ public LeappBridgeTask() { private HashMap filesFoundDocuments; private ALeappConfig config; + private PythonHook pt; static private File aleappDir; @@ -112,7 +114,17 @@ public LeappBridgeTask() { public static Object open(Collection args, Map kargs) { Iterator iargs = args.iterator(); - return new FileHook(iargs.next().toString()); + Object[] o = args.toArray(); + if (o.length == 1) { + return new FileHook((String) o[0]); + } + if (o.length == 2) { + return new FileHook((String) o[0], (String) o[1]); + } + if (o.length == 3) { + return new FileHook((String) o[0], (String) o[1], (String) o[3]); + } + return null; } @Override @@ -140,13 +152,17 @@ public void init(ConfigurationManager configurationManager) throws Exception { Ilapfuncs.install(jep); PythonHook pt = PythonHook.installHook(jep); pt.wrapsClass("scripts.artifact_report", "ArtifactHtmlReport", ArtifactJavaReport.class); - + pluginsManager.init(jep, getAleappScriptsDir()); } else { throw new Exception("ALeapp plugin scripts path not found:" + artifactsPath.getCanonicalPath()); } } + public FileHook open(String filePath, String mode) { + return new FileHook(filePath, mode); + } + @Override public void finish() throws Exception { int decremented = taskCount.decrementAndGet(); @@ -229,6 +245,12 @@ public void executePlugin(IItem evidence, LeapArtifactsPlugin p, List fi jep.set("mappedEvidences", mappedEvidences); jep.eval("logfunc('" + PLUGIN_EXECUTION_MESSAGE + ":" + p.getModuleName() + "')"); + + // + // PythonHook pt = PythonHook.installHook(jep); + // pt.overrideFileOpen(LeappBridgeTask.class.getMethod("open", String.class, + // String.class)); + jep.eval("parse(" + lists + ",'" + reportPath.getCanonicalPath().replace("\\", "\\\\") + "',dumb,True,'UTC')"); } finally { @@ -531,7 +553,7 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid for (String pattern : p.patterns) { IPEDSearcher filesSearcher = new IPEDSearcher(ipedCase); - + String query = patternToLuceneQuery(dumpEvidence, pattern); filesSearcher.setQuery(query); SearchResult filesResult = filesSearcher.search(); @@ -554,17 +576,39 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid IItem item = ipedCase.getItemByLuceneID(artLuceneId); File tmp = item.getTempFile(); - String sourcePath = new File( - ipedCase.getCaseDir() + "/" + artdoc.get(IndexItem.SOURCE_PATH)) - .getCanonicalPath(); + if (!IOUtil.isTemporaryFile(tmp)) { + if (tmp.getCanonicalPath().startsWith(ipedCase.getCaseDir().getCanonicalPath())) { + // getTempFile returned the file exported in the output case path + String artParentPath = artpath.substring(0, artpath.lastIndexOf("/")); + String artname = artpath.substring(artParentPath.length()); + File artfolder = new File(reportDumpPath, artParentPath); + artfolder.mkdirs(); + File file_found = new File(artfolder, artname); - if (tmp.getCanonicalPath().startsWith(sourcePath)) { - reportDumpPath = new File(sourcePath); - // the file returned by getTempFile() is the file itself - String fileStr = preparePythonLiteralPath(tmp.getCanonicalPath()); - filesFound.add(fileStr); - filesFoundDocuments.put(fileStr, artdoc); + if (!file_found.exists()) { + // if the file wasn't already placed by prior iterations, copy its content + if (!tmp.isDirectory()) { + Files.copy(tmp.toPath(), file_found.toPath()); + } else { + // should not occur + System.out.println(); + } + } + String fileStr = preparePythonLiteralPath(file_found.getCanonicalPath()); + filesFound.add(fileStr); + filesFoundDocuments.put(fileStr, artdoc); + } else { + // the file returned by getTempFile() is the file itself. It must occur + // only if the source evidence is a "path" (not a container) + String sourcePath = new File( + ipedCase.getCaseDir() + "/" + artdoc.get(IndexItem.SOURCE_PATH)) + .getCanonicalPath(); + reportDumpPath = new File(sourcePath); + String fileStr = preparePythonLiteralPath(tmp.getCanonicalPath()); + filesFound.add(fileStr); + filesFoundDocuments.put(fileStr, artdoc); + } } else { // the file returned by getTempFile() is a copy to the file in a temp folder // so recreate the path structure inside the temp folder