-1

This is the code of my Agent.jar

public class Agent
{

    public static void agentmain(String s, Instrumentation instrumentation)
    {
        try
        {
            ClassLoader classLoader = null;
            for (Class clazz : instrumentation.getAllLoadedClasses())
            {
                String className = clazz.getName();
                if (className.equalsIgnoreCase("ave")) /* Just a class from the running Programm */
                {
                    classLoader = clazz.getClassLoader();
                }
            }

            /* In the Cheat.jar are Classes which im trying to load */
            ClassLoader loader = URLClassLoader.newInstance(new URL[]{new URL("C:\\Users\\michi\\Desktop\\Injection\\Cheat.jar")}, classLoader);
            Class.forName("de.simplyblack.client.client.module.Category", true, loader);
            Class.forName("de.simplyblack.client.client.module.Module", true, loader);
            Class.forName("de.simplyblack.client.client.module.ModuleManager", true, loader);
        } catch (Throwable t)
        {
            t.printStackTrace();
        }
    }
}

I am loading this with an extra Programm.

 VirtualMachine virtualMachine = VirtualMachine.attach(id);
 virtualMachine.loadAgent(new File("C:\\Users\\michi\\Desktop\\Injection\\Client.jar").getAbsolutePath());
 virtualMachine.detach();

But this is not working.

Later I visit an Class, and make an call for the ModuleManager class.

If I Inject it, i get an

Class not found: de.simplyblack.client.client.module.ModuleManager

error.

Could you please tell me how I can fix that? It would help me a lot! Thanks.

BlueCodeZ
  • 17
  • 2

1 Answers1

1

When references within a class are resolved, its defining class loader is used. Your code identifies the defining class loader of the class you want to instrument, but then, creates a new class loader using it as parent loader.

When you ask that new loader to load your classes, the classes very likely are loaded, but they are only reachable through your newly created URLClassLoader. They are not available to other class loaders.

Instrumenting classes with code containing new dependencies to other classes can be very tricky. If the instrumented classes have been loaded by the application class loader, you can use Instrumentation.appendToSystemClassLoaderSearch(JarFile) to add your Cheat.jar to the search path, to make the classes available.

For classes loaded by other loaders, things get more complicated. If they follow the standard parent delegation model, you can use appendToBootstrapClassLoaderSearch(JarFile) to make your classes available to all these class loaders.

For a loader not following the delegation model, you would have to dig deeper, e.g. use Reflection with access override, to call defineClass on it making the classes available in that scope.

Holger
  • 285,553
  • 42
  • 434
  • 765