Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1856956
  • 博文数量: 38
  • 博客积分: 690
  • 博客等级: 中士
  • 技术积分: 3715
  • 用 户 组: 普通用户
  • 注册时间: 2012-06-27 14:06
文章分类

全部博文(38)

文章存档

2018年(8)

2016年(4)

2015年(2)

2014年(1)

2013年(3)

2012年(20)

分类: Android平台

2018-05-17 13:27:21

前面几篇文章,备忘了android的编译环境搭建,搞定之后,现在简单尝试一下修改编译某个模块试一试。这里拿常用脱壳方法dex2oat试一试

一、修改 art\dex2oat\dex2oat.cc 文件

二、找到 函数 static int dex2oat(int argc, char** argv)

在936 行

  1. // Ensure opened dex files are writable for dex-to-dex transformations.
  2. for (const auto& dex_file : dex_files) {
  3.   if (!dex_file->EnableWrite()) {
  4.    PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
  5.   }
  6. }

插入

  1. // Ensure opened dex files are writable for dex-to-dex transformations.
  2. for (const auto& dex_file : dex_files) {
  3.   if (!dex_file->EnableWrite()) {
  4.     PLOG(ERROR) << "Failed to make .dex file writeable '" << dex_file->GetLocation() << "'\n";
  5.   }
  6. ////////////////////////////分割线 以下为添加的代码///////////////////////////////////////////////////////////
  7.   std::string dex_name = dex_file->GetLocation();
  8.   LOG(INFO)<<":stevenrao:"<<__LINE__<<":" << " dex2oat::dex_file name-->" << dex_name;
  9.   LOG(INFO)<<":stevenrao:"<<__LINE__<<":" << " dex2oat::oat_location name-->" << oat_location;


  10.   if (dex_name.find("jiagu") != std::string::npos
  11.   || dex_name.find("cache") != std::string::npos
  12.   || dex_name.find("files") != std::string::npos
  13.   || dex_name.find("tx_shell") != std::string::npos
  14.   || dex_name.find("app_dex") != std::string::npos
  15.   || dex_name.find("nagain") != std::string::npos)
  16.   {
  17.     int nDexLen = dex_file->Size();
  18.     char pszDexFileName[260] = {0};
  19.     sprintf(pszDexFileName, "%s_%d", dex_name.c_str(), nDexLen);
  20.     int fd = open(pszDexFileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
  21.     if (fd > 0)
  22.     {
  23.       if ( write(fd, (char*)dex_file->Begin(), nDexLen) <= 0)
  24.       {
  25.         LOG(INFO)<<":stevenrao:"<<__LINE__<<":" << "stevenrao dex2oat::write dex file failed-->" << pszDexFileName;
  26.       }
  27.       else
  28.       {
  29.         LOG(INFO)<<":stevenrao:"<<__LINE__<<":" << "stevenrao dex2oat::write dex file success-->" << pszDexFileName;
  30.       }
  31.       close(fd);
  32.     }
  33.     else
  34.     {
  35.       LOG(INFO)<<":stevenrao:"<<__LINE__<<":" << "stevenrao dex2oat::open dex file failed-->" << pszDexFileName;
  36.     }
  37.   }
  38. ////////////////////////////分割线 以上为添加的代码///////////////////////////////////////////////////////////
  39. }


三、重新编译

编译指令 解释
m 在源码树的根目录执行编译
mm 编译当前路径下所有模块,但不包含依赖
mmm [module_path] 编译指定路径下所有模块,但不包含依赖
mma 编译当前路径下所有模块,且包含依赖
mmma [module_path] 编译指定路径下所有模块,且包含依赖
make [module_name] 无参数,则表示编译整个Android代码



  1. #进入源码目录,执行这两步
  2. $ source build/envsetup.sh
  3. $ lunch #选择 hammerhead
  4. $ cd art/dex2oat
  5. stevenrao@dex2oat$ mm
  6. make: Entering directory '/data/code/aosp-4.4.4_r1'
  7. ============================================
  8. PLATFORM_VERSION_CODENAME=REL
  9. PLATFORM_VERSION=4.4.4
  10. TARGET_PRODUCT=aosp_hammerhead
  11. TARGET_BUILD_VARIANT=userdebug
  12. TARGET_BUILD_TYPE=release
  13. TARGET_BUILD_APPS=
  14. TARGET_ARCH=arm
  15. TARGET_ARCH_VARIANT=armv7-a-neon
  16. TARGET_CPU_VARIANT=krait
  17. HOST_ARCH=x86
  18. HOST_OS=linux
  19. HOST_OS_EXTRA=Linux-4.13.0-41-generic-x86_64-with-Ubuntu-16.04-xenial
  20. HOST_BUILD_TYPE=release
  21. BUILD_ID=KTU84P
  22. OUT_DIR=out
  23. ============================================
  24. Using target GCC 4.7 disables thread-safety checks.
  25. PRODUCT_COPY_FILES device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml ignored.
  26. target thumb C++: dex2oat <= art/dex2oat/dex2oat.cc
  27. target Executable: dex2oat (out/target/product/hammerhead/obj/EXECUTABLES/dex2oat_intermediates/LINKED/dex2oat)
  28. target Symbolic: dex2oat (out/target/product/hammerhead/symbols/system/bin/dex2oat)
  29. target Strip: dex2oat (out/target/product/hammerhead/obj/EXECUTABLES/dex2oat_intermediates/dex2oat)
  30. Install: out/target/product/hammerhead/system/bin/dex2oat
  31. host C++: dex2oat <= art/dex2oat/dex2oat.cc
  32. host Executable: dex2oat (out/host/linux-x86/obj/EXECUTABLES/dex2oat_intermediates/dex2oat)
  33. Install: out/host/linux-x86/bin/dex2oat
  34. target thumb C++: dex2oatd <= art/dex2oat/dex2oat.cc
  35. target Executable: dex2oatd (out/target/product/hammerhead/obj/EXECUTABLES/dex2oatd_intermediates/LINKED/dex2oatd)
  36. target Symbolic: dex2oatd (out/target/product/hammerhead/symbols/system/bin/dex2oatd)
  37. target Strip: dex2oatd (out/target/product/hammerhead/obj/EXECUTABLES/dex2oatd_intermediates/dex2oatd)
  38. Install: out/target/product/hammerhead/system/bin/dex2oatd
  39. host C++: dex2oatd <= art/dex2oat/dex2oat.cc
  40. host Executable: dex2oatd (out/host/linux-x86/obj/EXECUTABLES/dex2oatd_intermediates/dex2oatd)
  41. Install: out/host/linux-x86/bin/dex2oatd
编译二进制 文件为 out/target/product/hammerhead/system/bin/dex2oat


三、替换到真机上

  1. #先把前面编译的copy到机器的临时目录中
  2. stevenrao@aosp-4.4.4_r1$ adb push out/target/product/hammerhead/system/bin/dex2oat /data/tmp/
  3. out/target/product/hammerhead/system/bin/dex2oat: 1 file pushed. 0.7 MB/s (62804 bytes in 0.090s)
  4. stevenrao@aosp-4.4.4_r1$ adb shell
  5. shell@hammerhead:/ $ su
  6. root@hammerhead:/ # cd /data/tmp/
  7. root@hammerhead:/data/tmp # ll
  8. -rwxrwxrwx shell shell 1107664 2017-01-11 09:38 busybox-armv6l
  9. -rwxrwxrwx shell shell 62804 2018-05-17 04:35 dex2oat
  10. #copy覆盖
  11. root@hammerhead:/data/tmp # cp dex2oat /system/bin/dex2oat
  12. cp: /system/bin/dex2oat: Read-only file system
  13. 文件系统只读,需要重新mount
  14. root@hammerhead:/data/tmp # mount -o remount,rw /dev/block/mmcblk0p25 /system
  15. root@hammerhead:/data/tmp # cp dex2oat /system/bin/dex2oat

四、测试

1、修改虚拟机为art虚拟机

在开发者选项里面,有个 select runtime。 选择art


2、重启机器,这是一个漫长的过程

以下不粘贴某个具体app的过程了,避嫌

3、adb install ××××.apk 安装程序包

4、logcat | grep 'stevenrao' 通过日志观察 dex 文件输出位置

5、如果能成功的话,可以看到一系列dex文件。

阅读(4712) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~