为了账号安全,请及时绑定邮箱和手机立即绑定

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

标签:
iOS

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即可。

    https://img1.sycdn.imooc.com//5d5e7aeb0001dfa207900602.png

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

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

    https://img1.sycdn.imooc.com//5d5e7aef0001c5e807250210.png


  4. 查看提取的结果:

    https://img1.sycdn.imooc.com//5d5e7af600011e2e07080420.png


             


作者:关于岚
链接:https://www.jianshu.com/p/103d25ac05f4

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消