├── .gitignore
├── .idea
├── .gitignore
├── compiler.xml
├── gradle.xml
├── misc.xml
└── vcs.xml
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ ├── androidTest
│ └── java
│ │ └── com
│ │ └── example
│ │ └── jnihook
│ │ └── ExampleInstrumentedTest.kt
│ ├── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ │ ├── CMakeLists.txt
│ │ ├── test_dispatch_table.c
│ │ ├── test_jni_hook.c
│ │ └── test_jni_hook.h
│ ├── java
│ │ └── com
│ │ │ └── example
│ │ │ └── jnihook
│ │ │ ├── DispatchTableHook.kt
│ │ │ └── MainActivity.kt
│ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ ├── activity_dispatch_table_hook.xml
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-mdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── mipmap-xxxhdpi
│ │ ├── ic_launcher.webp
│ │ └── ic_launcher_round.webp
│ │ ├── values-night
│ │ └── themes.xml
│ │ ├── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── themes.xml
│ │ └── xml
│ │ ├── backup_rules.xml
│ │ └── data_extraction_rules.xml
│ └── test
│ └── java
│ └── com
│ └── example
│ └── jnihook
│ └── ExampleUnitTest.kt
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── jniTest
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ │ ├── CMakeLists.txt
│ │ └── test_jni.c
│ └── java
│ │ └── com
│ │ └── example
│ │ └── jnitest
│ │ └── NativeLib.kt
│ └── test
│ └── java
│ └── com
│ └── example
│ └── jnitest
│ └── ExampleUnitTest.kt
├── jnihook
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
│ ├── main
│ ├── AndroidManifest.xml
│ ├── cpp
│ │ ├── CMakeLists.txt
│ │ ├── dispatchtable_hook.c
│ │ ├── dl_symbol_search.c
│ │ ├── dl_symbol_search.h
│ │ ├── include
│ │ │ ├── dispatchtable_hook.h
│ │ │ └── jni_hook.h
│ │ └── jni_hook.c
│ └── java
│ │ └── com
│ │ └── pika
│ │ └── jnihook
│ │ └── JniHook.kt
│ └── test
│ └── java
│ └── com
│ └── pika
│ └── jnihook
│ └── ExampleUnitTest.kt
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # JniHook
2 | 一个简单的工具库,专门处理Jni 函数调用Hook
3 |
4 | 原理讲解:https://juejin.cn/post/7268894037464367140
5 |
6 | # 使用指南
7 | ## 开启prefad
8 | ```
9 | build.gradle 中开启prefab
10 | buildFeatures {
11 | prefab true
12 | }
13 | ```
14 | ## 初始化hook
15 | ```
16 | JniHook.jniHookInit()
17 | ```
18 |
19 | ## hook jni函数
20 | 需要的时候调用hook_jni函数即可
21 |
22 | * env:jni 环境
23 | * method:java层中需要hook的jni函数,即Method
24 | * new_entrance:native层的代理函数指针
25 | * origin_entrance:原函数指针的指针(指针的地址)
26 | ```
27 | int hook_jni(JNIEnv *env, jobject method, void *new_entrance, void **origin_entrance)
28 | ```
29 | 返回值:1(hook 成功) 0(当前已经hook) -1(hook失败) -2(当前jni函数还没有注册,可以通过注册RegisterNatives函数监听,见set_register_natives_call)
30 | ## unhook jni函数
31 | 如果需要解除hook,调用unhook_jni
32 | * env:jni 环境
33 | * method:java层中hook的jni函数,即Method
34 | * origin_entrance:原函数指针
35 | ```
36 | void unhook_jni(JNIEnv *env, jobject method, void *origin_entrance)
37 | ```
38 | ## set_register_natives_call
39 | 当jni 函数还未被加载时,此时hook_jni会返回无效状态-2,因此可以监听RegisterNatives 函数调用进行查看是否有需要的jni函数正在被注册,注册之后hook_jni才会返回1
40 | ```
41 | void set_register_natives_call(register_native_call call)
42 | ```
43 |
44 |
45 |
46 |
47 | ## 项目层级介绍
48 | * **app下是使用例子**
49 | * **jnihook 是jnihook的核心实现**
50 |
51 | ## 环境准备
52 | 建议直接用最新的稳定版本Android Studio打开工程。目前项目已适配`Android Studio Arctic Fox | 2022.3.1`
53 | ###
54 |
55 | ## 感谢
56 | [btrace]([https://www.runoob.com](https://github.com/bytedance/btrace)https://github.com/bytedance/btrace)
57 |
58 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'com.example.jnihook'
8 | compileSdk 32
9 |
10 | packagingOptions {
11 | pickFirst '**/libxdl.so'
12 | }
13 |
14 | defaultConfig {
15 | applicationId "com.example.jnihook"
16 | minSdk 23
17 | targetSdk 32
18 | versionCode 1
19 | versionName "1.0"
20 |
21 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
22 | externalNativeBuild {
23 | cmake {
24 | arguments '-DANDROID_STL=c++_shared'
25 | }
26 | }
27 | ndk {
28 | abiFilters 'armeabi-v7a', 'arm64-v8a'
29 | }
30 | }
31 |
32 | buildTypes {
33 | release {
34 | minifyEnabled false
35 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
36 | }
37 | }
38 | compileOptions {
39 | sourceCompatibility JavaVersion.VERSION_1_8
40 | targetCompatibility JavaVersion.VERSION_1_8
41 | }
42 | kotlinOptions {
43 | jvmTarget = '1.8'
44 | }
45 | externalNativeBuild {
46 | cmake {
47 | path file('src/main/cpp/CMakeLists.txt')
48 | version '3.18.1'
49 | }
50 | }
51 | buildFeatures {
52 | viewBinding true
53 | }
54 | buildFeatures {
55 | prefab true
56 | }
57 | }
58 |
59 | dependencies {
60 |
61 | implementation 'androidx.core:core-ktx:1.7.0'
62 | implementation 'androidx.appcompat:appcompat:1.5.1'
63 | implementation 'com.google.android.material:material:1.5.0'
64 | implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
65 | implementation project(":jnihook")
66 | implementation project(":jniTest")
67 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/example/jnihook/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnihook
2 |
3 | import androidx.test.ext.junit.runners.AndroidJUnit4
4 | import androidx.test.platform.app.InstrumentationRegistry
5 | import org.junit.Assert.*
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | /**
10 | * Instrumented test, which will execute on an Android device.
11 | *
12 | * See [testing documentation](http://d.android.com/tools/testing).
13 | */
14 | @RunWith(AndroidJUnit4::class)
15 | class ExampleInstrumentedTest {
16 | @Test
17 | fun useAppContext() {
18 | // Context of the app under test.
19 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
20 | assertEquals("com.example.jnihook", appContext.packageName)
21 | }
22 | }
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
15 |
18 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
30 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/cpp/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # For more information about using CMake with Android Studio, read the
2 | # documentation: https://d.android.com/studio/projects/add-native-code.html
3 |
4 | # Sets the minimum version of CMake required to build the native library.
5 |
6 | cmake_minimum_required(VERSION 3.18.1)
7 |
8 | # Declares and names the project.
9 |
10 | project("jnihooktest")
11 |
12 | # Creates and names a library, sets it as either STATIC
13 | # or SHARED, and provides the relative paths to its source code.
14 | # You can define multiple libraries, and CMake builds them for you.
15 | # Gradle automatically packages shared libraries with your APK.
16 |
17 | add_library( # Sets the name of the library.
18 | jnihooktest
19 |
20 | # Sets the library as a shared library.
21 | SHARED
22 |
23 | # Provides a relative path to your source file(s).
24 | test_jni_hook.c
25 | test_dispatch_table.c
26 | )
27 |
28 | # Searches for a specified prebuilt library and stores the path as a
29 | # variable. Because CMake includes system libraries in the search path by
30 | # default, you only need to specify the name of the public NDK library
31 | # you want to add. CMake verifies that the library exists before
32 | # completing its build.
33 |
34 | find_library( # Sets the name of the path variable.
35 | log-lib
36 |
37 | # Specifies the name of the NDK library that
38 | # you want CMake to locate.
39 | log)
40 |
41 | # Specifies libraries CMake should link to your target library. You
42 | # can link multiple libraries, such as libraries you define in this
43 | # build script, prebuilt third-party libraries, or system libraries.
44 |
45 | target_link_libraries( # Specifies the target library.
46 | jnihooktest
47 |
48 | # Links the target library to the log library
49 | # included in the NDK.
50 | ${log-lib})
51 |
52 | find_package(jnihook REQUIRED CONFIG)
53 | ####
54 | target_link_libraries(jnihooktest jnihook::jnihook)
--------------------------------------------------------------------------------
/app/src/main/cpp/test_dispatch_table.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 |
5 |
6 | static void *origin_calloc;
7 |
8 | static void *my_calloc(size_t __item_count, size_t __item_size) {
9 | __android_log_print(ANDROID_LOG_ERROR, "hello", "calloc hook %zu %zu", __item_count,
10 | __item_size);
11 | MallocCalloc c = (MallocCalloc) origin_calloc;
12 | return c(__item_count, __item_size);
13 | }
14 |
15 | JNIEXPORT void JNICALL
16 | Java_com_example_jnihook_DispatchTableHook_testDispatchTableHook(JNIEnv *env, jobject thiz) {
17 | init_dispatch_table();
18 | dispatch_table_hook(CALLOC, my_calloc, &origin_calloc);
19 | calloc(1,4);
20 | }
21 |
--------------------------------------------------------------------------------
/app/src/main/cpp/test_jni_hook.c:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | #include
4 | #include
5 | #include "test_jni_hook.h"
6 | #include "jni_hook.h"
7 |
8 |
9 | // 定义原始函数
10 | static void (*test_jni_original)(JNIEnv *, jobject);
11 |
12 | static void test_jni_hook_proxy(JNIEnv *env, jobject java_this) {
13 | // 先走到代理函数,然后走到原函数
14 | __android_log_print(ANDROID_LOG_ERROR, "hello", "%s", "test_jni_hook_proxy");
15 |
16 | test_jni_original(env, java_this);
17 | }
18 |
19 |
20 | JNIEXPORT void JNICALL
21 | Java_com_example_jnihook_MainActivity_hooktest(JNIEnv *env, jobject thiz, jobject method) {
22 | int result = hook_jni(env, method, (void *) test_jni_hook_proxy, (void **) &test_jni_original);
23 | __android_log_print(ANDROID_LOG_ERROR, "hello", "jni hook result %d", result);
24 | }
25 |
26 | JNIEXPORT void JNICALL
27 | Java_com_example_jnihook_MainActivity_unhooktest(JNIEnv *env, jobject thiz, jobject method) {
28 | unhook_jni(env, method, test_jni_original);
29 |
30 | }
31 |
32 |
33 | static void
34 | test_register_native(JNIEnv *env, jclass c, const JNINativeMethod *methods, jint nMethods) {
35 | __android_log_print(ANDROID_LOG_ERROR, "hello", "当前so进行了jni方法注册 %p", (*methods).fnPtr);
36 | }
37 |
38 | JNIEXPORT void JNICALL
39 | Java_com_example_jnihook_MainActivity_testRegisterNative(JNIEnv *env, jobject thiz) {
40 | set_register_natives_call(test_register_native);
41 | }
--------------------------------------------------------------------------------
/app/src/main/cpp/test_jni_hook.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/jnihook/DispatchTableHook.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnihook
2 |
3 | import android.app.Activity
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.util.Log
7 | import androidx.appcompat.app.AppCompatActivity
8 | import com.example.jnihook.databinding.ActivityDispatchTableHookBinding
9 |
10 | class DispatchTableHook : AppCompatActivity() {
11 | private lateinit var binding: ActivityDispatchTableHookBinding
12 | override fun onCreate(savedInstanceState: Bundle?) {
13 | super.onCreate(savedInstanceState)
14 | binding = ActivityDispatchTableHookBinding.inflate(layoutInflater)
15 | setContentView(binding.root)
16 | binding.btnTest.setOnClickListener {
17 | testDispatchTableHook()
18 | }
19 | }
20 |
21 | external fun testDispatchTableHook()
22 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/jnihook/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnihook
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import androidx.appcompat.app.AppCompatActivity
6 | import com.example.jnihook.databinding.ActivityMainBinding
7 | import com.example.jnitest.NativeLib
8 | import com.pika.jnihook.JniHook
9 | import java.lang.reflect.Method
10 |
11 | class MainActivity : AppCompatActivity() {
12 |
13 | private lateinit var binding: ActivityMainBinding
14 |
15 | override fun onCreate(savedInstanceState: Bundle?) {
16 | super.onCreate(savedInstanceState)
17 |
18 | binding = ActivityMainBinding.inflate(layoutInflater)
19 | setContentView(binding.root)
20 | // 初始化hook
21 | JniHook.jniHookInit()
22 |
23 | val hookMethod = NativeLib::class.java.getDeclaredMethod("testJNI")
24 | val nativeTest = NativeLib()
25 |
26 | binding.originalCall.setOnClickListener {
27 | // 正常调用函数
28 | nativeTest.initSo()
29 | nativeTest.testJNI()
30 | }
31 | binding.jnihook.setOnClickListener {
32 | hooktest(hookMethod)
33 | }
34 |
35 | binding.unhook.setOnClickListener {
36 | unhooktest(hookMethod)
37 | }
38 |
39 | binding.registerNativeCall.setOnClickListener {
40 | testRegisterNative()
41 | }
42 |
43 | binding.entry.setOnClickListener {
44 | startActivity(Intent(this, DispatchTableHook::class.java))
45 | }
46 |
47 | }
48 |
49 | // 需要hook的函数
50 | companion object {
51 | init {
52 | System.loadLibrary("jnihooktest")
53 | }
54 | }
55 |
56 | // 替换的函数
57 | external fun hooktest(method: Method)
58 | external fun unhooktest(method: Method)
59 |
60 | external fun testRegisterNative()
61 |
62 |
63 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_dispatch_table_hook.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
19 |
20 |
28 |
29 |
37 |
38 |
46 |
47 |
54 |
55 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-hdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-hdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-mdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-mdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | JniHook
3 |
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/backup_rules.xml:
--------------------------------------------------------------------------------
1 |
8 |
9 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/xml/data_extraction_rules.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
8 |
12 |
13 |
19 |
--------------------------------------------------------------------------------
/app/src/test/java/com/example/jnihook/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnihook
2 |
3 | import org.junit.Assert.*
4 | import org.junit.Test
5 |
6 | /**
7 | * Example local unit test, which will execute on the development machine (host).
8 | *
9 | * See [testing documentation](http://d.android.com/tools/testing).
10 | */
11 | class ExampleUnitTest {
12 | @Test
13 | fun addition_isCorrect() {
14 | assertEquals(4, 2 + 2)
15 | }
16 | }
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 | plugins {
3 | id 'com.android.application' version '7.3.1' apply false
4 | id 'com.android.library' version '7.3.1' apply false
5 | id 'org.jetbrains.kotlin.android' version '1.7.20' apply false
6 | }
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app's APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Kotlin code style for this project: "official" or "obsolete":
19 | kotlin.code.style=official
20 | # Enables namespacing of each library's R class so that its R class includes only the
21 | # resources declared in the library itself and none from the library's dependencies,
22 | # thereby reducing the size of the R class for that library
23 | android.nonTransitiveRClass=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Aug 19 15:57:35 CST 2023
2 | distributionBase=GRADLE_USER_HOME
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
4 | distributionPath=wrapper/dists
5 | zipStorePath=wrapper/dists
6 | zipStoreBase=GRADLE_USER_HOME
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | #
4 | # Copyright 2015 the original author or authors.
5 | #
6 | # Licensed under the Apache License, Version 2.0 (the "License");
7 | # you may not use this file except in compliance with the License.
8 | # You may obtain a copy of the License at
9 | #
10 | # https://www.apache.org/licenses/LICENSE-2.0
11 | #
12 | # Unless required by applicable law or agreed to in writing, software
13 | # distributed under the License is distributed on an "AS IS" BASIS,
14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | # See the License for the specific language governing permissions and
16 | # limitations under the License.
17 | #
18 |
19 | ##############################################################################
20 | ##
21 | ## Gradle start up script for UN*X
22 | ##
23 | ##############################################################################
24 |
25 | # Attempt to set APP_HOME
26 | # Resolve links: $0 may be a link
27 | PRG="$0"
28 | # Need this for relative symlinks.
29 | while [ -h "$PRG" ] ; do
30 | ls=`ls -ld "$PRG"`
31 | link=`expr "$ls" : '.*-> \(.*\)$'`
32 | if expr "$link" : '/.*' > /dev/null; then
33 | PRG="$link"
34 | else
35 | PRG=`dirname "$PRG"`"/$link"
36 | fi
37 | done
38 | SAVED="`pwd`"
39 | cd "`dirname \"$PRG\"`/" >/dev/null
40 | APP_HOME="`pwd -P`"
41 | cd "$SAVED" >/dev/null
42 |
43 | APP_NAME="Gradle"
44 | APP_BASE_NAME=`basename "$0"`
45 |
46 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
47 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
48 |
49 | # Use the maximum available, or set MAX_FD != -1 to use that value.
50 | MAX_FD="maximum"
51 |
52 | warn () {
53 | echo "$*"
54 | }
55 |
56 | die () {
57 | echo
58 | echo "$*"
59 | echo
60 | exit 1
61 | }
62 |
63 | # OS specific support (must be 'true' or 'false').
64 | cygwin=false
65 | msys=false
66 | darwin=false
67 | nonstop=false
68 | case "`uname`" in
69 | CYGWIN* )
70 | cygwin=true
71 | ;;
72 | Darwin* )
73 | darwin=true
74 | ;;
75 | MINGW* )
76 | msys=true
77 | ;;
78 | NONSTOP* )
79 | nonstop=true
80 | ;;
81 | esac
82 |
83 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
84 |
85 |
86 | # Determine the Java command to use to start the JVM.
87 | if [ -n "$JAVA_HOME" ] ; then
88 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
89 | # IBM's JDK on AIX uses strange locations for the executables
90 | JAVACMD="$JAVA_HOME/jre/sh/java"
91 | else
92 | JAVACMD="$JAVA_HOME/bin/java"
93 | fi
94 | if [ ! -x "$JAVACMD" ] ; then
95 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
96 |
97 | Please set the JAVA_HOME variable in your environment to match the
98 | location of your Java installation."
99 | fi
100 | else
101 | JAVACMD="java"
102 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
103 |
104 | Please set the JAVA_HOME variable in your environment to match the
105 | location of your Java installation."
106 | fi
107 |
108 | # Increase the maximum file descriptors if we can.
109 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
110 | MAX_FD_LIMIT=`ulimit -H -n`
111 | if [ $? -eq 0 ] ; then
112 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
113 | MAX_FD="$MAX_FD_LIMIT"
114 | fi
115 | ulimit -n $MAX_FD
116 | if [ $? -ne 0 ] ; then
117 | warn "Could not set maximum file descriptor limit: $MAX_FD"
118 | fi
119 | else
120 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
121 | fi
122 | fi
123 |
124 | # For Darwin, add options to specify how the application appears in the dock
125 | if $darwin; then
126 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
127 | fi
128 |
129 | # For Cygwin or MSYS, switch paths to Windows format before running java
130 | if [ "$cygwin" = "true" -o "$msys" = "true" ] ; then
131 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
132 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
133 |
134 | JAVACMD=`cygpath --unix "$JAVACMD"`
135 |
136 | # We build the pattern for arguments to be converted via cygpath
137 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
138 | SEP=""
139 | for dir in $ROOTDIRSRAW ; do
140 | ROOTDIRS="$ROOTDIRS$SEP$dir"
141 | SEP="|"
142 | done
143 | OURCYGPATTERN="(^($ROOTDIRS))"
144 | # Add a user-defined pattern to the cygpath arguments
145 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
146 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
147 | fi
148 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
149 | i=0
150 | for arg in "$@" ; do
151 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
152 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
153 |
154 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
155 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
156 | else
157 | eval `echo args$i`="\"$arg\""
158 | fi
159 | i=`expr $i + 1`
160 | done
161 | case $i in
162 | 0) set -- ;;
163 | 1) set -- "$args0" ;;
164 | 2) set -- "$args0" "$args1" ;;
165 | 3) set -- "$args0" "$args1" "$args2" ;;
166 | 4) set -- "$args0" "$args1" "$args2" "$args3" ;;
167 | 5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
168 | 6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
169 | 7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
170 | 8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
171 | 9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
172 | esac
173 | fi
174 |
175 | # Escape application args
176 | save () {
177 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
178 | echo " "
179 | }
180 | APP_ARGS=`save "$@"`
181 |
182 | # Collect all arguments for the java command, following the shell quoting and substitution rules
183 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
184 |
185 | exec "$JAVACMD" "$@"
186 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @rem
2 | @rem Copyright 2015 the original author or authors.
3 | @rem
4 | @rem Licensed under the Apache License, Version 2.0 (the "License");
5 | @rem you may not use this file except in compliance with the License.
6 | @rem You may obtain a copy of the License at
7 | @rem
8 | @rem https://www.apache.org/licenses/LICENSE-2.0
9 | @rem
10 | @rem Unless required by applicable law or agreed to in writing, software
11 | @rem distributed under the License is distributed on an "AS IS" BASIS,
12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | @rem See the License for the specific language governing permissions and
14 | @rem limitations under the License.
15 | @rem
16 |
17 | @if "%DEBUG%" == "" @echo off
18 | @rem ##########################################################################
19 | @rem
20 | @rem Gradle startup script for Windows
21 | @rem
22 | @rem ##########################################################################
23 |
24 | @rem Set local scope for the variables with windows NT shell
25 | if "%OS%"=="Windows_NT" setlocal
26 |
27 | set DIRNAME=%~dp0
28 | if "%DIRNAME%" == "" set DIRNAME=.
29 | set APP_BASE_NAME=%~n0
30 | set APP_HOME=%DIRNAME%
31 |
32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter.
33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
34 |
35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
37 |
38 | @rem Find java.exe
39 | if defined JAVA_HOME goto findJavaFromJavaHome
40 |
41 | set JAVA_EXE=java.exe
42 | %JAVA_EXE% -version >NUL 2>&1
43 | if "%ERRORLEVEL%" == "0" goto execute
44 |
45 | echo.
46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
47 | echo.
48 | echo Please set the JAVA_HOME variable in your environment to match the
49 | echo location of your Java installation.
50 |
51 | goto fail
52 |
53 | :findJavaFromJavaHome
54 | set JAVA_HOME=%JAVA_HOME:"=%
55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
56 |
57 | if exist "%JAVA_EXE%" goto execute
58 |
59 | echo.
60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
61 | echo.
62 | echo Please set the JAVA_HOME variable in your environment to match the
63 | echo location of your Java installation.
64 |
65 | goto fail
66 |
67 | :execute
68 | @rem Setup the command line
69 |
70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
71 |
72 |
73 | @rem Execute Gradle
74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
75 |
76 | :end
77 | @rem End local scope for the variables with windows NT shell
78 | if "%ERRORLEVEL%"=="0" goto mainEnd
79 |
80 | :fail
81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
82 | rem the _cmd.exe /c_ return code!
83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
84 | exit /b 1
85 |
86 | :mainEnd
87 | if "%OS%"=="Windows_NT" endlocal
88 |
89 | :omega
90 |
--------------------------------------------------------------------------------
/jniTest/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/jniTest/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'com.example.jnitest'
8 | compileSdk 33
9 |
10 | defaultConfig {
11 | minSdk 23
12 |
13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14 | consumerProguardFiles "consumer-rules.pro"
15 | externalNativeBuild {
16 | cmake {
17 | cppFlags ""
18 | }
19 | }
20 | }
21 |
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
26 | }
27 | }
28 | externalNativeBuild {
29 | cmake {
30 | path "src/main/cpp/CMakeLists.txt"
31 | version "3.22.1"
32 | }
33 | }
34 | compileOptions {
35 | sourceCompatibility JavaVersion.VERSION_1_8
36 | targetCompatibility JavaVersion.VERSION_1_8
37 | }
38 | kotlinOptions {
39 | jvmTarget = '1.8'
40 | }
41 | }
42 |
43 | dependencies {
44 | implementation 'androidx.core:core-ktx:1.7.0'
45 | implementation 'androidx.appcompat:appcompat:1.5.1'
46 | implementation 'com.google.android.material:material:1.5.0'
47 | implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
48 | }
--------------------------------------------------------------------------------
/jniTest/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/jniTest/consumer-rules.pro
--------------------------------------------------------------------------------
/jniTest/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/jniTest/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/jniTest/src/main/cpp/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # For more information about using CMake with Android Studio, read the
2 | # documentation: https://d.android.com/studio/projects/add-native-code.html.
3 | # For more examples on how to use CMake, see https://github.com/android/ndk-samples.
4 |
5 | # Sets the minimum CMake version required for this project.
6 | cmake_minimum_required(VERSION 3.22.1)
7 |
8 | # Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
9 | # Since this is the top level CMakeLists.txt, the project name is also accessible
10 | # with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
11 | # build script scope).
12 | project("jnitest")
13 |
14 | # Creates and names a library, sets it as either STATIC
15 | # or SHARED, and provides the relative paths to its source code.
16 | # You can define multiple libraries, and CMake builds them for you.
17 | # Gradle automatically packages shared libraries with your APK.
18 | #
19 | # In this top level CMakeLists.txt, ${CMAKE_PROJECT_NAME} is used to define
20 | # the target library name; in the sub-module's CMakeLists.txt, ${PROJECT_NAME}
21 | # is preferred for the same purpose.
22 | #
23 | # In order to load a library into your app from Java/Kotlin, you must call
24 | # System.loadLibrary() and pass the name of the library defined here;
25 | # for GameActivity/NativeActivity derived applications, the same library name must be
26 | # used in the AndroidManifest.xml file.
27 | add_library(${CMAKE_PROJECT_NAME} SHARED
28 | # List C/C++ source files with relative paths to this CMakeLists.txt.
29 | test_jni.c)
30 |
31 | # Specifies libraries CMake should link to your target library. You
32 | # can link libraries from various origins, such as libraries defined in this
33 | # build script, prebuilt third-party libraries, or Android system libraries.
34 | target_link_libraries(${CMAKE_PROJECT_NAME}
35 | # List libraries link to the target library
36 | android
37 | log)
--------------------------------------------------------------------------------
/jniTest/src/main/cpp/test_jni.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | void test_jni(JNIEnv *jniEnv, jobject obj) {
5 | __android_log_print(ANDROID_LOG_ERROR, "hello", "%s", "I am test_jni");
6 | }
7 |
8 | JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
9 | JNIEnv *env;
10 | jclass cls;
11 | jclass binderCls;
12 | if (NULL == vm) return -1;
13 | if (JNI_OK != (*vm)->GetEnv(vm, (void **) &env, JNI_VERSION_1_6)) return -1;
14 | if (NULL == (cls = (*env)->FindClass(env, "com/example/jnitest/NativeLib"))) return -1;
15 | JNINativeMethod jniNativeMethods[] = {
16 | {"testJNI", "()V", (void *) (test_jni)}
17 | };
18 | if ((*env)->RegisterNatives(env, cls, jniNativeMethods,
19 | sizeof(jniNativeMethods) / sizeof((jniNativeMethods)[0])) < 0) {
20 | return JNI_ERR;
21 | }
22 | return JNI_VERSION_1_6;
23 | }
--------------------------------------------------------------------------------
/jniTest/src/main/java/com/example/jnitest/NativeLib.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnitest
2 |
3 | class NativeLib {
4 |
5 | // 设置为调用方法后延迟加载
6 | fun initSo(){
7 | System.loadLibrary("jnitest")
8 | }
9 | external fun testJNI()
10 | }
--------------------------------------------------------------------------------
/jniTest/src/test/java/com/example/jnitest/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.example.jnitest
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
--------------------------------------------------------------------------------
/jnihook/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/jnihook/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'org.jetbrains.kotlin.android'
4 | }
5 |
6 | android {
7 | namespace 'com.pika.jnihook'
8 | compileSdk 32
9 |
10 | packagingOptions {
11 | pickFirst '**/libxdl.so'
12 | }
13 |
14 | defaultConfig {
15 | minSdk 23
16 | targetSdk 32
17 |
18 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
19 | consumerProguardFiles "consumer-rules.pro"
20 | externalNativeBuild {
21 | cmake {
22 | cppFlags ""
23 | }
24 | }
25 | externalNativeBuild {
26 | cmake {
27 | arguments '-DANDROID_STL=c++_shared'
28 | }
29 | }
30 | }
31 |
32 | buildTypes {
33 | release {
34 | minifyEnabled false
35 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
36 | }
37 | }
38 | externalNativeBuild {
39 | cmake {
40 | path "src/main/cpp/CMakeLists.txt"
41 | version "3.18.1"
42 | }
43 | }
44 | compileOptions {
45 | sourceCompatibility JavaVersion.VERSION_1_8
46 | targetCompatibility JavaVersion.VERSION_1_8
47 | }
48 | kotlinOptions {
49 | jvmTarget = '1.8'
50 | }
51 |
52 | buildFeatures {
53 | prefabPublishing true
54 | prefab true
55 | }
56 | prefab {
57 | jnihook {
58 | headers "src/main/cpp/include"
59 | }
60 | }
61 | }
62 |
63 | dependencies {
64 |
65 | implementation 'androidx.core:core-ktx:1.7.0'
66 | implementation 'io.github.hexhacking:xdl:2.1.1'
67 | }
--------------------------------------------------------------------------------
/jnihook/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TestPlanB/JNIHook/65b59a68668b11fbf1fd474f46cb1bb196185fc3/jnihook/consumer-rules.pro
--------------------------------------------------------------------------------
/jnihook/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
--------------------------------------------------------------------------------
/jnihook/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # For more information about using CMake with Android Studio, read the
2 | # documentation: https://d.android.com/studio/projects/add-native-code.html
3 |
4 | # Sets the minimum version of CMake required to build the native library.
5 |
6 | cmake_minimum_required(VERSION 3.18.1)
7 |
8 | # Declares and names the project.
9 |
10 | project("jnihook")
11 | add_library( # Sets the name of the library.
12 | jnihook
13 |
14 | # Sets the library as a shared library.
15 | SHARED
16 |
17 | # Provides a relative path to your source file(s).
18 | jni_hook.c
19 | dispatchtable_hook.c
20 | dl_symbol_search.c
21 | )
22 | find_library( # Sets the name of the path variable.
23 | log-lib
24 |
25 | # Specifies the name of the NDK library that
26 | # you want CMake to locate.
27 | log)
28 | target_link_libraries( # Specifies the target library.
29 | jnihook
30 |
31 | # Links the target library to the log library
32 | # included in the NDK.
33 | ${log-lib})
34 |
35 | find_package(xdl REQUIRED CONFIG)
36 | target_link_libraries(jnihook xdl::xdl)
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/dispatchtable_hook.c:
--------------------------------------------------------------------------------
1 | #include "include/dispatchtable_hook.h"
2 | #include "include/jni_hook.h"
3 | #include "dl_symbol_search.h"
4 | #include
5 | #include
6 | #include
7 | #include
8 |
9 | static struct MallocDispatch *dynamic;
10 | static const struct MallocDispatch *pika_dispatch_table;
11 |
12 |
13 | void init_dispatch_table() {
14 | void *handle = xdl_open("libc.so", XDL_DEFAULT);
15 | struct MallocDispatch *c_dispatcher = ((struct MallocDispatch *(*)()) find_symbol(
16 | handle, "_Z23NativeAllocatorDispatchv"))();
17 | if (c_dispatcher == NULL) {
18 | return;
19 | }
20 | pika_dispatch_table = c_dispatcher;
21 | dynamic = malloc(sizeof(struct MallocDispatch));
22 | dynamic->calloc = pika_dispatch_table->calloc;
23 | dynamic->free = pika_dispatch_table->free;
24 | dynamic->mallinfo = pika_dispatch_table->mallinfo;
25 | dynamic->malloc = pika_dispatch_table->malloc;
26 | dynamic->malloc_usable_size = pika_dispatch_table->malloc_usable_size;
27 | dynamic->memalign = pika_dispatch_table->memalign;
28 | dynamic->posix_memalign = pika_dispatch_table->posix_memalign;
29 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
30 | dynamic->pvalloc = predispatcher->pvalloc;
31 | #endif
32 | dynamic->realloc = pika_dispatch_table->realloc;
33 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
34 | dynamic->valloc = predispatcher->valloc;
35 | #endif
36 | dynamic->malloc_iterate = pika_dispatch_table->malloc_iterate;
37 | dynamic->malloc_disable = pika_dispatch_table->malloc_disable;
38 | dynamic->malloc_enable = pika_dispatch_table->malloc_enable;
39 | dynamic->mallopt = pika_dispatch_table->mallopt;
40 | dynamic->aligned_alloc = pika_dispatch_table->aligned_alloc;
41 | dynamic->malloc_info = pika_dispatch_table->malloc_info;
42 | }
43 |
44 |
45 | int dispatch_table_hook(enum dispatch_table_type type, void *hook_func, void **callee) {
46 | void *handle = xdl_open("libc.so", XDL_DEFAULT);
47 | struct libc_globals *c_global = (struct libc_globals *) find_symbol(handle,
48 | "__libc_globals");
49 | if (mprotect(c_global, PAGE_SIZE, PROT_WRITE | PROT_READ) == -1) {
50 | return 0;
51 | }
52 |
53 | switch (type) {
54 | case MALLOC: {
55 | *callee = pika_dispatch_table->malloc;
56 | dynamic->malloc = hook_func;
57 | break;
58 | }
59 | case CALLOC: {
60 | *callee = pika_dispatch_table->calloc;
61 | dynamic->calloc = hook_func;
62 | break;
63 | }
64 | case FREE: {
65 | *callee = pika_dispatch_table->free;
66 | dynamic->free = hook_func;
67 | break;
68 | }
69 | // You can add any function in here which should at the dispatch table
70 | default: {
71 | return 0;
72 | }
73 | }
74 |
75 | c_global->malloc_dispatch_table = *dynamic;
76 | atomic_store(&c_global->default_dispatch_table, dynamic);
77 |
78 | if (c_global->current_dispatch_table == NULL) {
79 | atomic_store(&c_global->current_dispatch_table,
80 | dynamic);
81 | }
82 |
83 | if (mprotect(c_global, PAGE_SIZE, PROT_READ) == -1) {
84 | return 0;
85 | }
86 | return 1;
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/dl_symbol_search.c:
--------------------------------------------------------------------------------
1 | #include
2 | #include "dl_symbol_search.h"
3 |
4 | void *find_symbol(void *handle, const char *sym_name) {
5 | void *addr = xdl_sym(handle, sym_name, NULL);
6 | if (NULL == addr) {
7 | addr = xdl_dsym(handle, sym_name, NULL);
8 | }
9 | return addr;
10 | }
11 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/dl_symbol_search.h:
--------------------------------------------------------------------------------
1 | void *find_symbol(void *handle, const char *sym_name);
2 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/include/dispatchtable_hook.h:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include
7 | #include
8 | #include
9 |
10 | enum dispatch_table_type {
11 | MALLOC,
12 | CALLOC,
13 | FREE
14 | };
15 |
16 | void init_dispatch_table();
17 |
18 | int dispatch_table_hook(enum dispatch_table_type type, void *hook_func, void **callee);
19 |
20 | #ifndef JNIHOOK_DISPATCHTABLE_HOOK_H
21 | #define JNIHOOK_DISPATCHTABLE_HOOK_H
22 |
23 | typedef void *(*MallocCalloc)(size_t, size_t);
24 |
25 | typedef void (*MallocFree)(void *);
26 |
27 | typedef struct mallinfo (*MallocMallinfo)();
28 |
29 | typedef void *(*MallocMalloc)(size_t);
30 |
31 | typedef int (*MallocMallocInfo)(int, FILE *);
32 |
33 | typedef size_t (*MallocMallocUsableSize)(const void *);
34 |
35 | typedef void *(*MallocMemalign)(size_t, size_t);
36 |
37 | typedef int (*MallocPosixMemalign)(void **, size_t, size_t);
38 |
39 | typedef void *(*MallocRealloc)(void *, size_t);
40 |
41 | typedef int (*MallocIterate)(uintptr_t, size_t, void (*)(uintptr_t, size_t, void *), void *);
42 |
43 | typedef void (*MallocMallocDisable)();
44 |
45 | typedef void (*MallocMallocEnable)();
46 |
47 | typedef int (*MallocMallopt)(int, int);
48 |
49 | typedef void *(*MallocAlignedAlloc)(size_t, size_t);
50 |
51 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
52 | typedef void* (*MallocPvalloc)(size_t);
53 | typedef void* (*MallocValloc)(size_t);
54 | #endif
55 |
56 | struct MallocDispatch {
57 | MallocCalloc calloc;
58 | MallocFree free;
59 | MallocMallinfo mallinfo;
60 | MallocMalloc malloc;
61 | MallocMallocUsableSize malloc_usable_size;
62 | MallocMemalign memalign;
63 | MallocPosixMemalign posix_memalign;
64 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
65 | MallocPvalloc pvalloc;
66 | #endif
67 | MallocRealloc realloc;
68 | #if defined(HAVE_DEPRECATED_MALLOC_FUNCS)
69 | MallocValloc valloc;
70 | #endif
71 | MallocIterate malloc_iterate;
72 | MallocMallocDisable malloc_disable;
73 | MallocMallocEnable malloc_enable;
74 | MallocMallopt mallopt;
75 | MallocAlignedAlloc aligned_alloc;
76 | MallocMallocInfo malloc_info;
77 | } __attribute__((aligned(32)));
78 |
79 | struct vdso_entry {
80 | const char *name;
81 | void *fn;
82 | };
83 | enum {
84 | VDSO_CLOCK_GETTIME = 0,
85 | VDSO_CLOCK_GETRES,
86 | VDSO_GETTIMEOFDAY,
87 | #if defined(VDSO_TIME_SYMBOL)
88 | VDSO_TIME,
89 | #endif
90 | #if defined(VDSO_RISCV_HWPROBE_SYMBOL)
91 | VDSO_RISCV_HWPROBE,
92 | #endif
93 | VDSO_END
94 | };
95 |
96 | struct libc_globals {
97 | struct vdso_entry vdso[VDSO_END];
98 | long setjmp_cookie;
99 | uintptr_t heap_pointer_tag;
100 | _Atomic (bool) decay_time_enabled;
101 | _Atomic (bool) memtag;
102 |
103 | // In order to allow a complete switch between dispatch tables without
104 | // the need for copying each function by function in the structure,
105 | // use a single atomic pointer to switch.
106 | // The current_dispatch_table pointer can only ever be set to a complete
107 | // table. Any dispatch table that is pointed to by current_dispatch_table
108 | // cannot be modified after that. If the pointer changes in the future,
109 | // the old pointer must always stay valid.
110 | // The malloc_dispatch_table is modified by malloc debug, malloc hooks,
111 | // and heaprofd. Only one of these modes can be active at any given time.
112 | _Atomic (const struct MallocDispatch *) current_dispatch_table;
113 | // This pointer is only used by the allocation limit code when both a
114 | // limit is enabled and some other hook is enabled at the same time.
115 | _Atomic (const struct MallocDispatch *) default_dispatch_table;
116 | struct MallocDispatch malloc_dispatch_table;
117 | };
118 |
119 |
120 | #endif //JNIHOOK_DISPATCHTABLE_HOOK_H
121 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/include/jni_hook.h:
--------------------------------------------------------------------------------
1 | #include
2 |
3 |
4 |
5 | typedef void (*register_native_call)(JNIEnv *, jclass, const JNINativeMethod *, jint);
6 |
7 | int hook_jni(JNIEnv *env, jobject method, void *new_entrance, void **origin_entrance);
8 |
9 | void unhook_jni(JNIEnv *env, jobject method, void *origin_entrance);
10 |
11 | void set_register_natives_call(register_native_call call);
12 |
13 |
14 |
--------------------------------------------------------------------------------
/jnihook/src/main/cpp/jni_hook.c:
--------------------------------------------------------------------------------
1 |
2 | #include
3 | #include
4 | #include
5 | #include
6 | #include "include/jni_hook.h"
7 | #include "dl_symbol_search.h"
8 |
9 | static int jni_entrance_index = -1;
10 | static void *jni_stub = NULL;
11 | struct JNINativeInterface *original_functions;
12 | static void *backup;
13 | #define PAGE_START(addr) ((addr) & (uintptr_t)PAGE_MASK)
14 | static register_native_call register_natives_call = NULL;
15 |
16 | static void **get_art_method(JNIEnv *env, jobject foo) {
17 | void **fooArtMethod;
18 | if (android_get_device_api_level() >= 30) {
19 | jclass Executable = (*env)->FindClass(env, "java/lang/reflect/Executable");
20 | jfieldID artMethodField = (*env)->GetFieldID(env, Executable, "artMethod", "J");
21 | fooArtMethod = (void **) (*env)->GetLongField(env, foo, artMethodField);
22 | } else {
23 | fooArtMethod = (void **) (*env)->FromReflectedMethod(env, foo);
24 | }
25 | return fooArtMethod;
26 | }
27 |
28 | static void init_jni_hook(JNIEnv *env, jobject foo, void *fooJNI) {
29 | void **fooArtMethod = get_art_method(env, foo);
30 | for (int i = 0; i < 50; ++i) {
31 | if (fooArtMethod[i] == fooJNI) {
32 | jni_entrance_index = i;
33 | break;
34 | }
35 | }
36 | }
37 |
38 | static jint
39 | hook_jni_RegisterNatives(JNIEnv *env, jclass c, const JNINativeMethod *methods, jint nMethods) {
40 | jint ret = ((jint (*)(JNIEnv *, jclass, const JNINativeMethod *, jint)) backup)(env, c, methods,
41 | nMethods);
42 | __android_log_print(ANDROID_LOG_ERROR, "jnihook", "hook_jni_RegisterNatives %p",
43 | (*methods).fnPtr);
44 | if (register_natives_call != NULL) {
45 | register_natives_call(env, c, methods, nMethods);
46 | }
47 | return ret;
48 | }
49 |
50 | int hook_jni(JNIEnv *env, jobject method, void *new_entrance, void **origin_entrance) {
51 | if (jni_entrance_index == -1) {
52 | return -1;
53 | }
54 | void **target_art_method = get_art_method(env, method);
55 | if (target_art_method[jni_entrance_index] == new_entrance) {
56 | return 0;
57 | }
58 | if (target_art_method[jni_entrance_index] == jni_stub ||
59 | target_art_method[jni_entrance_index] == NULL) {
60 | // 当前jni函数还未加载,可以注册RegisterNative监听
61 | return -2;
62 | }
63 | *origin_entrance = target_art_method[jni_entrance_index];
64 | target_art_method[jni_entrance_index] = new_entrance;
65 | return 1;
66 | }
67 |
68 | void unhook_jni(JNIEnv *env, jobject method, void *origin_entrance) {
69 | void **target_art_method = get_art_method(env, method);
70 | if (target_art_method[jni_entrance_index] == origin_entrance) {
71 | return;
72 | }
73 | target_art_method[jni_entrance_index] = origin_entrance;
74 | }
75 |
76 | void set_register_natives_call(register_native_call call) {
77 | register_natives_call = call;
78 | }
79 |
80 |
81 | JNIEXPORT void JNICALL
82 | Java_com_pika_jnihook_JniHook_jniPlaceHolder(JNIEnv *env, jclass clazz) {
83 | }
84 |
85 | JNIEXPORT void JNICALL
86 | Java_com_pika_jnihook_JniHook_jniHookInitByHolder(JNIEnv *env, jobject thiz,
87 | jobject native_place_holder) {
88 | init_jni_hook(env, native_place_holder,
89 | (void *) Java_com_pika_jnihook_JniHook_jniPlaceHolder);
90 | void *handle = xdl_open("libart.so", XDL_DEFAULT);
91 | jni_stub = find_symbol(handle, "art_jni_dlsym_lookup_stub");
92 | original_functions = *env;
93 | int offset = offsetof(struct JNINativeInterface, RegisterNatives);
94 | void **target = (void **) (((char *) original_functions) + offset);
95 | uintptr_t start_addr = PAGE_START((uintptr_t) (target));
96 | uintptr_t end_addr = PAGE_START((uintptr_t) target + sizeof(uintptr_t) - 1) + PAGE_SIZE;
97 | size_t size = end_addr - start_addr;
98 |
99 | if (mprotect((void *) start_addr, size, PROT_WRITE | PROT_READ) == -1) {
100 | __android_log_print(ANDROID_LOG_ERROR, "jnihook", "%s", "mprotect fail");
101 | }
102 | backup = *target;
103 | *target = hook_jni_RegisterNatives;
104 | if (mprotect((void *) start_addr, size, PROT_READ) == -1) {
105 | __android_log_print(ANDROID_LOG_ERROR, "jnihook", "%s", "mprotect fail");
106 | }
107 | }
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/jnihook/src/main/java/com/pika/jnihook/JniHook.kt:
--------------------------------------------------------------------------------
1 | package com.pika.jnihook
2 |
3 | import java.lang.reflect.Method
4 |
5 | object JniHook {
6 |
7 | fun jniHookInit() {
8 | jniPlaceHolder()
9 | val placeHolder = JniHook::class.java.getDeclaredMethod("jniPlaceHolder")
10 | jniHookInitByHolder(placeHolder)
11 | }
12 |
13 | private external fun jniHookInitByHolder(nativePlaceHolder: Method)
14 |
15 |
16 | private external fun jniPlaceHolder()
17 |
18 |
19 | }
--------------------------------------------------------------------------------
/jnihook/src/test/java/com/pika/jnihook/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.pika.jnihook
2 |
3 | import org.junit.Assert.*
4 | import org.junit.Test
5 |
6 | /**
7 | * Example local unit test, which will execute on the development machine (host).
8 | *
9 | * See [testing documentation](http://d.android.com/tools/testing).
10 | */
11 | class ExampleUnitTest {
12 | @Test
13 | fun addition_isCorrect() {
14 | assertEquals(4, 2 + 2)
15 | }
16 | }
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | pluginManagement {
2 | repositories {
3 | gradlePluginPortal()
4 | google()
5 | mavenCentral()
6 | }
7 | }
8 | dependencyResolutionManagement {
9 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
10 | repositories {
11 | google()
12 | mavenCentral()
13 | }
14 | }
15 | rootProject.name = "JniHook"
16 | include ':app'
17 | include ':jnihook'
18 | include ':jniTest'
19 |
--------------------------------------------------------------------------------