Skip to content

Commit

Permalink
Add static initializer for WindowsSandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
pandaninjas committed Oct 12, 2023
1 parent 2dc0301 commit 14cdcfc
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package gq.malwarefight.nosession.win;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;

public class WindowsSandbox {
/**
* Run a process sandboxed
Expand All @@ -12,4 +18,31 @@ public class WindowsSandbox {
* @return whether we are running in a sandbox
*/
public static native boolean isSandboxed();

/**
* This method was pulled from the Utils class so people can use this module as a standalone library
*/
public static void copy(InputStream i, OutputStream o) throws IOException {
byte[] buffer = new byte[16384];
int read;
while ((read = i.read(buffer)) > 0) {
o.write(buffer, 0, read);
}
i.close();
o.close();
}

static {
try {
File tempFile = Files.createTempFile("minceraft_sandbox", ".dll").toFile();
try (InputStream is = WindowsSandbox.class.getResourceAsStream("/native/" + System.getProperty("os.arch") + "/windows/" + System.mapLibraryName("nosession_libc"))) {
assert is != null: "Native library not compiled";
copy(is, Files.newOutputStream(tempFile.toPath()));
}
System.load(tempFile.getAbsolutePath());
tempFile.deleteOnExit();
} catch (Exception e) {
throw new RuntimeException("Cannot instantiate WindowsSandbox");
}
}
}

0 comments on commit 14cdcfc

Please sign in to comment.