Omar Ahmed Omar Ahmed - 1 year ago 139
Java Question

How to convert a bytearray to Jar

I'm trying to load a jar from a byte array without having it written to a file (loading it into memory). I have made a custom ClassLoader but when I try to use it and load a class it gives me ClassNotFoundException.

ClassLoader

public class NetworkClassLoader extends ClassLoader
{
/*
* Default ClassLoader.
*/
private final ClassLoader startup;

/*
* Byte array used to load classes.
*/
private final byte[] bytes;

/*
* HashMap used to contain cached classes.
*/
private HashMap<String, byte[]> classes = new HashMap<>();

/*
* Initializes byte array used for loading classes.
* @param ClassLoader classLoader
* @param byte[] bytes
*/
public NetworkClassLoader(ClassLoader classLoader, byte[] bytes)
{
this.startup = classLoader;
this.bytes = bytes;
}

/*
* Loads class from name.
* (non-Javadoc)
* @see java.lang.ClassLoader#loadClass(java.lang.String, boolean)
* @param String name
* @param boolean resolve
* @throws ClassNotFoundException
* @returns clazz
*/
@Override
public Class<?> loadClass(String name, boolean resolve)
throws ClassNotFoundException
{
Class<?> clazz = findLoadedClass(name);
if (clazz == null)
{
try
{
InputStream in = getResourceAsStream(name.replace('.', '/') + ".class");
if (in == null) return null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.writeStream(in, out);
in.close();
byte[] bytes = out.toByteArray();
out.close();
clazz = defineClass(name, bytes, 0, bytes.length);
if (resolve)
{
resolveClass(clazz);
}
}
catch (Exception e)
{
clazz = super.loadClass(name, resolve);
}
}
return clazz;
}

/*
* Returns resource.
* (non-Javadoc)
* @see java.lang.ClassLoader#getResource(java.lang.String)
* @param String name
*/
@Override
public URL getResource(String name)
{
return null;
}

/*
* Returns resource as stream.
* (non-Javadoc)
* @see java.lang.ClassLoader#getResourceAsStream(java.lang.String)
* @param String name
* @return ByteArrayInputStream
*/
@Override
public InputStream getResourceAsStream(String name)
{
InputStream jarRes = this.startup.getResourceAsStream(name);
if (jarRes != null)
{
return jarRes;
}
if (!this.classes.containsKey(name))
{
return null;
}
return new ByteArrayInputStream((byte[])this.classes.get(name));
}

/*
* Loads classes using byte array.
*/
public void inject()
{
if (bytes == null) return;
try
{
JarInputStream jis = new JarInputStream(new ByteArrayInputStream(bytes));
try
{
JarEntry entry;
while ((entry = jis.getNextJarEntry()) != null)
{
String entryName = entry.getName();
ByteArrayOutputStream out = new ByteArrayOutputStream();
IOUtils.writeStream(jis, out);
byte[] bytes = out.toByteArray();
this.classes.put(entryName, bytes);
this.loadClass(entryName, false);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}

}


Main

byte[] array =
IOUtils.readFileBytes(new File("C:\\Users\\o_m_a\\Desktop\\HWID.jar"));
ByteClassLoader loader = new ByteClassLoader(Main.class.getClassLoader(), array);
loader.inject();
//System.out.println(Arrays.toString(array));

try {
Class<?> clazz = loader.loadClass("Main", true).newInstance();
Method m = clazz.getMethod(method, (Class<?>[]) null);
m.setAccessible(true);
m.invoke(clazz.newInstance(), (Object[]) null);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}


It loads my class correctly and runs it but I keep getting random errors.

Answer Source

This is a bit hackish but do his works, this code basically create a fake url scheme (myjarprotocol) that when opened return the jarBytes field (where your real jar data goes). Then via reflection it call SystemClassLoader.addURL that take a URL as parameter of the jar he have to load. In conclusion the SystemClassLoader is tricked to load a jar from a fake url scheme that return any InputStream you want.

public class JarLoader {

    private static final byte[] jarBytes = new byte[] { 0x00 /* .... etc*/ };

    public static void main(String[] args) throws Exception {
        URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
            public URLStreamHandler createURLStreamHandler(String urlProtocol) {
                System.out.println("Someone asked for protocol: " + urlProtocol);
                if ("myjarprotocol".equalsIgnoreCase(urlProtocol)) {
                    return new URLStreamHandler() {
                        @Override
                        protected URLConnection openConnection(URL url) throws IOException {
                            return new URLConnection(url) {
                                public void connect() throws IOException {}
                                public InputStream getInputStream() throws IOException {
                                    System.out.println("Someone is getting my jar!!");
                                    return new ByteArrayInputStream(jarBytes);
                                }
                            };
                        }
                    };
                }
                return null;
            }
        });

        System.out.println("Loading jar with fake protocol!");
        loadJarFromURL(new URL("myjarprotocol:fakeparameter"));
    }

    public static final void loadJarFromURL(URL jarURL) throws Exception {
        URLClassLoader systemClassloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
        Method systemClassloaderMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
        systemClassloaderMethod.setAccessible(true);
        systemClassloaderMethod.invoke(systemClassloader, jarURL);

        // This make classloader open the connection
        systemClassloader.findResource("/resource-404");
    }

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download