Skip to content
This repository has been archived by the owner on Jul 27, 2024. It is now read-only.

Commit

Permalink
Add option --use-aapt2 (#84)
Browse files Browse the repository at this point in the history
* add --use-aapt2 flag

* add --use-aapt2 flag to README
  • Loading branch information
Dado1513 authored Apr 12, 2021
1 parent 6d99a9b commit dbcf18c
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ Let's start by looking at the help message:
$ obfuscapk --help
obfuscapk [-h] -o OBFUSCATOR [-w DIR] [-d OUT_APK] [-i] [-p] [-k VT_API_KEY]
[--keystore-file KEYSTORE_FILE] [--keystore-password KEYSTORE_PASSWORD]
[--key-alias KEY_ALIAS] [--key-password KEY_PASSWORD]
[--key-alias KEY_ALIAS] [--key-password KEY_PASSWORD] [--use-aapt2]
<APK_FILE>
```

Expand Down Expand Up @@ -303,6 +303,7 @@ shown in the example below:
com.mycompany.ignore
...
```
* `--use-aapt2` is a flag for use aapt2 option to rebuild app when using apktool.

Let's consider now a simple working example to see how Obfuscapk works:

Expand Down
8 changes: 7 additions & 1 deletion src/obfuscapk/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def get_cmd_args(args: list = None):
dest="interactive",
help="Show obfuscation progress (as a progress bar)",
)
parser.add_argument(
"--use-aapt2",
action="store_true",
help="Use aapt2 for rebuild app"
)
parser.add_argument(
"-k",
"--virus-total-key",
Expand Down Expand Up @@ -120,7 +125,7 @@ def main():
"""
A full command to obfuscate an application:
python3 -m obfuscapk.cli -p -i -w /working/dir/path -d /path/to/obfuscated.apk \
python3 -m obfuscapk.cli -p -i -w /working/dir/path --use-aapt2 -d /path/to/obfuscated.apk \
-o DebugRemoval -o LibEncryption -o CallIndirection -o MethodRename \
-o AssetEncryption -o MethodOverload -o ConstStringEncryption \
-o ResStringEncryption -o ArithmeticBranch -o FieldRename -o Nop -o Goto \
Expand Down Expand Up @@ -177,6 +182,7 @@ def main():
arguments.key_alias,
arguments.key_password,
arguments.ignore_packages_file,
arguments.use_aapt2,
)


Expand Down
3 changes: 3 additions & 0 deletions src/obfuscapk/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def perform_obfuscation(
key_alias: str = None,
key_password: str = None,
ignore_packages_file: str = None,
use_aapt2: bool = False
):
"""
Apply the obfuscation techniques to an input application and generate an obfuscated
Expand Down Expand Up @@ -87,6 +88,7 @@ def perform_obfuscation(
file).
:param ignore_packages_file: The file containing the package names to be ignored
during the obfuscation (one package name per line).
:param use_aapt2 If True, use aapt2 for rebuild app
"""

check_external_tool_dependencies()
Expand All @@ -109,6 +111,7 @@ def perform_obfuscation(
key_alias,
key_password,
ignore_packages_file,
use_aapt2
)

manager = ObfuscatorManager()
Expand Down
4 changes: 3 additions & 1 deletion src/obfuscapk/obfuscation.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def __init__(
key_alias: str = None,
key_password: str = None,
ignore_packages_file: str = None,
use_aapt2: bool = False,
):
self.logger = logging.getLogger(__name__)

Expand All @@ -44,6 +45,7 @@ def __init__(
self.key_alias: str = key_alias
self.key_password: str = key_password
self.ignore_packages_file: str = ignore_packages_file
self.use_aapt2 = use_aapt2

# Random string (32 chars long) generation with ASCII letters and digits
self.encryption_secret = "".join(
Expand Down Expand Up @@ -501,7 +503,7 @@ def build_obfuscated_apk(self) -> None:
apktool: Apktool = Apktool()

try:
apktool.build(self._decoded_apk_path, self.obfuscated_apk_path)
apktool.build(self._decoded_apk_path, self.obfuscated_apk_path, self.use_aapt2)
except Exception as e:
self.logger.error("Error during apk building: {0}".format(e))
raise
Expand Down
6 changes: 5 additions & 1 deletion src/obfuscapk/tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def decode(
self.logger.error("Error during decoding: {0}".format(e))
raise

def build(self, source_dir_path: str, output_apk_path: str = None) -> str:
def build(self, source_dir_path: str, output_apk_path: str = None, use_aapt2: bool = False) -> str:

# Check if the input directory exists.
if not os.path.isdir(source_dir_path):
Expand All @@ -143,6 +143,7 @@ def build(self, source_dir_path: str, output_apk_path: str = None) -> str:
'default path: "{0}"'.format(output_apk_path)
)


build_cmd: List[str] = [
self.apktool_path,
"--frame-path",
Expand All @@ -154,6 +155,9 @@ def build(self, source_dir_path: str, output_apk_path: str = None) -> str:
output_apk_path,
]

if use_aapt2:
build_cmd.insert(-2, "--use-aapt2")

try:
self.logger.info('Running build command "{0}"'.format(" ".join(build_cmd)))
# A new line character is sent as input since newer versions of Apktool
Expand Down

0 comments on commit dbcf18c

Please sign in to comment.