├── res └── sign.keystore ├── .gitignore ├── tests ├── add.c ├── test.c ├── sub.c ├── add.h ├── sub.h └── xmake.lua ├── CHANGELOG.md ├── xmake.lua ├── .github └── workflows │ └── main.yml ├── CONTRIBUTING.md ├── README_zh.md ├── README.md ├── src ├── lni │ └── main.cpp └── lua │ └── main.lua └── LICENSE.md /res/sign.keystore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hack0z/luject/HEAD/res/sign.keystore -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xmake cache 2 | .xmake/ 3 | build/ 4 | 5 | # MacOS Cache 6 | .DS_Store 7 | 8 | 9 | -------------------------------------------------------------------------------- /tests/add.c: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | 3 | int add(int a, int b) 4 | { 5 | return a + b; 6 | } 7 | -------------------------------------------------------------------------------- /tests/test.c: -------------------------------------------------------------------------------- 1 | #include "add.h" 2 | #include 3 | 4 | int main(int argc, char** argv) 5 | { 6 | printf("add(1, 2) = %d\n", add(1, 2)); 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog ([中文](#中文)) 2 | 3 | ## master (unreleased) 4 | 5 | ### New features 6 | 7 | ### Change 8 | 9 | ### Bugs fixed 10 | 11 |

12 | 13 | # 更新日志 14 | 15 | ## master (开发中) 16 | 17 | ### 新特性 18 | 19 | ### 改进 20 | 21 | ### Bugs修复 22 | 23 | -------------------------------------------------------------------------------- /tests/sub.c: -------------------------------------------------------------------------------- 1 | #include "sub.h" 2 | #include 3 | 4 | int sub(int a, int b) 5 | { 6 | return a + b; 7 | } 8 | #if defined(_WIN32) 9 | #include 10 | BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, PVOID lpReserved) 11 | { 12 | printf("sub: injected ok!\n"); 13 | return TRUE; 14 | } 15 | #else 16 | void __attribute__ ((constructor)) sub_init(void) 17 | { 18 | printf("sub: injected ok!\n"); 19 | } 20 | #endif 21 | -------------------------------------------------------------------------------- /tests/add.h: -------------------------------------------------------------------------------- 1 | #ifdef __cplusplus 2 | extern "C" { 3 | #endif 4 | 5 | #if defined(_WIN32) 6 | # define __export __declspec(dllexport) 7 | #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) 8 | # define __export __attribute__((visibility("default"))) 9 | #else 10 | # define __export 11 | #endif 12 | 13 | /*! calculate add(a, b) 14 | * 15 | * @param a the first argument 16 | * @param b the second argument 17 | * 18 | * @return the result 19 | */ 20 | __export int add(int a, int b); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /tests/sub.h: -------------------------------------------------------------------------------- 1 | #ifdef __cplusplus 2 | extern "C" { 3 | #endif 4 | 5 | #if defined(_WIN32) 6 | # define __export __declspec(dllexport) 7 | #elif defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)) 8 | # define __export __attribute__((visibility("default"))) 9 | #else 10 | # define __export 11 | #endif 12 | 13 | /*! calculate sub(a, b) 14 | * 15 | * @param a the first argument 16 | * @param b the second argument 17 | * 18 | * @return the result 19 | */ 20 | __export int sub(int a, int b); 21 | 22 | #ifdef __cplusplus 23 | } 24 | #endif 25 | -------------------------------------------------------------------------------- /xmake.lua: -------------------------------------------------------------------------------- 1 | set_xmakever("2.9.9") 2 | 3 | add_rules("mode.debug", "mode.release") 4 | add_requires("libxmake", "lief 0.11.5") 5 | 6 | if is_plat("windows") then 7 | if is_mode("release") then 8 | add_cxflags("-MT") 9 | elseif is_mode("debug") then 10 | add_cxflags("-MTd") 11 | end 12 | add_cxxflags("-EHsc", "-FIiso646.h") 13 | add_ldflags("-nodefaultlib:msvcrt.lib") 14 | end 15 | 16 | target("luject") 17 | add_rules("xmake.cli") 18 | add_files("src/lni/*.cpp") 19 | add_files("src/lua/*.lua", {rootdir = "src"}) 20 | set_languages("c++14") 21 | add_packages("libxmake", "lief") 22 | add_installfiles("res/*", {prefixdir = "share/luject/res"}) 23 | 24 | includes("tests") 25 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: build 2 | 3 | on: [push] 4 | 5 | jobs: 6 | build: 7 | strategy: 8 | matrix: 9 | os: [ubuntu-latest, windows-latest, macOS-latest] 10 | 11 | runs-on: ${{ matrix.os }} 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | - uses: xmake-io/github-action-setup-xmake@v1 16 | with: 17 | xmake-version: branch@master 18 | - name: test 19 | run: | 20 | xmake build -y -vD test 21 | xmake run test 22 | - name: artifact 23 | run: | 24 | xmake -y 25 | xmake install -o build/luject 26 | - uses: actions/upload-artifact@v2 27 | with: 28 | name: luject-${{matrix.os}} 29 | path: build/luject 30 | -------------------------------------------------------------------------------- /tests/xmake.lua: -------------------------------------------------------------------------------- 1 | target("sub") 2 | set_default(false) 3 | add_deps("luject") 4 | set_kind("shared") 5 | add_files("sub.c") 6 | 7 | target("add") 8 | set_default(false) 9 | add_deps("sub", {inherit = false}) 10 | set_kind("shared") 11 | add_files("add.c") 12 | after_build(function (target, opt) 13 | import("utils.progress") 14 | progress.show(opt.progress, "injecting.$(mode) %s", path.filename(target:dep("sub"):targetfile())) 15 | local luject = target:dep("luject") 16 | os.setenv("XMAKE_MODULES_DIR", path.join(luject:scriptdir(), "src")) 17 | os.setenv("XMAKE_PROGRAM_DIR", path.join(path.directory(luject:pkg("libxmake"):get("linkdirs")), "share", "xmake")) 18 | 19 | -- inject libsub to libadd 20 | local rpathdir = "" 21 | if target:is_plat("linux", "android") then 22 | rpathdir = "$ORIGIN/" 23 | elseif target:is_plat("macosx", "iphoneos") then 24 | rpathdir = "@loader_path/" 25 | end 26 | os.vrunv(luject:targetfile(), {"-i", target:targetfile(), "-o", target:targetfile(), rpathdir .. path.filename(target:dep("sub"):targetfile())}) 27 | end) 28 | 29 | target("test") 30 | set_default(false) 31 | set_kind("binary") 32 | add_deps("add") 33 | add_deps("sub", {inherit = false}) -- we only add libadd/rpath to test 34 | add_files("test.c") 35 | 36 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | If you discover issues, have ideas for improvements or new features, or 4 | want to contribute a new module, please report them to the 5 | [issue tracker][1] of the repository or submit a pull request. Please, 6 | try to follow these guidelines when you do so. 7 | 8 | ## Issue reporting 9 | 10 | * Check that the issue has not already been reported. 11 | * Check that the issue has not already been fixed in the latest code 12 | (a.k.a. `master`). 13 | * Be clear, concise and precise in your description of the problem. 14 | * Open an issue with a descriptive title and a summary in grammatically correct, 15 | complete sentences. 16 | * Include any relevant code to the issue summary. 17 | 18 | ## Pull requests 19 | 20 | * Use a topic branch to easily amend a pull request later, if necessary. 21 | * Write good commit messages. 22 | * Use the same coding conventions as the rest of the project. 23 | * Ensure your edited codes with four spaces instead of TAB. 24 | * Please commit code to `dev` branch and we will merge into `master` branch in future. 25 | 26 | # 贡献代码 27 | 28 | 如果你发现一些问题,或者想新增或者改进某些新特性,或者想贡献一个新的模块 29 | 那么你可以在[issues][1]上提交反馈,或者发起一个提交代码的请求(pull request). 30 | 31 | ## 问题反馈 32 | 33 | * 确认这个问题没有被反馈过 34 | * 确认这个问题最近还没有被修复,请先检查下 `master` 的最新提交 35 | * 请清晰详细地描述你的问题 36 | * 如果发现某些代码存在问题,请在issue上引用相关代码 37 | 38 | ## 提交代码 39 | 40 | * 请先更新你的本地分支到最新,再进行提交代码请求,确保没有合并冲突 41 | * 编写友好可读的提交信息 42 | * 请使用与工程代码相同的代码规范 43 | * 确保提交的代码缩进是四个空格,而不是tab 44 | * 请提交代码到`dev`分支,如果通过,我们会在特定时间合并到`master`分支上 45 | * 为了规范化提交日志的格式,commit消息,不要用中文,请用英文描述 46 | 47 | [1]: https://github.com/lanoox/luject/issues 48 | 49 | -------------------------------------------------------------------------------- /README_zh.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

