【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 个类加载器都有的脱壳点 , 可以兼容两种类加载器 ;


目录
相关文章
|
存储 缓存 前端开发
关于Android SurfaceView截屏总结
关于Android SurfaceView截屏总结
1749 0
|
编解码 Android开发
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
619 0
Android | 老生常谈!屏幕适配原理 & 方案总结笔记
|
存储 编解码 Android开发
如何使用 VMware 安装安卓虚拟机,如何配置虚拟机的网络和存储?
如何使用 VMware 安装安卓虚拟机,如何配置虚拟机的网络和存储?
1241 0
|
开发工具 Android开发 虚拟化
windows电脑安装 Android Studio 的时候提示“创建Android虚拟机或安装安卓模拟器失败”
windows电脑安装 Android Studio 的时候提示“创建Android虚拟机或安装安卓模拟器失败”
|
开发工具 Android开发
Android推送集成方案总结
刚做完推送集成方案,记录下坑。 这里记录的特性和使用时针对写blog时采用的sdk的,具体使用流程和限制还请参考官方给出的sdk. #### 1、推送规则 小米手机用小米推送; 华为手机用华为推送; 其他手机用友盟推送。
|
存储 算法 Java
聊聊ART虚拟机_对象的分配问题
ART 虚拟机(下图 Runtime 层),相信各位搞 Android 开发的同学都有知道,总体的印象呢就是:ART 与 JVM 不同,其不符合 JVM 规范不属于 JVM ,且为 Dalvik 的进阶版。
聊聊ART虚拟机_对象的分配问题
|
存储 缓存 算法
聊聊ART虚拟机_对象的使用和销毁问题
在上一篇文章中,我们聊到了对象的分配问题,简单说明了何为 ART 虚拟机,以及对象中类的加载、内存布局等问题。本文继接上文,将会讲完对象的使用和销毁的问题,希望本文对你有所帮助。
聊聊ART虚拟机_对象的使用和销毁问题
|
Java 编译器 Android开发
聊聊ART虚拟机_如何保证高效执行
我们在前面两篇文章中聊到了 ART 虚拟机中对于对象的分配、使用和销毁的问题。主要了解其如何分配到对应的内存、通过几种手段来使用,且如何 ART 是如何最好的解决内存碎片的问题。
聊聊ART虚拟机_如何保证高效执行
|
存储 前端开发 数据管理
淘宝安卓端搜索架构升级总结
推荐语:这篇文章图文并茂地介绍了淘宝搜索滚动容器的技术演进过程,结合代码讲解页面结构划分、数据处理、交互效果,还包含了对逻辑抽象、功能拓展的思考,最后总结了可复用的架构。非常具有实践意义,推荐阅读学习! ——大淘宝技术终端开发工程师 门柳
416 0
|
Android开发 开发者
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决
629 0
Android Studio 安装APK在虚拟机时报 Installation failed due to: 'null' 解决

热门文章

最新文章