Skip to content

Instantly share code, notes, and snippets.

@superblaubeere27
Last active August 2, 2022 02:24
Show Gist options
  • Save superblaubeere27/cffe74faf3545c760f7d5d198403424c to your computer and use it in GitHub Desktop.
Save superblaubeere27/cffe74faf3545c760f7d5d198403424c to your computer and use it in GitHub Desktop.
Code injection with Java Agents

Code injection with Java Agents

General information

Java Agents are used to replace code when the application is running. When you are debugging with IntelliJ, Eclipse etc. and you saving / rebuilding your code, an agent will be loaded to do it.

Restrictions

The environment has to be a Java Development Kit (not a Java Runtime Enviroment) to inject the code at runtime. If you want to apply the agent with -javaagent:<agent-jar> it will work for both.

Furthermore the agent can only add classes and change method contents at runtime. It can't:

  • Change Field / Method access
  • Add Fields or Fethods
  • Change Class's Version
  • etc.

When should I use it?

Here are some use cases where this can come to handy:

  1. To impress
  2. When you want to change the behaviour of your added code depending on the program's state
  3. For debugging

Tools

To make code injection easier I made some tools:

asm-delta

asm-delta is a tool to calculate the difference between two jars on bytecode level. It also detects if the changes are valid for an agent and if it isn't it tells you what the problem is.

Link: https://github.com/superblaubeere27/asm-delta

asm-delta-agent

asm-delta-agent is the corresponding agent for asm-delta. It applies an asm-delta patch file at runtime.

Link: https://github.com/superblaubeere27/asm-delta-agent

Agent-Attacher (Gist)

Just a simple tool that searches for a running Minecraft Session, attaches itself to it and loads the agent.

Link: https://gist.github.com/superblaubeere27/97cba0f22d06232cb98033d28c21d37b

Discord

If you have problems or questions you can join my discord server: https://gist.github.com/superblaubeere27/bc2f3ed5694c78efa812d6fbeb4d241c

@RealNoahgg
Copy link

download

@Euro-pol
Copy link

Euro-pol commented Jun 4, 2022

free download

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment