当前位置:   article > 正文

安卓学习笔记之二:APKtool工具实现反编译-编译-签名全过程

apktool

APKTool 介绍

        APKTool 是一个开源的、跨平台的反编译、回编译 Android 应用程序的工具。它能够将 APK 文件解压并还原成 Android 应用程序的资源文件和 Smali 代码,还能将修改后的资源文件和 Smali 代码重新打包成 APK 文件。APKTool 是 Android 应用程序开发和逆向开发的重要工具之一,它能够帮助开发者对 APK 文件进行修改、分析和定制,也能够帮助安全研究人员和黑客对应用程序进行逆向工程和漏洞分析。

        APKTool需要java环境支持,请先安装好java环境,可以使用 java -version,java版本尽量在1.8以上;

APKTool 的主要功能包括:

  1. 解压 APK 文件:APKTool 能够将 APK 文件解压为应用程序的源代码和资源文件。解压后的资源文件包括 AndroidManifest.xml、res 目录、assets 目录等;解压后的源代码包括 Java 代码和 Smali 代码。
  2. 反编译 APK 文件:APKTool 能够将 APK 文件反编译为 Java 代码和 Smali 代码。反编译后的 Java 代码和 Smali 代码能够帮助开发者了解应用程序的逻辑和实现方式,也能够帮助安全研究人员和黑客分析应用程序的漏洞和安全性。
  3. 重打包 APK 文件:APKTool 能够将修改后的源代码和资源文件重新打包成 APK 文件。重打包后的 APK 文件可以直接安装到 Android 设备上运行,也可以用于二次开发、分析和测试。
  4. 支持多语言:APKTool 能够处理多语言的应用程序,它支持多种语言的资源文件,可以方便地进行多语言的应用程序开发和本地化。
  5. 支持自定义:APKTool 提供了多种配置选项,可以根据需要自定义应用程序的打包和解包过程,也可以定制应用程序的资源文件和 Smali 代码。
     

APKTool 下载

首先下载apktool.bat和apktool.jar
官网地址:https://ibotpeaches.github.io/Apktool/install/

