Apktool源码解析——第一篇

2024-02-17 00:38

本文主要是介绍Apktool源码解析——第一篇,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

著名的apktool是android逆向界用的最普遍的一个工具,这个项目的原始地址在这里http://code.google.com/p/android-apktool/,但是你们都懂的在天朝谷歌是无法访问的,所以直接上github的 https://github.com/brutall/brut.apktool。

在brut.apktool路径是主要代码所在,当然还有brut.apktool.smali是反编译smali的目录,目测还有brut.j.common,brut.j.dir,brut.j.utils是用到java的一些类。

brut.apktool路径下的apktool-cli还不知道是干嘛的,总之下面的apktool-lib正是我们最需要了解的。

 

android目录是利用XmlPullParser实现的专门用于android当中xml的解码工具包。

brut/androlib目录正是我们研究的主题。

com/mindprod/ledatastream目录是le数据支持库。

org/xmlpull/mxp1_serializer目录是XmlPullParser的Xml解析库。

androlib目录一览。

先看一下ApkDecoder这个类。

复制代码
    public ApkDecoder(File apkFile, Androlib androlib) {mAndrolib = androlib;setApkFile(apkFile);}public void setApkFile(File apkFile) {mApkFile = new ExtFile(apkFile);mResTable = null;}
复制代码
mResTable这个一直很迷惑,很多地方用到了它,但是目前还不住到具体是干嘛的,先不研究这个,继续往下看。
复制代码
   public void setOutDir(File outDir) throws AndrolibException {mOutDir = outDir;}public void setApi(int api) {mApi = api;}
复制代码

这两个方法一看名字就不言而喻,这里就不啰嗦了。多看了几行代码发现,这代码质量相当高,简直不要太优质,比起什么AxmlPrinter2的代码好看多了,呵呵!

下面就是反编译的核心方法了,客观请往下看。

