做自己的Android ROM,屏蔽对framework中的系统APK的签名检查

分类: Android

最近两天一直在尝试更新Android中的关键库以达到定制ROM的效果,中间比较曲折,记录下来供自己和大家参考。

因为我需要基于Android的原生代码做一定的修改,所以如果无法将我自己编译出的APK或Jar替换系统中的东西,则无法达成我的目标。

我的测试的机器:htc one 电信定制版(802d),Android版本:4.2.2;LG Nexus 4,Android版本:4.3。

测试第一步:

    将手机root掉,以替换修改系统文件的权限。不同厂商的手机的root步骤不一样,自己上网搜索吧。

测试第二步:

   使用我自己编译出来的SystemUI.apk,替换Nexus 4的SystemUI.apk,通过 adb logcat > 1.txt 查看日志,直接报下面的错;

  1. W/PackageManager(  530): Signature mismatch for shared user : SharedUserSetting{41ea0bc8 android.uid.system/1000}  
  2.   
  3. E/PackageManager(  530): Package com.android.systemui has no signatures that match those in shared user android.uid.system; ignoring!  

  签名有问题,根据这个关键字“has no signatures that match those”去搜索Android源代码,可以在PackageManagerService.java中找到如下函数:

  1. private boolean verifySignaturesLP(PackageSetting pkgSetting,  
  2.         PackageParser.Package pkg) {  
  3.     if (pkgSetting.signatures.mSignatures != null) {  
  4.         // Already existing package. Make sure signatures match  
  5.         if (compareSignatures(pkgSetting.signatures.mSignatures, pkg.mSignatures) !=  
  6.             PackageManager.SIGNATURE_MATCH) {  
  7.                 Slog.e(TAG, "Package " + pkg.packageName  
  8.                         + " signatures do not match the previously installed version; ignoring!");  
  9.                 mLastScanError = PackageManager.INSTALL_FAILED_UPDATE_INCOMPATIBLE;  
  10.                 return false;  
  11.             }  
  12.     }  
  13.     // Check for shared user signatures  
  14.     if (pkgSetting.sharedUser != null && pkgSetting.sharedUser.signatures.mSignatures != null) {  
  15.         if (compareSignatures(pkgSetting.sharedUser.signatures.mSignatures,  
  16.                 pkg.mSignatures) != PackageManager.SIGNATURE_MATCH) {  
  17.             Slog.e(TAG, "Package " + pkg.packageName  
  18.                     + " has no signatures that match those in shared user "  
  19.                     + pkgSetting.sharedUser.name + "; ignoring!");  
  20.             mLastScanError = PackageManager.INSTALL_FAILED_SHARED_USER_INCOMPATIBLE;  
  21.             return false;  
  22.         }  
  23.     }  
  24.     return true;  
  25. }  

  直接替换,肯定是不行了,签名通不过,那我可否尝试将签名检查的代码给屏蔽掉?


测试第三步:

  尝试屏蔽对系统级APK的签名检查,代码已经找到,在这个目录下:frameworks/base/services/java/com/android/server/pm。

  进一步分析,services目录下的内容,最终会被编译成 services.jar,adb shell 后,在 /system/framework 下可以找到 services.jar。

  1. root@android:/system/framework # ls servi*  
  2. ls servi*  
  3. services.jar  

在 out/target/product/generic/dex_bootjars/system/framework 这个目录下可以找到 services.jar,

直接使用自己编译出来的services.jar去替换 /system/framework下的同名jar包,重启os后,好像报的这个错误:DexOpt: mismatch dep signature

然后os一直处于死循环中,无法开机成功。

根据“DexOpt: mismatch dep signature”这个关键字搜索,在DexPrepare.cpp中有对应的检查:

  1. if (memcmp(signature, ptr, kSHA1DigestLen) != 0) {    
  2.     LOGI("DexOpt: mismatch dep signature for '%s'", cacheFileName);    
  3.     goto bail;    
  4. }    


测试第四步:

  既然第三步也不行,那我用自己编译出来的“libdvm.so” 将/system/lib下的同名文件更新掉,重启os后,还是类似的错误,签名摘要校验不过。

当前陷入了短时间的僵局,上网找了很多资料参考,大部分仅能参考,写得太不详细,直接我遇到了这个文档:《MIUI ROM定制教程》,

已经在我的百度网盘共享: http://pan.baidu.com/share/link?shareid=198381908&uk=4145338314

神一般的文档,将Android的系统框架,到刷机原理,再到一些刷机的具体细节,都写得非常清楚。

下面的过程是基于HTC ONE(802d)来测试的:

1、先找一个可刷机的ROM,我找到的是Nic_One_M7_802d_4.2.2_Devil_3.5_signed.zip,这个包可以刷机成功并且系统可用。

之前找的两个包可以刷机成功,但屏幕驱动似乎有问题,触屏无反应。

2、将原有包中的services.jar转成smali文件,通过 baksmali-1.4.2.jar 进行将 jar中的dex文件转成smali文件,将PackageManagerService.smali中的verifySignaturesLP函数的实现给修改掉,然后再使用smali-1.4.2.jar将反编译出的包含smali文件的目录转成dex文件,使用winrar打开services.jar,将新生成的classes.dex覆盖jar包中的classes.dex,将这个新jar包替换原ROM zip包中的同名文件。

再使用“土豆ROM工具箱” 将新ROM zip包重新签名下,再进行刷机。