下载说明:

  1. 下载Windows包装脚本(右键单击,将链接另存为apktool.bat)
  2. 下载apktool-2(在此处查找最新版本)
  3. 将下载的jar重命名为apktool.jar
  4. 将两个文件(apktool.jar和apktool.bat)移动到Windows目录(通常为C://Windows)
  5. 如果您没有访问C://Windows的权限,可以将这两个文件放在任何位置,然后将该目录添加到环境变量系统PATH变量中。
  6. 尝试通过命令提示符运行apktool
  1. >apktool
  2. D:\wxds450>echo off
  3. Apktool 2.9.1 - a tool for reengineering Android apk files
  4. with smali v3.0.3 and baksmali v3.0.3
  5. Copyright 2010 Ryszard Wiśniewski <brut.alll@gmail.com>
  6. Copyright 2010 Connor Tumbleson <connor.tumbleson@gmail.com>
  7. usage: apktool
  8. -advance,--advanced Print advanced information.
  9. -version,--version Print the version.
  10. usage: apktool if|install-framework [options] <framework.apk>
  11. -p,--frame-path <dir> Store framework files into <dir>.
  12. -t,--tag <tag> Tag frameworks using <tag>.
  13. usage: apktool d[ecode] [options] <file_apk>
  14. -f,--force Force delete destination directory.
  15. -o,--output <dir> The name of folder that gets written. (default: apk.out)
  16. -p,--frame-path <dir> Use framework files located in <dir>.
  17. -r,--no-res Do not decode resources.
  18. -s,--no-src Do not decode sources.
  19. -t,--frame-tag <tag> Use framework files tagged by <tag>.
  20. usage: apktool b[uild] [options] <app_path>
  21. -f,--force-all Skip changes detection and build all files.
  22. -o,--output <dir> The name of apk that gets written. (default: dist/name.apk)
  23. -p,--frame-path <dir> Use framework files located in <dir>.
  24. For additional info, see: https://apktool.org
  25. For smali/baksmali info, see: https://github.com/google/smali

使用APKTool 反编译

使用一个现有的apk文件wxdu450.apk,在Adroid 4.4手机上安装会出现解析错误,拷贝到d:\wxds450下,进行反编译。

  1. D:\wxds450>apktool d wxdu450.apk
  2. I: Using Apktool 2.9.1 on wxdu450.apk
  3. I: Loading resource table...
  4. I: Decoding file-resources...
  5. I: Loading resource table from file: C:\Users\u\AppData\Local\apktool\framework\1.apk
  6. W: Cant find 9patch chunk in file: "b/b16.9.png". Renaming it to *.png.
  7. W: Cant find 9patch chunk in file: "b/b14.9.png". Renaming it to *.png.
  8. W: Cant find 9patch chunk in file: "e/b17.9.png". Renaming it to *.png.
  9. W: Cant find 9patch chunk in file: "b/b17.9.png". Renaming it to *.png.
  10. W: Cant find 9patch chunk in file: "e/b15.9.png". Renaming it to *.png.
  11. W: Cant find 9patch chunk in file: "a5/b14.9.png". Renaming it to *.png.
  12. W: Cant find 9patch chunk in file: "e/b16.9.png". Renaming it to *.png.
  13. W: Cant find 9patch chunk in file: "a5/b17.9.png". Renaming it to *.png.
  14. W: Cant find 9patch chunk in file: "a5/b16.9.png". Renaming it to *.png.
  15. W: Cant find 9patch chunk in file: "e/b14.9.png". Renaming it to *.png.
  16. W: Cant find 9patch chunk in file: "b/b15.9.png". Renaming it to *.png.
  17. W: Cant find 9patch chunk in file: "a5/b15.9.png". Renaming it to *.png.
  18. I: Decoding values */* XMLs...
  19. I: Decoding AndroidManifest.xml with resources...
  20. I: Regular manifest package...
  21. I: Baksmaling classes.dex...
  22. I: Baksmaling classes2.dex...
  23. I: Baksmaling classes3.dex...
  24. I: Baksmaling classes4.dex...
  25. I: Baksmaling classes5.dex...
  26. I: Copying assets and libs...
  27. I: Copying unknown files...
  28. I: Copying original files...

反编译结果,生成一个wsdu450子目录,

  1. D:\wxds450>dir wxdu450
  2. Volume in drive D has no label.
  3. Volume Serial Number is 1234-5678
  4. Directory of D:\wxds450\wxdu450
  5. 01/02/2024 11:16 PM <DIR> .
  6. 01/02/2024 11:16 PM <DIR> ..
  7. 01/02/2024 11:16 PM <DIR> res
  8. 01/02/2024 11:16 PM 26,279 AndroidManifest.xml
  9. 01/02/2024 11:16 PM <DIR> smali
  10. 01/02/2024 11:16 PM <DIR> smali_classes2
  11. 01/02/2024 11:18 PM <DIR> smali_classes3
  12. 01/02/2024 11:20 PM <DIR> smali_classes4
  13. 01/02/2024 11:21 PM <DIR> smali_classes5
  14. 01/02/2024 11:25 PM <DIR> assets
  15. 01/02/2024 11:25 PM <DIR> lib
  16. 01/02/2024 11:25 PM <DIR> kotlin
  17. 01/02/2024 11:25 PM <DIR> unknown
  18. 01/02/2024 11:25 PM <DIR> original
  19. 01/02/2024 11:26 PM 3,705 apktool.yml
  20. 2 File(s) 29,984 bytes
  21. 13 Dir(s) 79,236,169,728 bytes free

找到要修改的文件,进行修改。

这里修改apktool.yml文件,将 minSdkVersion从21【对应Android version 5.0】改为19【对应Android version 4.4】。

  1. sdkInfo:
  2. minSdkVersion: 21
  3. targetSdkVersion: 28

改为

  1. sdkInfo:
  2. minSdkVersion: 19
  3. targetSdkVersion: 28

 使用APKTool编译,重打包

  1. D:\wxds450>apktool b wxdu450
  2. D:\wxds450>echo off
  3. I: Using Apktool 2.9.1
  4. I: Checking whether sources has changed...
  5. I: Smaling smali folder into classes.dex...
  6. I: Checking whether sources has changed...
  7. I: Smaling smali_classes2 folder into classes2.dex...
  8. I: Checking whether sources has changed...
  9. I: Smaling smali_classes3 folder into classes3.dex...
  10. I: Checking whether sources has changed...
  11. I: Smaling smali_classes4 folder into classes4.dex...
  12. I: Checking whether sources has changed...
  13. I: Smaling smali_classes5 folder into classes5.dex...
  14. I: Checking whether resources has changed...
  15. I: Building resources...
  16. I: Copying libs... (/lib)
  17. I: Copying libs... (/kotlin)
  18. I: Building apk file...
  19. I: Copying unknown files/dir...
  20. I: Built apk into: wxdu450\dist\wxdu450.apk

新的文件生成在dist目录下,将新文件更名为wxdu450_0.apk,并移动到d:\wxds450下。

打包完成后会发现指定的目录下出现了打包好的nopassword.apk文件。但是这个apk没有签名,需要重新签名之后,才能安装。

使用APKTool生成签名

生成keystore

keytooljarsigner 工具是JAVA JDK自带的,配置好JAVA环境即可!

输入命令:keytool -genkey -alias new.keystore -keyalg RSA -validity 20000 -keystore new.keystore,然后在输入两次最低六位数的密钥口令,例如123456,下面的信息直接Enter,最后y即可。

  1. D:\wxds450>keytool -genkey -alias new.keystore -keyalg RSA -validity 20000 -keystore new.keystore
  2. Enter keystore password:
  3. Re-enter new password:
  4. What is your first and last name?
  5. [Unknown]:
  6. What is the name of your organizational unit?
  7. [Unknown]:
  8. What is the name of your organization?
  9. [Unknown]:
  10. What is the name of your City or Locality?
  11. [Unknown]:
  12. What is the name of your State or Province?
  13. [Unknown]:
  14. What is the two-letter country code for this unit?
  15. [Unknown]:
  16. Is CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown correct?
  17. [no]: y
  18. Enter key password for <new.keystore>
  19. (RETURN if same as keystore password):
  20. Re-enter new password:
  21. Warning:
  22. The JKS keystore uses a proprietary format. It is recommended to migrate to PKCS12 which is an industry standard format using "keytool -importkeystore -srckeystore new.keystore -destkeystore new.keystore -deststoretype pkcs12".

结果生成新文件new.keystore

 签名APK

未签名APK不能在安卓手机上安装,想要安装则想要对齐签名。

输入命令:jarsigner -verbose -keystore new.keystore -signedjar wxdu450_sign.apk wxdu450_0.apk new.keystore然后再输入密钥库的密码短语即你之前设置的密钥口令,即可签名!

  1. D:\wxds450>jarsigner -verbose -keystore new.keystore -signedjar wxdu450_sign.apk wxdu450_0.apk new.keystore
  2. Enter Passphrase for keystore:
  3. adding: META-INF/MANIFEST.MF
  4. adding: META-INF/NEW_KEYS.SF
  5. adding: META-INF/NEW_KEYS.RSA
  6. signing: classes.dex
  7. signing: classes2.dex
  8. ......
  9. ......
  10. signing: pinyindb/pinyin_gwoyeu_mapping.xml
  11. signing: pinyindb/pinyin_mapping.xml
  12. signing: pinyindb/unicode_to_hanyu_pinyin.txt
  13. signing: src/com/qq/jce/wup/wup.properties
  14. jar signed.
  15. Warning:
  16. The signer's certificate is self-signed.
  17. No -tsa or -tsacert is provided and this jar is not timestamped. Without a timestamp, users may not be able to validate this jar after the signer certificate's expiration date (2078-10-06) or after any future revocation date.

结果生成新文件wxdu450_sign.apk,这个新文件就可以在Android4.4手机上安装而不出现解析错误。修改之后虽然能安装成功,但也只是能安装成功,还远远没有达到真正能用的水平:点击打开应用,直接闪退。如果要真的用起来,还需要解决很多问题。 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/447167
推荐阅读
相关标签
  

闽ICP备14008679号