Python使用ApkTool和Subprocess反编译APK

问题描述 投票:2回答:1

我正在构建一个反编译Android APK的Python应用程序进行一些修改并重新编译它。

这是代码:

from shutil import copyfile

# Copy the APK to the output directory.
copyfile('./source/rat.apk', './output/rat.apk')

# Decompiles the APK.
subprocess.call(['apktool','d','./output/rat.apk','-f'], stdout=FNULL, stderr=subprocess.STDOUT)

# Modifies address and port.
with open('./output/RAT/app/src/main/res/values/strings.xml','w+') as netaddress:
    address = netaddress.read()
    address.replace('youraddress','x')
    address.replace('yourport','8080')
    netaddress.write(address)
    print(netaddress.read())
    netaddress.close()

# Recompiles it.
subprocess.call(['apktool','b','./output/RAT'], stdout=FNULL, stderr=subprocess.STDOUT)

但它在运行时抛出此异常:

with open('./output/RAT/app/src/main/res/values/strings.xml','w+') as netaddress:
FileNotFoundError: [Errno 2] No such file or directory: './output/RAT/app/src/main/res/values/strings.xml'

更新: 当我尝试使用命令apktool d source/rat.apk

这是我收到的输出:

18:45:51 up 1 min,  0 users,  load average: 0.52, 0.58, 0.59
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
I: Using Apktool 2.0.2-dirty on rat.apk
I: Loading resource table...
I: Decoding AndroidManifest.xml with resources...
I: Loading resource table from file: /home/stecco/apktool/framework/1.apk
W: Could not decode attr value, using undecoded value instead: ns=android, name=versionCode, value=0x00000001
I: Loading resource table from file: /home/stecco/apktool/framework/1.apk
W: Could not decode attr value, using undecoded value instead: ns=android, name=versionName, value=0x00000014
I: Loading resource table from file: /home/stecco/apktool/framework/1.apk
W: Could not decode attr value, using undecoded value instead: ns=android, name=versionCode, value=0x00000001
I: Loading resource table from file: /home/stecco/apktool/framework/1.apk
W: Could not decode attr value, using undecoded value instead: ns=android, name=versionName, value=0x00000014
Exception in thread "main" java.lang.NullPointerException
        at java.io.Writer.write(Writer.java:157)
        at brut.androlib.res.util.ExtMXSerializer.writeAttributeValue(ExtMXSerializer.java:38)
        at org.xmlpull.mxp1_serializer.MXSerializer.attribute(MXSerializer.java:696)
        at org.xmlpull.v1.wrapper.classic.XmlSerializerDelegate.attribute(XmlSerializerDelegate.java:106)
        at org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper.writeStartTag(StaticXmlSerializerWrapper.java:267)
        at org.xmlpull.v1.wrapper.classic.StaticXmlSerializerWrapper.event(StaticXmlSerializerWrapper.java:211)
        at brut.androlib.res.decoder.XmlPullStreamDecoder$1.event(XmlPullStreamDecoder.java:83)
        at brut.androlib.res.decoder.XmlPullStreamDecoder.decode(XmlPullStreamDecoder.java:141)
        at brut.androlib.res.decoder.XmlPullStreamDecoder.decodeManifest(XmlPullStreamDecoder.java:153)
        at brut.androlib.res.decoder.ResFileDecoder.decodeManifest(ResFileDecoder.java:140)
        at brut.androlib.res.AndrolibResources.decodeManifestWithResources(AndrolibResources.java:199)
        at brut.androlib.Androlib.decodeManifestWithResources(Androlib.java:140)
        at brut.androlib.ApkDecoder.decode(ApkDecoder.java:100)
        at brut.apktool.Main.cmdDecode(Main.java:165)
        at brut.apktool.Main.main(Main.java:81)

通过安装Apktool 2.4.0解决了每个问题,似乎版本2.0.2它确实是错误的,甚至2.2.2和2.3.2。

但是当我尝试重新编译它时出现了另一个问题它给了我一个classes.dex而不是apk我怎么能解决这个问题呢?

android python file subprocess apktool
1个回答
1
投票

FileNotFoundError是一个显式名称,这意味着您的代码函数无法找到此文件。如果没有关于此异常发生位置的更多详细信息,我可以给您的唯一答案是验证:

  1. 目录“./output/”存在
  2. 使用shutil复制文件后正确创建该文件
© www.soinside.com 2019 - 2024. All rights reserved.