提取系统动态库文件

dyld_shared_cache文件

有时候我们想通过反编译来分析系统的动态库,那么首先就得找到动态库的Mach-O文件。根据tbd对二进制文件位置的描述可以知道系统绝大部分的动态库都是存放在设备的/System/Library/Frameworks目录下。然而通过iFile进入上述目录并未找到对应库的Mach-O文件,这不由让我们对动态库存放的位置产生了疑惑。

实际上苹果已经将大部分的系统动态库与插件都合并到了dyld_shared_cache文件当中,以提升性能。该文件存放在设备的/System/Library/Caches/com.apple.dyld/目录下,文件名为dyld_shared_cache_armX(X所代表的是当前设备CPU所支持的指令集架构)。

提取动态库

接下来就是要从dyld_shared_cache_armX文件中提取动态库的Mach-O文件了。网上有jtool、dyld_decache等工具可以提取动态库。这里我们借助苹果提供的dsc_extractor程序来完成提取的工作。

  1. 首先要将设备里的dyld_shared_cache_armX文件拷贝到本机。

  2. 从苹果的开源网站里面下载dyld源码,这里下载的是最新的dyld-519.2.2.tar.gz版本。

dyld程序是用来加载链接动态库的,该程序在设备的/usr/lib/dyld路径下

  1. 打开dyld-519.2.2/launch-cache/dsc_extractor.cpp源文件。该文件里面有一段main函数的代码。下面是dsc_extractor.cpp中的main函数代码:
int main(int argc, const char* argv[])
{
  if ( argc != 3 ) {
      fprintf(stderr, "usage: dsc_extractor <path-to-cache-file> <path-to-device-dir>\n");
      return 1;
  }
  
  //void* handle = dlopen("/Volumes/my/src/dyld/build/Debug/dsc_extractor.bundle", RTLD_LAZY);
  void* handle = dlopen("/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/usr/lib/dsc_extractor.bundle", RTLD_LAZY);
  if ( handle == NULL ) {
      fprintf(stderr, "dsc_extractor.bundle could not be loaded\n");
      return 1;
  }
  
  extractor_proc proc = (extractor_proc)dlsym(handle, "dyld_shared_cache_extract_dylibs_progress");
  if ( proc == NULL ) {
      fprintf(stderr, "dsc_extractor.bundle did not have dyld_shared_cache_extract_dylibs_progress symbol\n");
      return 1;
  }
  
  int result = (*proc)(argv[1], argv[2], ^(unsigned c, unsigned total) { printf("%d/%d\n", c, total); } );
  fprintf(stderr, "dyld_shared_cache_extract_dylibs_progress() => %d\n", result);
  return 0;
}

这段代码判断了参数和资源载入的有效性,同时通过usage: dsc_extractor <path-to-cache-file> <path-to-device-dir>错误参数打印的语句可以知道dsc_extractor的使用方式。

  1. 修改dsc_extractor.cpp文件:将#if预处理指令的条件判断0改为1即可。

  2. 编译dsc_extractor.cpp文件:
    $ clang++ -o dsc_extractor ./dsc_extractor.cpp dsc_iterator.cpp

  3. 运行dsc_extractor开始提取动态库:
    $ ./dsc_extractor dyld_shared_cache文件路径 输出动态库的路径

  4. 查看提取的结果:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1 dyld 1.1 dyld简介 在iOS系统中,几乎所有的程序都会用到动态库,而动态库在加载的时候都需要用d...
    Kevin_Junbaozi阅读 11,977评论 4 44
  • 动态库是开发中必然会用的,在Xcode中我们会碰到各种格式的“库”,framework、dylib、tbd,其实系...
    haiyangHiya阅读 3,846评论 1 10
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,019评论 3 119
  • 她是你的心 她是你的肝 她是你生命的四分之三 她在你怀里微笑 她在你怀里睡着 她是你这辈子的依靠 手牵手 相互依恋...
    潇湘青女阅读 263评论 8 15
  • 零秒思考 阅读,写作是培养逻辑思维能力的正道,但是要想短时间有一定的收获,需要一些相当有技巧的办法,最近看了一本叫...
    Gunther阅读 379评论 1 5