【Android 逆向】ART 脱壳 ( DexClassLoader 脱壳 | ART 虚拟机下 DexClassLoader 类加载器脱壳点总结 )

简介: 【Android 逆向】ART 脱壳 ( DexClassLoader 脱壳 | ART 虚拟机下 DexClassLoader 类加载器脱壳点总结 )

文章目录

一、ART 虚拟机下 DexClassLoader 类加载器脱壳点总结

1、file_magic.cc#OpenAndReadMagic 函数

2、dex_file.cc#DexFile::OpenCommon

3、dex_file.cc#DexFile::DexFile

总结 ( 兼容 InMemoryDexClassLoader 和 DexClassLoader 两种类加载器的 脱壳点 )





一、ART 虚拟机下 DexClassLoader 类加载器脱壳点总结


从 /art/runtime/dex_file.cc#DexFile::Open 函数开始分析脱壳点位置 ;


其中调用的 /art/runtime/base/file_magic.cc#OpenAndReadMagic 函数 , 可以作为脱壳点 ;


在 /art/runtime/dex_file.cc#OpenFile 函数中 , 调用了 /art/runtime/dex_file.cc#OpenCommon 函数 , 是脱壳点 ;


bool DexFile::Open(const char* filename,
                   const std::string& location,
                   bool verify_checksum,
                   std::string* error_msg,
                   std::vector<std::unique_ptr<const DexFile>>* dex_files) {
  ScopedTrace trace(std::string("Open dex file ") + std::string(location));
  DCHECK(dex_files != nullptr) << "DexFile::Open: out-param is nullptr";
  uint32_t magic;
  // ★ 脱壳点后汉书 
  File fd = OpenAndReadMagic(filename, &magic, error_msg);
  if (fd.Fd() == -1) {
    DCHECK(!error_msg->empty());
    return false;
  }
  if (IsZipMagic(magic)) {
    return DexFile::OpenZip(fd.Release(), location, verify_checksum, error_msg, dex_files);
  }
  if (IsDexMagic(magic)) {
    // ★ 脱壳点函数
    std::unique_ptr<const DexFile> dex_file(DexFile::OpenFile(fd.Release(),
                                                              location,
                                                              /* verify */ true,
                                                              verify_checksum,
                                                              error_msg));
    if (dex_file.get() != nullptr) {
      dex_files->push_back(std::move(dex_file));
      return true;
    } else {
      return false;
    }
  }
  *error_msg = StringPrintf("Expected valid zip or dex file: '%s'", filename);
  return false;
}



1、file_magic.cc#OpenAndReadMagic 函数


file_magic.cc#OpenAndReadMagic 函数是 脱壳点 , 第一个参数 const char* filename 是 Dex 文件的路径 ;


调用该函数的上一个调用位置是 /art/runtime/dex_file.cc#DexFile::Open 函数 ;



file_magic.cc 源码 :


#include "file_magic.h"
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "android-base/stringprintf.h"
#include "base/logging.h"
#include "base/unix_file/fd_file.h"
#include "dex_file.h"
namespace art {
using android::base::StringPrintf;
File OpenAndReadMagic(const char* filename, uint32_t* magic, std::string* error_msg) {
  CHECK(magic != nullptr);
  File fd(filename, O_RDONLY, /* check_usage */ false);
  if (fd.Fd() == -1) {
    *error_msg = StringPrintf("Unable to open '%s' : %s", filename, strerror(errno));
    return File();
  }
  int n = TEMP_FAILURE_RETRY(read(fd.Fd(), magic, sizeof(*magic)));
  if (n != sizeof(*magic)) {
    *error_msg = StringPrintf("Failed to find magic in '%s'", filename);
    return File();
  }
  if (lseek(fd.Fd(), 0, SEEK_SET) != 0) {
    *error_msg = StringPrintf("Failed to seek to beginning of file '%s' : %s", filename,
                              strerror(errno));
    return File();
  }
  return fd;
}
bool IsZipMagic(uint32_t magic) {
  return (('P' == ((magic >> 0) & 0xff)) &&
          ('K' == ((magic >> 8) & 0xff)));
}
bool IsDexMagic(uint32_t magic) {
  return DexFile::IsMagicValid(reinterpret_cast<const uint8_t*>(&magic));
}
}  // namespace art


源码路径 : /art/runtime/base/file_magic.cc#OpenAndReadMagic



2、dex_file.cc#DexFile::OpenCommon


dex_file.cc#DexFile::OpenCommon 函数中 , 可以获取 Dex 文件在内存中的起始地址 ;


注意 : 该脱壳点 与 InMemoryDexClassLoader 类加载器的脱壳点重合 ;



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



3、dex_file.cc#DexFile::DexFile


在 dex_file.cc#DexFile::DexFile 构造函数中 , 可以获取到 Dex 文件地址 ;


注意 : 该脱壳点 与 InMemoryDexClassLoader 类加载器的脱壳点重合 ;



