ClaudiuGeorgiu / Obfuscapk

An automatic obfuscation tool for Android apps that works in a black-box fashion, supports advanced obfuscation features and has a modular architecture easily extensible with new techniques
MIT License
1.09k stars 285 forks source link

apktool error during rebuilding #152

Closed diabitogoaway closed 9 months ago

diabitogoaway commented 1 year ago

Prerequisites

Before opening this issue, I tried the following steps:

Description

I have these errors all the time and I'm guessing It's because of apktool or my app

Steps to reproduce

  1. I followed the readme and used docker to install all the dependencies and tools
  2. for fixing this issue I added more flags to apktool inside the file tool.py and even using -r flag it didn't work and I'm still getting the same error
  3. I used different versions of apktool 2.4 2.7.0
  4. changed logging.ERROR in the file main.py to logging.DEBUG but still got the same error and no explanations
  5. I've tried to decompile the apk using apktool and It worked successfully even without -r flag
  6. using normal apk editor appks like apktool M on android I can easily edit and decompile and compile it again but on this Obfuscator It's not possible and I get error
  7. I've tried every solution in the issues and none of them worked

Expected behavior:

Actual behavior:

01/02/2023 23:38:47> [DEBUG][obfuscapk.main][check_external_tool_dependencies()] Checking external tool dependencies
01/02/2023 23:38:47> [DEBUG][obfuscapk.main][check_external_tool_dependencies()] Checking external tool dependencies
01/02/2023 23:38:47> [DEBUG][obfuscapk.obfuscation][__init__()] Auto-generated random secret key for encryption: "v4w2oLOaC6Z9QTi4TiATE50DPo6zOkps"
01/02/2023 23:38:47> [DEBUG][obfuscapk.obfuscation][__init__()] No working directory provided, the operations will take place in the same directory as the input file, in the directory "obfuscation_working_dir"
01/02/2023 23:38:47> [DEBUG][obfuscapk.obfuscation][__init__()] No obfuscated apk path provided, the result will be saved as "obfuscation_working_dir/original_obfuscated.apk"
01/02/2023 23:38:47> [INFO][yapsy_loaded_plugin_RandomManifest_1.random_manifest.RandomManifest][obfuscate()] Running "RandomManifest" obfuscator
01/02/2023 23:38:47> [INFO][obfuscapk.tool.Apktool][decode()] Running decode command "/usr/local/bin/apktool --frame-path /tmp d --force original.apk -r -o obfuscation_working_dir/original"
01/02/2023 23:38:48> [ERROR][obfuscapk.tool.Apktool][decode()] Error during decode command: I: Using Apktool 2.7.0 on original.apk
Exception in thread "main" brut.androlib.AndrolibException: brut.directory.DirectoryException: java.util.zip.ZipException: zip END header not found
        at brut.androlib.ApkDecoder.hasResources(ApkDecoder.java:294)
        at brut.androlib.ApkDecoder.decode(ApkDecoder.java:96)
        at brut.apktool.Main.cmdDecode(Main.java:175)
        at brut.apktool.Main.main(Main.java:79)
Caused by: brut.directory.DirectoryException: java.util.zip.ZipException: zip END header not found
        at brut.directory.ZipRODirectory.<init>(ZipRODirectory.java:55)
        at brut.directory.ZipRODirectory.<init>(ZipRODirectory.java:38)
        at brut.directory.ExtFile.getDirectory(ExtFile.java:49)
        at brut.androlib.ApkDecoder.hasResources(ApkDecoder.java:292)
        ... 3 more
Caused by: java.util.zip.ZipException: zip END header not found
        at java.base/java.util.zip.ZipFile$Source.zerror(ZipFile.java:1607)
        at java.base/java.util.zip.ZipFile$Source.findEND(ZipFile.java:1497)
        at java.base/java.util.zip.ZipFile$Source.initCEN(ZipFile.java:1504)
        at java.base/java.util.zip.ZipFile$Source.<init>(ZipFile.java:1308)
        at java.base/java.util.zip.ZipFile$Source.get(ZipFile.java:1271)
        at java.base/java.util.zip.ZipFile$CleanableResource.<init>(ZipFile.java:733)
        at java.base/java.util.zip.ZipFile$CleanableResource.get(ZipFile.java:850)
        at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:248)
        at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:177)
        at java.base/java.util.zip.ZipFile.<init>(ZipFile.java:191)
        at brut.directory.ZipRODirectory.<init>(ZipRODirectory.java:53)
        ... 6 more

