Skip to content

Commit

Permalink
'#43 initial file hook implementations.
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdalla committed Oct 19, 2023
1 parent 715d5d5 commit 0ff7e5d
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 1 deletion.
57 changes: 57 additions & 0 deletions iped-utils/src/main/java/iped/utils/pythonhook/FileHook.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package iped.utils.pythonhook;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Collection;

public class FileHook {
FileInputStream fis;
File f;

public FileHook(String path, String... args) {
f = new File(path);
}

public FileHook(String path, String mode) {
f = new File(path);
}

public FileHook(String path) {
this(path, "r");
}

public String read() throws FileNotFoundException {
if (fis == null) {
fis = new FileInputStream(f);
}
return "old";
}

public void enter() {
try {
System.out.println("Enter:" + f.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void exit(Collection args) {
try {
System.out.println("Exit:" + f.getCanonicalPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void close() throws IOException {
if (fis != null) {
fis.close();
}

}

}
38 changes: 37 additions & 1 deletion iped-utils/src/main/java/iped/utils/pythonhook/PythonHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,36 @@ public String processHook(String filename, String module, String source) {
return source;
}

public void overrideFileOpen(Method method) {
String packageName = method.getDeclaringClass().getPackageName();
String className = method.getDeclaringClass().getSimpleName();
String methodName = method.getName();

jep.eval(contextWrapper);

StringBuffer def = new StringBuffer("lambda *args, **kwargs:");
def.append("ContextWrapper(");
def.append(className + "." + methodName + "(");
def.append("locals()['args'],locals()['kwargs']");
def.append(")");
def.append(")");

// overrides open
jep.eval("from " + packageName + " import " + className);
jep.eval("globals()['__builtins__']['oldopen']=globals()['__builtins__']['open']");
jep.eval("globals()['__builtins__']['open']=" + def);
}

public void installHook() {
jep.eval("import traceback");
jep.eval("import sys");
jep.eval("import importlib");
jep.eval("import builtins");
jep.eval("from java.lang import System");
jep.eval("from importlib.util import spec_from_loader");
jep.eval("from iped.utils.pythonhook import FileHook");
jep.set("javahook", this);

jep.eval(installHookClass);

jep.eval("sys.meta_path.insert(0, ImphookFileLoader())");
Expand Down Expand Up @@ -107,7 +130,7 @@ public static void main(String args[]) {
+ " return None\n"
+ " def create_module(self, spec):\n"
+ " modules=spec.name.split('.')\n"
+ " with open(spec.origin+'/'+modules[len(modules)-1]+'.py', encoding='utf8') as f:\n"
+ " with open(spec.origin+'/'+modules[len(modules)-1]+'.py') as f:\n"
+ " source = f.read()\n"
+ " source = javahook.processHook(spec.origin, spec.name, source)\n"
+ " if(source == None):\n"
Expand Down Expand Up @@ -194,5 +217,18 @@ private void addAppend(String module, String str) {
+ " return result\n" + " return newfunc\n" + " else:\n"
+ " return attr\n" + "";

private static String contextWrapper = "class ContextWrapper:\n" + " def __init__(self, w):\n"
+ " self.wrapped = w\n" + " pass\n"
+ " def __enter__(self):\n" + " self.wrapped.enter()\n" + " return self.wrapped\n"
+ " def __exit__(self, *args):\n"
+ " self.wrapped.exit(locals()['args'])\n"
+ " def __getattribute__(self, name):\n" + " if(name == \"wrapped\"):\n"
+ " return object.__getattribute__(self, name)\n"
+ " attr = object.__getattribute__(self.wrapped, name)\n" + " if hasattr(attr, '__call__'):\n"
+ " def newfunc(*args, **kwargs):\n" + " a = list(args)\n"
+ " a.insert(0,self.wrapped)\n" + " args = tuple(a)\n"
+ " result = attr(*args)\n" + " return result\n"
+ " return newfunc\n" + " else:\n" + " return attr\n" + "";

}

0 comments on commit 0ff7e5d

Please sign in to comment.