簡體   English   中英

Python 使用 ApkTool 和 Subprocess 反編譯 APK

[英]Python Decompile APK using ApkTool and Subprocess

我正在構建一個反編譯 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 我該如何解決這個問題?

FileNotFoundError 是一個顯式名稱,這意味着您的代碼函數找不到此文件。 如果沒有關於此異常發生位置的更多詳細信息,我可以給您的唯一答案是驗證:

  1. 目錄“./output/”存在
  2. 使用shutil復制文件后正確創建文件

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM