Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
RikkaW committed Aug 20, 2018
0 parents commit 602e58d
Show file tree
Hide file tree
Showing 39 changed files with 4,481 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/.externalNativeBuild
/build
/libs
/obj
/release
/magisk-module/system
/magisk-module/*.zip
26 changes: 26 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion rootProject.ext.targetSdkVersion
defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion

externalNativeBuild {
ndkBuild {
abiFilters 'arm64-v8a', 'armeabi-v7a'
arguments "NDK_PROJECT_PATH=jni/"
}
}
}
externalNativeBuild {
ndkBuild {
path 'jni/Android.mk'
}
}
}

task zip(type: Exec) {
workingDir '..'
commandLine 'sh', 'build.sh', project.name, 'v1'
}
11 changes: 11 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function copy_files {
# /data/misc/riru/modules/template exists -> libriru_template.so will be loaded
# Change "template" to your module name
# You can also use this folder as your config folder
NAME="location_report_enabler"
mkdir -p $TMP_DIR_MAGISK/data/misc/riru/modules/$NAME
cp $MODULE_NAME/template_override/riru_module.prop $TMP_DIR_MAGISK/data/misc/riru/modules/$NAME/module.prop

cp $MODULE_NAME/template_override/config.sh $TMP_DIR_MAGISK
cp $MODULE_NAME/template_override/module.prop $TMP_DIR_MAGISK
}
1 change: 1 addition & 0 deletions jni/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
libs/** binary
3 changes: 3 additions & 0 deletions jni/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
LOCAL_PATH := $(call my-dir)

include $(call all-makefiles-under, $(LOCAL_PATH))
6 changes: 6 additions & 0 deletions jni/Application.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
APP_ABI := arm64-v8a armeabi-v7a# x86 x86_64
APP_PLATFORM := android-23
APP_CFLAGS := -std=gnu99
APP_CPPFLAGS := -std=c++11
APP_STL := c++_static
APP_SHORT_COMMANDS := true
16 changes: 16 additions & 0 deletions jni/external/Android.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := xhook
LOCAL_SRC_FILES := xhook/xhook.c \
xhook/xh_core.c \
xhook/xh_elf.c \
xhook/xh_jni.c \
xhook/xh_log.c \
xhook/xh_util.c \
xhook/xh_version.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_CFLAGS := -Wall -Wextra -Werror -fvisibility=hidden
LOCAL_CONLYFLAGS := -std=c11
LOCAL_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
192 changes: 192 additions & 0 deletions jni/external/include/hookzz/hookzz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
/**
* Copyright 2017 jmpews
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef hookzz_h
#define hookzz_h

// clang-format off
#ifdef __cplusplus
extern "C" {
#endif //__cplusplus

#include <stdbool.h>
#include <stdint.h>

#ifndef REGISTER_STATE_STRUCT
#define REGISTER_STATE_STRUCT
#if defined(__arm64__) || defined(__aarch64__)
#define Tx(type) type##arm64
#define TX() type##ARM64
#define xT() arm64##type
#define XT() ARM64##type
typedef union _FPReg {
__int128_t q;
struct {
double d1;
double d2;
} d;
struct {
float f1;
float f2;
float f3;
float f4;
} f;
} FPReg;

typedef struct _RegState {
uint64_t dmmpy_0;

union {
uint64_t x[29];
struct {
uint64_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15, x16, x17, x18, x19, x20, x21,
x22, x23, x24, x25, x26, x27, x28;
} regs;
} general;

uint64_t fp;
uint64_t lr;

union {
FPReg q[8];
struct {
FPReg q0, q1, q2, q3, q4, q5, q6, q7;
} regs;
} floating;
} RegState;
#elif defined(__arm__)
#define Tx(type) type##arm
#define TX() type##ARM
#define xT() arm##type
#define XT() ARM##type
typedef struct _RegState {
uint32_t dummy_0;
uint32_t dummy_1;

union {
uint32_t r[13];
struct {
uint32_t r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12;
} regs;
} general;

uint32_t lr;
} RegState;
#elif defined(__i386__)
#define Tx(type) type##arm
#define TX() type##ARM
#define xT() arm##type
#define XT() ARM##type
typedef struct _RegState {
} RegState;
#elif defined(__x86_64__)
#define Tx(type) type##x64
#define TX() type##X64
#define xT() x64##type
#define XT() X64##type
typedef struct _RegState {
} RegState;
#endif

#define REG_SP(rs) (void *)((uintptr_t)rs + sizeof(RegState))
#endif

typedef enum _RetStatus {
RS_UNKOWN = -1,
RS_DONE = 0,
RS_SUCCESS,
RS_FAILED,
RS_DONE_HOOK,
RS_DONE_INIT,
RS_DONE_ENABLE,
RS_ALREADY_HOOK,
RS_ALREADY_INIT,
RS_ALREADY_ENABLED,
RS_NEED_INIT,
RS_NO_BUILD_HOOK
} RetStatus;

typedef enum _HookType {
// HOOK_TYPE_SINGLE_INSTRUCTION_DELETED = 0,
HOOK_TYPE_FUNCTION_via_PRE_POST = 0,
HOOK_TYPE_FUNCTION_via_REPLACE,
HOOK_TYPE_FUNCTION_via_GOT,
HOOK_TYPE_DBI
}HookType;

typedef struct _CallStackPublic {
unsigned long call_id;
struct _ThreadStack *ts;
} CallStackPublic;

typedef struct _ThreadStackPublic {
unsigned long thread_id;
unsigned long size;
} ThreadStackPublic;

typedef struct _HookEntryInfo {
unsigned long hook_id;
void *hook_address;
} HookEntryInfo;

typedef void (*PRECALL)(RegState *rs, ThreadStackPublic *ts, CallStackPublic *cs, const HookEntryInfo *info);
typedef void (*POSTCALL)(RegState *rs, ThreadStackPublic *ts, CallStackPublic *cs, const HookEntryInfo *info);
typedef void (*STUBCALL)(RegState *rs, const HookEntryInfo *info);

#define STACK_CHECK_KEY(cs, key) (bool)CallStackGetThreadLocalData(cs, key)
#define STACK_GET(cs, key, type) *(type *)CallStackGetThreadLocalData(cs, key)
#define STACK_SET(cs, key, value, type) CallStackSetThreadLocalData(cs, key, &(value), sizeof(type))

void *CallStackGetThreadLocalData(CallStackPublic *callstack_ptr, char *key_str);
bool CallStackSetThreadLocalData(CallStackPublic *callstack_ptr, char *key_str, void *value_ptr, unsigned long value_size);

RetStatus ZzHook(void *target_ptr, void *replace_call, void **origin_call_ptr, PRECALL pre_call_ptr, POSTCALL post_call_ptr, bool try_near_jump);
RetStatus ZzHookPrePost(void *target_ptr, PRECALL pre_call_ptr, POSTCALL post_call_ptr);
RetStatus ZzHookReplace(void *target_ptr, void *replace_call, void **origin_call_ptr);

// got hook (only support darwin)
RetStatus ZzHookGOT(void *header, const char *name, void *replace_call, void **origin_call_ptr, PRECALL pre_call_ptr, POSTCALL post_call_ptr);

// dynamic binary instrumentation
RetStatus ZzDynamicBinaryInstrumentation(void *address, STUBCALL stub_call_ptr);

// hook only one instruciton with instruction address
// void ZzHookSingleInstruction(void *insn_address, PRECALL pre_call_ptr, POSTCALL post_call_ptr, bool try_near_jump);

// runtime code patch
RetStatus ZzRuntimeCodePatch(void *address, void *code_data, unsigned long code_length);

// enable debug info
void DebugLogControlerEnableLog();

// disable hook
RetStatus ZzDisableHook(void *target_ptr);

#if defined(__arm64__) || defined(__aarch64__)
#if defined(__APPLE__) && defined(__MACH__)
#define iOS 1
#endif
#endif

#ifdef TARGET_IS_IOS
RetStatus StaticBinaryInstrumentation(void *target_fileoff, void *replace_call_ptr, void **origin_call_ptr, PRECALL pre_call_ptr,
POSTCALL post_call_ptr);
#endif

#ifdef __cplusplus
}
#endif //__cplusplus
#endif
50 changes: 50 additions & 0 deletions jni/external/include/xhook/xhook.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2018-present, iQIYI, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

// Created by caikelun on 2018-04-11.

#ifndef XHOOK_H
#define XHOOK_H 1

#ifdef __cplusplus
extern "C" {
#endif

#define XHOOK_EXPORT __attribute__((visibility("default")))

int xhook_register(const char *pathname_regex_str, const char *symbol,
void *new_func, void **old_func) XHOOK_EXPORT;

int xhook_ignore(const char *pathname_regex_str, const char *symbol) XHOOK_EXPORT;

int xhook_refresh(int async) XHOOK_EXPORT;

void xhook_clear() XHOOK_EXPORT;

void xhook_enable_debug(int flag) XHOOK_EXPORT;

void xhook_enable_sigsegv_protection(int flag) XHOOK_EXPORT;

#ifdef __cplusplus
}
#endif

#endif
Loading

0 comments on commit 602e58d

Please sign in to comment.