Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
M
Magisk
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Magisk
Commits
540b4b7e
Commit
540b4b7e
authored
Jan 10, 2021
by
topjohnwu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update pre/post hooks implementation
parent
bbef22da
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
205 additions
and
203 deletions
+205
-203
Android.mk
native/jni/Android.mk
+1
-2
hook.cpp
native/jni/inject/hook.cpp
+83
-2
inject.hpp
native/jni/inject/inject.hpp
+0
-25
jni_hooks.hpp
native/jni/inject/jni_hooks.hpp
+121
-174
No files found.
native/jni/Android.mk
View file @
540b4b7e
...
@@ -33,8 +33,7 @@ LOCAL_SRC_FILES := \
...
@@ -33,8 +33,7 @@ LOCAL_SRC_FILES := \
su/su_daemon.cpp \
su/su_daemon.cpp \
inject/entry.cpp \
inject/entry.cpp \
inject/utils.cpp \
inject/utils.cpp \
inject/hook.cpp \
inject/hook.cpp
inject/jni_hooks.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS := -llog
include $(BUILD_EXECUTABLE)
include $(BUILD_EXECUTABLE)
...
...
native/jni/inject/hook.cpp
View file @
540b4b7e
...
@@ -13,14 +13,48 @@ static JavaVM *g_jvm;
...
@@ -13,14 +13,48 @@ static JavaVM *g_jvm;
// For some reason static vector won't work, use a pointer instead
// For some reason static vector won't work, use a pointer instead
static
vector
<
tuple
<
const
char
*
,
const
char
*
,
void
**>>
*
hook_list
;
static
vector
<
tuple
<
const
char
*
,
const
char
*
,
void
**>>
*
hook_list
;
namespace
{
struct
HookContext
{
int
pid
;
};
}
// JNI method declarations
namespace
JNI
{
namespace
Zygote
{
const
JNINativeMethod
*
nativeForkAndSpecialize_orig
=
nullptr
;
const
JNINativeMethod
*
nativeSpecializeAppProcess_orig
=
nullptr
;
const
JNINativeMethod
*
nativeForkSystemServer_orig
=
nullptr
;
extern
const
JNINativeMethod
nativeForkAndSpecialize_methods
[];
extern
const
int
nativeForkAndSpecialize_methods_num
;
extern
const
JNINativeMethod
nativeSpecializeAppProcess_methods
[];
extern
const
int
nativeSpecializeAppProcess_methods_num
;
extern
const
JNINativeMethod
nativeForkSystemServer_methods
[];
extern
const
int
nativeForkSystemServer_methods_num
;
}
namespace
SystemProperties
{
const
JNINativeMethod
*
native_set_orig
=
nullptr
;
extern
const
JNINativeMethod
native_set_methods
[];
constexpr
int
native_set_methods_num
=
1
;
}
}
#define DEF_HOOK_FUNC(ret, func, ...) \
#define DEF_HOOK_FUNC(ret, func, ...) \
static ret (*old_##func)(__VA_ARGS__); \
static ret (*old_##func)(__VA_ARGS__); \
static ret new_##func(__VA_ARGS__)
static ret new_##func(__VA_ARGS__)
#define HOOK_JNI(clazz, method) \
#define HOOK_JNI(clazz, method) \
if (newMethods[i].name == #method##sv) { \
if (newMethods[i].name == #method##sv) { \
JNI::clazz::method##_orig = new JNINativeMethod(); \
auto orig = new JNINativeMethod(); \
memcpy(JNI::clazz::method##_orig, &newMethods[i], sizeof(JNINativeMethod)); \
memcpy(orig, &newMethods[i], sizeof(JNINativeMethod)); \
JNI::clazz::method##_orig = orig; \
for (int j = 0; j < JNI::clazz::method##_methods_num; ++j) { \
for (int j = 0; j < JNI::clazz::method##_methods_num; ++j) { \
if (strcmp(newMethods[i].signature, JNI::clazz::method##_methods[j].signature) == 0) { \
if (strcmp(newMethods[i].signature, JNI::clazz::method##_methods[j].signature) == 0) { \
newMethods[i] = JNI::clazz::method##_methods[j]; \
newMethods[i] = JNI::clazz::method##_methods[j]; \
...
@@ -65,6 +99,49 @@ DEF_HOOK_FUNC(int, jniRegisterNativeMethods,
...
@@ -65,6 +99,49 @@ DEF_HOOK_FUNC(int, jniRegisterNativeMethods,
return
old_jniRegisterNativeMethods
(
env
,
className
,
newMethods
.
get
()
?:
methods
,
numMethods
);
return
old_jniRegisterNativeMethods
(
env
,
className
,
newMethods
.
get
()
?:
methods
,
numMethods
);
}
}
static
void
nativeForkAndSpecialize_pre
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
,
jint
&
uid
,
jint
&
gid
,
jintArray
&
gids
,
jint
&
runtime_flags
,
jobjectArray
&
rlimits
,
jint
&
mount_external
,
jstring
&
se_info
,
jstring
&
nice_name
,
jintArray
&
fds_to_close
,
jintArray
&
fds_to_ignore
,
jboolean
&
is_child_zygote
,
jstring
&
instruction_set
,
jstring
&
app_data_dir
,
jboolean
&
is_top_app
,
jobjectArray
&
pkg_data_info_list
,
jobjectArray
&
whitelisted_data_info_list
,
jboolean
&
mount_data_dirs
,
jboolean
&
mount_storage_dirs
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
}
static
void
nativeForkAndSpecialize_post
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
// Demonstrate self unload in child process
if
(
ctx
->
pid
==
0
)
self_unload
();
}
// -----------------------------------------------------------------
static
void
nativeSpecializeAppProcess_pre
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
,
jint
uid
,
jint
gid
,
jintArray
gids
,
jint
runtime_flags
,
jobjectArray
rlimits
,
jint
mount_external
,
jstring
se_info
,
jstring
nice_name
,
jboolean
is_child_zygote
,
jstring
instruction_set
,
jstring
app_data_dir
,
jboolean
&
is_top_app
,
jobjectArray
&
pkg_data_info_list
,
jobjectArray
&
whitelisted_data_info_list
,
jboolean
&
mount_data_dirs
,
jboolean
&
mount_storage_dirs
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
}
static
void
nativeSpecializeAppProcess_post
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
}
// -----------------------------------------------------------------
static
void
nativeForkSystemServer_pre
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
,
uid_t
&
uid
,
gid_t
&
gid
,
jintArray
&
gids
,
jint
&
runtime_flags
,
jobjectArray
&
rlimits
,
jlong
&
permitted_capabilities
,
jlong
&
effective_capabilities
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
}
static
void
nativeForkSystemServer_post
(
HookContext
*
ctx
,
JNIEnv
*
env
,
jclass
clazz
)
{
LOGD
(
"hook: %s
\n
"
,
__FUNCTION__
);
}
static
bool
hook_refresh
()
{
static
bool
hook_refresh
()
{
if
(
xhook_refresh
(
0
)
==
0
)
{
if
(
xhook_refresh
(
0
)
==
0
)
{
xhook_clear
();
xhook_clear
();
...
@@ -107,6 +184,7 @@ bool unhook_functions() {
...
@@ -107,6 +184,7 @@ bool unhook_functions() {
if
(
g_jvm
->
GetEnv
(
reinterpret_cast
<
void
**>
(
&
env
),
JNI_VERSION_1_6
)
!=
JNI_OK
)
if
(
g_jvm
->
GetEnv
(
reinterpret_cast
<
void
**>
(
&
env
),
JNI_VERSION_1_6
)
!=
JNI_OK
)
return
false
;
return
false
;
// Unhook JNI methods
vector
<
JNINativeMethod
>
methods
;
vector
<
JNINativeMethod
>
methods
;
push_method
(
Zygote
,
nativeForkAndSpecialize
);
push_method
(
Zygote
,
nativeForkAndSpecialize
);
...
@@ -130,6 +208,7 @@ bool unhook_functions() {
...
@@ -130,6 +208,7 @@ bool unhook_functions() {
return
false
;
return
false
;
}
}
// Unhook xhook
for
(
auto
&
[
path
,
sym
,
old_func
]
:
*
hook_list
)
{
for
(
auto
&
[
path
,
sym
,
old_func
]
:
*
hook_list
)
{
if
(
xhook_register
(
path
,
sym
,
*
old_func
,
nullptr
)
!=
0
)
{
if
(
xhook_register
(
path
,
sym
,
*
old_func
,
nullptr
)
!=
0
)
{
LOGE
(
"hook: Failed to register hook
\"
%s
\"\n
"
,
sym
);
LOGE
(
"hook: Failed to register hook
\"
%s
\"\n
"
,
sym
);
...
@@ -139,3 +218,5 @@ bool unhook_functions() {
...
@@ -139,3 +218,5 @@ bool unhook_functions() {
delete
hook_list
;
delete
hook_list
;
return
hook_refresh
();
return
hook_refresh
();
}
}
#include "jni_hooks.hpp"
native/jni/inject/inject.hpp
View file @
540b4b7e
...
@@ -20,28 +20,3 @@ uintptr_t get_remote_lib(int pid, const char *lib);
...
@@ -20,28 +20,3 @@ uintptr_t get_remote_lib(int pid, const char *lib);
void
self_unload
();
void
self_unload
();
void
hook_functions
();
void
hook_functions
();
bool
unhook_functions
();
bool
unhook_functions
();
// JNI method declarations
namespace
JNI
{
namespace
Zygote
{
extern
JNINativeMethod
*
nativeForkAndSpecialize_orig
;
extern
JNINativeMethod
*
nativeSpecializeAppProcess_orig
;
extern
JNINativeMethod
*
nativeForkSystemServer_orig
;
extern
const
JNINativeMethod
nativeForkAndSpecialize_methods
[];
extern
const
int
nativeForkAndSpecialize_methods_num
;
extern
const
JNINativeMethod
nativeSpecializeAppProcess_methods
[];
extern
const
int
nativeSpecializeAppProcess_methods_num
;
extern
const
JNINativeMethod
nativeForkSystemServer_methods
[];
extern
const
int
nativeForkSystemServer_methods_num
;
}
namespace
SystemProperties
{
extern
JNINativeMethod
*
native_set_orig
;
extern
const
JNINativeMethod
native_set_methods
[];
constexpr
int
native_set_methods_num
=
1
;
}
}
native/jni/inject/jni_hooks.
c
pp
→
native/jni/inject/jni_hooks.
h
pp
View file @
540b4b7e
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment