前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 逆向】ART 脱壳 ( 修改 /art/runtime/dex_file.cc#OpenCommon 系统源码进行脱壳 )

【Android 逆向】ART 脱壳 ( 修改 /art/runtime/dex_file.cc#OpenCommon 系统源码进行脱壳 )

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

文章目录

一、要修改的源码 /art/runtime/dex_file.cc#OpenCommon


/art/runtime/dex_file.cc#OpenCommon 方法可以作为脱壳点 , 在该函数中可以获取 DEX 文件在内存中的 起始地址 和 文件大小 , 直接将该文件保存到本地 SD 卡即可 ;

脱壳的代码与 【Android 逆向】整体加固脱壳 ( 脱壳点简介 | 修改系统源码进行脱壳 ) 博客中的代码类似 ;

/art/runtime/dex_file.cc#OpenCommon 函数源码 :

代码语言:javascript
复制
std::unique_ptr<DexFile> DexFile::OpenCommon(const uint8_t* base,
                                             size_t size,
                                             const std::string& location,
                                             uint32_t location_checksum,
                                             const OatDexFile* oat_dex_file,
                                             bool verify,
                                             bool verify_checksum,
                                             std::string* error_msg,
                                             VerifyResult* verify_result) {
  if (verify_result != nullptr) {
    *verify_result = VerifyResult::kVerifyNotAttempted;
  }
  std::unique_ptr<DexFile> dex_file(new DexFile(base,
                                                size,
                                                location,
                                                location_checksum,
                                                oat_dex_file));
  if (dex_file == nullptr) {
    *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
                              error_msg->c_str());
    return nullptr;
  }
  if (!dex_file->Init(error_msg)) {
    dex_file.reset();
    return nullptr;
  }
  if (verify && !DexFileVerifier::Verify(dex_file.get(),
                                         dex_file->Begin(),
                                         dex_file->Size(),
                                         location.c_str(),
                                         verify_checksum,
                                         error_msg)) {
    if (verify_result != nullptr) {
      *verify_result = VerifyResult::kVerifyFailed;
    }
    return nullptr;
  }
  if (verify_result != nullptr) {
    *verify_result = VerifyResult::kVerifySucceeded;
  }
  return dex_file;
}

源码路径 : /art/runtime/dex_file.cc#OpenCommon

二、修改 /art/runtime/dex_file.cc#OpenCommon 函数源码进行脱壳


/art/runtime/dex_file.cc#OpenCommon 函数中 ,

const uint8_t* base 参数是 dex 文件在内存的首地址 ,

size_t size 参数是 dex 文件在内存中的大小 ;

修改源码后 , 重新编译系统 , 在新编译的系统中 , 运行要脱壳的应用 , 即可将应用脱壳后的 dex 文件 , 输出到 /sdcard/pid_dexCount_output.dex 路径中 ;

编译结束后 , 运行虚拟机 , 或者刷到 Pixel 手机中 , 即可运行该系统 ;

修改后的 /art/runtime/dex_file.cc#OpenCommon 函数源码 :

代码语言:javascript
复制
/* 记录当前 dex 文件索引 */
int dexCount = 0;

std::unique_ptr<DexFile> DexFile::OpenCommon(const uint8_t* base,
                                             size_t size,
                                             const std::string& location,
                                             uint32_t location_checksum,
                                             const OatDexFile* oat_dex_file,
                                             bool verify,
                                             bool verify_checksum,
                                             std::string* error_msg,
                                             VerifyResult* verify_result) {

  // ---------- ★ 下面是脱壳内容 ----------

  // 系统启动后 , 可能会生成很多 dex 文件 , 
  // DEX 文件保存路径
  char output[50]={0};
  
  // 获取当前进程 ID , 这是为了区分准备的 
  int pid = getpid();
  
  // 生成文件名称 , 由于单个 APK 可能有多个 DEX 文件 
  // 这里将每个 DEX 文件的 进程 ID 和 DEX 文件字节大小 
  // 放入 DEX 文件名中 , 加以识别
  sprintf(output, "/sdcard/%d_%d_output.dex", pid, dexCount);
  
  // dex 文件索引自增
  dexCount++;
  	
  // 以写的方式 , 打开文件 , 如果没有就创建该文件
  int fd = open(output, "wb+");
  
  // 文件打开成功 , 则 dump 内存数据到 /sdcard/output.dex 文件中
  if (fd > 0)
  {
  	// 将 base 地址的内存数据拷贝到 fd 文件中 , 拷贝 size 字节
  	write(fd, base, size);
  	// 关闭文件 
  	close(fd);
  }
  
  // ---------- ★ 上面是脱壳内容 ----------

  if (verify_result != nullptr) {
    *verify_result = VerifyResult::kVerifyNotAttempted;
  }
  std::unique_ptr<DexFile> dex_file(new DexFile(base,
                                                size,
                                                location,
                                                location_checksum,
                                                oat_dex_file));
  if (dex_file == nullptr) {
    *error_msg = StringPrintf("Failed to open dex file '%s' from memory: %s", location.c_str(),
                              error_msg->c_str());
    return nullptr;
  }
  if (!dex_file->Init(error_msg)) {
    dex_file.reset();
    return nullptr;
  }
  if (verify && !DexFileVerifier::Verify(dex_file.get(),
                                         dex_file->Begin(),
                                         dex_file->Size(),
                                         location.c_str(),
                                         verify_checksum,
                                         error_msg)) {
    if (verify_result != nullptr) {
      *verify_result = VerifyResult::kVerifyFailed;
    }
    return nullptr;
  }
  if (verify_result != nullptr) {
    *verify_result = VerifyResult::kVerifySucceeded;
  }
  return dex_file;
}

源码路径 : /art/runtime/dex_file.cc#OpenCommon

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、要修改的源码 /art/runtime/dex_file.cc#OpenCommon
  • 二、修改 /art/runtime/dex_file.cc#OpenCommon 函数源码进行脱壳
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档