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

Commit 59d77ce

Browse files
committed
Auto-generated random secret key for encryption
1 parent 2b15194 commit 59d77ce

File tree

6 files changed

+21
-10
lines changed

6 files changed

+21
-10
lines changed

src/obfuscapk/obfuscation.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
import logging
55
import os
6+
import secrets
7+
import string
68
from typing import List, Union
79

810
from . import util
@@ -27,6 +29,10 @@ def __init__(self, apk_path: str, working_dir_path: str = None, obfuscated_apk_p
2729
self.interactive: bool = interactive
2830
self.virus_total_api_key: List[str] = virus_total_api_key
2931

32+
# Random string (32 chars long) generation with ASCII letters and digits
33+
self.encryption_secret = ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(32))
34+
self.logger.debug('Auto-generated random secret key for encryption: "{0}"'.format(self.encryption_secret))
35+
3036
# The list of obfuscators already used on the application.
3137
self.used_obfuscators: List[str] = []
3238

src/obfuscapk/obfuscators/asset_encryption/asset_encryption.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ class AssetEncryption(obfuscator_category.IEncryptionObfuscator):
1919
def __init__(self):
2020
self.logger = logging.getLogger('{0}.{1}'.format(__name__, self.__class__.__name__))
2121
super().__init__()
22-
2322
self.encryption_secret = 'This-key-need-to-be-32-character'
2423

2524
def obfuscate(self, obfuscation_info: Obfuscation):
2625
self.logger.info('Running "{0}" obfuscator'.format(self.__class__.__name__))
2726

27+
self.encryption_secret = obfuscation_info.encryption_secret
2828
try:
2929
# This instruction takes 2 registers, the latter contains the name of the asset file to load.
3030
open_asset_invoke_pattern = re.compile(r'\s+invoke-virtual\s'
@@ -127,7 +127,7 @@ def obfuscate(self, obfuscation_info: Obfuscation):
127127
destination_dir = os.path.dirname(obfuscation_info.get_smali_files()[0])
128128
destination_file = os.path.join(destination_dir, 'DecryptAsset.smali')
129129
with open(destination_file, 'w', encoding='utf-8') as decrypt_asset_smali:
130-
decrypt_asset_smali.write(util.get_decrypt_asset_smali_code())
130+
decrypt_asset_smali.write(util.get_decrypt_asset_smali_code(self.encryption_secret))
131131
obfuscation_info.decrypt_asset_smali_file_added_flag = True
132132

133133
else:

src/obfuscapk/obfuscators/const_string_encryption/const_string_encryption.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def encrypt_string(self, string_to_encrypt: str) -> str:
3939
def obfuscate(self, obfuscation_info: Obfuscation):
4040
self.logger.info('Running "{0}" obfuscator'.format(self.__class__.__name__))
4141

42+
self.encryption_secret = obfuscation_info.encryption_secret
4243
try:
4344
encrypted_strings: Set[str] = set()
4445

@@ -187,7 +188,7 @@ def obfuscate(self, obfuscation_info: Obfuscation):
187188
destination_dir = os.path.dirname(obfuscation_info.get_smali_files()[0])
188189
destination_file = os.path.join(destination_dir, 'DecryptString.smali')
189190
with open(destination_file, 'w', encoding='utf-8') as decrypt_string_smali:
190-
decrypt_string_smali.write(util.get_decrypt_string_smali_code())
191+
decrypt_string_smali.write(util.get_decrypt_string_smali_code(self.encryption_secret))
191192
obfuscation_info.decrypt_string_smali_file_added_flag = True
192193

193194
except Exception as e:

src/obfuscapk/obfuscators/lib_encryption/lib_encryption.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def __init__(self):
2525
def obfuscate(self, obfuscation_info: Obfuscation):
2626
self.logger.info('Running "{0}" obfuscator'.format(self.__class__.__name__))
2727

28+
self.encryption_secret = obfuscation_info.encryption_secret
2829
try:
2930
native_libs = obfuscation_info.get_native_lib_files()
3031

@@ -133,7 +134,7 @@ def obfuscate(self, obfuscation_info: Obfuscation):
133134
destination_dir = os.path.dirname(obfuscation_info.get_smali_files()[0])
134135
destination_file = os.path.join(destination_dir, 'DecryptAsset.smali')
135136
with open(destination_file, 'w', encoding='utf-8') as decrypt_asset_smali:
136-
decrypt_asset_smali.write(util.get_decrypt_asset_smali_code())
137+
decrypt_asset_smali.write(util.get_decrypt_asset_smali_code(self.encryption_secret))
137138
obfuscation_info.decrypt_asset_smali_file_added_flag = True
138139

139140
# Remove the original native libraries (the encrypted ones will be used instead).

src/obfuscapk/obfuscators/res_string_encryption/res_string_encryption.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def encrypt_string_array_resources(self, string_array_resources_xml_file: str,
7070
def obfuscate(self, obfuscation_info: Obfuscation):
7171
self.logger.info('Running "{0}" obfuscator'.format(self.__class__.__name__))
7272

73+
self.encryption_secret = obfuscation_info.encryption_secret
7374
try:
7475
string_res_field_pattern = re.compile(r'\.field\spublic\sstatic\sfinal\s(?P<string_name>\S+?):I\s=\s'
7576
r'(?P<string_id>[0-9a-fA-FxX]+)', re.UNICODE)
@@ -303,7 +304,7 @@ def obfuscate(self, obfuscation_info: Obfuscation):
303304
destination_dir = os.path.dirname(obfuscation_info.get_smali_files()[0])
304305
destination_file = os.path.join(destination_dir, 'DecryptString.smali')
305306
with open(destination_file, 'w', encoding='utf-8') as decrypt_string_smali:
306-
decrypt_string_smali.write(util.get_decrypt_string_smali_code())
307+
decrypt_string_smali.write(util.get_decrypt_string_smali_code(self.encryption_secret))
307308
obfuscation_info.decrypt_string_smali_file_added_flag = True
308309

309310
except Exception as e:

src/obfuscapk/util.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
random_seed = 42
2020
random.seed(random_seed)
2121

22-
2322
##########################################################################################
2423
# Common regex patterns. #
2524
##########################################################################################
@@ -70,6 +69,7 @@
7069
const_string_pattern = re.compile(r'\s+const-string(/jumbo)?\s(?P<register>[vp0-9]+),\s'
7170
r'"(?P<string>.+)"', re.UNICODE)
7271

72+
7373
##########################################################################################
7474

7575

@@ -176,14 +176,16 @@ def get_smali_method_overload() -> str:
176176
'resources', 'smali', 'overloaded_method_body.smali'))
177177

178178

179-
def get_decrypt_asset_smali_code() -> str:
179+
def get_decrypt_asset_smali_code(encryption_secret: str) -> str:
180180
return get_text_from_file(os.path.join(os.path.dirname(__file__),
181-
'resources', 'smali', 'DecryptAsset.smali'))
181+
'resources', 'smali', 'DecryptAsset.smali'
182+
).replace('This-key-need-to-be-32-character', encryption_secret))
182183

183184

184-
def get_decrypt_string_smali_code() -> str:
185+
def get_decrypt_string_smali_code(encryption_secret: str) -> str:
185186
return get_text_from_file(os.path.join(os.path.dirname(__file__),
186-
'resources', 'smali', 'DecryptString.smali'))
187+
'resources', 'smali', 'DecryptString.smali')
188+
).replace('This-key-need-to-be-32-character', encryption_secret)
187189

188190

189191
def get_api_reflection_smali_code() -> str:

0 commit comments

Comments
 (0)