Skip to content

Commit

Permalink
Batch sync.
Browse files Browse the repository at this point in the history
1. Fix occasional crashes.
2. Improve the performance of the proxy function.
3. Enhanced hook capability.
4. Added hook/unhook operation recording module and data dump API.
  • Loading branch information
caikelun committed Nov 5, 2021
1 parent f3c499f commit 19f99d9
Show file tree
Hide file tree
Showing 33 changed files with 6,398 additions and 196 deletions.
39 changes: 39 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,45 @@ SUCH DAMAGE.



Source code Licensed under the BSD 3-Clause License
===================================================

linux_syscall_support.h

Copyright (c) 2005-2011, Google Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

---
Author: Markus Gutschke



Terms of the MIT License
========================

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# bhook

![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.3-red.svg?style=flat)
![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat)
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat)

[README 中文版](README.zh-CN.md)

ByteHook(aka bhook) is a PLT hook framework for Android app. It provides an overall solution for using PLT hook in Android app, not just replacing addresses.

Most of ByteDance's Android apps(including Douyin, Toutiao, Xigua Video) use ByteHook as the PLT hook solution in the online environment.
Most of ByteDance's Android apps(including Douyin, Toutiao, Xigua Video) use ByteHook as the PLT hook solution in production.


## Features
Expand Down Expand Up @@ -53,7 +53,7 @@ android {
}
dependencies {
implementation 'com.bytedance:bytehook:1.0.2'
implementation 'com.bytedance:bytehook:1.0.3'
}
```

Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# bhook

![](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.2-red.svg?style=flat)
![](https://img.shields.io/badge/release-1.0.3-red.svg?style=flat)
![](https://img.shields.io/badge/Android-4.1%20--%2012-blue.svg?style=flat)
![](https://img.shields.io/badge/arch-armeabi--v7a%20%7C%20arm64--v8a%20%7C%20x86%20%7C%20x86__64-blue.svg?style=flat)

Expand Down Expand Up @@ -53,7 +53,7 @@ android {
}
dependencies {
implementation 'com.bytedance:bytehook:1.0.2'
implementation 'com.bytedance:bytehook:1.0.3'
}
```

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ ext {
targetSdkVersion = 31
buildToolsVersion = "31.0.0"
javaVersion = JavaVersion.VERSION_1_7
ndkVersion = "23.0.7599858"
ndkVersion = "23.1.7779620"
cmakeVersion = "3.18.1"
abiFilters = "armeabi-v7a,arm64-v8a,x86,x86_64"
useASAN = false
dependencyOnLocalLibrary = true

POM_GROUP_ID = "com.bytedance"
POM_ARTIFACT_ID = "bytehook"
POM_VERSION_NAME = "1.0.2"
POM_VERSION_NAME = "1.0.3"

POM_NAME = "bytehook"
POM_DESCRIPTION = "ByteHook is a PLT hook framework for Android app."
Expand Down
80 changes: 80 additions & 0 deletions bytehook/src/main/cpp/bh_cfi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2020-present, ByteDance, Inc.
//
// 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 Li Han (hanli.lee@bytedance.com) on 2020-11-04.

#include "bh_cfi.h"

#if defined(__aarch64__)

#include <stdlib.h>
#include <unistd.h>
#include <dlfcn.h>
#include <sys/mman.h>
#include "bh_util.h"
#include "bytesig.h"

#define BH_CFI_LIB_DL "libdl.so"
#define BH_CFI_SLOWPATH "__cfi_slowpath"
#define BH_CFI_SLOWPATH_DIAG "__cfi_slowpath_diag"
#define BH_CFI_ARM64_RET_INST 0xd65f03c0

int bh_cfi_disable_slowpath(void)
{
if(bh_util_get_api_level() < __ANDROID_API_O__) return 0;

void *handle = dlopen(BH_CFI_LIB_DL, RTLD_NOW);
if(NULL == handle) return -1;

void *cfi_slowpath = dlsym(handle, BH_CFI_SLOWPATH);
if(NULL == cfi_slowpath) goto err;
void *cfi_slowpath_diag = dlsym(handle, BH_CFI_SLOWPATH_DIAG);
if(NULL == cfi_slowpath_diag) goto err;

void *start = cfi_slowpath <= cfi_slowpath_diag ? cfi_slowpath : cfi_slowpath_diag;
void *end = cfi_slowpath <= cfi_slowpath_diag ? cfi_slowpath_diag : cfi_slowpath;
if(0 != bh_util_set_protect(start, (void *)((uintptr_t)end + sizeof(uint32_t)), PROT_READ | PROT_WRITE | PROT_EXEC)) goto err;

BYTESIG_TRY(SIGSEGV, SIGBUS)
*((uint32_t *)cfi_slowpath) = BH_CFI_ARM64_RET_INST;
*((uint32_t *)cfi_slowpath_diag) = BH_CFI_ARM64_RET_INST;
BYTESIG_CATCH()
goto err;
BYTESIG_EXIT

__builtin___clear_cache(start, (void *)((size_t)end + sizeof(uint32_t)));

dlclose(handle);
return 0;

err:
dlclose(handle);
return -1;
}

#else

int bh_cfi_disable_slowpath(void)
{
return 0;
}

#endif
26 changes: 26 additions & 0 deletions bytehook/src/main/cpp/bh_cfi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2020-present, ByteDance, Inc.
//
// 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 Li Han (hanli.lee@bytedance.com) on 2020-11-04.

#pragma once

int bh_cfi_disable_slowpath(void);
25 changes: 17 additions & 8 deletions bytehook/src/main/cpp/bh_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "bh_dl_monitor.h"
#include "bh_linker.h"
#include "bytesig.h"
#include "bh_cfi.h"

static bh_core_t bh_core = {
.init_status = BYTEHOOK_STATUS_CODE_UNINIT,
Expand Down Expand Up @@ -104,6 +105,11 @@ int bh_core_init(int mode, bool debug)
status = BYTEHOOK_STATUS_CODE_INITERR_SIG;
goto end;
}
if(0 != bh_cfi_disable_slowpath())
{
status = BYTEHOOK_STATUS_CODE_INITERR_CFI;
goto end;
}
status = BYTEHOOK_STATUS_CODE_OK; // everything OK

end:
Expand All @@ -121,12 +127,13 @@ bytehook_stub_t bh_core_hook_single(
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg)
void *hooked_arg,
uintptr_t caller_addr)
{
if(NULL == caller_path_name || NULL == sym_name || NULL == new_func) return NULL;
if(BYTEHOOK_STATUS_CODE_OK != bh_core.init_status) return NULL;

bh_task_t *task = bh_task_create_single(caller_path_name, callee_path_name, sym_name, new_func, hooked, hooked_arg);
bh_task_t *task = bh_task_create_single(caller_path_name, callee_path_name, sym_name, new_func, hooked, hooked_arg, caller_addr);
if(NULL != task)
{
bh_task_manager_add(bh_core.task_mgr, task);
Expand All @@ -142,12 +149,13 @@ bytehook_stub_t bh_core_hook_partial(
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg)
void *hooked_arg,
uintptr_t caller_addr)
{
if(NULL == caller_allow_filter || NULL == sym_name || NULL == new_func) return NULL;
if(BYTEHOOK_STATUS_CODE_OK != bh_core.init_status) return NULL;

bh_task_t *task = bh_task_create_partial(caller_allow_filter, caller_allow_filter_arg, callee_path_name, sym_name, new_func, hooked, hooked_arg);
bh_task_t *task = bh_task_create_partial(caller_allow_filter, caller_allow_filter_arg, callee_path_name, sym_name, new_func, hooked, hooked_arg, caller_addr);
if(NULL != task)
{
bh_task_manager_add(bh_core.task_mgr, task);
Expand All @@ -161,12 +169,13 @@ bytehook_stub_t bh_core_hook_all(
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg)
void *hooked_arg,
uintptr_t caller_addr)
{
if(NULL == sym_name || NULL == new_func) return NULL;
if(BYTEHOOK_STATUS_CODE_OK != bh_core.init_status) return NULL;

bh_task_t *task = bh_task_create_all(callee_path_name, sym_name, new_func, hooked, hooked_arg);
bh_task_t *task = bh_task_create_all(callee_path_name, sym_name, new_func, hooked, hooked_arg, caller_addr);
if(NULL != task)
{
bh_task_manager_add(bh_core.task_mgr, task);
Expand All @@ -175,14 +184,14 @@ bytehook_stub_t bh_core_hook_all(
return (bytehook_stub_t)task;
}

int bh_core_unhook(bytehook_stub_t stub)
int bh_core_unhook(bytehook_stub_t stub, uintptr_t caller_addr)
{
if(NULL == stub) return BYTEHOOK_STATUS_CODE_INVALID_ARG;
if(BYTEHOOK_STATUS_CODE_OK != bh_core.init_status) return bh_core.init_status;

bh_task_t *task = (bh_task_t *)stub;
bh_task_manager_del(bh_core.task_mgr, task);
int status_code = bh_task_manager_unhook(bh_core.task_mgr, task);
int status_code = bh_task_manager_unhook(bh_core.task_mgr, task, caller_addr);
bh_task_destroy(&task);

return status_code;
Expand Down
11 changes: 7 additions & 4 deletions bytehook/src/main/cpp/bh_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ bytehook_stub_t bh_core_hook_single(
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
void *hooked_arg,
uintptr_t caller_addr);

bytehook_stub_t bh_core_hook_partial(
bytehook_caller_allow_filter_t caller_allow_filter,
Expand All @@ -59,16 +60,18 @@ bytehook_stub_t bh_core_hook_partial(
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
void *hooked_arg,
uintptr_t caller_addr);

bytehook_stub_t bh_core_hook_all(
const char *callee_path_name,
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
void *hooked_arg,
uintptr_t caller_addr);

int bh_core_unhook(bytehook_stub_t stub);
int bh_core_unhook(bytehook_stub_t stub, uintptr_t caller_addr);

void bh_core_set_debug(bool debug);

Expand Down
Loading

0 comments on commit 19f99d9

Please sign in to comment.