01/02/2023 23:38:48> [ERROR][obfuscapk.obfuscation][decode_apk()] Error during apk decoding: Command '['/usr/local/bin/apktool', '--frame-path', '/tmp', 'd', '--force', 'original.apk', '-r', '-o', 'obfuscation_working_dir/original']' returned non-zero exit status 1.
01/02/2023 23:38:48> [ERROR][yapsy_loaded_plugin_RandomManifest_1.random_manifest.RandomManifest][obfuscate()] Error during execution of "RandomManifest" obfuscator: Command '['/usr/local/bin/apktool', '--frame-path', '/tmp', 'd', '--force', 'original.apk', '-r', '-o', 'obfuscation_working_dir/original']' returned non-zero exit status 1.
01/02/2023 23:38:48> [CRITICAL][obfuscapk.main][perform_obfuscation()] Error during obfuscation: Command '['/usr/local/bin/apktool', '--frame-path', '/tmp', 'd', '--force', 'original.apk', '-r', '-o', 'obfuscation_working_dir/original']' returned non-zero exit status 1.
Traceback (most recent call last):
  File "/workdir/obfuscapk/main.py", line 153, in perform_obfuscation
    (obfuscator_name_to_function[obfuscator_name])(obfuscation)
  File "/workdir/obfuscapk/obfuscators/random_manifest/random_manifest.py", line 111, in obfuscate
    obfuscation_info.get_manifest_file(), parser=xml_parser
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/obfuscapk/obfuscation.py", line 635, in get_manifest_file
    self.decode_apk()
  File "/workdir/obfuscapk/obfuscation.py", line 360, in decode_apk
    apktool.decode(self.apk_path, self._decoded_apk_path, force=True)
  File "/workdir/obfuscapk/tool.py", line 105, in decode
    output = subprocess.check_output(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/local/bin/apktool', '--frame-path', '/tmp', 'd', '--force', 'original.apk', '-r', '-o', 'obfuscation_working_dir/original']' returned non-zero exit status 1.
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/workdir/obfuscapk/cli.py", line 189, in <module>
    main()
  File "/workdir/obfuscapk/cli.py", line 171, in main
    perform_obfuscation(
  File "/workdir/obfuscapk/main.py", line 153, in perform_obfuscation
    (obfuscator_name_to_function[obfuscator_name])(obfuscation)
  File "/workdir/obfuscapk/obfuscators/random_manifest/random_manifest.py", line 111, in obfuscate
    obfuscation_info.get_manifest_file(), parser=xml_parser
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/workdir/obfuscapk/obfuscation.py", line 635, in get_manifest_file
    self.decode_apk()
  File "/workdir/obfuscapk/obfuscation.py", line 360, in decode_apk
    apktool.decode(self.apk_path, self._decoded_apk_path, force=True)
  File "/workdir/obfuscapk/tool.py", line 105, in decode
    output = subprocess.check_output(
             ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 466, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/subprocess.py", line 571, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/usr/local/bin/apktool', '--frame-path', '/tmp', 'd', '--force', 'original.apk', '-r', '-o', 'obfuscation_working_dir/original']' returned non-zero exit status 1.

Versions

Additional information

This is the app that I'm using: app

Apk file(s):

ClaudiuGeorgiu commented 1 year ago

Hi, thanks for the detailed report! From the logs, it looks like apktool fails when invoked from Obfuscapk, but you wrote that it works when you use apktool directly. Can you please provide the exact command that you are using with Docker and it's generating the error?

diabitogoaway commented 1 year ago

Hello @ClaudiuGeorgiu > Thank you so much for replying I didn't expected HAHA

So here are my commands that I get the same error:

With docker: docker run --rm -it -u $(id -u):$(id -g) -v "${PWD}":"/workdir" obfuscapk -o RandomManifest -o Rebuild -o NewAlignment -o NewSignature original.apk With docker without RandomManifest: docker run --rm -it -u $(id -u):$(id -g) -v "${PWD}":"/workdir" obfuscapk -o Rebuild -o NewAlignment -o NewSignature original.apk Using python (I installed the tool using python before using docker and got the same error but It might be helpful for you to help me: python3 -m obfuscapk.cli -o RandomManifest -o Rebuild -o NewAlignment -o NewSignature -p --use-aapt2 -w obfuscated/apktool/ -d obfuscated/obfuscated.apk original.apk

ClaudiuGeorgiu commented 1 year ago

I just tried with the latest Docker image and this apk mentioned in your message and I got no error, so I guess there's something wrong with your apk file.

docker run --rm -it -u $(id -u):$(id -g) -v "${PWD}":"/workdir" -e LOG_LEVEL=DEBUG claudiugeorgiu/obfuscapk -o RandomManifest -o Rebuild -o NewAlignment -o NewSignature original.apk
02/02/2023 13:25:17> [DEBUG][obfuscapk.main][check_external_tool_dependencies()] Checking external tool dependencies
02/02/2023 13:25:17> [DEBUG][obfuscapk.main][check_external_tool_dependencies()] Checking external tool dependencies
02/02/2023 13:25:17> [DEBUG][obfuscapk.obfuscation][__init__()] Auto-generated random secret key for encryption: "4CCL2SkRT91RVZcpvHWGJxLuoeDQiR0l"
02/02/2023 13:25:17> [DEBUG][obfuscapk.obfuscation][__init__()] No working directory provided, the operations will take place in the same directory as the input file, in the directory "obfuscation_working_dir"
02/02/2023 13:25:17> [DEBUG][obfuscapk.obfuscation][__init__()] No obfuscated apk path provided, the result will be saved as "obfuscation_working_dir/original_obfuscated.apk"
02/02/2023 13:25:17> [INFO][yapsy_loaded_plugin_RandomManifest_1.random_manifest.RandomManifest][obfuscate()] Running "RandomManifest" obfuscator
02/02/2023 13:25:17> [INFO][obfuscapk.tool.Apktool][decode()] Running decode command "/usr/local/bin/apktool --frame-path /tmp d --force original.apk -o obfuscation_working_dir/original"
02/02/2023 13:25:21> [INFO][yapsy_loaded_plugin_Rebuild_1.rebuild.Rebuild][obfuscate()] Running "Rebuild" obfuscator
02/02/2023 13:25:21> [INFO][obfuscapk.tool.Apktool][build()] Running build command "/usr/local/bin/apktool --frame-path /tmp b --force-all obfuscation_working_dir/original -o obfuscation_working_dir/original_obfuscated.apk"
02/02/2023 13:25:38> [INFO][yapsy_loaded_plugin_NewAlignment_1.new_alignment.NewAlignment][obfuscate()] Running "NewAlignment" obfuscator
02/02/2023 13:25:38> [INFO][obfuscapk.tool.Zipalign][align()] Running align command "/android-sdk-linux/build-tools/33.0.0/zipalign -p -v -f 4 obfuscation_working_dir/original_obfuscated.copy.apk obfuscation_working_dir/original_obfuscated.apk"
02/02/2023 13:25:38> [INFO][yapsy_loaded_plugin_NewSignature_1.new_signature.NewSignature][obfuscate()] Running "NewSignature" obfuscator
02/02/2023 13:25:38> [INFO][obfuscapk.tool.ApkSigner][resign()] Removing current signature from apk "obfuscation_working_dir/original_obfuscated.apk"
02/02/2023 13:25:39> [INFO][obfuscapk.tool.ApkSigner][sign()] Running sign command "/android-sdk-linux/build-tools/33.0.0/apksigner sign -v --ks /Obfuscapk/obfuscapk/resources/obfuscation_keystore.jks --ks-key-alias obfuscation_key --ks-pass pass:obfuscation_password obfuscation_working_dir/original_obfuscated.apk"
diabitogoaway commented 1 year ago

@ClaudiuGeorgiu Could you please tell me these so I try it too? Version of docker? Version of apktool? version of zipalign? OS and version of OS? and other tools being used?

ClaudiuGeorgiu commented 1 year ago

Os: Ubuntu 22.04.1 LTS Docker: 20.10.22, build 3a2c30b All the other tools are those inside the latest Docker image.

diabitogoaway commented 1 year ago

@ClaudiuGeorgiu I still get the same error When you tried did you try it on the apk file that I provided? Because I tried on many versions of the tools and also different type of OS But still get the same error If the problem is with the apk how can I fix it? Because I used any type of decompiler and could be able to decompile the apk

ClaudiuGeorgiu commented 1 year ago

When you tried did you try it on the apk file that I provided?

Yes.

There is a similar issue in Apktool's repository: https://github.com/iBotPeaches/Apktool/issues/2173. The problem seems to be that the apk file is corrupted, maybe try downloading it again or try with other apk files to see if you still get the same error. I never had this error and I can't replicate, sorry.