Android hot-fix热修复
AndFix
项目地址:https://github.com/alibaba/AndFix
AndFix is a solution to fix the bugs online instead of redistributing Android App.
AndFix is an acronym for “Android hot-fix”.
AndFix supports Android version from 2.3 to 6.0, both ARM and X86 architecture, both Dalvik and ART runtime.
The compressed file format of AndFix’s patch is .apatch. It is dispatched from your own server to client to fix your App’s bugs.
优点:实时修复,仅支持java层修改
缺点:不支持添加文件到assets文件夹
不支持layout文件添加组件
不支持add filed(R.xx.xx), new class, 内部类, 匿名内部类
Principle
The implementation principle of AndFix is method body’s replacing,
具体的实现原理就是方法替换
Method replacing
AndFix judges the methods should be replaced by java custom annotation and replaces it by hooking it. AndFix has a native method art_replaceMethod
in ART or dalvik_replaceMethod
in Dalvik.
For more details, here.
Fix Process
Integration
How to get?
Directly add AndFix aar to your project as compile libraries.
For your maven dependency,
1 | <dependency> |
For your gradle dependency,
1 | dependencies { |
How to use?
- Initialize PatchManager,
1 | patchManager = new PatchManager(context); |
- Load patch,
1 | patchManager.loadPatch(); |
You should load patch as early as possible, generally, in the initialization phase of your application(such as Application.onCreate()
).
- Add patch,
1 | patchManager.addPatch(path);//path of the patch file that was downloaded |
When a new patch file has been downloaded, it will become effective immediately by addPatch
.
Developer Tool
AndFix provides a patch-making tool called apkpatch.
How to get?
The apkpatch
tool can be found here.
How to use?
- Prepare two android packages, one is the online package, the other one is the package after you fix bugs by coding.
- Generate
.apatch
file by providing the two package,
1 | usage: apkpatch -f <new> -t <old> -o <output> -k <keystore> -p <***> -a <alias> -e <***> |
Now you get the application savior, the patch file. Then you need to dispatch it to your client in some way, push or pull.
Sometimes, your team members may fix each other’s bugs, and generate not only one .apatch
. For this situation, you can
merge .apatch
files using this tool,
1 | usage: apkpatch -m <apatch_path...> -o <output> -k <keystore> -p <***> -a <alias> -e <***> |
Running sample
- Import samplesI/AndFixDemo to your IDE, append AndFixDemo dependencies with AndFix(library project or aar).
- Build project, save the package as 1.apk, and then install on device/emulator.
- Modify com.euler.test.A, references com.euler.test.Fix.
- Build project, save the package as 2.apk.
- Use apkpatch tool to make a patch.
- Rename the patch file to out.apatch, and then copy it to sdcard.
- Run 1.apk and view log.
Notice
ProGuard
If you enable ProGuard, you must save the mapping.txt, so your new version’s build can use it with “-applymapping”.
And it is necessary to keep classes as follow,
Native method
com.alipay.euler.andfix.AndFix
Annotation
com.alipay.euler.andfix.annotation.MethodReplace
To ensure that these classes can be found after running an obfuscation and static analysis tool like ProGuard, add the configuration below to your ProGuard configuration file.
1 | -keep class * extends java.lang.annotation.Annotation |
Self-Modifying Code
If you use it, such as Bangcle. To generate patch file, you’d better to use raw apk.
Security
The following is important but out of AndFix’s range.
- verify the signature of patch file
- verify the fingerprint of optimize file
API Documentation
The libraries javadoc can be found here.
License
Copyright (c) 2015, alipay.com
使用方法
Server
Server端使用apkpatch生成差分补丁,后缀.apatch
./apkpatch.sh -f demo-debug2.apk -t demo-debug1.apk -o out -k demo.jks -p 123456 -a key -e 123456
Client
Application初始化AndFix组件,下载补丁,加载补丁,删除补丁
源码解析
Application.onCreate()初始化AndFix组件
1 | private void initAndFix() { |
PatchManager.java
补丁文件的管理类(加载、删除)
1 | private static final String SUFFIX = ".apatch"; // 补丁后缀 |
AndFixManager.java
fix dex files
1 | /** |
SecurityChecker.java
用于验证apk和dex的签名
1 | init()获取apk的签名mPublicKey |
Compat.java
检查当前系统是否支持andfix
AndFix supports Android version from 2.3 to 6.0, both ARM and X86 architecture, both Dalvik and ART runtime.
not support alibaba’s YunOs
MethodReplace.java
1 | /** |
AndFix.java
1 | // initialize art or dalvik |
其他
生成的补丁out.apatch是带有签名信息的压缩包,
META_INFO文件夹包含MANIFEST.MF、CERT.SF和CERT.RSA、PATCH.MF。这三个文件分别表征以下含义:
(1)MANIFEST.MF:这是摘要文件。程序遍历Apk包中的所有文件(entry),对非文件夹非签名文件的文件,逐个用SHA1生成摘要信息(用SHA1算法摘要的消息最终有160比特位的输出),再用Base64进行编码。如果你改变了apk包中的文件,那么在apk安装校验时,改变后的文件摘要信息与MANIFEST.MF的检验信息不同,于是程序就不能成功安装。
说明:如果攻击者修改了程序的内容,有重新生成了新的摘要,那么就可以通过验证,所以这是一个非常简单的验证。
(2)CERT.SF:这是对摘要的签名文件。对前一步生成的MANIFEST.MF,使用SHA1-RSA算法,用开发者的私钥进行签名。在安装时只能使用公钥才能解密它。解密之后,将它与未加密的摘要信息(即,MANIFEST.MF文件)进行对比,如果相符,则表明内容没有被异常修改。
说明:在这一步,即使开发者修改了程序内容,并生成了新的摘要文件,但是攻击者没有开发者的私钥,所以不能生成正确的签名文件(CERT.SF)。系统在对程序进行验证的时候,用开发者公钥对不正确的签名文件进行解密,得到的结果和摘要文件(MANIFEST.MF)对应不起来,所以不能通过检验,不能成功安装文件。
(3)CERT.RSA文件中保存了公钥、所采用的加密算法等信息。
说明:系统对签名文件进行解密,所需要的公钥就是从这个文件里取出来的。
结论:从上面的总结可以看出,META-INFO里面的说那个文件环环相扣,从而保证Android程序的安全性。(只是防止开发者的程序不被攻击者修改,如果开发者的公私钥对对攻击者得到或者开发者开发出攻击程序,Android系统都无法检测出来。)
(4)PATCH.MF 由apkpatch
tool 生成,主要内容:
Manifest-Version: 1.0
Patch-Name: demo-debug2
Created-Time: 15 Apr 2016 10:10:12 GMT
From-File: demo-debug2.apk
To-File: demo-debug1.apk
Patch-Classes: com.euler.andfix.SecondAvtivity_CF,com.euler.andfix.MainApplication_CF
Created-By: 1.0 (ApkPatch)
// 获取CERT.RSA公钥信息
openssl pkcs7 -inform DER -in CERT.RSA -noout -print_certs -text
加载apatch过程
1、isSupport 判断设备是否支持andfix
2、copy /data/data/packageName/files/apatch/ 下
3、verify 校验apatch签名,对比apk的publickey和apatch的publickey
4、loaddex /data/data/packageName/files/apatch_opt/
5、repleaseMethod 根据PATCH.MF中Patch-Classes找到需要替换的class,再由class反射提取带有MethodReplace注解的方法,jni层替换,立即修复
Apk重签名
1、解压apk,删除META_INFO文件夹,再压缩改后缀.apk
2、生成keystore签名文件
keytool -genkey -alias demo -keyalg RSA -validity 20000 -keystore demo.keystore
-genkey 产生证书文件
-keystore 指定密钥库的.keystore文件中
-keyalg 指定密钥的算法
-validity 为证书有效天数,这里我们写的是20000天
-alias 产生别名
3、apk签名
jarsigner -verbose -keystore demo.keystore -signedjar demo.apk demo_old.apk demo -digestalg SHA1 -sigalg MD5withRSA
参考
https://github.com/alibaba/AndFix
Alibaba-AndFix Bug热修复框架原理及源码解析