程序员程序猿阵线联盟-汇总各类技术干货

iOS 攻防实践: 提取系统动态库文件

2018-07-26  本文已影响13人  关于岚

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. 查看提取的结果:

上一篇 下一篇

猜你喜欢

热点阅读