https://github.com/rdbo/jnihook
Hook Java methods natively from C/C++
https://github.com/rdbo/jnihook
Last synced: about 1 year ago
JSON representation
Hook Java methods natively from C/C++
- Host: GitHub
- URL: https://github.com/rdbo/jnihook
- Owner: rdbo
- License: agpl-3.0
- Created: 2023-12-11T19:23:59.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-10-21T19:09:27.000Z (over 1 year ago)
- Last Synced: 2025-03-28T22:11:44.580Z (about 1 year ago)
- Language: C++
- Size: 180 KB
- Stars: 164
- Watchers: 6
- Forks: 24
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JNIHook

Hook Java methods natively from C/C++
## License
This project is licensed under the `GNU AGPL-3.0`. No later version is allowed.
Read the file `LICENSE` for more information.
## Example
Hooking the function `static int myFunction(int mynumber, String name)` from a class `Dummy`:
```c++
jmethodID originalMethod;
jint hkMyFunction(JNIEnv *env, jclass clazz, jint number, jstring name)
{
// Print parameters
std::cout << "[*] mynumber value: " << mynumber << std::endl;
std::cout << "[*] name object: " << name << std::endl;
// Call the original method with 'mynumber' set to '1337'
env->CallStaticIntMethod(dummyClass, originalMethod, 1337, name);
return 42; // Modify the return value to '42'
}
void start(JavaVM *jvm)
{
jclass dummyClass = env->FindClass("dummy/Dummy");
jmethodID myFunctionID = env->GetStaticMethodID(dummyClass, "myFunction",
"(ILjava/lang/String;)I");
JNIHook_Init(jvm);
JNIHook_Attach(myFunctionID, hkMyFunction, &originalMethod);
}
```
## Building
To build this, you can either compile all the files in `src` into your project, or
use CMake to build a static library, which can be compiled into your project.
NOTE: This requires C++17, which is available on newer versions of Visual Studio and GCC.
The steps ahead are for building a static library with CMake.
1. Setup your `JAVA_HOME` environment variable. It will be used for accessing `jni.h`, `jvmti.h`, and linking
the `jvm` library. On Linux, it's usually on `/usr/lib/jvm/`, and on Windows at `%ProgramFiles%\Java\jdk-`.
---
2. Create a build directory inside the root directory of the repository and enter it:
NOTE: Use the `x64 Native Tools Command Prompt` on Windows.
```
mkdir build
cd build
```
---
3. Run CMake to setup the project
**Linux/\*nix**:
```
cmake ..
```
**Windows**:
```
cmake .. -G "NMake Makefiles" -DCMAKE_CXX_STANDARD=17
```
---
4. Build using `nmake` (Windows) or `make` (*nix):
**Linux/\*nix**:
```
make
```
**Windows**:
```
nmake
```
---
After running these commands, you will have `libjnihook.a` or `jnihook.lib` in your `build` directory, which you can compile along with your project.
NOTE: Don't forget to include JNIHook's `include` dir in your project so that you can `#include `.
## Acknowledgements
Special thanks to [@Lefraudeur](https://github.com/Lefraudeur) for helping me with information about JVM functionality