Code Monkey home page Code Monkey logo

jnihook's Introduction

JNIHook

jnihook-logo

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:

jvalue hkMyFunction(JNIEnv *jni, jmethodID callableMethod, jvalue *args, size_t nargs, void *arg)
{
	// Print parameters
	jvalue mynumber = args[0];
	jvalue name = args[1];
	std::cout << "[*] mynumber value: " << mynumber.i << std::endl;
	std::cout << "[*] name object: " << name.l << std::endl;

	// Call the original function with 'mynumber' set to '1337'
	mynumber.i = 1337;
	jni->CallStaticIntMethod(dummyClass, callableMethod, mynumber, (jstring)&name);

	// Get original function parameters
	return jvalue { .i = 42 }; // Modify the return value to '42'
}

void start(JavaVM *jvm, JNIEnv *jni)
{
	jclass dummyClass = jni->FindClass("dummy/Dummy");
	jmethodID myFunctionID = jni->GetStaticMethodID(dummyClass, "myFunction", "(ILjava/lang/String;)I");

	JNIHook_Init(jvm);
	JNIHook_Attach(myFunctionID, hkMyFunction, NULL);
}

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++20, which is available on newer versions of Visual Studio and GCC.

The steps ahead are for building a static library with CMake.

  1. 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
  1. Run CMake and set up the includes directories and additional OS-specific configuration. Windows example (%JAVA_HOME% is %ProgramFiles%\Java\jdk-<VERSION>; might be necessary to copy jvm.lib into your build directory):
cmake .. -G "NMake Makefiles" -DCMAKE_CXX_FLAGS="/I \"%JAVA_HOME%\include\" /I \"%JAVA_HOME%\include\win32\"" -DCMAKE_CXX_STANDARD=20

Linux example ($JAVA_HOME is usually /usr/lib/jvm/default-jvm or /usr/lib/jvm/default-java):

cmake .. -DCMAKE_CXX_FLAGS="-I \"$JAVA_HOME/include\" -I \"$JAVA_HOME/include/linux\""
  1. Build using nmake (Windows) or make (*nix):

Windows:

nmake

*nix:

make

After running these commands, you will have jnihook.lib or libjnihook.a 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 <jnihook.h>.

Credits

Special thanks to @Lefraudeur for helping me a lot through the development of this library

jnihook's People

Contributors

rdbo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.