很多情况下我们希望自己写的代码能够在其他应用中运行,如果代码简单的话,可以写 Tweak 或者使用 Cycript。但如果代码多的话,那最好是写一个动态库,然后把文件注入到应用中得到代码的执行。一般有三种方法,本文简单的讲解一下。
一、编写 dylib
首先得先写一个测试用的动态库。Xcode 新建 iOS 工程的时候,选择 Framework 工程,默认文件格式为动态库,如图:

动态库的入口函数可以这样写
__attribute__((constructor)) static void EntryPoint()
{
NSLog(@"EntryPoint");
}
二、DynamicLibraries
注入动态库的第一种方法可以将 dylib 文件放入 DynamicLibraries 目录。写过 Tweak 的朋友,应该知道 Tweak 产生的 dylib 实际会安装到 /Library/MobileSubstrate/DynamicLibraries 目录,在这个目录的 dylib 会被应用加载,里面的 dylib 会有一个 plist 文件,标识哪些进程会加载,如图:

这样我们只需要将我们自己的 dylib 放入这个目录,然后新建一个 plist 把微信的包名加进去,就可以让微信加载我们的代码了。
三、修改可执行文件
注入动态库的第二种方法就是修改可执行文件。OSX 和 iOS 应用的可执行文件都属于 Mach-O 文件格式,只要我们在可执行文件的添加了一条 LoadCommand 为 LC_LOAD_DYLIB,将路径指定我们的 dylib,不就行了吗?下面的图可以看出,LoadCommand 加载系统的动态库。

在 MachOView 看到 LoadCommand 信息如下:

使用开源工具 optool 可以添加 LoadCommand,方法如下:
git clone –recursive https://github.com/alexzielenski/optool.git
cd optool
xcodebuild -project optool.xcodeproj -configuration Release ARCHS=”x86_64″ build //编译
/path/to/optool install -c load -p “@executable_path/yourdylib.dylib” -t /yourexefile
因为 optool 添加了 submodule,所以需要使用 –recuresive 选项,将子模块全部 clone 下来。optool 执行之后的效果如图:

我们添加的 dylib 的路径是 executable_path/yourdylib.dylib, 所以需要将 dylib 复制到应用可执行文件自身目录下,这样应用打开就能加载我们的动态库。
如果对文件重签名,打包成 ipa,就可以安装到未越狱的手机上,也就是实现了不越狱也能注入动态库。
四、DYLD_INSERT_LIBRARIES
第三种方法是使用 DYLD_INSERT_LIBRARIES 环境变量启动进程。
DYLD_INSERT_LIBRARIES=test.dylib /var/mobile/Containers/Bundle/Application/143A710D-4395-4765-872C-148EA6C86936/WeChat.app/WeChat
通过设置 DYLD_INSERT_LIBRARIES 环境变量也可以实现注入,还记得 dumpdecrypted 脱壳吗?它就是使用 DYLD_INSERT_LIBRARIES 注入进程,然后把文件从内存中给 dump 下来。
有人会奇怪,为什么 DYLD_INSERT_LIBRARIES 能够注入呢?其实这就是苹果本身提供的一个功能,我们可以看苹果开源的 dyld 的源码,在 main 函数里相关的代码,判断了 DYLD_INSERT_LIBRARIES 环境变量,如果有的话就会加载。
// load any inserted libraries
if ( sEnv.DYLD_INSERT_LIBRARIES != NULL ) {
for (const char* const* lib = sEnv.DYLD_INSERT_LIBRARIES; *lib != NULL; ++lib)
loadInsertedDylib(*lib);
}
// state of all environment variables dyld uses
//
struct EnvironmentVariables {
const char* const * DYLD_FRAMEWORK_PATH;
const char* const * DYLD_FALLBACK_FRAMEWORK_PATH;
const char* const * DYLD_LIBRARY_PATH;
const char* const * DYLD_FALLBACK_LIBRARY_PATH;
const char* const * DYLD_INSERT_LIBRARIES;
const char* const * LD_LIBRARY_PATH; // for unix conformance
const char* const * DYLD_VERSIONED_LIBRARY_PATH;
const char* const * DYLD_VERSIONED_FRAMEWORK_PATH;
bool DYLD_PRINT_LIBRARIES;
bool DYLD_PRINT_LIBRARIES_POST_LAUNCH;
bool DYLD_BIND_AT_LAUNCH;
bool DYLD_PRINT_STATISTICS;
bool DYLD_PRINT_OPTS;
bool DYLD_PRINT_ENV;
bool DYLD_DISABLE_DOFS;
bool DYLD_PRINT_CS_NOTIFICATIONS;
// DYLD_SHARED_CACHE_DONT_VALIDATE ==> sSharedCacheIgnoreInodeAndTimeStamp
// DYLD_SHARED_CACHE_DIR ==> sSharedCacheDir
// DYLD_ROOT_PATH ==> gLinkContext.rootPaths
// DYLD_IMAGE_SUFFIX ==> gLinkContext.imageSuffix
// DYLD_PRINT_OPTS ==> gLinkContext.verboseOpts
// DYLD_PRINT_ENV ==> gLinkContext.verboseEnv
// DYLD_FORCE_FLAT_NAMESPACE ==> gLinkContext.bindFlat
// DYLD_PRINT_INITIALIZERS ==> gLinkContext.verboseInit
// DYLD_PRINT_SEGMENTS ==> gLinkContext.verboseMapping
// DYLD_PRINT_BINDINGS ==> gLinkContext.verboseBind
// DYLD_PRINT_WEAK_BINDINGS ==> gLinkContext.verboseWeakBind
// DYLD_PRINT_REBASINGS ==> gLinkContext.verboseRebase
// DYLD_PRINT_DOFS ==> gLinkContext.verboseDOF
// DYLD_PRINT_APIS ==> gLogAPIs
// DYLD_IGNORE_PREBINDING ==> gLinkContext.prebindUsage
// DYLD_PREBIND_DEBUG ==> gLinkContext.verbosePrebinding
// DYLD_NEW_LOCAL_SHARED_REGIONS ==> gLinkContext.sharedRegionMode
// DYLD_SHARED_REGION ==> gLinkContext.sharedRegionMode
// DYLD_PRINT_WARNINGS ==> gLinkContext.verboseWarnings
// DYLD_PRINT_RPATHS ==> gLinkContext.verboseRPaths
// DYLD_PRINT_INTERPOSING ==> gLinkContext.verboseInterposing
};
苹果开源的 dyld 源码地址,里面有各种版本的
https://opensource.apple.com/source/dyld/
苹果的开源项目,里面东西很多,还有 xun 内核的源码,方便做内核调试。
https://opensource.apple.com/
https://opensource.apple.com/release/os-x-1011.html
https://opensource.apple.com/source/dyld/dyld-360.14/
网友评论