复制代码
 public void decode() throws AndrolibException, IOException,DirectoryException {File outDir = getOutDir();if (!mForceDelete && outDir.exists()) {//如果输出目录不是因为删除而不存在就抛出异常throw new OutDirExistsException();}if (!mApkFile.isFile() || !mApkFile.canRead()) {//如果apk文件不是文件类型或者不能读也抛出异常throw new InFileNotFoundException();}try {OS.rmdir(outDir);//暂不明} catch (BrutException ex) {throw new AndrolibException(ex);}outDir.mkdirs();LOGGER.info("Using Apktool " + Androlib.getVersion() + " on " + mApkFile.getName());if (hasResources()) {//判断依据mApkFile.getDir().contraincontainsFile("resources.arsc")setTargetSdkVersion();setAnalysisMode(mAnalysisMode, true);//如果后面的参数为true,将会影响mResTable的取值setCompressionMode();//mCompressResources赋值,res.arsc是个zip获取它的压缩模式是否是ZipEntry.DEFLATED默认switch (mDecodeResources) {//这里默认是FULLcase DECODE_RESOURCES_NONE://不解码直接解压到指定res目录mAndrolib.decodeResourcesRaw(mApkFile, outDir);break;case DECODE_RESOURCES_FULL://调用androilib.decodearesources方法,其实本质还是调AndrolibResources.decode()方法mAndrolib.decodeResourcesFull(mApkFile, outDir, getResTable());break;}} else {//没有资源文件// if there's no resources.asrc, decode the manifest without looking// up attribute referencesif (hasManifest()) {//如果没有res.asrc就不需要查找manifest中的引用属性了switch (mDecodeResources) {case DECODE_RESOURCES_NONE:mAndrolib.decodeManifestRaw(mApkFile, outDir);break;case DECODE_RESOURCES_FULL://调mAndRes.decodeManifest(resTable, apkFile, outDir)mAndrolib.decodeManifestFull(mApkFile, outDir,getResTable());break;}}}if (hasSources()) {//如果有源文件switch (mDecodeSources) {case DECODE_SOURCES_NONE://直接复制classes.dexmAndrolib.decodeSourcesRaw(mApkFile, outDir, "classes.dex");break;case DECODE_SOURCES_SMALI://反编译成smali,SmaliDecoder.decode()mAndrolib.decodeSourcesSmali(mApkFile, outDir, "classes.dex", mDebug, mDebugLinePrefix, mBakDeb, mApi);break;case DECODE_SOURCES_JAVA://反编译jarnew AndrolibJava().decode()mAndrolib.decodeSourcesJava(mApkFile, outDir, mDebug);break;}}if (hasMultipleSources()) {//如果有多个dex// foreach unknown dex file in root, lets disassemble itSet<String> files = mApkFile.getDirectory().getFiles(true);for (String file : files) {//反编译多个dexif (file.endsWith(".dex")) {if (! file.equalsIgnoreCase("classes.dex")) {switch(mDecodeSources) {case DECODE_SOURCES_NONE:mAndrolib.decodeSourcesRaw(mApkFile, outDir, file);break;case DECODE_SOURCES_SMALI:mAndrolib.decodeSourcesSmali(mApkFile, outDir, file, mDebug, mDebugLinePrefix, mBakDeb, mApi);break;case DECODE_SOURCES_JAVA:mAndrolib.decodeSourcesJava(mApkFile, outDir, mDebug);break;}}}}}mAndrolib.decodeRawFiles(mApkFile, outDir);//复制libs和assetsmAndrolib.decodeUnknownFiles(mApkFile, outDir, mResTable);//发现一个问题,brutall的代码没有更新,新的在这里https://github.com/iBotPeaches/ApktoolmAndrolib.writeOriginalFiles(mApkFile, outDir);//输出原始文件,包括AndroidManifest.xml和签名证书writeMetaFile();//写如描述文件apktool.yml}
复制代码
下面几个方法不解释咯
复制代码
   public void setAnalysisMode(boolean mode, boolean pass) throws AndrolibException{mAnalysisMode = mode;// only set mResTable, once it existsif (pass) {if (mResTable == null) {mResTable = getResTable();}mResTable.setAnalysisMode(mode);}}public void setCompressionMode() throws AndrolibException, IOException {// read the resources.arsc checking for STORED vs DEFLATE// this will determine whether we compress on rebuild or not.ZipExtFile zef = new ZipExtFile(mApkFile.getAbsolutePath());ZipArchiveEntry ze = zef.getEntry("resources.arsc");if (ze != null) {int compression = ze.getMethod();mCompressResources = (compression == ZipEntry.DEFLATED);}zef.close();}public void setTargetSdkVersion() throws AndrolibException, IOException {if (mResTable == null) {mResTable = mAndrolib.getResTable(mApkFile);}Map<String, String> sdkInfo = mResTable.getSdkInfo();if (sdkInfo.get("targetSdkVersion") != null) {mApi = Integer.parseInt(sdkInfo.get("targetSdkVersion"));}}
复制代码
复制代码
public ResTable getResTable() throws AndrolibException {if (mResTable == null) {boolean hasResources = hasResources();boolean hasManifest = hasManifest();if (! (hasManifest || hasResources)) {//一个apk文件不能没有AndroidManifest.xml和resource.arsc,否则就不是合法的apk文件throw new AndrolibException("Apk doesn't contain either AndroidManifest.xml file or resources.arsc file");}AndrolibResources.sKeepBroken = mKeepBrokenResources;mResTable = mAndrolib.getResTable(mApkFile, hasResources);}return mResTable;}
复制代码

 

End!
 补充:
在decode()中有个writeMetaFile()方法没讲到,这里再看看。
复制代码
   private void writeMetaFile() throws AndrolibException {Map<String, Object> meta = new LinkedHashMap<String, Object>();meta.put("version", Androlib.getVersion());//静态方法?meta.put("apkFileName", mApkFile.getName());//apk名称if (mDecodeResources != DECODE_RESOURCES_NONE && (hasManifest() || hasResources())) {//如果要反编译资源并且有资源文件或者manifest文件meta.put("isFrameworkApk", mAndrolib.isFrameworkApk(getResTable()));putUsesFramework(meta);putSdkInfo(meta);putPackageInfo(meta);putVersionInfo(meta);putCompressionInfo(meta);}putUnknownInfo(meta);//加入Unknown文件目录说明
     mAndrolib.writeMetaFile(mOutDir, meta); //将meta数据写入apktool.yml写入到输出目录
  }
复制代码
复制代码
  private void putUsesFramework(Map<String, Object> meta) throws AndrolibException {Set<ResPackage> pkgs = getResTable().listFramePackages();if (pkgs.isEmpty()) {return;}Integer[] ids = new Integer[pkgs.size()];int i = 0;for (ResPackage pkg : pkgs) {ids[i++] = pkg.getId();}Arrays.sort(ids);Map<String, Object> uses = new LinkedHashMap<String, Object>();uses.put("ids", ids);//idsif (mAndrolib.apkOptions.frameworkTag != null) {//taguses.put("tag", mAndrolib.apkOptions.frameworkTag);}meta.put("usesFramework", uses);}private void putSdkInfo(Map<String, Object> meta) throws AndrolibException {Map<String, String> info = getResTable().getSdkInfo();//ResTable可以获取sdk信息?if (info.size() > 0) {meta.put("sdkInfo", info);}}private void putPackageInfo(Map<String, Object> meta) throws AndrolibException {String renamed = getResTable().getPackageRenamed();//还可以获取package信息?String original = getResTable().getPackageOriginal();int id = getResTable().getPackageId();HashMap<String, String> packages = new HashMap<String, String>();// only put rename-manifest-package into apktool.yml, if the change will be requiredif (!renamed.equalsIgnoreCase(original)) {packages.put("rename-manifest-package", renamed);}packages.put("forced-package-id", String.valueOf(id));meta.put("packageInfo", packages);}private void putVersionInfo(Map<String, Object> meta) throws AndrolibException {Map<String, String> info = getResTable().getVersionInfo();//获取版本信息if (info.size() > 0) {meta.put("versionInfo", info);}}private void putUnknownInfo(Map<String, Object> meta) throws AndrolibException {Map<String,String> info = mAndrolib.mResUnknownFiles.getUnknownFiles();//Androlib会收集Unknwn文件if (info.size() > 0) {meta.put("unknownFiles", info);}}private void putCompressionInfo(Map<String, Object> meta) throws AndrolibException {meta.put("compressionType", getCompressionType());//压缩方式}
复制代码

这篇关于Apktool源码解析——第一篇的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/716173

相关文章

Mybatis Plus JSqlParser解析sql语句及JSqlParser安装步骤

《MybatisPlusJSqlParser解析sql语句及JSqlParser安装步骤》JSqlParser是一个用于解析SQL语句的Java库,它可以将SQL语句解析为一个Java对象树,允许... 目录【一】jsqlParser 是什么【二】JSqlParser 的安装步骤【三】使用场景【1】sql语

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

Java 关键字transient与注解@Transient的区别用途解析

《Java关键字transient与注解@Transient的区别用途解析》在Java中,transient是一个关键字,用于声明一个字段不会被序列化,这篇文章给大家介绍了Java关键字transi... 在Java中,transient 是一个关键字,用于声明一个字段不会被序列化。当一个对象被序列化时,被

Java JSQLParser解析SQL的使用指南

《JavaJSQLParser解析SQL的使用指南》JSQLParser是一个Java语言的SQL语句解析工具,可以将SQL语句解析成为Java类的层次结构,还支持改写SQL,下面我们就来看看它的具... 目录一、引言二、jsQLParser常见类2.1 Class Diagram2.2 Statement

python进行while遍历的常见错误解析

《python进行while遍历的常见错误解析》在Python中选择合适的遍历方式需要综合考虑可读性、性能和具体需求,本文就来和大家讲解一下python中while遍历常见错误以及所有遍历方法的优缺点... 目录一、超出数组范围问题分析错误复现解决方法关键区别二、continue使用问题分析正确写法关键点三

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

使用Java实现Navicat密码的加密与解密的代码解析

《使用Java实现Navicat密码的加密与解密的代码解析》:本文主要介绍使用Java实现Navicat密码的加密与解密,通过本文,我们了解了如何利用Java语言实现对Navicat保存的数据库密... 目录一、背景介绍二、环境准备三、代码解析四、核心代码展示五、总结在日常开发过程中,我们有时需要处理各种软

Python多进程、多线程、协程典型示例解析(最新推荐)

《Python多进程、多线程、协程典型示例解析(最新推荐)》:本文主要介绍Python多进程、多线程、协程典型示例解析(最新推荐),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 目录一、multiprocessing(多进程)1. 模块简介2. 案例详解:并行计算平方和3. 实现逻

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

MyBatis分页插件PageHelper深度解析与实践指南

《MyBatis分页插件PageHelper深度解析与实践指南》在数据库操作中,分页查询是最常见的需求之一,传统的分页方式通常有两种内存分页和SQL分页,MyBatis作为优秀的ORM框架,本身并未提... 目录1. 为什么需要分页插件?2. PageHelper简介3. PageHelper集成与配置3.