luject

4 | 5 |
6 | 7 | github-ci 8 | 9 | 10 | Github All Releases 11 | 12 | 13 | license 14 | 15 |
16 |
17 | 18 | Reddit 19 | 20 | 21 | Gitter 22 | 23 | 24 | Telegram 25 | 26 | 27 | QQ 28 | 29 | 30 | Donate 31 | 32 |
33 | 34 |

A static injector of dynamic library for application

35 |
36 | 37 | ## 简介 38 | 39 | luject是一个可以将动态库静态注入到指定应用程序包的工具,目前支持以下应用程序的注入: 40 | 41 | * Android APK 42 | * iPhoneOS IPA 43 | * Windows可执行程序 (还不支持) 44 | * MacOS可执行程序 45 | * Linux可执行程序 46 | 47 | 如果你想要了解更多,请参考: 48 | 49 | * [在线文档](https://xmake.io/#/zh-cn/getting_started) 50 | * [项目主页](https://xmake.io/#/zh-cn/) 51 | * [Github](https://github.com/lanoox/luject) 52 | * [Gitee](https://gitee.com/lanoox/luject) 53 | 54 | ## 准备工作 55 | 56 | 我们需要先安装[xmake](https://github.com/xmake-io/xmake)来编译此项目。 57 | 58 | ## 编译 59 | 60 | ```console 61 | $ xmake 62 | ``` 63 | 64 | ## 安装 65 | 66 | ```console 67 | $ xmake install 68 | ``` 69 | 70 | ## 使用 71 | 72 | ```console 73 | $ luject -i app.apk lib1.so lib2.so 74 | $ luject -i app.ipa lib1.dylib lib2.dylib 75 | $ luject -i liba.so lib1.so lib2.so 76 | $ luject -i app.exe lib1.dll lib2.dll 77 | $ luject -i a.dll lib1.dll lib2.dll 78 | $ luject -i liba.dylib lib1.dylib lib2.dyib 79 | $ luject -i bin lib1.so lib2.so 80 | ``` 81 | 82 | ## 示例 83 | 84 | ### 注入libfrida-gadget.so到APK 85 | 86 | 使用frida系列工具对app进行动态分析,相关详情见:[frida](https://github.com/frida/frida) 87 | 88 | ```console 89 | $ luject -i app.apk -p libtest /tmp/libfrida-gadget.so 90 | ``` 91 | 92 | 其中,libtest是指定apk中需要匹配注入的so库,并且支持模式匹配实现批量注入,例如:libtest_*.so,如果不指定`-p`参数,默认多所有so进行批量全注入。 93 | 94 | 参考文档: [How to use frida on a non-rooted device](https://lief.quarkslab.com/doc/latest/tutorials/09_frida_lief.html) 95 | 96 | ## 开发 97 | 98 | ### 编译运行 99 | 100 | ```console 101 | $ xmake 102 | $ xmake run luject -i [input] liba.so libb.so 103 | ``` 104 | 105 | ### 执行测试 106 | 107 | ```console 108 | $ xmake build test 109 | $ xmake run test 110 | ``` 111 | 112 | ## 联系方式 113 | 114 | * 邮箱:[waruqi@gmail.com](mailto:waruqi@gmail.com) 115 | * 主页:[tboox.org](https://tboox.org/cn) 116 | * 社区:[Reddit论坛](https://www.reddit.com/r/tboox/) 117 | * 聊天:[Telegram群组](https://t.me/tbooxorg), [Gitter聊天室](https://gitter.im/tboox/tboox?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 118 | * QQ群:343118190(满), 662147501 119 | * 微信公众号:tboox-os 120 | 121 | ## 支持项目 122 | 123 | luject项目属于个人开源项目,它的发展需要您的帮助,如果您愿意支持xmake-gradle项目的开发,欢迎为其捐赠,支持它的发展。 🙏 [[支持此项目](https://opencollective.com/xmake#backer)] 124 | 125 | 126 | 127 | ## 赞助项目 128 | 129 | 通过赞助支持此项目,您的logo和网站链接将显示在这里。[[赞助此项目](https://opencollective.com/xmake#sponsor)] 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
2 |

