Bytecode modification of an existing loaded class

Viewed 80

I want to mutate a function of a class that is loaded at runtime (it has a bug in it but the project is long gone so i cannot build the binary). What i want to do instead is write a piece of code which will run during the application initialization phase and mutate this function so that it works fine. And simply keep that code around until the replacement is ready.

Having no experience with bytecode modification what library could i use to modify and reload a class at runtime? Specifically i need to replace a throw instruction with a noop instruction (i did this once using hex editor but lost the binary).

Also if you know any tutorial on how to do something like that please share.

I can see many libraries for doing this but i cant know which ones are good/bad do the job...

1 Answers

I think use Java Attach API. Java Attach API is procedure of loading a Java agent into an already running JVM. you can understand the work of javaagents by reading the Java Instrument javadoc. AgentMain help to you.

Agentmain is invoked when an agent is started after the application is already running. Agents started with agentmain can be attached programatically using the Sun tools API (for Sun/Oracle JVMs only -- the method for introducing dynamic agents is implementation-dependent).

This tutorial is useful about java instrumentation.

Related