Skip to content

Commit 78723cf

Browse files
authored
Create README.md
1 parent 537277d commit 78723cf

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Runtime Java Class Dumper
2+
Runtime dumping java classes using JNI
3+
4+
### How it works?
5+
To be able to dump the classes of a Java program at runtime, we need to have access to them when they are being loaded. For this, we use the JNI (Java Native Interface) library, which provides us with the means to do so.
6+
7+
#### First, we obtain the JVM, and then the JVMTI (Java Virtual Machine Tool Interface).
8+
9+
```
10+
FARPROC processAddress = GetProcAddress(reinterpret_cast<HMODULE>(jvm), "JNI_GetCreatedJavaVMs");
11+
t_createdvms created_java_vms = reinterpret_cast<t_createdvms>(processAddress);
12+
13+
created_java_vms(&vm, 1, nullptr);
14+
vm->GetEnv(reinterpret_cast<void**>(&jvmti_env), JVMTI_VERSION_1_1);
15+
```
16+
17+
#### After this, we will use the JVMTI to handle events through callbacks. [Documentation](https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#Events).
18+
19+
```
20+
jvmtiEventCallbacks callbacks;
21+
memset(&callbacks, 0, sizeof(callbacks));
22+
23+
callbacks.ClassFileLoadHook = &__callback_class_file_load_hook;
24+
25+
jvmti_env->SetEventCallbacks(&callbacks, (jint) sizeof(callbacks));
26+
jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
27+
```
28+
29+
#### The event we are looking for is [ClassFileLoadHook](https://docs.oracle.com/javase/8/docs/platform/jvmti/jvmti.html#ClassFileLoadHook). This event is sent when the VM obtains class file data.
30+
31+
```
32+
void __stdcall __callback_class_file_load_hook(jvmtiEnv* jvmti_env,
33+
JNIEnv* jni_env,
34+
jclass class_being_redefined,
35+
jobject loader,
36+
const char* name,
37+
jobject protection_domain,
38+
jint class_data_len,
39+
const unsigned char* class_data,
40+
jint* new_class_data_len,
41+
unsigned char** new_class_data)
42+
43+
```
44+
45+
#### To conclude, we read the classes using the parameters of our callback, and upon dettach our DLL, we clean up the callbacks and dettach our thread.
46+
47+
```
48+
jvmti_env->SetEventCallbacks(NULL, NULL);
49+
jvmti_env->SetEventNotificationMode(JVMTI_DISABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL);
50+
51+
vm->DetachCurrentThread();
52+
```

0 commit comments

Comments
 (0)