Skip to content

Commit

Permalink
'#43 creates symbolik links with the original path an name to the tmp
Browse files Browse the repository at this point in the history
file returned by IPED (instead of moving it).
  • Loading branch information
patrickdalla committed Oct 20, 2023
1 parent 4e7272f commit 37e435e
Showing 1 changed file with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.commons.lang.SystemUtils;
import org.apache.lucene.document.Document;
import org.apache.tika.io.TemporaryResources;
import org.apache.tika.metadata.Metadata;
Expand Down Expand Up @@ -143,9 +144,9 @@ public void init(ConfigurationManager configurationManager) throws Exception {
File scriptsPath = new File(aleappPath, "scripts");
File artifactsPath = new File(scriptsPath, "artifacts");
if (artifactsPath.exists()) {
jep.eval("sys.path.append('" + aleappPath.getCanonicalPath().replace("\\", "\\\\") + "')");
jep.eval("sys.path.append('" + scriptsPath.getCanonicalPath().replace("\\", "\\\\") + "')");
jep.eval("sys.path.append('" + artifactsPath.getCanonicalPath().replace("\\", "\\\\") + "')");
jep.eval("sys.path.append('" + preparePythonLiteralPath(aleappPath.getCanonicalPath()) + "')");
jep.eval("sys.path.append('" + preparePythonLiteralPath(scriptsPath.getCanonicalPath()) + "')");
jep.eval("sys.path.append('" + preparePythonLiteralPath(artifactsPath.getCanonicalPath()) + "')");
jep.eval("from geopy.geocoders import Nominatim");

PythonHook pt = new PythonHook(jep);
Expand Down Expand Up @@ -191,7 +192,7 @@ public void executePlugin(IItem evidence, LeapArtifactsPlugin p, List<String> fi

File scriptsDir = new File(getAleappScriptsDir(), "scripts");

jep.eval("sys.path.append('" + scriptsDir.getCanonicalPath().replace("\\", "\\\\") + "')");
jep.eval("sys.path.append('" + preparePythonLiteralPath(scriptsDir.getCanonicalPath()) + "')");

jep.eval("import scripts.artifact_report");
jep.eval("from multiprocessing import Process");
Expand Down Expand Up @@ -384,6 +385,17 @@ static private void moveDir(File fromDir, File toDir) throws IOException {

static HashSet<LeapArtifactsPlugin> processedPlugins = new HashSet<LeapArtifactsPlugin>();

static public String preparePythonLiteralPath(String path) {
if (SystemUtils.IS_OS_WINDOWS) {
// prepares to file path str to be used as a string literal inside python
return path.replace("\\", "\\\\");
} else {
// does not makes the replacement as the input does not uses "\" as path
// separator avoiding unnecessary CPU usage
return path;
}
}

private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvidence, String dumpPath,
File reportDumpPath) throws IOException {
try {
Expand Down Expand Up @@ -425,7 +437,8 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid
if (tmp.getCanonicalPath().startsWith(sourcePath)) {
reportDumpPath = new File(sourcePath);
// the file returned by getTempFile() is the file itself
filesFound.add(tmp.getCanonicalPath().replace("\\", "\\\\"));
String fileStr = tmp.getCanonicalPath();
filesFound.add(preparePythonLiteralPath(fileStr));
// mappedEvidences.put(tmp.getCanonicalPath(), (Item) item);
} else {
// the file returned by getTempFile() is a copy to the file in a temp folder
Expand All @@ -439,12 +452,10 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid

try {
File file_found = new File(artfolder, artname);
if (!tmp.isDirectory()) {
Files.move(tmp.toPath(), file_found.toPath());
} else {
moveDir(tmp, file_found);
}
filesFound.add(file_found.getCanonicalPath().replace("\\", "\\\\"));
Path slink = Files.createSymbolicLink(file_found.toPath(), tmp.toPath());
slink.toFile().deleteOnExit();
String fileStr = file_found.getCanonicalPath();
filesFound.add(preparePythonLiteralPath(fileStr));
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -461,8 +472,8 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid
} else {
Metadata m = evidence.getMetadata();
for (String file : filesFound) {
String filel = file.substring(reportDumpPath.getCanonicalPath().replace("\\", "\\\\").length());
filel = filel.replace("\\\\", "/");
String filel = file.substring(preparePythonLiteralPath(reportDumpPath.getCanonicalPath()).length());
filel = prepareIPEDLiteralPath(filel);
String filename = filel.substring(filel.lastIndexOf("/") + 1);
m.add(ExtraProperties.LINKED_ITEMS, "path:\"*" + filel + "\" && name:\"" + filename + "\"");
}
Expand All @@ -474,6 +485,14 @@ private void processPlugin(LeapArtifactsPlugin p, IItem evidence, IItem dumpEvid

}

private String prepareIPEDLiteralPath(String filel) {
if (SystemUtils.IS_OS_WINDOWS) {
return filel.replace("\\\\", "/");
} else {
return filel;
}
}

static HashMap<String, String> escapedFiles = new HashMap<String, String>();

synchronized static public String replaceSpecialChars(String artpath) {
Expand Down

0 comments on commit 37e435e

Please sign in to comment.