前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile.java 对应的 dalvik_system_DexFile.cc 本地函数分析 )

【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile.java 对应的 dalvik_system_DexFile.cc 本地函数分析 )

作者头像
韩曙亮
发布2023-03-30 09:33:28
3390
发布2023-03-30 09:33:28
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、DexFile 对应的 dalvik_system_DexFile.cc 中的 Native 方法


在上一篇博客 【Android 逆向】ART 脱壳 ( InMemoryDexClassLoader 脱壳 | DexFile 构造函数及相关调用函数 | Android 源码中查找 native 函数 ) 中 , 分析了 DexFile 构造函数 , 以及 makeInMemoryDexElements 函数 ; 并查找了 DexFile 中的 native 函数 createCookieWithDirectBuffer 和 createCookieWithArray 函数定义在 /art/runtime/native/dalvik_system_DexFile.cc 中 ;

1、dalvik_system_DexFile.cc 的 DexFile_createCookieWithDirectBuffer 函数

在 DexFile_createCookieWithDirectBuffer 函数中 , 调用的 memcpy 方法中的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的长度 , 这里可以将内存中的 dex 数据导出 ;

代码语言:javascript
复制
  // ★ 此处的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的长度
  memcpy(dex_mem_map->Begin(), base_address, length);
代码语言:javascript
复制
// DexFile 中 createCookieWithDirectBuffer 函数对应的 C++ 函数
static jobject DexFile_createCookieWithDirectBuffer(JNIEnv* env,
                                                    jclass,
                                                    jobject buffer,
                                                    jint start,
                                                    jint end) {
  uint8_t* base_address = reinterpret_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
  if (base_address == nullptr) {
    ScopedObjectAccess soa(env);
    ThrowWrappedIOException("dexFileBuffer not direct");
    return 0;
  }

  std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
  if (dex_mem_map == nullptr) {
    DCHECK(Thread::Current()->IsExceptionPending());
    return 0;
  }

  size_t length = static_cast<size_t>(end - start);
	
  // ★ 此处的 dex_mem_map->Begin() 就是 dex 文件的起始地址 , length 是 dex 文件的长度
  memcpy(dex_mem_map->Begin(), base_address, length);

  // ★ 核心跳转
  return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
}

源码路径 : /art/runtime/native/dalvik_system_DexFile.cc

2、dalvik_system_DexFile.cc 的 DexFile_createCookieWithArray 函数

DexFile_createCookieWithDirectBuffer 和 DexFile_createCookieWithArray

2

个 native 方法 , 在最后返回时都调用了 CreateSingleDexFileCookie 方法 ;

代码语言:javascript
复制
// DexFile 中 createCookieWithArray 函数对应的 C++ 函数
static jobject DexFile_createCookieWithArray(JNIEnv* env,
                                             jclass,
                                             jbyteArray buffer,
                                             jint start,
                                             jint end) {
  std::unique_ptr<MemMap> dex_mem_map(AllocateDexMemoryMap(env, start, end));
  if (dex_mem_map == nullptr) {
    DCHECK(Thread::Current()->IsExceptionPending());
    return 0;
  }

  auto destination = reinterpret_cast<jbyte*>(dex_mem_map.get()->Begin());
  env->GetByteArrayRegion(buffer, start, end - start, destination);

  // ★ 核心跳转
  return CreateSingleDexFileCookie(env, std::move(dex_mem_map));
}

源码路径 : /art/runtime/native/dalvik_system_DexFile.cc

二、dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函数


在该方法中 , 主要创建 dex_file 实例 , 然后将该实例对象返回到 Java 层 ; 传入的参数是 CreateDexFile(env, std::move(data)) , 下面开始分析该函数 ;

dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函数源码 :

代码语言:javascript
复制
static jobject CreateSingleDexFileCookie(JNIEnv* env, std::unique_ptr<MemMap> data) {
  // ★ 创建 dex_file 
  std::unique_ptr<const DexFile> dex_file(CreateDexFile(env, std::move(data)));
  if (dex_file.get() == nullptr) {
    DCHECK(env->ExceptionCheck());
    return nullptr;
  }
  std::vector<std::unique_ptr<const DexFile>> dex_files;
  dex_files.push_back(std::move(dex_file));
  return ConvertDexFilesToJavaArray(env, nullptr, dex_files);
}

源码路径 : /art/runtime/native/dalvik_system_DexFile.cc#CreateSingleDexFileCookie

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-15,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、DexFile 对应的 dalvik_system_DexFile.cc 中的 Native 方法
    • 1、dalvik_system_DexFile.cc 的 DexFile_createCookieWithDirectBuffer 函数
      • 2、dalvik_system_DexFile.cc 的 DexFile_createCookieWithArray 函数
      • 二、dalvik_system_DexFile.cc 的 CreateSingleDexFileCookie 函数
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档