DexFile::DexFile(const uint8_t* base,
                 size_t size,
                 const std::string& location,
                 uint32_t location_checksum,
                 const OatDexFile* oat_dex_file)
    : begin_(base),
      size_(size),
      location_(location),
      location_checksum_(location_checksum),
      header_(reinterpret_cast<const Header*>(base)),
      string_ids_(reinterpret_cast<const StringId*>(base + header_->string_ids_off_)),
      type_ids_(reinterpret_cast<const TypeId*>(base + header_->type_ids_off_)),
      field_ids_(reinterpret_cast<const FieldId*>(base + header_->field_ids_off_)),
      method_ids_(reinterpret_cast<const MethodId*>(base + header_->method_ids_off_)),
      proto_ids_(reinterpret_cast<const ProtoId*>(base + header_->proto_ids_off_)),
      class_defs_(reinterpret_cast<const ClassDef*>(base + header_->class_defs_off_)),
      method_handles_(nullptr),
      num_method_handles_(0),
      call_site_ids_(nullptr),
      num_call_site_ids_(0),
      oat_dex_file_(oat_dex_file) {
  CHECK(begin_ != nullptr) << GetLocation();
  CHECK_GT(size_, 0U) << GetLocation();
  // Check base (=header) alignment.
  // Must be 4-byte aligned to avoid undefined behavior when accessing
  // any of the sections via a pointer.
  CHECK_ALIGNED(begin_, alignof(Header));
  InitializeSectionsFromMapList();
}


源码地址 : /art/runtime/dex_file.cc#DexFile


总结 ( 兼容 InMemoryDexClassLoader 和 DexClassLoader 两种类加载器的 脱壳点 )

加固厂商可能使用 InMemoryDexClassLoader 类加载器 , 也可能使用 DexClassLoader 类加载器 , 这里为了保证不管使用什么类加载器 , 都可以进行脱壳 , 选择 2 22 个类加载器都有的脱壳点 , 可以兼容两种类加载器 ;


目录
相关文章
|
3月前
|
存储 Java 编译器
🔍深入Android底层,揭秘JVM与ART的奥秘,性能优化新视角!🔬
【7月更文挑战第28天】在Android开发中,掌握底层机制至关重要。从Dalvik到ART, Android通过采用AOT编译在应用安装时预编译字节码至机器码,显著提升了执行效率。ART还优化了垃圾回收,减少内存占用及停顿。为了优化性能,可减少DEX文件数量、优化代码结构利用内联等技术、合理管理内存避免泄漏,并使用ART提供的调试工具。
106 7
|
1月前
|
存储 Java 编译器
🔍深入Android底层,揭秘JVM与ART的奥秘,性能优化新视角!🔬
【9月更文挑战第12天】在Android开发领域,深入了解其底层机制对提升应用性能至关重要。本文详述了从早期Dalvik虚拟机到现今Android Runtime(ART)的演变过程,揭示了ART通过预编译技术实现更快启动速度和更高执行效率的奥秘。文中还介绍了ART的编译器与运行时环境,并提出了减少DEX文件数量、优化代码结构及合理管理内存等多种性能优化策略。通过掌握这些知识,开发者可以从全新的角度提升应用性能。
53 11
|
存储 编解码 Android开发
如何使用 VMware 安装安卓虚拟机,如何配置虚拟机的网络和存储?
如何使用 VMware 安装安卓虚拟机,如何配置虚拟机的网络和存储?
1139 0
|
开发工具 Android开发 虚拟化
windows电脑安装 Android Studio 的时候提示“创建Android虚拟机或安装安卓模拟器失败”
windows电脑安装 Android Studio 的时候提示“创建Android虚拟机或安装安卓模拟器失败”
|
存储 安全 IDE
android studio 输出apk过程,apk中的文件格式(bsh文件,dex文件),JVM、DVM、ART的区别,IOS与安卓的区别,ART和Dalvi
android studio 输出apk过程,apk中的文件格式(bsh文件,dex文件),JVM、DVM、ART的区别,IOS与安卓的区别,ART和Dalvi
260 0
android studio 输出apk过程,apk中的文件格式(bsh文件,dex文件),JVM、DVM、ART的区别,IOS与安卓的区别,ART和Dalvi
|
Android开发 开发者
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决
573 0
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决
|
缓存 监控 安全
❤️Android Runtime (ART) 和 Dalvik❤️
目录 1. Dalvik 1.1 Dalvik 和 JVM 区别 1.2 Dalvik 如何运行 java 1.3 dex文件 1.4 65535 2. Android Runtime (ART) 2.1 ART 功能 2.1.1 预先 (AOT) 编译 2.1.2 垃圾回收方面的优化 2.1.3 开发和调试方面的优化 2.2 Android 8.0 中的 ART 功能改进 2.2.1 并发压缩式垃圾回收器 2.2.2 循环优化 2.2.3 类层次结构分析 2.2.4 .oat 文件中的内嵌缓存 2.2.5 Dexlayout 2.2.6 Dex 缓存移除
492 0
❤️Android Runtime (ART) 和 Dalvik❤️
|
移动开发 Java API
android studio虚拟机运行react-native项目全流程(避坑笔记)
android studio虚拟机运行react-native项目全流程(避坑笔记)
android studio虚拟机运行react-native项目全流程(避坑笔记)
|
存储 Android开发 索引
Android逆向:resource.arsc文件解析(Config List)
resource.arsc是APK打包过程中生成一个重要的文件,主要存储了整个应用哦中的资源索引。但是这个文件是一个二进制文件,并不可读,所以本文就通过解析它的二进制内容来读懂这个文件。
726 0
|
存储 Java 编译器
Android逆向之--------常见Davlik字节码解释
Android逆向之--------常见Davlik字节码解释
148 0
Android逆向之--------常见Davlik字节码解释