Invoke an embedded jar without dropping files to HDD - Pumpkins - Sep 14, 2016
I made this about a year ago for my projects, decided to share it as I have a new one:
Note:
-any non-class resources have to be moved from embedded jar the wrappers jar with the same name/package (ie: dll files, images, etc) unless you want to fix the class path stuff. 🤝 Other than that it loads class files perfectly.
Usage:
Code BCJInjector loader = new BCJInjector(mainclass, new ByteArrayInputStream(filebytes));
loader.startJar(args);
Class:
Code package fuzzle;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
/**
* Created by Pumpkins on 29/05/2015.
*
*/
public class BCJInjector extends ClassLoader {
private Map<String, byte[]> classes = new HashMap<String, byte[]>();
public String mainClass;
private Method m;
public BCJInjector(String main, InputStream in) {
super(BCJInjector.class.getClassLoader());
try {
JarInputStream jis = new JarInputStream(in);
mainClass = main;//jis.getManifest().getMainAttributes().getValue("Main-Class");
JarEntry je = null;
String entryName = null;
while ((je = jis.getNextJarEntry()) != null) {
entryName = je.getName();
if (je.getName().endsWith(".class")) {
String canonicalName = entryName.replaceAll("/", ".").replaceAll(".class", "");
try {
byte[] classBytes = readClass(jis);
classes.put(canonicalName, classBytes);
/*if (!canonicalName.equals(mainClass)) { //This gets done later to extract methods.
resolveClass(defineClass(canonicalName, classBytes, 0, classBytes.length));
}*/
} catch (Exception e) {
System.out.println("Failed to dynamically map a class.");
} finally {
//System.out.println("Resolved & defined: " + canonicalName);
}
}
}
jis.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
Class<?> tClass = null;
try {
tClass = loadClass(mainClass);
m = tClass.getMethod("main", new Class[] { new String[] {}.getClass() });
m.setAccessible(true);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
public void startJar( String[] args ) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
int mods = m.getModifiers();
if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
throw new NoSuchMethodException("main");
}
try {
m.invoke(null, new Object[] { args });
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
private byte[] readClass(InputStream stream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while(true){
int qwe = stream.read();
if(qwe == -1) break;
baos.write(qwe);
}
return baos.toByteArray();
}
public Class loadClass(String name) throws ClassNotFoundException {
try {
return this.getParent().loadClass(name);
} catch (ClassNotFoundException e) {
return findClass(name);
}
}
public Class findClass(String name) throws ClassNotFoundException {
byte[] classBytes = classes.get(name);
return defineClass(name, classBytes, 0, classBytes.length);
}
}
* - sweed - Jul 29, 2017
please i want to buy your crypter but i can't contact here
please add me on skype: sweed.sweed7
* - matt - Jul 29, 2017
I made something like this a while back that instead loaded the class from a hard-coded byte array, it was pretty cool. :P
Thanks for sharing though, I always love to see contributions to the Java sub-forum! :-)
* - Thepoeltje - Jul 31, 2017
Pumpkins u should update people on Oscelestial you scamming people.
|