{"id":13480506,"url":"https://github.com/hack0z/byopen","last_synced_at":"2025-03-27T11:30:37.075Z","repository":{"id":40678858,"uuid":"276589355","full_name":"hack0z/byopen","owner":"hack0z","description":"🎉A dlopen library that bypasses mobile system limitation","archived":false,"fork":false,"pushed_at":"2021-08-16T08:38:04.000Z","size":274,"stargazers_count":320,"open_issues_count":8,"forks_count":87,"subscribers_count":15,"default_branch":"master","last_synced_at":"2024-10-26T11:31:41.536Z","etag":null,"topics":["android","c","dlopen","dlsym","ios","java","macos","ndk","strict"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hack0z.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-07-02T08:19:41.000Z","updated_at":"2024-09-25T13:43:45.000Z","dependencies_parsed_at":"2022-07-14T23:16:59.367Z","dependency_job_id":null,"html_url":"https://github.com/hack0z/byopen","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hack0z%2Fbyopen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hack0z%2Fbyopen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hack0z%2Fbyopen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hack0z%2Fbyopen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hack0z","download_url":"https://codeload.github.com/hack0z/byopen/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222239361,"owners_count":16953925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","c","dlopen","dlsym","ios","java","macos","ndk","strict"],"created_at":"2024-07-31T17:00:40.700Z","updated_at":"2024-10-30T14:30:42.112Z","avatar_url":"https://github.com/hack0z.png","language":"C","readme":"\u003cdiv align=\"center\"\u003e\n  \u003ch1\u003edyOpen\u003c/h1\u003e\n  \u003cp\u003eA dlopen library that bypasses mobile system limitation\u003c/p\u003e\n\u003c/div\u003e\n\n## 简介\n\nbyOpen是一个绕过移动端系统限制的增强版dlfunctions库。\n\n## 支持特性\n\n### Android\n\n支持App中加载和使用Android系统库接口（即使maps中还没有被加载也支持）。\n\nAndroid 7以上dlopen, System.load都是被限制调用的，虽然目前网上有[Nougat_dlfunctions](https://github.com/avs333/Nougat_dlfunctions)等库通过从maps中找so库来绕过加载限制。\n\n不过对于app中还没被加载到maps的so库，这种方式就不行了。\n\n而byOpen不仅支持fake dlopen方式从maps加载，还可以将还没加载到maps的so库绕过系统限制强行加载进来使用，实现更加通用化得dlopen。\n\n注：目前的实现方式理论上还是比较通用的，至少我这Android 10上测试ok，但还没完整详细测试过，是否使用请自行评估。\n\n#### 相关原理\n\n具体实现原理还是比较简单的，主要还是借鉴了[一种绕过Android P对非SDK接口限制的简单方法](http://weishu.me/2018/06/07/free-reflection-above-android-p/)的思想和实现方式。\n\n虽然这篇文章中主要目的是为了绕过hide api，不过它里面使用的将自己假装成系统调用的方式，一样可以用到`System.loadLibrary`上去，让系统以为是系统自身在调用`System.loadLibrary`\n\n从而绕过Android N的classloader-namespace限制，将系统/system/lib中任意so库加载到maps中，然后再通过fake dlopen的方式去dlsym。\n\n#### 增强版fake dlopen\n\n关于fake dlopen的方式实现，网上已有很多实现，比如：\n\n* [Nougat_dlfunctions](https://github.com/avs333/Nougat_dlfunctions)\n* [Enhanced_dlfunctions](https://github.com/turing-technician/Enhanced_dlfunctions)\n\nbyOpen参考了里面的实现，重新实现了一遍，并且做了一些小改进：\n\n* 不在/proc/self/maps中的系统库，也能绕过限制强行加载进来使用\n* 除了从.dynsym中检索符号，还支持从.symtab中检索符号（参考：Enhanced_dlfunctions，顺带修复了里面的一些bug）\n* 整个dlopen过程只有一次malloc分配（省去整个符号表的内存分配和copy）\n\n#### Android例子\n\nAndroid相关测试App例子在：[Android Sample](https://github.com/hack0z/byOpen/tree/master/src/android)\n\n注：目前自带的App测试例子里面的系统库我写死了，有些系统版本上有可能不存在，请先改成用户自己的库和符号名，再编译测试\n\n```java\npublic class MainActivity extends AppCompatActivity {\n    private static final String SYSTEM_LIBRARY = \"curl\";\n    private static final String SYMBOL_NAME = \"curl_version\";\n```\n\n除了Native版本dlopen接口，byOpen额外提供了java版本的[System.loadLibrary](https://github.com/hack0z/byOpen/blob/master/src/android/lib/src/main/java/dyopen/lib/SystemLoader.java)接口在java层直接绕过系统库加载。\n\n关键代码如下：\n\n```java\nstatic public boolean loadLibrary(String libraryName) {\n    Method forName = Class.class.getDeclaredMethod(\"forName\", String.class);\n    Method getDeclaredMethod = Class.class.getDeclaredMethod(\"getDeclaredMethod\", String.class, Class[].class);\n    Class\u003c?\u003e systemClass = (Class\u003c?\u003e) forName.invoke(null, \"java.lang.System\");\n    Method loadLibrary = (Method) getDeclaredMethod.invoke(systemClass, \"loadLibrary\", new Class[]{String.class});\n    loadLibrary.invoke(systemClass, libraryName);\n}\n```\n\n而native版本的[dlopen_android.c](https://github.com/hack0z/byOpen/blob/master/src/native/byopen_android.c)实现中，我将这段绕过的系统加载的方式，通过jni重新实现了一遍，然后和fake dlopen无缝结合到了一起。\n\n### iOS\n\n虽然ios可以直接使用dlopen，但是审核上会有风险，苹果有可能会对提交AppStore的app扫描相关dlopen/dlsym等调用，来判断是否存在一些敏感的私有调用。\n\n为了在通过调用一些私有接口的时候避免被苹果检测到，byOpen也通过自己实现dlopen/dlsym直接从已经加载进来的images列表里面直接查找对应symbol地址来调用。\n\n当然，为了更加安全，相关调用的库符号硬编码字符串等，用户可以自行做层变换加密，不要直接编译进app。\n\n## 接口用法\n\n相关静态库和接口在：[dlopen.h](https://github.com/hack0z/byOpen/blob/master/src/native/byopen.h)\n\n相关使用方式跟原生dlopen完全相同：\n\n```c\ntypedef by_char_t const* (*curl_version_t)();\nby_pointer_t handle = by_dlopen(\"libcurl.so\", BY_RTLD_LAZY);\nif (handle)\n{\n    by_pointer_t addr = by_dlsym(handle, \"curl_version\");\n    if (addr)\n    {\n        curl_version_t curl_version = (curl_version_t)addr;\n        by_print(\"curl_version: %s\", curl_version());\n    }\n    by_dlclose(handle);\n}\n```\n\n## 编译\n\n编译需要先安装：[xmake](https://github.com/xmake-io/xmake)\n\n### Android\n\n#### 直接编译库\n\n```console\n$ xmake f -p android --ndk=~/file/android-ndk-r20b\n$ xmake\n```\n\n#### 通过gradle编译测试Apk\n\n```console\n$ cd src/android\n$ ./gradlew app:assembleDebug\n```\n\n#### 通过xmake直接编译apk\n\n```console\n$ xmake apk_build\n```\n\n#### 通过xmake直接安装测试apk\n\n```console\n$ xmake apk_test\n```\n\n### iOS\n\n#### 直接编译库\n\n```console\n$ xmake f -p iphoneos -a [armv7|arm64]\n$ xmake\n```\n\n### MacOS\n\n我们也可以在macOS下编译测试，也是支持的：\n\n```console\n$ xmake\n$ xmake run\n```\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhack0z%2Fbyopen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhack0z%2Fbyopen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhack0z%2Fbyopen/lists"}