Java: Method hooking & Finding object instances

Zabuzard

Situation

Hi, I have 2 problems.
The situation is that I'm writing a Java API for Windows that also provides tools for injecting code into a process and then manipulate the target. I have already implemented the injection-part, for example injecting a jar into another jar. At this point my jar gets called (while the target already is at runtime) and starts in a complete static context.

Goals & problems

From here I have two goals:

  1. I'd like to interact with the targets objects, thus I need references. For many objects this is already possible because they provide static access to their instances. For example awt.Frames#getFrames() provides access to all created Frame objects. But it would be awesome if there is a possibility to get access to arbitrary objects on the heap. Something like 'Heap#getAllObjectInstances()'.
  2. Given an object instance, I'd like to hook up onto arbitrary functions of this object. For example whenever BufferStrategy#show() gets called, I want it to call another method first.

So I summarize the problems as follows:

  1. How to get arbitrary object references from a static context?
  2. How to hook up onto arbitrary functions?

Remarks

What I've done so far, remarks and ideas:

  1. The JDI (Java Debugger Interface) provides such a method via VirtualMachine#allClasses() -> ReferenceType#instances(0). But the JDI needs the target JVM to be started with additional debug parameter which is no option for me. One could go down to low-level and analyze the heap with memory tools, but I hope someone knows a more high-level approach. Using the Windows API would be an option for me as I'm familiar with JNA/JNI, but I don't know such a tool.
  2. The last resort would be to use IAT hooking with C-Code, a very low-level approach, I'd like to avoid this. As I can assume having a object reference at this point, maybe does the Reflection API provide a method to change an objects method? Or at least simply provide a hooking mechanism?

Be aware that changing the targeted code certainly is no option for me. And that it is already at runtime, thus ByteCode-Manipulation could also be an option.

Scenario

A scenario where this would come in handy:
The target is a game, deployed as jar. It renders with a Double-Buffer-Strategy, using the BufferStrategy class. It displays the image with BufferStrategy#show(). We inject our jar inside the game and like to draw an overlay with additional information. For this we get an reference to the used BufferStrategy and hook up onto its show-method. So that it calls our drawOverlay-method everytime it gets called, then we pass back to the original show-method.

apangin

What you need is JVMTI agent - a native library that makes use of JVM Tool Interface.

Agents can be attached dynamically to a running VM using the Attach API.
See VirtualMachine.loadAgentPath.

  1. To get all instances of a given class use JVMTI IterateOverInstancesOfClass function.
    See the related question for details.

  2. To intercept a method of a foreign class you'll need JVMTI RetransformClasses API. The same can be also achieved by using Java-level instrumentation API, see Instrumentation.retransformClasses.

For the example of JVMTI-level method interception refer to demo/jvmti/mtrace from Oracle JDK demos and samples package.

Java-level instrumentation will be easier with bytecode manipulation libraries like Byte Buddy.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Hooking virtual method on Win32 (return object larger than ptr crashes)?

From Dev

set a (method) breakpoint for a particular object (and not all instances of that type) in java

From Dev

java instanceof not finding method

From Dev

Finding ansible RDS instances

From Dev

Instantiating a new object in Java resets all data in other instances of that object

From Dev

Java Chess: Should finding nearby square be a method of Square or Board class?

From Dev

Finding multiple instances in a table

From Dev

Finding object in ArrayList via user input - Java

From Dev

Calling a method on all instances of an object

From Dev

Java object fromString method?

From Dev

Android app : java / JNI call hooking strategies

From Dev

Hooking into main application's onCreate method from Adobe AIR extension

From Dev

Java new object instance changing the old instances

From Dev

Finding instances of Alloy predicate executions

From Dev

Finding Android Java usable methods in a Shared Object File

From Dev

Hooking method with ArrayList<MyObject> parameter

From Dev

Ruby finding object instances already initialized

From Dev

Finding ansible RDS instances

From Dev

Parsing a text for finding java method call

From Dev

Finding a method exists in rest service or not in Java

From Dev

Finding if an Object has a specific Method

From Dev

Finding a instances of a string inside a string

From Dev

How Do I Run Multiple instances .java Files (running one of them as an Object or Method)

From Dev

Hooking into main application's onCreate method from Adobe AIR extension

From Dev

Regarding Object/Class Instances in Java

From Dev

Java: Method hooking & Finding object instances

From Dev

When creating multiple instances of the same object, does Java replicate the method implementations?

From Dev

Finding multiple instances in multiple columns

From Dev

Method 'Range' of object '_worksheet' failed when finding a range

Related Related

  1. 1

    Hooking virtual method on Win32 (return object larger than ptr crashes)?

  2. 2

    set a (method) breakpoint for a particular object (and not all instances of that type) in java

  3. 3

    java instanceof not finding method

  4. 4

    Finding ansible RDS instances

  5. 5

    Instantiating a new object in Java resets all data in other instances of that object

  6. 6

    Java Chess: Should finding nearby square be a method of Square or Board class?

  7. 7

    Finding multiple instances in a table

  8. 8

    Finding object in ArrayList via user input - Java

  9. 9

    Calling a method on all instances of an object

  10. 10

    Java object fromString method?

  11. 11

    Android app : java / JNI call hooking strategies

  12. 12

    Hooking into main application's onCreate method from Adobe AIR extension

  13. 13

    Java new object instance changing the old instances

  14. 14

    Finding instances of Alloy predicate executions

  15. 15

    Finding Android Java usable methods in a Shared Object File

  16. 16

    Hooking method with ArrayList<MyObject> parameter

  17. 17

    Ruby finding object instances already initialized

  18. 18

    Finding ansible RDS instances

  19. 19

    Parsing a text for finding java method call

  20. 20

    Finding a method exists in rest service or not in Java

  21. 21

    Finding if an Object has a specific Method

  22. 22

    Finding a instances of a string inside a string

  23. 23

    How Do I Run Multiple instances .java Files (running one of them as an Object or Method)

  24. 24

    Hooking into main application's onCreate method from Adobe AIR extension

  25. 25

    Regarding Object/Class Instances in Java

  26. 26

    Java: Method hooking & Finding object instances

  27. 27

    When creating multiple instances of the same object, does Java replicate the method implementations?

  28. 28

    Finding multiple instances in multiple columns

  29. 29

    Method 'Range' of object '_worksheet' failed when finding a range

HotTag

Archive