
最近有位讀者私信,說能不能推薦一些大廠使用的安卓類開源專案,想了下,讀者中也有不少搞安卓開發的,所以今天給大家推薦一個大廠出品的安卓開源專案,
這個專案是「ByteHook」,目前在 GitHub 識訓了 670 Star,是一個針對 Android App 的 PLT hook 框架,
這個框架也是最近才開源,位元組跳動團隊大多數 Android App 開發在線上使用了這個專案作為方案,對于創業公司或者安卓開發者是一個學習參考,它提供了一個 Android 應用程式使用 PLT 鉤子的整體方案,而可選擇替換地址,
我們平常玩的抖音、今日頭條、西瓜視頻 APP ,位元組跳動在在線上環境中使用了ByteHook作為PLT鉤子方案,
這個框架主要有以下特征:
支持 Android 4.1 - 12 (API 級別 16 - 31),
支持 armeabi-v7a、arm64-v8a、x86 和 x86_64,
對同一個函式的多個hook和unhook相互不沖突,
可以勾上行程中的個別、部分或全部的動態庫,
自動鉤子新加載的動態庫,
自動避免代理函式之間的回圈呼叫和回圈呼叫,
代理功能中支持回溯呼叫堆疊,
使用 MIT 許可授權,
如何上手使用?
- 在build.gradle中增加依賴
所有專案{
存盤庫{
MavenCentral()
}
}
安卓{
構建功能{
預制真
}
}
依賴{
實作’ com.bytedance:bytehook:1.0.2 ’
}
2.在 CMakeLists.txt 中增加依賴
find_package(bytehook REQUIRED CONFIG)
add_library(mylib SHARED mylib.c)
target_link_libraries(mylib bytehook::bytehook)
-
指定一個或多個你需要的 ABI
android {
defaultConfig {
ndk {
abiFilters ‘armeabi-v7a’, ‘arm64-v8a’, ‘x86’, ‘x86_64’
}
}
} -
增加打包選項
-
初始化
import com.bytedance.android.bytehook.ByteHook;
public class MySdk {
public static synchronized void init() {
ByteHook.init();
}
}
- 鉤和解鉤
bytehook_stub_t bytehook_hook_single(
const char *caller_path_name,
const char *callee_path_name,
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
bytehook_stub_t bytehook_hook_partial(
bytehook_caller_allow_filter_t caller_allow_filter,
void *caller_allow_filter_arg,
const char *callee_path_name,
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
bytehook_stub_t bytehook_hook_all(
const char *callee_path_name,
const char *sym_name,
void *new_func,
bytehook_hooked_t hooked,
void *hooked_arg);
int bytehook_unhook(bytehook_stub_t stub);
那么這個框架的原理是什么樣的?
ELF 檔案的起始處,有一個固定格式的定長的檔案頭,ELF 檔案頭中包含了 SHT和 PHT在當前 ELF 檔案中的起始位置和長度,SHT 和 PHT 分別描述了 ELF 的“連接視圖”和“執行視圖”的基本資訊,

linker(動態聯結器)

00
relocation 完成之后的函式呼叫關系如下

ByteHook 中另外幾個關鍵模塊

最后,為了鼓勵鼓勵,不點贊來一下嗎,祝各位程式員開發無 Bug,發大財,行大運,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/319654.html
標籤:其他