luject

3 | 4 |
5 | 6 | github-ci 7 | 8 | 9 | Github All Releases 10 | 11 | 12 | license 13 | 14 |
15 |
16 | 17 | Reddit 18 | 19 | 20 | Gitter 21 | 22 | 23 | Telegram 24 | 25 | 26 | QQ 27 | 28 | 29 | Donate 30 | 31 |
32 | 33 |

A static injector of dynamic library for application

34 |
35 | 36 | ## Introduction ([中文](/README_zh.md)) 37 | 38 | luject is a static injector of dynamic library for application. It support the following applications: 39 | 40 | * Android APK 41 | * iPhoneOS IPA 42 | * Windows Program (does not supported yet) 43 | * Linux Program 44 | * MacOS Program 45 | 46 | If you want to know more, please refer to: 47 | 48 | * [Documents](https://xmake.io/#/home) 49 | * [HomePage](https://xmake.io) 50 | * [Github](https://github.com/lanoox/luject) 51 | * [Gitee](https://gitee.com/lanoox/luject) 52 | 53 | ## Prerequisites 54 | 55 | XMake installed on the system. Available [here](https://github.com/xmake-io/xmake). 56 | 57 | ## Build 58 | 59 | ```console 60 | $ xmake 61 | ``` 62 | 63 | ## Installation 64 | 65 | ```console 66 | $ xmake install 67 | ``` 68 | 69 | ## Usage 70 | 71 | ```console 72 | $ luject -i app.apk lib1.so lib2.so 73 | $ luject -i app.ipa lib1.dylib lib2.dylib 74 | $ luject -i liba.so lib1.so lib2.so 75 | $ luject -i app.exe lib1.dll lib2.dll 76 | $ luject -i a.dll lib1.dll lib2.dll 77 | $ luject -i liba.dylib lib1.dylib lib2.dyib 78 | $ luject -i bin lib1.so lib2.so 79 | ``` 80 | 81 | ## Example 82 | 83 | ### Inject libfrida-gadget.so to apk 84 | 85 | Use frida tools to dynamically analyze the application. For details, see: [frida](https://github.com/frida/frida) 86 | 87 | ```console 88 | $ luject -i app.apk -p libtest /tmp/libfrida-gadget.so 89 | ``` 90 | 91 | libtest is a so library that requires matching injection in the apk, and supports pattern matching to achieve batch injection, for example: `libtest_*.so`, 92 | if you do not specify the `-p` parameter, all so are defaulted for batch full injection. 93 | 94 | refs: [How to use frida on a non-rooted device](https://lief.quarkslab.com/doc/latest/tutorials/09_frida_lief.html) 95 | 96 | ## Development 97 | 98 | ### Build and run 99 | 100 | ```console 101 | $ xmake 102 | $ xmake run luject -i [input] liba.so libb.so 103 | ``` 104 | 105 | ### Build and run tests 106 | 107 | ```console 108 | $ xmake build test 109 | $ xmake run test 110 | ``` 111 | 112 | ## Contacts 113 | 114 | * Email:[waruqi@gmail.com](mailto:waruqi@gmail.com) 115 | * Homepage:[tboox.org](https://tboox.org) 116 | * Community:[/r/tboox on reddit](https://www.reddit.com/r/tboox/) 117 | * ChatRoom:[Char on telegram](https://t.me/tbooxorg), [Chat on gitter](https://gitter.im/tboox/tboox?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) 118 | * QQ Group: 343118190(full), 662147501 119 | * Wechat Public: tboox-os 120 | 121 | ## Backers 122 | 123 | Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/xmake#backer)] 124 | 125 | 126 | 127 | ## Sponsors 128 | 129 | Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/xmake#sponsor)] 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /src/lni/main.cpp: -------------------------------------------------------------------------------- 1 | /*!A static injector of dynamic library for application 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | * 15 | * Copyright (C) 2020-present, TBOOX Open Source Group. 16 | * 17 | * @author ruki 18 | * @file main.cpp 19 | * 20 | */ 21 | /* ////////////////////////////////////////////////////////////////////////////////////// 22 | * includes 23 | */ 24 | extern "C"{ 25 | #include 26 | } 27 | #include 28 | 29 | /* ////////////////////////////////////////////////////////////////////////////////////// 30 | * private implemention 31 | */ 32 | 33 | static tb_byte_t const g_luafiles_data[] = { 34 | #include "luafiles.xmz.h" 35 | }; 36 | 37 | static tb_int_t lni_test_hello(lua_State* lua) { 38 | lua_pushliteral(lua, "hello xmake!"); 39 | return 1; 40 | } 41 | 42 | // pe.add_libraries("x.dll", {"a.dll", "b.dll"}) 43 | static tb_int_t lni_pe_add_libraries(lua_State* lua) 44 | { 45 | try 46 | { 47 | // get arguments 48 | tb_char_t const* inputfile = luaL_checkstring(lua, 1); 49 | tb_char_t const* outputfile = luaL_checkstring(lua, 2); 50 | if (!inputfile || !outputfile || !lua_istable(lua, 3)) throw "invalid arguments!"; 51 | 52 | // add libraries 53 | tb_size_t n = lua_objlen(lua, 3); 54 | if (n > 0) 55 | { 56 | auto pe_binary = std::unique_ptr{LIEF::PE::Parser::parse(inputfile)}; 57 | for (tb_size_t i = 1; i <= n; i++) 58 | { 59 | lua_pushnumber(lua, (tb_int_t)i); 60 | lua_rawget(lua, 3); 61 | tb_char_t const* library = luaL_checkstring(lua, -1); 62 | if (library) pe_binary->add_library(library); 63 | lua_pop(lua, 1); 64 | } 65 | #if 1 66 | // TODO it will crash 67 | LIEF::PE::Builder builder{pe_binary.get()}; 68 | builder.build_imports(true).patch_imports(true).build_tls(false).build_resources(false);//.build_relocations(true); 69 | builder.build(); 70 | builder.write(outputfile); 71 | #endif 72 | } 73 | } 74 | catch (std::exception const& e) 75 | { 76 | lua_pushboolean(lua, tb_false); 77 | lua_pushstring(lua, e.what()); 78 | return 2; 79 | } 80 | lua_pushboolean(lua, tb_true); 81 | return 1; 82 | } 83 | 84 | // elf.add_libraries("libx.so", {"liba.so", "libb.so"}) 85 | static tb_int_t lni_elf_add_libraries(lua_State* lua) 86 | { 87 | try 88 | { 89 | // get arguments 90 | tb_char_t const* inputfile = luaL_checkstring(lua, 1); 91 | tb_char_t const* outputfile = luaL_checkstring(lua, 2); 92 | if (!inputfile || !outputfile || !lua_istable(lua, 3)) throw "invalid arguments!"; 93 | 94 | // add libraries 95 | tb_size_t n = lua_objlen(lua, 3); 96 | if (n > 0) 97 | { 98 | auto elf_binary = std::unique_ptr{LIEF::ELF::Parser::parse(inputfile)}; 99 | for (tb_size_t i = 1; i <= n; i++) 100 | { 101 | lua_pushnumber(lua, (tb_int_t)i); 102 | lua_rawget(lua, 3); 103 | tb_char_t const* library = luaL_checkstring(lua, -1); 104 | if (library) elf_binary->add_library(library); 105 | lua_pop(lua, 1); 106 | } 107 | elf_binary->write(outputfile); 108 | } 109 | } 110 | catch (std::exception const& e) 111 | { 112 | lua_pushboolean(lua, tb_false); 113 | lua_pushstring(lua, e.what()); 114 | return 2; 115 | } 116 | lua_pushboolean(lua, tb_true); 117 | return 1; 118 | } 119 | 120 | // elf.detect_arch("libx.so") 121 | static tb_int_t lni_elf_detect_arch(lua_State* lua) 122 | { 123 | try 124 | { 125 | // get arguments 126 | tb_char_t const* inputfile = luaL_checkstring(lua, 1); 127 | if (!inputfile) throw "invalid arguments!"; 128 | 129 | // get arch 130 | auto elf_binary = std::unique_ptr{LIEF::ELF::Parser::parse(inputfile)}; 131 | switch (elf_binary->header().machine_type()) 132 | { 133 | case LIEF::ELF::ARCH::EM_AARCH64: 134 | lua_pushliteral(lua, "arm64-v8a"); 135 | break; 136 | case LIEF::ELF::ARCH::EM_ARM: 137 | lua_pushliteral(lua, "armeabi-v7a"); 138 | break; 139 | case LIEF::ELF::ARCH::EM_X86_64: 140 | lua_pushliteral(lua, "x86_64"); 141 | break; 142 | case LIEF::ELF::ARCH::EM_386: 143 | lua_pushliteral(lua, "x86"); 144 | break; 145 | default: 146 | lua_pushliteral(lua, "armeabi"); 147 | break; 148 | } 149 | } 150 | catch (std::exception const& e) 151 | { 152 | lua_pushnil(lua); 153 | lua_pushstring(lua, e.what()); 154 | return 2; 155 | } 156 | return 1; 157 | } 158 | 159 | // macho.add_libraries("libx.so", {"liba.dylib", "libb.dylib"}) 160 | static tb_int_t lni_macho_add_libraries(lua_State* lua) 161 | { 162 | try 163 | { 164 | // get arguments 165 | tb_char_t const* inputfile = luaL_checkstring(lua, 1); 166 | tb_char_t const* outputfile = luaL_checkstring(lua, 2); 167 | if (!inputfile || !outputfile || !lua_istable(lua, 3)) throw "invalid arguments!"; 168 | 169 | // add libraries 170 | tb_size_t n = lua_objlen(lua, 3); 171 | if (n > 0) 172 | { 173 | auto macho_binary = std::unique_ptr{LIEF::MachO::Parser::parse(inputfile)}; 174 | for (auto it = macho_binary->begin(); it != macho_binary->end(); ++it) 175 | { 176 | for (tb_size_t i = 1; i <= n; i++) 177 | { 178 | lua_pushnumber(lua, (tb_int_t)i); 179 | lua_rawget(lua, 3); 180 | tb_char_t const* library = luaL_checkstring(lua, -1); 181 | if (library) it->add_library(library); 182 | lua_pop(lua, 1); 183 | } 184 | } 185 | macho_binary->write(outputfile); 186 | } 187 | } 188 | catch (std::exception const& e) 189 | { 190 | lua_pushboolean(lua, tb_false); 191 | lua_pushstring(lua, e.what()); 192 | return 2; 193 | } 194 | lua_pushboolean(lua, tb_true); 195 | return 1; 196 | } 197 | 198 | // lni initalizer 199 | static tb_void_t lni_initalizer(xm_engine_ref_t engine, lua_State* lua) 200 | { 201 | // register pe module 202 | static luaL_Reg const lni_pe_funcs[] = 203 | { 204 | {"add_libraries", lni_pe_add_libraries} 205 | , {tb_null, tb_null} 206 | }; 207 | xm_engine_register(engine, "pe", lni_pe_funcs); 208 | 209 | // register elf module 210 | static luaL_Reg const lni_elf_funcs[] = 211 | { 212 | {"add_libraries", lni_elf_add_libraries} 213 | , {"detect_arch", lni_elf_detect_arch} 214 | , {tb_null, tb_null} 215 | }; 216 | xm_engine_register(engine, "elf", lni_elf_funcs); 217 | 218 | // register macho module 219 | static luaL_Reg const lni_macho_funcs[] = 220 | { 221 | {"add_libraries", lni_macho_add_libraries} 222 | , {tb_null, tb_null} 223 | }; 224 | xm_engine_register(engine, "macho", lni_macho_funcs); 225 | xm_engine_add_embedfiles(engine, g_luafiles_data, sizeof(g_luafiles_data)); 226 | } 227 | 228 | /* ////////////////////////////////////////////////////////////////////////////////////// 229 | * implemention 230 | */ 231 | tb_int_t main(tb_int_t argc, tb_char_t** argv) 232 | { 233 | tb_char_t* taskargv[] = {"lua", "-D", "lua.main", tb_null}; 234 | return xm_engine_run("luject", argc, argv, taskargv, lni_initalizer); 235 | } 236 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright 2015-2020 TBOOX Open Source Group 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | 204 | -------------------------------------------------------------------------------- /src/lua/main.lua: -------------------------------------------------------------------------------- 1 | --!A static injector of dynamic library for application 2 | -- 3 | -- Licensed under the Apache License, Version 2.0 (the "License"); 4 | -- you may not use this file except in compliance with the License. 5 | -- You may obtain a copy of the License at 6 | -- 7 | -- http://www.apache.org/licenses/LICENSE-2.0 8 | -- 9 | -- Unless required by applicable law or agreed to in writing, software 10 | -- distributed under the License is distributed on an "AS IS" BASIS, 11 | -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | -- See the License for the specific language governing permissions and 13 | -- limitations under the License. 14 | -- 15 | -- Copyright (C) 2020-present, TBOOX Open Source Group. 16 | -- 17 | -- @author ruki 18 | -- @file main.lua 19 | -- 20 | import("core.base.option") 21 | import("core.project.config") 22 | import("utils.archive") 23 | import("utils.ipa.resign", {alias = "ipa_resign"}) 24 | import("private.tools.codesign") 25 | import("detect.sdks.find_xcode") 26 | import("lib.detect.find_tool") 27 | import("lib.detect.find_program") 28 | import("lib.detect.find_directory") 29 | import("lib.lni.pe") 30 | import("lib.lni.elf") 31 | import("lib.lni.macho") 32 | 33 | -- the options 34 | local options = 35 | { 36 | {'i', "input", "kv", nil, "Set the input program path."} 37 | , {'o', "output", "kv", nil, "Set the output program path."} 38 | , {'p', "pattern", "kv", nil, "Inject to the libraries given pattern (only for apk).", 39 | " e.g. ", 40 | " - luject -i app.apk -p libtest liba.so", 41 | " - luject -i app.apk -p 'libtest_*' liba.so"} 42 | 43 | , {nil, "bundle_identifier", "kv", nil, "Set the bundle identifier of app/ipa"} 44 | , {nil, "codesign_identity", "kv", nil, "Set the codesign identity for app/ipa"} 45 | , {nil, "mobile_provision", "kv", nil, "Set the mobile provision for app/ipa"} 46 | , {'v', "verbose", "k", nil, "Enable verbose output."} 47 | , {nil, "libraries", "vs", nil, "Set all injected dynamic libraries path list."} 48 | } 49 | 50 | -- get resources directory 51 | function _get_resources_dir() 52 | local resourcesdir = path.join(os.scriptdir(), "..", "..", "res") 53 | if not os.isdir(resourcesdir) then 54 | resourcesdir = path.join(os.programdir(), "res") 55 | end 56 | assert(resourcesdir, "the resources directory not found!") 57 | return resourcesdir 58 | end 59 | 60 | -- get jarsigner 61 | function _get_jarsigner() 62 | local java_home = assert(os.getenv("JAVA_HOME"), "$JAVA_HOME not found!") 63 | local jarsigner = path.join(java_home, "bin", "jarsigner" .. (is_host("windows") and ".exe" or "")) 64 | assert(os.isfile(jarsigner), "%s not found!", jarsigner) 65 | return jarsigner 66 | end 67 | 68 | -- get zipalign 69 | function _get_zipalign(apkfile) 70 | return find_program("zipalign", {check = function (program) assert(os.execv(program, {"-c", "4", apkfile}, {try = true}) ~= nil) end}) 71 | end 72 | 73 | -- do inject for elf program 74 | function _inject_elf(inputfile, outputfile, libraries) 75 | elf.add_libraries(inputfile, outputfile, libraries) 76 | end 77 | 78 | -- do inject for macho program 79 | function _inject_macho(inputfile, outputfile, libraries) 80 | macho.add_libraries(inputfile, outputfile, libraries) 81 | end 82 | 83 | -- do inject for pe program 84 | function _inject_pe(inputfile, outputfile, libraries) 85 | pe.add_libraries(inputfile, outputfile, libraries) 86 | end 87 | 88 | -- resign apk 89 | function _resign_apk(inputfile, outputfile) 90 | 91 | -- trace 92 | cprint("${magenta}resign %s", path.filename(inputfile)) 93 | 94 | -- do resign 95 | local jarsigner = _get_jarsigner() 96 | local alias = "test" 97 | local storepass = "1234567890" 98 | local argv = {"-keystore", path.join(_get_resources_dir(), "sign.keystore"), "-signedjar", outputfile, "-digestalg", "SHA1", "-sigalg", "MD5withRSA", inputfile, alias, "--storepass", storepass} 99 | if option.get("verbose") then 100 | table.insert(argv, "-verbose") 101 | end 102 | os.vrunv(jarsigner, argv) 103 | end 104 | 105 | -- resign app 106 | function _resign_app(appdir) 107 | 108 | -- trace 109 | cprint("${magenta}resign %s", path.filename(appdir)) 110 | 111 | -- find xcode for codesign_allocate 112 | local xcode = find_xcode(nil, {verbose = option.get("verbose")}) 113 | if xcode then 114 | config.set("xcode", xcode.sdkdir, {force = true, readonly = true}) 115 | end 116 | 117 | -- do resign 118 | ipa_resign(appdir, option.get("codesign_identity"), option.get("mobile_provision"), option.get("bundle_identifier")) 119 | end 120 | 121 | -- optimize apk 122 | function _optimize_apk(apkfile) 123 | local zipalign = _get_zipalign(apkfile) 124 | if zipalign then 125 | 126 | -- trace 127 | cprint("${magenta}optimize %s", path.filename(apkfile)) 128 | 129 | -- do optimize 130 | local tmpfile = os.tmpfile() 131 | os.vrunv(zipalign, {"-f", "-v", "4", apkfile, tmpfile}) 132 | os.cp(tmpfile, apkfile) 133 | os.tryrm(tmpfile) 134 | end 135 | end 136 | 137 | -- do inject for apk program 138 | function _inject_apk(inputfile, outputfile, libraries) 139 | 140 | -- get zip 141 | local zip = assert(find_tool("zip"), "zip not found!") 142 | 143 | -- get the tmp directory 144 | local tmpdir = path.join(os.tmpdir(inputfile), path.basename(inputfile) .. ".tmp") 145 | local tmpapk = path.join(os.tmpdir(inputfile), path.basename(inputfile) .. ".apk") 146 | os.tryrm(tmpdir) 147 | os.tryrm(tmpapk) 148 | 149 | -- trace 150 | print("extract %s", path.filename(inputfile)) 151 | vprint(" -> %s", tmpdir) 152 | 153 | -- extract apk 154 | if not archive.extract(inputfile, tmpdir, {extension = ".zip"}) then 155 | raise("extract failed!") 156 | end 157 | 158 | -- detect architecture 159 | local arch 160 | for _, library in ipairs(libraries) do 161 | arch = os.isfile(library) and elf.detect_arch(library) 162 | if arch then 163 | break 164 | end 165 | end 166 | arch = arch or "armeabi-v7a" 167 | print("%s found!", arch) 168 | 169 | -- remove META-INF 170 | os.tryrm(path.join(tmpdir, "META-INF")) 171 | 172 | local libdir = path.join(tmpdir, "lib", arch) 173 | if not os.isdir(libdir) then 174 | arch = "armeabi" 175 | libdir = path.join(tmpdir, "lib", arch) 176 | end 177 | assert(os.isdir(libdir), "%s not found!", libdir) 178 | 179 | -- inject libraries to 'lib/arch/*.so' 180 | local libnames = {} 181 | for _, library in ipairs(libraries) do 182 | table.insert(libnames, path.filename(library)) 183 | end 184 | for _, libfile in ipairs(os.files(path.join(libdir, (option.get("pattern") or "*") .. ".so"))) do 185 | print("inject to %s/%s", path.filename(path.directory(libfile)), path.filename(libfile)) 186 | elf.add_libraries(libfile, libfile, libnames) 187 | end 188 | 189 | -- copy libraries to 'lib/arch/' 190 | for _, library in ipairs(libraries) do 191 | assert(os.isfile(library), "%s not found!", library) 192 | print("install %s", path.filename(library)) 193 | os.cp(library, libdir) 194 | end 195 | 196 | -- archive apk 197 | local zip_argv = {"-r"} 198 | table.insert(zip_argv, tmpapk) 199 | table.insert(zip_argv, ".") 200 | os.cd(tmpdir) 201 | os.vrunv(zip.program, zip_argv) 202 | os.cd("-") 203 | 204 | -- resign apk 205 | _resign_apk(tmpapk, outputfile) 206 | 207 | -- optimize apk 208 | _optimize_apk(outputfile) 209 | end 210 | 211 | -- do inject for app program 212 | function _inject_app(inputfile, outputfile, libraries) 213 | 214 | -- check 215 | assert(is_host("macosx"), "inject ipa only support for macOS!") 216 | 217 | -- get .app directory 218 | os.tryrm(outputfile) 219 | os.cp(inputfile, outputfile) 220 | local appdir = outputfile 221 | 222 | -- @note remove code signature first 223 | codesign.unsign(appdir) 224 | 225 | -- get binary 226 | local binaryfile 227 | for _, filepath in ipairs(os.files(path.join(appdir, "**"))) do 228 | local results = try { function () return os.iorunv("file", {filepath}) end} 229 | if results and results:find("Mach-O", 1, true) then 230 | binaryfile = filepath 231 | break 232 | end 233 | end 234 | assert(binaryfile, "image file not found!") 235 | 236 | -- inject libraries to the image file 237 | local libnames = {} 238 | for _, library in ipairs(libraries) do 239 | table.insert(libnames, "@loader_path/" .. path.filename(library)) 240 | end 241 | print("inject to %s", path.filename(binaryfile)) 242 | macho.add_libraries(binaryfile, binaryfile, libnames) 243 | 244 | -- resign app 245 | _resign_app(appdir) 246 | 247 | -- copy libraries to .app directory 248 | for _, library in ipairs(libraries) do 249 | assert(os.isfile(library), "%s not found!", library) 250 | print("install %s", path.filename(library)) 251 | os.cp(library, path.directory(binaryfile)) 252 | end 253 | end 254 | 255 | -- do inject for ipa program 256 | function _inject_ipa(inputfile, outputfile, libraries) 257 | 258 | -- check 259 | assert(is_host("macosx"), "inject ipa only support for macOS!") 260 | 261 | -- get zip 262 | local zip = assert(find_tool("zip"), "zip not found!") 263 | 264 | -- get the tmp directory 265 | local tmpdir = path.join(os.tmpdir(inputfile), path.basename(inputfile) .. ".tmp") 266 | local tmpipa = path.join(os.tmpdir(inputfile), path.basename(inputfile) .. ".ipa") 267 | os.tryrm(tmpdir) 268 | os.tryrm(tmpipa) 269 | 270 | -- trace 271 | print("extract %s", path.filename(inputfile)) 272 | vprint(" -> %s", tmpdir) 273 | 274 | -- extract ipa 275 | if not archive.extract(inputfile, tmpdir, {extension = ".zip"}) then 276 | raise("extract failed!") 277 | end 278 | 279 | -- get .app directory 280 | local appdir = find_directory("Payload/*.app", tmpdir) 281 | 282 | -- @note remove code signature first 283 | codesign.unsign(appdir) 284 | 285 | -- get binary 286 | local binaryfile 287 | for _, filepath in ipairs(os.files(path.join(appdir, "*"))) do 288 | local results = try { function () return os.iorunv("file", {filepath}) end} 289 | if results and results:find("Mach-O", 1, true) then 290 | binaryfile = filepath 291 | break 292 | end 293 | end 294 | assert(binaryfile, "image file not found!") 295 | 296 | -- inject libraries to the image file 297 | local libnames = {} 298 | for _, library in ipairs(libraries) do 299 | table.insert(libnames, "@loader_path/" .. path.filename(library)) 300 | end 301 | print("inject to %s", path.filename(binaryfile)) 302 | macho.add_libraries(binaryfile, binaryfile, libnames) 303 | 304 | -- copy libraries to .app directory 305 | for _, library in ipairs(libraries) do 306 | assert(os.isfile(library), "%s not found!", library) 307 | print("install %s", path.filename(library)) 308 | os.cp(library, path.directory(binaryfile)) 309 | end 310 | 311 | -- resign app 312 | _resign_app(appdir) 313 | 314 | -- archive ipa 315 | local zip_argv = {"-r"} 316 | table.insert(zip_argv, outputfile) 317 | table.insert(zip_argv, ".") 318 | os.cd(tmpdir) 319 | os.vrunv(zip.program, zip_argv) 320 | end 321 | 322 | -- do inject 323 | function _inject(inputfile, outputfile, libraries) 324 | if inputfile:endswith(".so") then 325 | _inject_elf(inputfile, outputfile, libraries) 326 | elseif inputfile:endswith(".dylib") then 327 | _inject_macho(inputfile, outputfile, libraries) 328 | elseif inputfile:endswith(".exe") then 329 | _inject_pe(inputfile, outputfile, libraries) 330 | elseif inputfile:endswith(".dll") then 331 | _inject_pe(inputfile, outputfile, libraries) 332 | elseif inputfile:endswith(".apk") then 333 | _inject_apk(inputfile, outputfile, libraries) 334 | elseif inputfile:endswith(".ipa") then 335 | _inject_ipa(inputfile, outputfile, libraries) 336 | elseif inputfile:endswith(".app") then 337 | _inject_app(inputfile, outputfile, libraries) 338 | else 339 | local result = try {function () return os.iorunv("file", {inputfile}) end} 340 | if result and result:find("ELF", 1, true) then 341 | _inject_elf(inputfile, outputfile, libraries) 342 | elseif result and result:find("Mach-O", 1, true) then 343 | _inject_macho(inputfile, outputfile, libraries) 344 | end 345 | end 346 | end 347 | 348 | -- init options 349 | function _init_options() 350 | 351 | -- parse arguments 352 | local argv = option.get("arguments") or {} 353 | option.save() 354 | local opts = option.parse(argv, options, "Statically inject dynamic library to the given program." 355 | , "" 356 | , "Usage: luject [options] libraries") 357 | 358 | -- save options 359 | for name, value in pairs(opts) do 360 | option.set(name, value) 361 | end 362 | end 363 | 364 | -- the main entry 365 | function main () 366 | 367 | -- init options 368 | _init_options() 369 | 370 | -- show help 371 | local help = option.get("help") 372 | local inputfile = option.get("input") 373 | local libraries = option.get("libraries") 374 | if not inputfile or not libraries then 375 | return help() 376 | end 377 | 378 | -- get input file 379 | assert(os.exists(inputfile), "%s not found!", inputfile) 380 | 381 | -- get output file 382 | local outputfile = option.get("output") 383 | if not outputfile then 384 | outputfile = path.join(path.directory(inputfile), path.basename(inputfile) .. "_injected" .. path.extension(inputfile)) 385 | end 386 | 387 | -- do inject 388 | _inject(inputfile, outputfile, libraries) 389 | 390 | -- trace 391 | cprint("${bright green}inject ok!") 392 | cprint("${yellow} -> ${clear bright}%s", outputfile) 393 | end 394 | --------------------------------------------------------------------------------