经过几分钟的等待后,系统可用,并且使用我自己编译出的 SystemUI.apk 直接覆盖 /system/framework/ 下的同名文件,也不会再报签名错误,只是无法正常运行,无法正常运行的原因是:HTC定制过此apk,将原生的代码作了很多修改,同时加入htc自己实现的代码。

为了进一步验证,我将HTC系统原生的 SystemUI.apk 复制我的电脑上,使用“土豆ROM工具箱”将其重新签名,然后再替换原生 SystemUI.apk,HTC手机也是可以正常运行的。

终于成功将系统APK签名验证这步给屏蔽了,这样我就可以基于此ROM做很多其它想做的事件了。

进一步的验证和测试:

下载一份Nexus 4原生的可刷机的4.2.2的ROM,直接使用自己编译的services.jar替换原ROM中的同名文件,刷机成功后,完全可以正常使用。

再使用自己编译出来的SystemUI.apk替换系统的同名文件,一样也可以正常运行。基于支持Android原生系统的机器进行验证和扩展,直接修改源码重新编译替换系统原文件即可,非常方便。

 smali-1.4.2.jar、baksmali-1.4.2.jar的使用命令参考:

  1. C:Userszhao3546DesktopAndroidAndroid_framework_4.2>java -jar smali-1.4.2.jar -h  
  2. usage: java -jar smali.jar [options] [--] [<smali-file>|folder]*  
  3. assembles a set of smali files into a dex file  
  4.  -?,--help                      prints the help message then exits. Specify  
  5.                                 twice for debug options  
  6.  -a,--api-level <API_LEVEL>     The numeric api-level of the file to generate,  
  7.                                 e.g. 14 for ICS. If not specified, it defaults  
  8.                                 to 14 (ICS).  
  9.  -o,--output <FILE>             the name of the dex file that will be written.  
  10.                                 The default is out.dex  
  11.  -v,--version                   prints the version then exits  
  12.  -x,--allow-odex-instructions   allow odex instructions to be compiled into the  
  13.                                 dex file. Only a few instructions are supported  
  14.                                 - the ones that can exist in a dead code path  
  15.                                 and not cause dalvik to reject the class  
  16.   
  17.   
  18. C:Userszhao3546DesktopAndroidAndroid_framework_4.2>java -jar baksmali-1.4.2.jar -h  
  19. usage: java -jar baksmali.jar [options] <dex-file>  
  20. disassembles and/or dumps a dex file  
  21.  -?,--help                                  prints the help message then exits.  
  22.                                             Specify twice for debug options  
  23.  -a,--api-level <API_LEVEL>                 The numeric api-level of the file  
  24.                                             being disassembled. If not  
  25.                                             specified, it defaults to 14 (ICS).  
  26.  -b,--no-debug-info                         don't write out debug info (.local,  
  27.                                             .param, .line, etc.)  
  28.  -c,--bootclasspath <BOOTCLASSPATH>         the bootclasspath jars to use, for  
  29.                                             analysis. Defaults to  
  30.                                             core.jar:ext.jar:framework.jar:andro  
  31.                                             id.policy.jar:services.jar. If the  
  32.                                             value begins with a :, it will be  
  33.                                             appended to the default  
  34.                                             bootclasspath instead of replacing  
  35.                                             it  
  36.  -d,--bootclasspath-dir <DIR>               the base folder to look for the  
  37.                                             bootclasspath files in. Defaults to  
  38.                                             the current directory  
  39.  -f,--code-offsets                          add comments to the disassembly  
  40.                                             containing the code offset for each  
  41.                                             address  
  42.  -l,--use-locals                            output the .locals directive with  
  43.                                             the number of non-parameter  
  44.                                             registers, rather than the .register  
  45.                                             directive with the total number of  
  46.                                             register  
  47.  -m,--no-accessor-comments                  don't output helper comments for  
  48.                                             synthetic accessors  
  49.  -o,--output <DIR>                          the directory where the disassembled  
  50.                                             files will be placed. The default is  
  51.                                             out  
  52.  -p,--no-parameter-registers                use the v<n> syntax instead of the  
  53.                                             p<n> syntax for registers mapped to  
  54.                                             method parameters  
  55.  -r,--register-info <REGISTER_INFO_TYPES>   print the specificed type(s) of  
  56.                                             register information for each  
  57.                                             instruction. "ARGS,DEST" is the  
  58.                                             default if no types are specified.  
  59.                                             Valid values are:  
  60.                                             ALL: all pre- and post-instruction  
  61.                                             registers.  
  62.                                             ALLPRE: all pre-instruction  
  63.                                             registers  
  64.                                             ALLPOST: all post-instruction  
  65.                                             registers  
  66.                                             ARGS: any pre-instruction registers  
  67.                                             used as arguments to the instruction  
  68.                                             DEST: the post-instruction  
  69.                                             destination register, if any  
  70.                                             MERGE: Any pre-instruction register  
  71.                                             has been merged from more than 1  
  72.                                             different post-instruction register  
  73.                                             from its predecessors  
  74.                                             FULLMERGE: For each register that  
  75.                                             would be printed by MERGE, also show  
  76.                                             the incoming register types that  
  77.                                             were merged  
  78.  -s,--sequential-labels                     create label names using a  
  79.                                             sequential numbering scheme per  
  80.                                             label type, rather than using the  
  81.                                             bytecode address  
  82.  -v,--version                               prints the version then exits  
  83.  -x,--deodex                                deodex the given odex file. This  
  84.                                             option is ignored if the input file  
  85.                                             is not an odex file  
原文地址:https://www.cnblogs.com/bigben0123/p/4308576.html